From f6f16b8e3ee34ba2ca1faa2622cbcf545b753c0f Mon Sep 17 00:00:00 2001 From: Andrea Amantini Date: Fri, 16 Aug 2024 11:44:34 +0200 Subject: [PATCH] Support strikethrough syntax --- deps.edn | 1 + src/nextjournal/markdown/parser/impl.clj | 9 +++++++-- test/nextjournal/markdown_test.cljc | 9 +++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/deps.edn b/deps.edn index 97a6e3e..09186b6 100644 --- a/deps.edn +++ b/deps.edn @@ -12,6 +12,7 @@ com.github.commonmark/commonmark-java {:mvn/version "faea2a2a61cfa1b1f26a4e914532b651b3602e34"} com.github.commonmark.commonmark-java/commonmark-ext-task-list-items {:mvn/version "faea2a2a61cfa1b1f26a4e914532b651b3602e34"} com.github.commonmark.commonmark-java/commonmark-ext-gfm-tables {:mvn/version "faea2a2a61cfa1b1f26a4e914532b651b3602e34"} + com.github.commonmark.commonmark-java/commonmark-ext-gfm-strikethrough {:mvn/version "faea2a2a61cfa1b1f26a4e914532b651b3602e34"} ;; jitpack bisect diff --git a/src/nextjournal/markdown/parser/impl.clj b/src/nextjournal/markdown/parser/impl.clj index 51daef0..a2b9bab 100644 --- a/src/nextjournal/markdown/parser/impl.clj +++ b/src/nextjournal/markdown/parser/impl.clj @@ -3,9 +3,10 @@ [nextjournal.markdown.parser.impl.types] [nextjournal.markdown.parser.impl.formulas :as formulas] [nextjournal.markdown.parser.impl.utils :as u]) - (:import (org.commonmark.parser Parser) + (:import (org.commonmark.ext.gfm.tables TableBlock TableBody TableRow TableHead TableCell TablesExtension) + (org.commonmark.ext.gfm.strikethrough Strikethrough StrikethroughExtension) (org.commonmark.ext.task.list.items TaskListItemsExtension TaskListItemMarker) - (org.commonmark.ext.gfm.tables TableBlock TableBody TableRow TableHead TableCell TablesExtension) + (org.commonmark.parser Parser) #_(org.commonmark.ext.footnotes FootnotesExtension FootnoteReference FootnoteDefinition InlineFootnote) (org.commonmark.node Node AbstractVisitor Document @@ -57,6 +58,7 @@ (extensions [(formulas/extension) (TaskListItemsExtension/create) (TablesExtension/create) + (StrikethroughExtension/create) #_(.. (FootnotesExtension/builder) (inlineFootnotes true) (build))]) @@ -144,6 +146,9 @@ :content [{:type :text :text (.getLiteral node)}]}) z/down z/rightmost)))) +(defmethod open-node Strikethrough [ctx _node] + (update-current ctx (fn [loc] (-> loc (z/append-child {:type :strikethrough :content []}) z/down z/rightmost)))) + (defmethod open-node Link [ctx ^Link node] (update-current ctx (fn [loc] (-> loc (z/append-child {:type :link diff --git a/test/nextjournal/markdown_test.cljc b/test/nextjournal/markdown_test.cljc index 628eaf2..c637ab3 100644 --- a/test/nextjournal/markdown_test.cljc +++ b/test/nextjournal/markdown_test.cljc @@ -188,6 +188,15 @@ $$\\int_a^bf(t)dt$$ "two"]]]] (md/->hiccup markdown-text)))) +(deftest strikethrough-test + (testing "single tilde") + ;; Here markdown-it follows Pandoc and uses single ~ for 'sub' (enabled by default https://github.com/markdown-it/markdown-it/tree/master?tab=readme-ov-file#syntax-extensions). + ;; It differs from GFM spec https://github.github.com/gfm/#strikethrough-extension- which allows signle tilde syntax + (testing "double tilde" + (is (= {:type :paragraph, :content [{:type :text, :text "some "} {:type :strikethrough, :content [{:type :text, :text "not ok"}]} {:type :text, :text " text"}]} + (-> (md/parse "some ~~not ok~~ text") + :content first))))) + (deftest tables-test (is (= {:type :doc :content [{:type :table