summaryrefslogtreecommitdiff
path: root/src/clarktown/correctors/fenced_code_block.clj
blob: 77bc02381e570d0cfd609912c1894a50f398dff9 (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
(ns clarktown.correctors.fenced-code-block
  (:require
    [clojure.string :as string]))


(defn newline-above?
  "Determines whether there's a need for an empty new line
  above the `line` at the current `index`. In the case of a 
  code block, which starts with three backticks (```), if 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) "```")
       (> index 0)
       (->> (take index lines)
            (filter #(string/starts-with? (string/trim %) "```"))
            count
            even?)
       (not (= (-> (nth lines (- index 1))
                   string/trim) ""))))


(defn newline-below?
  "Determines whether there's a need for an empty new line
  below the `line` at the current `index`. In the case of a 
  code block, which ends with three backticks (```), if 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) "```")
       (< index (- (count lines) 1))
       (->> (take index lines)
            (filter #(string/starts-with? (string/trim %) "```"))
            count
            odd?)
       (not (= (-> (nth lines (+ index 1))
                   string/trim) ""))))