Skip to content

Commit bdd31dd

Browse files
authored
Public API readme and notebooks (#50)
1 parent d8a0bf8 commit bdd31dd

File tree

3 files changed

+71
-47
lines changed

3 files changed

+71
-47
lines changed

README.md

Lines changed: 67 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ We adhere to [CommonMark Spec](https://spec.commonmark.org/0.30/) and comply wit
2424

2525
```clojure
2626
(ns hello-markdown
27-
(:require [nextjournal.markdown :as md]
28-
[nextjournal.markdown.transform :as md.transform]))
27+
(:require [nextjournal.markdown :as md]))
2928
```
3029

3130
Parsing markdown into an AST:
@@ -43,79 +42,104 @@ Parsing markdown into an AST:
4342
---
4443
"))
4544

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}]}
7188
```
7289

73-
and transform that AST into `hiccup` syntax:
90+
## Hiccup rendering
91+
92+
To transform the above AST into `hiccup` syntax:
7493

7594
```clojure
7695
(md/->hiccup data)
77-
;; =>
96+
#_#_=>
7897
[:div
7998
[: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:"]
81101
[: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 ("...")]]]
85109
[:p [:strong "Georges Perec"] ", " [:em "La Vie mode d'emploi"] "."]
86110
[:hr]]
87111
```
88112
89-
The transformation of markdown node types can be customised like this:
113+
The transformation of markdown node types can be customized like this:
90114
91115
```clojure
92116
^{:nextjournal.clerk/viewer 'nextjournal.clerk.viewer/html-viewer}
93117
(md/->hiccup
94-
(assoc md.transform/default-hiccup-renderers
118+
(assoc md/default-hiccup-renderers
95119
;; :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])
97121
;; :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 "#71717a"}} (md/node->text node)])
101123
;; :ruler gets to be funky, too
102124
:ruler (constantly [:hr {:style {:border "2px dashed #71717a"}}]))
103125
data)
104126
```
105127
128+
### HTML blocks and HTML inlines
129+
106130
Typically you'd want to customize the rendering of `:html-inline` and `:html` since these need to be rendered to raw strings:
107131
108132
``` clojure
109133
(require '[hiccup2.core :as hiccup])
110134
111135
(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)))
115139
116140
(str (hiccup/html (md/->hiccup renderers "<img src=\"...\"/>")))
117141
118-
;; =>
142+
#_#_=>
119143
"<div><img src=\"...\"/></div>"
120144
```
121145

notebooks/pandoc.clj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
[nextjournal.clerk :as clerk]
1010
[nextjournal.clerk.viewer :as v]
1111
[nextjournal.markdown :as md]
12-
[nextjournal.markdown.transform :as md.transform]
1312
[nextjournal.markdown.utils :as u]))
1413

1514
;; From the [docs](https://pandoc.org/MANUAL.html#description):
@@ -45,7 +44,7 @@
4544
:heading (fn [{:keys [content heading-level]}] {:t "Header" :c [heading-level ["id" [] []] (map md->pandoc content)]})
4645
:paragraph (fn [{:keys [content]}] {:t "Para" :c (map md->pandoc content)})
4746
:plain (fn [{:keys [content]}] {:t "Plain" :c (map md->pandoc content)})
48-
:code (fn [{:as node :keys [language]}] {:t "CodeBlock" :c [["" [language "code"] []] (md.transform/->text node)]})
47+
:code (fn [{:as node :keys [language]}] {:t "CodeBlock" :c [["" [language "code"] []] (md/node->text node)]})
4948
:block-formula (fn [{:keys [text]}] {:t "Para" :c [{:t "Math" :c [{:t "DisplayMath"} text]}]})
5049

5150
:em (fn [{:keys [content]}] {:t "Emph" :c (map md->pandoc content)})
@@ -166,7 +165,7 @@ this _is_ a
166165
(let [[_meta code] (:c node)]
167166
{:type :monospace :content [(u/text-node code)]}))
168167
:CodeBlock (fn [node]
169-
(let [[[_id classes _meta] code] (:c node)]
168+
(let [[[_id _classes _meta] code] (:c node)]
170169
{:type :code
171170
:content [(u/text-node code)]}))
172171
:SoftBreak (constantly {:type :softbreak})

src/nextjournal/markdown/transform.cljc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ a paragraph
110110
:bullet-list (partial into-markup [:ul])
111111
:list-item (partial into-markup [:li])
112112
:todo-list (partial into-markup [:ul.contains-task-list])
113-
:numbered-list (fn [ctx {:as node :keys [attrs]}] (into-markup [:ol attrs] ctx node))
113+
:numbered-list (fn [ctx {:as node :keys [attrs]}] (into-markup (cond-> [:ol]
114+
attrs (conj attrs)) ctx node))
114115

115116
:todo-item (fn [ctx {:as node :keys [attrs]}]
116117
(into-markup [:li [:input {:type "checkbox" :checked (:checked attrs)}]] ctx node))

0 commit comments

Comments
 (0)