@@ -24,8 +24,7 @@ We adhere to [CommonMark Spec](https://spec.commonmark.org/0.30/) and comply wit
24
24
25
25
``` clojure
26
26
(ns hello-markdown
27
- (:require [nextjournal.markdown :as md]
28
- [nextjournal.markdown.transform :as md.transform]))
27
+ (:require [nextjournal.markdown :as md]))
29
28
```
30
29
31
30
Parsing markdown into an AST:
@@ -43,79 +42,104 @@ Parsing markdown into an AST:
43
42
---
44
43
" ))
45
44
46
- ; ; =>
47
- {:type :doc ,
48
- :content [{:type :blockquote ,
49
- :content [{:type :paragraph ,
50
- :content [{:type :text ,
51
- :text " et tout autour, la longue cohorte de ses personnage, avec leur histoire, leur passé, leurs légendes:" }]}
52
- {:type :numbered-list ,
53
- :content [{:type :list-item ,
54
- :content [{:type :plain ,
55
- :content [{:type :text ,
56
- :text " Pélage vainqueur d'Alkhamah se faisant couronner à Covadonga" }]}]}
57
- {:type :list-item ,
58
- :content [{:type :plain ,
59
- :content [{:type :text ,
60
- :text " La cantatrice exilée de Russie suivant Schönberg à Amsterdam" }]}]}
61
- {:type :list-item ,
62
- :content [{:type :plain ,
63
- :content [{:type :text ,
64
- :text " Le petit chat sourd aux yeux vairons vivant au dernier étage" }]}]}]}]}
65
- {:type :paragraph ,
66
- :content [{:type :strong , :content [{:type :text , :text " Georges Perec" }]}
67
- {:type :text , :text " , " }
68
- {:type :em , :content [{:type :text , :text " La Vie mode d'emploi" }]}
69
- {:type :text , :text " ." }]}
70
- {:type :ruler }]}
45
+ #_#_=>
46
+ {:type :doc
47
+ :content
48
+ [{:type :blockquote ,
49
+ :content
50
+ [{:type :paragraph ,
51
+ :content
52
+ [{:type :text ,
53
+ :text
54
+ " et tout autour, la longue cohorte de ses personnages, avec leur histoire, leur passé, leurs légendes:" }]}
55
+ {:type :numbered-list ,
56
+ :content
57
+ [{:type :list-item ,
58
+ :content
59
+ [{:type :plain ,
60
+ :content
61
+ [{:type :text ,
62
+ :text
63
+ " Pélage vainqueur d'Alkhamah se faisant couronner à Covadonga" }]}]}
64
+ {:type :list-item ,
65
+ :content
66
+ [{:type :plain ,
67
+ :content
68
+ [{:type :text ,
69
+ :text
70
+ " La cantatrice exilée de Russie suivant Schönberg à Amsterdam" }]}]}
71
+ {:type :list-item ,
72
+ :content
73
+ [{:type :plain ,
74
+ :content
75
+ [{:type :text ,
76
+ :text
77
+ " Le petit chat sourd aux yeux vairons vivant au dernier étage" }]}]}
78
+ {:type :list-item ,
79
+ :content
80
+ [{:type :plain , :content [{:type :text , :text " ..." }]}]}]}]}
81
+ {:type :paragraph ,
82
+ :content
83
+ [{:type :strong , :content [{:type :text , :text " Georges Perec" }]}
84
+ {:type :text , :text " , " }
85
+ {:type :em , :content [{:type :text , :text " La Vie mode d'emploi" }]}
86
+ {:type :text , :text " ." }]}
87
+ {:type :ruler }]}
71
88
```
72
89
73
- and transform that AST into ` hiccup ` syntax:
90
+ ## Hiccup rendering
91
+
92
+ To transform the above AST into ` hiccup ` syntax:
74
93
75
94
``` clojure
76
95
(md/->hiccup data)
77
- ; ; =>
96
+ #_#_ =>
78
97
[:div
79
98
[:blockquote
80
- [:p " et tout autour, la longue cohorte de ses personnage, avec leur histoire, leur passé, leurs légendes:" ]
99
+ [:p
100
+ " et tout autour, la longue cohorte de ses personnages, avec leur histoire, leur passé, leurs légendes:" ]
81
101
[:ol
82
- [:li [:<> " Pélage vainqueur d'Alkhamah se faisant couronner à Covadonga" ]]
83
- [:li [:<> " La cantatrice exilée de Russie suivant Schönberg à Amsterdam" ]]
84
- [:li [:<> " Le petit chat sourd aux yeux vairons vivant au dernier étage" ]]]]
102
+ [:li
103
+ ("Pélage vainqueur d'Alkhamah se faisant couronner à Covadonga" )]
104
+ [:li
105
+ (" La cantatrice exilée de Russie suivant Schönberg à Amsterdam" )]
106
+ [:li
107
+ (" Le petit chat sourd aux yeux vairons vivant au dernier étage" )]
108
+ [:li (" ..." )]]]
85
109
[:p [:strong " Georges Perec" ] " , " [:em " La Vie mode d'emploi" ] " ." ]
86
110
[:hr]]
87
111
```
88
112
89
- The transformation of markdown node types can be customised like this:
113
+ The transformation of markdown node types can be customized like this:
90
114
91
115
```clojure
92
116
^{:nextjournal.clerk/viewer 'nextjournal.clerk.viewer/html-viewer}
93
117
(md/->hiccup
94
- (assoc md.transform /default-hiccup-renderers
118
+ (assoc md/default-hiccup-renderers
95
119
;; :doc specify a custom container for the whole doc
96
- :doc (partial md.transform /into-markup [:div.viewer-markdown ])
120
+ :doc (partial md/into-hiccup [:div.viewer-markdown])
97
121
;; :text is funkier when it's zinc toned
98
- :text (fn [_ctx node] [:span {:style {:color " #71717a" }} (:text node)])
99
- ; ; :plain fragments might be nice, but paragraphs help when no reagent is at hand
100
- :plain (partial md.transform/into-markup [:p {:style {:margin-top " -1.2rem" }}])
122
+ :text (fn [_ctx node] [:span {:style {:color " #71717 a" }} (md/node->text node)])
101
123
;; :ruler gets to be funky, too
102
124
:ruler (constantly [:hr {:style {:border " 2 px dashed #71717 a" }}]))
103
125
data)
104
126
```
105
127
128
+ ### HTML blocks and HTML inlines
129
+
106
130
Typically you'd want to customize the rendering of `:html-inline` and `:html` since these need to be rendered to raw strings:
107
131
108
132
``` clojure
109
133
(require '[hiccup2.core :as hiccup])
110
134
111
135
(def renderers
112
- (assoc md.transform /default-hiccup-renderers
113
- :html-inline (comp hiccup/raw md.transform/ ->text)
114
- :html-block (comp hiccup/raw md.transform/ ->text)))
136
+ (assoc md/default-hiccup-renderers
137
+ :html-inline (comp hiccup/raw md/node ->text)
138
+ :html-block (comp hiccup/raw md/node ->text)))
115
139
116
140
(str (hiccup/html (md/->hiccup renderers " <img src=\"...\"/>" )))
117
141
118
- ; ; =>
142
+ #_#_ =>
119
143
" <div><img src=\"...\"/></div>"
120
144
```
121
145
0 commit comments