summaryrefslogtreecommitdiff
path: root/src/clarktown/parsers/link_and_image.clj
blob: 79644486d1065d9a5f8452e51db9768b9a9baa8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
(ns clarktown.parsers.link-and-image
  (:require
    [clojure.string :as string]))


(defn render
  "Renders all occuring links and images."
  [block _]
  (loop [block block
         matches (-> (re-seq #"\!?\[(.*?)\]\((.*?)\)" block)
                     distinct)]
    (if (empty? matches)
      block
      (let [[whole-match label href] (first matches)
            image? (string/starts-with? whole-match "!")
            image (str "<img src=\"" href "\" alt=\"" label "\">")
            link (str "<a href=\"" href "\">" label "</a>")]
        (recur (if image?
                 (string/replace block whole-match image)
                 (string/replace block whole-match link))
               (drop 1 matches))))))