Skip to content

Commit

Permalink
Equalize code and links + add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zampino committed Aug 15, 2024
1 parent 1c39257 commit 6d113cc
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/nextjournal/markdown.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
- [x] ~~thing~~
")

(-> (nextjournal.markdown.graaljs/parse "[alt](https://this/is/a.link)") :content first :content first)
(-> (parse "[alt](https://this/is/a.link)") :content first :content first)

(with-new-parser
(parse "# Hello Markdown
- [ ] what
Expand Down
22 changes: 16 additions & 6 deletions src/nextjournal/markdown/parser/impl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,14 @@
id (assoc-in [:attrs :id] id)
emoji (assoc :emoji emoji))))
(as-> l
(if (= 1 (u/zdepth l)) ;; only add top level headings to ToC
(-> l z/up (z/edit u/add-to-toc (z/node l)) z/down z/rightmost)
(if (= 1 (u/zdepth l))
;; only add top level headings to ToC
(-> l z/up
(z/edit (fn [doc]
(-> doc
(u/add-to-toc (z/node l))
(u/set-title-when-missing (z/node l)))))
z/down z/rightmost)
l))
z/up)))))

Expand All @@ -137,19 +143,23 @@
(defmethod open-node Link [ctx ^Link node]
(update-current ctx (fn [loc]
(-> loc (z/append-child {:type :link
:attrs {:href (.getDestination node) :title (.getTitle node)}
:content []}) z/down z/rightmost))))
:content []
:attrs (cond-> {:href (.getDestination node)}
(.getTitle node)
(assoc :title (.getTitle node)))}) z/down z/rightmost))))

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

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

(defmethod open-node Image [ctx ^Image node]
Expand Down
2 changes: 1 addition & 1 deletion src/nextjournal/markdown/parser/impl/utils.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ end"

(defn parse-fence-info [info-str]
(try
(when (string? info-str)
(when (and (string? info-str) (seq info-str))
(let [tokens (-> info-str
str/trim
(str/replace #"[\{\}\,]" "") ;; remove Pandoc/Rmarkdown brackets and commas
Expand Down
60 changes: 60 additions & 0 deletions test/nextjournal/markdown_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,66 @@ some text
#### Section 3.1
"))))))


(deftest link-test
(testing "links with a title"
(is (= {:type :link
:attrs {:href "/some/path" :title "this is the link title"}
:content [{:type :text :text "ahoi"}]}
(-> (md/parse "[ahoi](/some/path 'this is the link title')")
:content first :content first))))
(testing "links with no title"
(is (= {:type :link
:attrs {:href "/some/path"}
:content [{:type :text :text "ahoi"}]}
(-> (md/parse "[ahoi](/some/path)")
:content first :content first))))
(testing "links with nested content"
(is (= {:type :link
:attrs {:href "/some/path"}
:content [{:type :text :text "some "}
{:type :em :content [{:type :text :text "markup"}]}
{:type :text :text " here"}]}
(-> (md/parse "[some _markup_ here](/some/path)")
:content first :content first)))))

(deftest code-test
(testing "inline code"
(is (= {:type :paragraph
:content [{:type :text :text "some "}
{:type :monospace :content [{:text "inline code" :type :text}]}
{:type :text :text " here"}]}
(-> (md/parse "some `inline code` here")
:content first))))
(testing "indented code blocks"
(is (= {:type :code
:content [{:type :text :text "and indented\ncode here\n"}]}
(-> (md/parse "some text
and indented
code here
back to text") :content second)))

(testing "fenced code blocks"
(is (= {:type :code :info ""
:content [{:type :text :text "and fenced\ncode here\n"}]}
(-> (md/parse "some text
```
and fenced
code here
```
back to text") :content second)))

(is (= {:type :code
:content [{:type :text :text "(this is code)\n"}]
:info "clojure id=12345 no-exec"
:language "clojure" :id "12345" :no-exec true}
(-> (md/parse "```clojure id=12345 no-exec
(this is code)
```")
:content first))))))

(comment
(clojure.test/run-test-var #'formulas)

Expand Down

0 comments on commit 6d113cc

Please sign in to comment.