Skip to content

Commit b6956b3

Browse files
authored
feat: add callback on codeblocks (#191)
* feat: add callback on codeblocks Changed the writing behavior of codeblock transformer (buffering it first) An example of a use case: (markdown/md-to-html-string "```python\ndef f(x):\n return x * 2\n```" :codeblock-callback (fn [code language] (trim (clygments/highlight code language :html)))) Code above would syntax highlight codeblocks without being bothered to convert the Markdown to HTML first then calling libraries like highlight.js with JavaScript References: - https://en.wikipedia.org/wiki/Data_buffer - https://github.com/bfontaine/clygments - https://pygments.org/ * refactor: remove let binding * refactor: remove redundant if check
1 parent 288a19b commit b6956b3

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/cljc/markdown/transformers.cljc

+13-9
Original file line numberDiff line numberDiff line change
@@ -195,23 +195,24 @@
195195
should-close? (assoc :indent-code-end true))]
196196
[text state])))))
197197

198-
(defn codeblock [text {:keys [codeblock codeblock-end indented-code next-line lists] :as state}]
198+
(defn codeblock [text {:keys [codeblock-buf codeblock-lang codeblock-callback codeblock codeblock-end indented-code next-line lists] :as state}]
199199
(let [trimmed (string/trim text)
200200
next-line-closes? (some-> next-line string/trim (string/ends-with? "```"))]
201201
(cond
202202
(and lists codeblock-end)
203-
["" (dissoc state :code :codeblock :codeblock-end)]
203+
["" (dissoc state :code :codeblock :codeblock-end :codeblock-lang :codeblock-buf)]
204204

205205
codeblock-end
206206
[text (-> state
207207
(assoc :last-line-empty? true)
208-
(dissoc :code :codeblock :codeblock-end))]
208+
(dissoc :code :codeblock :codeblock-end :codeblock-lang :codeblock-buf))]
209209

210210
(and next-line-closes? codeblock)
211-
[(str (escape-code (str text \newline (apply str (first (string/split next-line #"```"))))) "</code></pre>")
212-
(assoc state :skip-next-line? (not lists)
213-
:codeblock-end true
214-
:last-line-empty? (not lists))]
211+
(let [buffered-code (str codeblock-buf text)]
212+
[(str (escape-code (str (if codeblock-callback (codeblock-callback buffered-code codeblock-lang) buffered-code) \newline (apply str (first (string/split next-line #"```"))))) "</code></pre>")
213+
(assoc state :skip-next-line? (not lists)
214+
:codeblock-end true
215+
:last-line-empty? (not lists))])
215216

216217
(and
217218
(not indented-code)
@@ -229,10 +230,13 @@
229230
(when next-line-closes? "</code></pre>"))
230231
(if next-line-closes?
231232
(assoc state :codeblock-end true :skip-next-line? true)
232-
(assoc state :code true :codeblock true))])
233+
(assoc state :code true
234+
:codeblock true
235+
:codeblock-lang lang
236+
:codeblock-buf ""))])
233237

234238
codeblock
235-
[(str (escape-code text) "\n") state]
239+
["" (assoc state :codeblock-buf (str codeblock-buf text \newline))]
236240

237241
:default
238242
[text state])))

0 commit comments

Comments
 (0)