Skip to content

Commit d8a0bf8

Browse files
borkdudezampino
andauthored
Re-expose transform ns in main ns (#49)
* Re-expose transform ns in main ns * Fix alignment metadata in clj impl / add docstrings --------- Co-authored-by: Andrea Amantini <[email protected]>
1 parent 973af49 commit d8a0bf8

File tree

5 files changed

+89
-88
lines changed

5 files changed

+89
-88
lines changed

API.md

Lines changed: 40 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
# Table of contents
22
- [`nextjournal.markdown`](#nextjournal.markdown) - Markdown as data.
3-
- [`->hiccup`](#nextjournal.markdown/->hiccup) - Turns a markdown string into hiccup.
3+
- [`->hiccup`](#nextjournal.markdown/->hiccup) - Turns a markdown string or document node into hiccup.
4+
- [`default-hiccup-renderers`](#nextjournal.markdown/default-hiccup-renderers) - Default map of node type -> hiccup renderers, to be used with <code>-&gt;hiccup</code>.
45
- [`empty-doc`](#nextjournal.markdown/empty-doc) - Empty document to be used with <code>parse*</code>.
6+
- [`into-hiccup`](#nextjournal.markdown/into-hiccup) - Helper function to be used with custom hiccup renderer.
7+
- [`node->text`](#nextjournal.markdown/node->text) - Convert node into text.
58
- [`parse`](#nextjournal.markdown/parse) - Turns a markdown string into an AST of nested clojure data.
69
- [`parse*`](#nextjournal.markdown/parse*) - Turns a markdown string into an AST of nested clojure data.
7-
- [`nextjournal.markdown.transform`](#nextjournal.markdown.transform) - transform markdown data as returned by <code>nextjournal.markdown/parse</code> into other formats, currently: * hiccup.
8-
- [`->hiccup`](#nextjournal.markdown.transform/->hiccup)
9-
- [`->text`](#nextjournal.markdown.transform/->text) - Convert node into text.
10-
- [`default-hiccup-renderers`](#nextjournal.markdown.transform/default-hiccup-renderers)
11-
- [`into-markup`](#nextjournal.markdown.transform/into-markup) - Takes a hiccup vector, a context and a node, puts node's <code>:content</code> into markup mapping through <code>-&gt;hiccup</code>.
12-
- [`table-alignment`](#nextjournal.markdown.transform/table-alignment)
13-
- [`toc->hiccup`](#nextjournal.markdown.transform/toc->hiccup)
10+
- [`table-alignment`](#nextjournal.markdown/table-alignment) - Takes a table-ish node, returns a map suitable for hiccup style attributes with a :text-align property.
11+
- [`toc->hiccup`](#nextjournal.markdown/toc->hiccup) - Transform a toc node into hiccup data, suitable for using as renderer function in hiccup transform, see [->hiccup](#markdown.transform/toc->hiccup).
1412
- [`nextjournal.markdown.utils`](#nextjournal.markdown.utils)
1513
- [`block-formula`](#nextjournal.markdown.utils/block-formula)
1614
- [`empty-doc`](#nextjournal.markdown.utils/empty-doc) - The empty doc.
@@ -37,12 +35,21 @@ Markdown as data
3735
``` clojure
3836

3937
(->hiccup markdown)
40-
(->hiccup ctx markdown)
38+
(->hiccup hiccup-renderers markdown)
4139
```
4240
Function.
4341

44-
Turns a markdown string into hiccup.
45-
<p><sub><a href="https://github.com/nextjournal/markdown/blob/main/src/nextjournal/markdown.cljc#L48-L55">Source</a></sub></p>
42+
Turns a markdown string or document node into hiccup. Optionally takes
43+
`hiccup-renderers` as first argument.
44+
<p><sub><a href="https://github.com/nextjournal/markdown/blob/main/src/nextjournal/markdown.cljc#L45-L53">Source</a></sub></p>
45+
46+
## <a name="nextjournal.markdown/default-hiccup-renderers">`default-hiccup-renderers`</a>
47+
48+
49+
50+
51+
Default map of node type -> hiccup renderers, to be used with [`->hiccup`](#nextjournal.markdown/->hiccup)
52+
<p><sub><a href="https://github.com/nextjournal/markdown/blob/main/src/nextjournal/markdown.cljc#L41-L43">Source</a></sub></p>
4653

4754
## <a name="nextjournal.markdown/empty-doc">`empty-doc`</a>
4855

@@ -52,6 +59,22 @@ Turns a markdown string into hiccup.
5259
Empty document to be used with [`parse*`](#nextjournal.markdown/parse*)
5360
<p><sub><a href="https://github.com/nextjournal/markdown/blob/main/src/nextjournal/markdown.cljc#L8-L10">Source</a></sub></p>
5461

62+
## <a name="nextjournal.markdown/into-hiccup">`into-hiccup`</a>
63+
64+
65+
66+
67+
Helper function to be used with custom hiccup renderer.
68+
<p><sub><a href="https://github.com/nextjournal/markdown/blob/main/src/nextjournal/markdown.cljc#L59-L61">Source</a></sub></p>
69+
70+
## <a name="nextjournal.markdown/node->text">`node->text`</a>
71+
72+
73+
74+
75+
Convert node into text.
76+
<p><sub><a href="https://github.com/nextjournal/markdown/blob/main/src/nextjournal/markdown.cljc#L55-L57">Source</a></sub></p>
77+
5578
## <a name="nextjournal.markdown/parse">`parse`</a>
5679
``` clojure
5780

@@ -80,67 +103,21 @@ Turns a markdown string into an AST of nested clojure data.
80103
e.g. `(-> empty-doc (parse* text-1) (parse* text-2))`.
81104
<p><sub><a href="https://github.com/nextjournal/markdown/blob/main/src/nextjournal/markdown.cljc#L12-L20">Source</a></sub></p>
82105

83-
-----
84-
# <a name="nextjournal.markdown.transform">nextjournal.markdown.transform</a>
106+
## <a name="nextjournal.markdown/table-alignment">`table-alignment`</a>
85107

86108

87-
transform markdown data as returned by [`nextjournal.markdown/parse`](#nextjournal.markdown/parse) into other formats, currently:
88-
* hiccup
89109

90110

111+
Takes a table-ish node, returns a map suitable for hiccup style attributes with a :text-align property.
112+
<p><sub><a href="https://github.com/nextjournal/markdown/blob/main/src/nextjournal/markdown.cljc#L63-L65">Source</a></sub></p>
91113

114+
## <a name="nextjournal.markdown/toc->hiccup">`toc->hiccup`</a>
92115

93-
## <a name="nextjournal.markdown.transform/->hiccup">`->hiccup`</a>
94-
``` clojure
95116

96-
(->hiccup node)
97-
(->hiccup ctx {:as node, t :type})
98-
```
99-
Function.
100-
<p><sub><a href="https://github.com/nextjournal/markdown/blob/main/src/nextjournal/markdown/transform.cljc#L153-L162">Source</a></sub></p>
101117

102-
## <a name="nextjournal.markdown.transform/->text">`->text`</a>
103-
``` clojure
104118

105-
(->text node)
106-
(->text ctx {:as _node, :keys [type text content]})
107-
```
108-
Function.
109-
110-
Convert node into text
111-
<p><sub><a href="https://github.com/nextjournal/markdown/blob/main/src/nextjournal/markdown/transform.cljc#L8-L14">Source</a></sub></p>
112-
113-
## <a name="nextjournal.markdown.transform/default-hiccup-renderers">`default-hiccup-renderers`</a>
114-
115-
116-
117-
<p><sub><a href="https://github.com/nextjournal/markdown/blob/main/src/nextjournal/markdown/transform.cljc#L71-L151">Source</a></sub></p>
118-
119-
## <a name="nextjournal.markdown.transform/into-markup">`into-markup`</a>
120-
``` clojure
121-
122-
(into-markup mkup ctx {:as node, :keys [text content]})
123-
```
124-
Function.
125-
126-
Takes a hiccup vector, a context and a node, puts node's `:content` into markup mapping through [`->hiccup`](#nextjournal.markdown.transform/->hiccup).
127-
<p><sub><a href="https://github.com/nextjournal/markdown/blob/main/src/nextjournal/markdown/transform.cljc#L30-L37">Source</a></sub></p>
128-
129-
## <a name="nextjournal.markdown.transform/table-alignment">`table-alignment`</a>
130-
``` clojure
131-
132-
(table-alignment {:keys [style]})
133-
```
134-
Function.
135-
<p><sub><a href="https://github.com/nextjournal/markdown/blob/main/src/nextjournal/markdown/transform.cljc#L21-L24">Source</a></sub></p>
136-
137-
## <a name="nextjournal.markdown.transform/toc->hiccup">`toc->hiccup`</a>
138-
``` clojure
139-
140-
("toc->hiccup[{:as ctx ::keys [parent]} {:as node :keys [attrs content children]}]")
141-
```
142-
Function.
143-
<p><sub><a href="https://github.com/nextjournal/markdown/blob/main/src/nextjournal/markdown/transform.cljc#L39-L51">Source</a></sub></p>
119+
Transform a toc node into hiccup data, suitable for using as renderer function in hiccup transform, see [->hiccup](#markdown.transform/toc->hiccup)
120+
<p><sub><a href="https://github.com/nextjournal/markdown/blob/main/src/nextjournal/markdown.cljc#L67-L69">Source</a></sub></p>
144121

145122
-----
146123
# <a name="nextjournal.markdown.utils">nextjournal.markdown.utils</a>

src/nextjournal/markdown.cljc

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,37 @@
3636
::impl/path
3737
::impl/root))))
3838

39-
(comment
40-
(-> u/empty-doc
41-
(parse* "# title
42-
* one
43-
* two
44-
")
45-
(parse* "new par")
46-
(parse* "new par")))
39+
;; Transform
40+
41+
(def default-hiccup-renderers
42+
"Default map of node type -> hiccup renderers, to be used with `->hiccup`"
43+
markdown.transform/default-hiccup-renderers)
4744

4845
(defn ->hiccup
49-
"Turns a markdown string into hiccup."
50-
([markdown] (->hiccup markdown.transform/default-hiccup-renderers markdown))
51-
([ctx markdown]
46+
"Turns a markdown string or document node into hiccup. Optionally takes
47+
`hiccup-renderers` as first argument."
48+
([markdown] (->hiccup default-hiccup-renderers markdown))
49+
([hiccup-renderers markdown]
5250
(let [parsed (if (string? markdown)
5351
(parse markdown)
5452
markdown)]
55-
(markdown.transform/->hiccup ctx parsed))))
53+
(markdown.transform/->hiccup hiccup-renderers parsed))))
54+
55+
(def node->text
56+
"Convert node into text."
57+
markdown.transform/->text)
58+
59+
(def into-hiccup
60+
"Helper function to be used with custom hiccup renderer."
61+
markdown.transform/into-markup)
62+
63+
(def table-alignment
64+
"Takes a table-ish node, returns a map suitable for hiccup style attributes with a :text-align property."
65+
markdown.transform/table-alignment)
66+
67+
(def toc->hiccup
68+
"Transform a toc node into hiccup data, suitable for using as renderer function in hiccup transform, see [->hiccup](#markdown.transform/toc->hiccup)"
69+
markdown.transform/toc->hiccup)
5670

5771
(comment
5872
(parse "# 🎱 Hello")

src/nextjournal/markdown/impl.clj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,7 @@
183183
(u/zopen-node loc (cond-> {:type (if (.isHeader node) :table-header :table-data)
184184
:content []}
185185
alignment
186-
(assoc :alignment alignment
187-
;; TODO: drop/deprecate this, compute in transform
188-
:attrs {:style (str "text-align:" (name alignment))})))))))
186+
(assoc :alignment alignment)))))))
189187

190188
(defmethod open-node FootnoteDefinition [ctx ^FootnoteDefinition node]
191189
(-> ctx

src/nextjournal/markdown/transform.cljc

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(ns nextjournal.markdown.transform
22
"transform markdown data as returned by `nextjournal.markdown/parse` into other formats, currently:
3-
* hiccup")
3+
* hiccup"
4+
{:skip-wiki true})
45

56
;; helpers
67
(defn- guard [pred val] (when (pred val) val))
@@ -18,10 +19,13 @@
1819
[{:as doc :keys [toc]}]
1920
(update doc :content (partial into [] (map (fn [{:as node t :type}] (if (= :toc t) toc node))))))
2021

21-
(defn table-alignment [{:keys [style]}]
22-
(when (string? style)
23-
(let [[_ alignment] (re-matches #"^text-align:(.+)$" style)]
24-
(when alignment {:text-align alignment}))))
22+
(defn table-alignment [{:keys [attrs alignment]}]
23+
(cond
24+
(string? (:style attrs))
25+
(let [[_ alignment] (re-matches #"^text-align:(.+)$" (:style attrs))]
26+
(when alignment {:text-align alignment}))
27+
(keyword? alignment)
28+
{:text-align (name alignment)}))
2529

2630
(defn- heading-markup [{l :heading-level}] [(keyword (str "h" (or l 1)))])
2731

@@ -116,11 +120,11 @@ a paragraph
116120
:table-head (partial into-markup [:thead])
117121
:table-body (partial into-markup [:tbody])
118122
:table-row (partial into-markup [:tr])
119-
:table-header (fn [ctx {:as node :keys [attrs]}]
120-
(into-markup (let [ta (table-alignment attrs)] (cond-> [:th] ta (conj {:style ta})))
123+
:table-header (fn [ctx node]
124+
(into-markup (let [ta (table-alignment node)] (cond-> [:th] ta (conj {:style ta})))
121125
ctx node))
122-
:table-data (fn [ctx {:as node :keys [attrs]}]
123-
(into-markup (let [ta (table-alignment attrs)] (cond-> [:td] ta (conj {:style ta})))
126+
:table-data (fn [ctx node]
127+
(into-markup (let [ta (table-alignment node)] (cond-> [:td] ta (conj {:style ta})))
124128
ctx node))
125129

126130
;; footnotes & sidenodes

test/nextjournal/markdown/public_api_test.clj

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,15 @@
2828
set))))
2929

3030
(deftest public-vars-test
31-
(is (= '#{->hiccup empty-doc parse parse*}
31+
(is (= '#{default-hiccup-renderers
32+
into-hiccup
33+
toc->hiccup
34+
node->text
35+
table-alignment
36+
->hiccup
37+
empty-doc
38+
parse
39+
parse*}
3240
(public-vars 'nextjournal.markdown)))
3341
(is (= '#{default-hiccup-renderers
3442
toc->hiccup

0 commit comments

Comments
 (0)