Skip to content

Commit f632f8b

Browse files
authored
Add default renderers for html nodes pointing at the README (#70)
Add default renderers for html nodes pointing at the README (#69)
1 parent 9804f0a commit f632f8b

5 files changed

Lines changed: 45 additions & 10 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
## Unreleased
66

7-
...
7+
- Point at the README from the default `:html-block` and `:html-inline` renderers instead of reporting an unknown node type [#69](https://github.com/nextjournal/markdown/issues/69)
88

99
## 0.7.225
1010

‎README.md‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ The following options are available that affect parsing:
133133
134134
### HTML blocks and HTML inlines
135135
136-
Typically you'd want to customize the rendering of `:html-inline` and `:html` since these need to be rendered to raw strings:
136+
There is no default renderer for `:html-inline` and `:html-block`, since emitting raw strings is left to the consumer. Supply your own:
137137
138138
``` clojure
139139
(require '[hiccup2.core :as hiccup])
@@ -149,6 +149,20 @@ Typically you'd want to customize the rendering of `:html-inline` and `:html` si
149149
"<div><img src=\"...\"/></div>"
150150
```
151151
152+
Per CommonMark, an HTML block ends at the first blank line, and everything up to that point stays raw. Put a blank line after the opening tag to have the contents parsed as markdown:
153+
154+
``` clojure
155+
(str (hiccup/html (md/->hiccup renderers "<aside>\n\n*emphasis*\n\n</aside>")))
156+
157+
#_#_=>
158+
"<div><aside><p><em>emphasis</em></p></aside></div>"
159+
160+
(str (hiccup/html (md/->hiccup renderers "<aside>\n*emphasis*\n\n</aside>")))
161+
162+
#_#_=>
163+
"<div><aside>\n*emphasis*</aside></div>"
164+
```
165+
152166
## Transforming to other targets
153167
154168
We've built hiccup transformation in for convenience, but the same approach can be used to target [more formats](https://nextjournal.github.io/markdown/notebooks/pandoc).

‎src/nextjournal/markdown/impl.cljs‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,6 @@ _this #should be a tag_, but this [_actually #foo shouldnt_](/bar/) is not."
250250
(defmethod apply-token "link_close" [doc _token] (close-node doc))
251251
(defmethod apply-token "code_inline" [doc ^js token] (-> doc (open-node :monospace) (push-node (text-node (.-content token))) close-node))
252252

253-
;; html
254-
(defmethod apply-token "html_inline" [doc token]
255-
(-> doc (u/update-current-loc z/append-child {:type :html-inline :content [(text-node (.-content token))]})))
256-
257-
(defmethod apply-token "html_block" [doc token]
258-
(-> doc (u/update-current-loc z/append-child {:type :html-block :content [(text-node (.-content token))]})))
259-
260253
;; html
261254
(defmethod apply-token "html_inline" [doc token]
262255
(-> doc (u/update-current-loc z/append-child {:type :html-inline :content [(text-node (.-content token))]})))

‎src/nextjournal/markdown/transform.cljc‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ a paragraph
7272
(seq content) (conj [:span.title {:data-level heading-level} (:id node)])
7373
(seq children) (conj (into [:ul] (map (partial ->hiccup ctx)) children)))))))))
7474

75+
;; emitting raw HTML unescaped is left to the consumer
76+
(defn- missing-html-renderer [_ctx {:keys [type]}]
77+
[:span.message.red
78+
[:strong (str "No default renderer for '" type "'.")]
79+
" See https://github.com/nextjournal/markdown#html-blocks-and-html-inlines"])
80+
7581
(def default-hiccup-renderers
7682
{:doc (partial into-markup [:div])
7783
:heading (fn [ctx {:as node :keys [attrs]}] (-> (heading-markup node) (conj attrs) (into-markup ctx node)))
@@ -83,6 +89,10 @@ a paragraph
8389
:blockquote (partial into-markup [:blockquote])
8490
:ruler (constantly [:hr])
8591

92+
;; html
93+
:html-block missing-html-renderer
94+
:html-inline missing-html-renderer
95+
8696
;; by default we always wrap images in paragraph to restore compliance with commonmark
8797
:image (fn [{:as _ctx ::keys [parent]} {:as node :keys [attrs]}]
8898
(let [img-markup [:img (assoc attrs :alt (->text node))]]

‎test/nextjournal/markdown_test.cljc‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1089,7 +1089,25 @@ Bye") :content)))
10891089
10901090
line
10911091
1092-
link</a>")))))))
1092+
link</a>"))))))
1093+
(testing "default renderers point at the README instead of failing with an unknown type"
1094+
(is (match? [:div
1095+
[:span.message.red
1096+
[:strong "No default renderer for ':html-block'."]
1097+
#".*markdown#html-blocks-and-html-inlines"]
1098+
[:p [:em "foo"]]
1099+
[:span.message.red
1100+
[:strong "No default renderer for ':html-block'."]
1101+
#".*markdown#html-blocks-and-html-inlines"]]
1102+
(md/->hiccup "<del>\n\n*foo*\n\n</del>"))))
1103+
(testing "an html block ends at the first blank line, https://spec.commonmark.org/0.30/#example-167"
1104+
(is (match? [{:type :html-block :content [{:text #"<del>\s?"}]}
1105+
{:type :paragraph :content [{:type :em}]}
1106+
{:type :html-block :content [{:text #"</del>\s?"}]}]
1107+
(:content (md/parse "<del>\n\n*foo*\n\n</del>"))))
1108+
(is (match? [{:type :html-block :content [{:text #"<del>\n\*foo\*\s?"}]}
1109+
{:type :html-block :content [{:text #"</del>\s?"}]}]
1110+
(:content (md/parse "<del>\n*foo*\n\n</del>"))))))
10931111

10941112
(deftest disable-custom-extensions-test
10951113
(is (= {:toc {:type :toc},

0 commit comments

Comments
 (0)