diff options
Diffstat (limited to 'src/clarktown')
| -rw-r--r-- | src/clarktown/correctors/atx_heading_block.clj | 11 | ||||
| -rw-r--r-- | src/clarktown/engine.clj | 15 | ||||
| -rw-r--r-- | src/clarktown/matchers/heading_block.clj | 6 | ||||
| -rw-r--r-- | src/clarktown/renderers/heading_block.clj | 13 |
4 files changed, 27 insertions, 18 deletions
diff --git a/src/clarktown/correctors/atx_heading_block.clj b/src/clarktown/correctors/atx_heading_block.clj index 361b9b9..a2a948b 100644 --- a/src/clarktown/correctors/atx_heading_block.clj +++ b/src/clarktown/correctors/atx_heading_block.clj @@ -1,6 +1,7 @@ (ns clarktown.correctors.atx-heading-block (:require - [clojure.string :as string])) + [clojure.string :as string] + [clarktown.matchers.heading-block :refer [is-atx-heading?]])) (defn- in-code-block? @@ -19,12 +20,12 @@ there's no empty newline above, we need to create one, and so this function must then return `true`." [lines line index] - (and (string/starts-with? (string/trim line) "#") + (and (is-atx-heading? (string/trim line)) (> index 0) (not (= (-> (nth lines (- index 1)) string/trim) "")) - (not (in-code-block? lines index)))) - + (not (in-code-block? lines index)))) + (defn empty-line-below? "Determines whether there's a need for an empty new line @@ -33,7 +34,7 @@ there's no empty newline below, we need to create one, and so this function must then return `true`." [lines line index] - (and (string/starts-with? (string/trim line) "#") + (and (is-atx-heading? line) (< index (- (count lines) 1)) (not (= (-> (nth lines (+ index 1)) string/trim) "")) diff --git a/src/clarktown/engine.clj b/src/clarktown/engine.clj index deca176..eaa0fb0 100644 --- a/src/clarktown/engine.clj +++ b/src/clarktown/engine.clj @@ -69,13 +69,20 @@ lines))) +(defn- remove-excess-newlines + "Replaces all occurences of 3 or more concecutive newlines into + two newlines." + [markdown] + (string/replace markdown #"\n{3,}" "\n\n")) + + (defn- correct-markdown "Corrects invalid Markdown for the parser." [markdown given-correctors] - (let [lines (string/split-lines markdown)] - (->> lines - (correct-block-separations (:block-separations given-correctors)) - (string/join \newline)))) + (->> (string/split-lines markdown) + (correct-block-separations (:block-separations given-correctors)) + (string/join \newline) + (remove-excess-newlines))) (defn- find-parser-by-block diff --git a/src/clarktown/matchers/heading_block.clj b/src/clarktown/matchers/heading_block.clj index 2295f26..1a9a451 100644 --- a/src/clarktown/matchers/heading_block.clj +++ b/src/clarktown/matchers/heading_block.clj @@ -6,9 +6,7 @@ (defn is-atx-heading? "Determines whether the given block is a atx heading." [block] - (-> (string/replace block #"\n" "") - string/trim - (string/starts-with? "#"))) + (re-matches #"^\#{1,6}\s.*" block)) (defn is-settext-heading? @@ -26,4 +24,4 @@ "Determines whether the given block is a heading block." [block] (or (is-atx-heading? block) - (is-settext-heading? block)))
\ No newline at end of file + (is-settext-heading? block))) diff --git a/src/clarktown/renderers/heading_block.clj b/src/clarktown/renderers/heading_block.clj index 4da9bda..7f4c7de 100644 --- a/src/clarktown/renderers/heading_block.clj +++ b/src/clarktown/renderers/heading_block.clj @@ -7,8 +7,7 @@ (defn render-atx-heading "Renders the hashbang heading block." [block] - (let [single-line-block (-> (string/replace block #"\n" "") - string/trim) + (let [single-line-block (string/trim block) size (-> (string/split single-line-block #" ") first string/trim @@ -39,6 +38,10 @@ (defn render "Renders the heading block." [block _ _] - (if (matcher/is-atx-heading? block) - (render-atx-heading block) - (render-settext-heading block))) + (cond (matcher/is-atx-heading? block) + (render-atx-heading block) + + (matcher/is-settext-heading? block) + (render-settext-heading block) + + :else block)) |
