Skip to content

Commit 19d945f

Browse files
authored
Render code blocks according to standard (#41)
Fixes #39.
1 parent cfd3d4b commit 19d945f

File tree

3 files changed

+98
-83
lines changed

3 files changed

+98
-83
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Support HTML blocks and inline HTML (see [#7](https://github.com/nextjournal/markdown/issues/7))
77
* Bump commonmark to 0.24.0
88
* Bump markdown-it to 14.1.0
9+
* Render `:code` according to spec into `<pre>` and `<code>` block with language class (see [#39](https://github.com/nextjournal/markdown/issues/39))
910

1011
## 0.6.157
1112

src/nextjournal/markdown/transform.cljc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,12 @@ a paragraph
8383
img-markup)))
8484

8585
;; code
86-
:code (partial into-markup [:pre.viewer-code.not-prose])
86+
:code (fn [_ {:keys [language] :as m}]
87+
[:pre
88+
[(if language
89+
(keyword (str "code.language-" language))
90+
:code)
91+
(-> m :content first :text)]])
8792

8893
;; breaks
8994
:softbreak (constantly " ")

test/nextjournal/markdown_test.cljc

Lines changed: 91 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ some **strong** _assertion_ and a [link] and a $\\pi$ formula
4343
(+ 1 2 3)
4444
```
4545
46+
```
47+
no language
48+
```
49+
4650
$$\\int_a^bf(t)dt$$
4751
4852
* one
@@ -69,61 +73,64 @@ $$\\int_a^bf(t)dt$$
6973

7074
(deftest parse-test
7175
(testing "ingests markdown returns nested nodes"
72-
(is (= {:type :doc
73-
:footnotes []
74-
:title "🎱 Hello"
75-
:content [{:content [{:text "🎱 Hello"
76-
:type :text}]
77-
:heading-level 1
78-
:attrs {:id "hello"}
79-
:emoji "🎱"
80-
:type :heading}
81-
{:content [{:text "some "
82-
:type :text}
83-
{:content [{:text "strong"
84-
:type :text}]
85-
:type :strong}
86-
{:text " "
87-
:type :text}
88-
{:content [{:text "assertion"
89-
:type :text}]
90-
:type :em}
91-
{:text " and a "
92-
:type :text}
93-
{:attrs {:href "/path/to/something"}
94-
:content [{:text "link"
95-
:type :text}]
96-
:type :link}
97-
{:text " and a "
98-
:type :text}
99-
{:text "\\pi"
100-
:type :formula}
101-
{:text " formula"
102-
:type :text}]
103-
:type :paragraph}
104-
{:content [{:text "(+ 1 2 3)\n" :type :text}]
105-
:info "clojure"
106-
:language "clojure"
107-
:type :code}
108-
{:text "\\int_a^bf(t)dt"
109-
:type :block-formula}
110-
{:content [{:content [{:content [{:text "one"
111-
:type :text}]
112-
:type :paragraph}]
113-
:type :list-item}
114-
{:content [{:content [{:text "two"
115-
:type :text}]
116-
:type :paragraph}]
117-
:type :list-item}]
118-
:type :bullet-list}]
119-
:toc {:type :toc
120-
:children [{:type :toc
121-
:content [{:type :text, :text "🎱 Hello"}]
122-
:heading-level 1
123-
:attrs {:id "hello"}
124-
:emoji "🎱"
125-
:path [:content 0]}]}}
126-
(md/parse markdown-text))))
76+
(is (match?
77+
{:type :doc
78+
:footnotes []
79+
:title "🎱 Hello"
80+
:content [{:content [{:text "🎱 Hello"
81+
:type :text}]
82+
:heading-level 1
83+
:attrs {:id "hello"}
84+
:emoji "🎱"
85+
:type :heading}
86+
{:content [{:text "some "
87+
:type :text}
88+
{:content [{:text "strong"
89+
:type :text}]
90+
:type :strong}
91+
{:text " "
92+
:type :text}
93+
{:content [{:text "assertion"
94+
:type :text}]
95+
:type :em}
96+
{:text " and a "
97+
:type :text}
98+
{:attrs {:href "/path/to/something"}
99+
:content [{:text "link"
100+
:type :text}]
101+
:type :link}
102+
{:text " and a "
103+
:type :text}
104+
{:text "\\pi"
105+
:type :formula}
106+
{:text " formula"
107+
:type :text}]
108+
:type :paragraph}
109+
{:content [{:text "(+ 1 2 3)\n" :type :text}]
110+
:info "clojure"
111+
:language "clojure"
112+
:type :code}
113+
{:content [{:text "no language\n" :type :text}]
114+
:type :code}
115+
{:text "\\int_a^bf(t)dt"
116+
:type :block-formula}
117+
{:content [{:content [{:content [{:text "one"
118+
:type :text}]
119+
:type :paragraph}]
120+
:type :list-item}
121+
{:content [{:content [{:text "two"
122+
:type :text}]
123+
:type :paragraph}]
124+
:type :list-item}]
125+
:type :bullet-list}]
126+
:toc {:type :toc
127+
:children [{:type :toc
128+
:content [{:type :text, :text "🎱 Hello"}]
129+
:heading-level 1
130+
:attrs {:id "hello"}
131+
:emoji "🎱"
132+
:path [:content 0]}]}}
133+
(md/parse markdown-text))))
127134

128135
(testing "parses internal links / plays well with todo lists"
129136
(is (match? {:type :doc
@@ -175,34 +182,36 @@ $$\\int_a^bf(t)dt$$
175182

176183
(deftest ->hiccup-test
177184
(testing "ingests markdown returns hiccup"
178-
(is (= [:div
179-
[:h1 {:id "hello"} "🎱 Hello"]
185+
(is (match?
186+
[:div
187+
[:h1 {:id "hello"} "🎱 Hello"]
188+
[:p
189+
"some "
190+
[:strong
191+
"strong"]
192+
" "
193+
[:em
194+
"assertion"]
195+
" and a "
196+
[:a
197+
{:href "/path/to/something"}
198+
"link"]
199+
" and a "
200+
[:span.formula
201+
"\\pi"]
202+
" formula"]
203+
[:pre [:code.language-clojure "(+ 1 2 3)\n"]]
204+
[:pre [:code "no language\n"]]
205+
[:figure.formula
206+
"\\int_a^bf(t)dt"]
207+
[:ul
208+
[:li
180209
[:p
181-
"some "
182-
[:strong
183-
"strong"]
184-
" "
185-
[:em
186-
"assertion"]
187-
" and a "
188-
[:a
189-
{:href "/path/to/something"}
190-
"link"]
191-
" and a "
192-
[:span.formula
193-
"\\pi"]
194-
" formula"]
195-
[:pre.viewer-code.not-prose "(+ 1 2 3)\n"]
196-
[:figure.formula
197-
"\\int_a^bf(t)dt"]
198-
[:ul
199-
[:li
200-
[:p
201-
"one"]]
202-
[:li
203-
[:p
204-
"two"]]]]
205-
(md/->hiccup markdown-text)))))
210+
"one"]]
211+
[:li
212+
[:p
213+
"two"]]]]
214+
(md/->hiccup markdown-text)))))
206215

207216
(deftest strikethrough-test
208217
(testing "single tilde")

0 commit comments

Comments
 (0)