-
Notifications
You must be signed in to change notification settings - Fork 0
/
heading_block.clj
63 lines (53 loc) · 1.81 KB
/
heading_block.clj
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
(ns clarktown.renderers.heading-block
(:require
[clojure.string :as string]
[clarktown.matchers.heading-block :as matcher])
(:import
(java.text Normalizer Normalizer$Form)))
(defn- slugify
"Turn `input` in a URL slug."
[input]
(let [split-s(-> (Normalizer/normalize input Normalizer$Form/NFD)
(string/replace #"[\P{ASCII}]+" "")
string/lower-case
string/triml
(string/split #"[\p{Space}\p{P}]+"))
combined (string/join "-" split-s)]
(apply str (take 250 combined))))
(defn render-atx-heading
"Renders the hashbang heading block."
[block]
(let [single-line-block (string/trim block)
size (-> (string/split single-line-block #" ")
first
string/trim
count)
value (->> (string/split single-line-block #" ")
next
(string/join " ")
string/trim)
id (slugify value)]
(str "<h" size " id=\"" id "\">" value "</h" size ">")))
(defn render-settext-heading
"Renders the settext heading block."
[block]
(let [lines (string/split-lines block)
value (->> (split-at (- (count lines) 1) lines)
first
(string/join "\n"))
h1? (= "=" (-> (last lines)
string/trim
(string/split #"")
first))
id (slugify value)]
(if h1?
(str "<h1 id=\"" id "\">" value "</h1>")
(str "<h2 id=\"" id "\">" value "</h2>"))))
(defn render
"Renders the heading block."
[block _ _]
(cond (matcher/is-atx-heading? block)
(render-atx-heading block)
(matcher/is-settext-heading? block)
(render-settext-heading block)
:else block))