summaryrefslogtreecommitdiff
path: root/src/clarktown/correctors/atx_heading_block.clj
blob: 8246ec325635e48fac81023538377f3437d91b11 (plain)
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
(ns clarktown.correctors.atx-heading-block
  (:require
    [clojure.string :as string]))


(defn- in-code-block?
  "Determines whether the current `line` is within a code block."
  [lines index]
  (->> (take index lines)
       (filter #(string/starts-with? (string/trim %) "```"))
       count
       odd?))


(defn empty-line-above?
  "Determines whether there's a need for an empty new line 
  above the `line` at the current `index`. In the case of a 
  ATX heading block that starts with the `#` character, if 
  there's no empty newline above, we need to create one, and 
  so this function must then return `true`."
  [lines line index]
  (and (is-atx-heading? line)
       (> index 0)
       (not (= (-> (nth lines (- index 1))
                   string/trim) ""))
       (not (in-code-block? lines index))))     
       

(defn empty-line-below?
  "Determines whether there's a need for an empty new line
  below the `line` at the current `index`. In the case of a 
  ATX heading block that starts with the `#` character, if
  there's no empty newline below, we need to create one, and
  so this function must then return `true`."
  [lines line index]
  (and (is-atx-heading? line)
       (< index (- (count lines) 1))
       (not (= (-> (nth lines (+ index 1))
                   string/trim) ""))
       (not (in-code-block? lines index))))