Skip to content

Commit

Permalink
Code blocks and monospace
Browse files Browse the repository at this point in the history
  • Loading branch information
zampino committed Apr 4, 2024
1 parent a0b6495 commit 4ab0440
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/nextjournal/markdown/parser2.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
OrderedList
Code
FencedCodeBlock
IndentedCodeBlock
Heading
Text
Paragraph
Expand Down Expand Up @@ -122,11 +123,26 @@
(defmethod open-node StrongEmphasis [loc _node]
(-> loc (z/append-child {:type :strong :content []}) z/down z/rightmost))

(defmethod open-node Code [loc ^Code node]
(-> loc (z/append-child {:type :monospace
:content [{:type :text
:text (.getLiteral node)}]}) z/down z/rightmost))

(defmethod open-node Link [loc ^Link node]
(-> loc (z/append-child {:type :link
:attrs {:href (.getDestination node) :title (.getTitle node)}
:content []}) z/down z/rightmost))

(defmethod open-node IndentedCodeBlock [loc ^IndentedCodeBlock node]
(-> loc (z/append-child {:type :code
:content [{:text (.getLiteral node)}]}) z/down z/rightmost))

(defmethod open-node FencedCodeBlock [loc ^FencedCodeBlock node]
(-> loc (z/append-child (merge {:type :code
:info (.getInfo node)
:content [{:text (.getLiteral node)}]}
(parser/parse-fence-info (.getInfo node)))) z/down z/rightmost))

(defmethod open-node Image [loc ^Image node]
(-> loc (z/append-child {:type :image
:attrs {:src (.getDestination node) :title (.getTitle node)}
Expand All @@ -146,6 +162,8 @@
(let [!loc (atom (parser/->zip {:type :doc :content []}))]
(.accept node
(proxy [AbstractVisitor] []

;; proxy can't overload method by arg type, while gen-class can: https://groups.google.com/g/clojure/c/TVRsy4Gnf70
(visit [^Node node]
#_ (prn :visit (str node) (z/node @!loc))
(assert @!loc (format "Can't add node: '%s' to an empty location" node))
Expand All @@ -158,9 +176,9 @@
InlineFormula (swap! !loc z/append-child {:type :inline-formula
:text (.getLiteral ^InlineFormula node)})

;; not used directly
LinkReferenceDefinition (prn :link-ref node)


(if (get-method open-node (class node))
(with-tight-list node
(swap! !loc open-node node)
Expand All @@ -174,6 +192,7 @@
(node->data (.parse parser md)))

(comment
(parse "some `marks` inline")
(parse "# Ahoi
par
Expand All @@ -191,19 +210,6 @@ broken
---
![img](/some/src 'title')")


;; link refs

(parse "some text with a [^link] ahoi
[^link]: https://application.garden 'whatatitle'
# text continues here
")



;; block footnotes
(md/parse "_hello_ what and foo[^note1] and^[some other note].
Expand Down

0 comments on commit 4ab0440

Please sign in to comment.