Skip to content

Commit

Permalink
Added a function to squash code-block's content.
Browse files Browse the repository at this point in the history
  • Loading branch information
svetlyak40wt committed Mar 27, 2022
1 parent 61c1c41 commit 6438d6d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/blocks/code.lisp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
(uiop:define-package #:reblocks-text-editor/blocks/code
(:use #:cl))
(:use #:cl)
(:import-from #:reblocks-text-editor/document/ops
#:map-document))
(in-package #:reblocks-text-editor/blocks/code)


Expand All @@ -9,3 +11,31 @@
(if children
(common-doc:text (first children))
"")))


(defun normalize (root-node)
"Normalizes code-block nodes by joining it's children into a single text-node.
This helper is required until this issue will be resolved:
https://github.com/CommonDoc/scriba/issues/15"
(labels ((normalize-code-blocks (current-node depth)
(declare (ignore depth))
(when (typep current-node 'common-doc:code-block)
(let ((children (common-doc:children current-node)))
(when (not (and (= (length children) 1)
(typep (first children)
'common-doc:text-node)))
(setf (common-doc:children current-node)
(list (squash current-node))))))
current-node)
(squash (code-block)
(common-doc:make-text
(with-output-to-string (s)
(common-doc.ops:with-document-traversal (code-block node)
(etypecase node
(common-doc:text-node
(write-string (common-doc:text node) s))
(common-doc:content-node
nil)))))))
(map-document root-node #'normalize-code-blocks)))
15 changes: 15 additions & 0 deletions src/utils/common-doc.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(uiop:define-package #:reblocks-text-editor/utils/common-doc
(:use #:cl)
(:export
#:print-tree))
(in-package #:reblocks-text-editor/utils/common-doc)


(defun print-tree (doc)
"Prints CommonDoc nodes as a nested tree, to visualize document structure."
(common-doc.ops:with-document-traversal (doc node depth)
(loop repeat (1- depth)
do (write-char #\Space)
(write-char #\Space))
(format t "~A~%" node))
(values doc))

0 comments on commit 6438d6d

Please sign in to comment.