-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
markdown.cljs
210 lines (185 loc) · 5.68 KB
/
markdown.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
(ns portal.ui.viewer.markdown
(:require ["marked" :refer [marked]]
[clojure.string :as str]
[hickory.core :as h]
[hickory.utils :as utils]
[portal.colors :as c]
[portal.ui.icons :as icons]
[portal.ui.inspector :as ins]
[portal.ui.lazy :as l]
[portal.ui.theme :as theme]
[portal.ui.viewer.hiccup :refer [inspect-hiccup]]))
(declare ->inline)
(declare ->hiccup)
(def ^:dynamic *context* nil)
(defn- unescape [^string string]
(-> string
(.replaceAll "&" "&")
(.replaceAll "<" "<")
(.replaceAll ">" ">")
(.replaceAll """ "\"")
(.replaceAll "'" "'")))
(defn- ->text [^js token]
(unescape (.-text token)))
(defn- parse-html [html]
(with-redefs [utils/html-escape identity]
(h/as-hiccup (first (h/parse-fragment html)))))
(defn- absolute-link? [href]
(or (str/starts-with? href "http")
(str/starts-with? href "https")))
(defn- ->link [^js token]
(let [href (.-href token)]
(->inline
(let [title (.-title token)]
[:a (cond-> {:href href
:target "_blank"
:on-click
(fn [e]
(when-not (absolute-link? href)
(.preventDefault e)))}
title (assoc :title title))])
(.-tokens token))))
(defn- ->image [^js token]
(let [title (.-title token) alt (->text token)]
[:img (cond-> {:src (.-href token)}
title (assoc :title title)
alt (assoc :alt alt))]))
(def ^:private callouts
{"Note"
{:icon icons/info-circle
:color ::c/boolean}
"Warning"
{:icon icons/exclamation-triangle
:color ::c/tag}})
(defn- ->strong [^js token]
(let [theme (:theme *context*)
tokens (->inline [] (.-tokens token))]
(into
(if-let [{:keys [icon color]}
(and (= :blockquote (:parent *context*))
(get callouts (first tokens)))]
[:strong
{:style {:color (get theme color)}}
[icon {:size "1x"}] " "]
[:strong {}])
tokens)))
(defn- ->header [^js token]
[:thead
{}
(persistent!
(reduce
(fn [out ^js cell]
(conj!
out
(->inline
[:th {:align (.-align token)}]
(.-tokens cell))))
(transient [:tr {}])
(.-header token)))])
(defn ->row [^js token ^js row]
(persistent!
(reduce
(fn [out ^js cell]
(conj!
out
(->inline
[:td {:align (.-align token)}]
(.-tokens cell))))
(transient [:tr {}])
row)))
(defn- ->rows [^js token]
(persistent!
(reduce
(fn [out row]
(conj! out (->row token row)))
(transient [:tbody {}])
(.-rows token))))
(defn- ->table [^js token]
[:table (->header token) (->rows token)])
(defn- ->list [^js token]
(let [ordered (.-ordered token)
start (.-start token)
_loose (.-loose token)
tag (if ordered :ol :ul)]
(persistent!
(reduce
(fn [out ^js item]
(let [_checked (.-checked item)
_task (.-task item)]
(conj! out [:li {} (->hiccup (.-tokens item))])))
(transient [tag {:start start}])
(.-items token)))))
(defn- ->code [^js token]
(let [lang (.-lang token)]
[:pre {}
[:code
(cond-> {} (seq lang) (assoc :class lang))
(->text token)]]))
(defn- ->heading [^js token]
(let [tag (keyword (str "h" (.-depth token)))]
(->inline [tag {}] (.-tokens token))))
(defn- ->paragraph [^js token]
(->inline [:p {}] (.-tokens token)))
(defn- ->blockquote [^js token]
(binding [*context* (assoc *context* :parent :blockquote)]
(->hiccup [:blockquote {}] (.-tokens token))))
(defn- ->inline [out tokens]
(reduce
(fn [out ^js token]
(case (.-type token)
"escape" (conj out (->text token))
"html" (conj out (parse-html (.-text token)))
"link" (conj out (->link token))
"image" (conj out (->image token))
"strong" (conj out (->strong token))
"em" (conj out (->inline [:em {}] (.-tokens token)))
"codespan" (conj out [:code {} (->text token)])
"br" (conj out [:br {}])
"del" (conj out (->inline [:del {}] (.-tokens token)))
"text" (conj out (->text token))))
out
tokens))
(defn ->hiccup
([tokens]
(->hiccup [:<>] tokens))
([out tokens]
(reduce
(fn [out ^js token]
(case (.-type token)
"space" out
"hr" (conj out [:hr {}])
"heading" (conj out (->heading token))
"code" (conj out (->code token))
"table" (conj out (->table token))
"blockquote" (conj out (->blockquote token))
"list" (conj out (->list token))
"html" (conj out (parse-html (->text token)))
"paragraph" (conj out (->paragraph token))
"text" (if-let [tokens (.-tokens token)]
(->inline out tokens)
(conj out (->text token)))))
out
tokens)))
(defn ^:no-doc parse-markdown [value]
(->hiccup (.lexer marked value)))
(defn- inspect-markdown* [value]
(binding [*context* {:theme (theme/use-theme)}]
[inspect-hiccup
[:div
{:style
(merge
{:gap 16
:width 896
:max-width "100%"
:display :flex
:box-sizing :border-box
:flex-direction :column}
(get-in (ins/use-context) [:props :style]))}
(parse-markdown value)]]))
(defn inspect-markdown [value]
[l/lazy-render [inspect-markdown* value]])
(def viewer
{:predicate string?
:component inspect-markdown
:name :portal.viewer/markdown
:doc "Parse string as markdown and view as html."})