blob: 3dbbac790e4970e4eff3afbf07d4a16da45bb06a (
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 occurring links and images."
[block _]
(loop [block block
matches (-> (re-seq #"\!?\[(\w+( \w+|\.|\,)*)\]\((.*?)\)" 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))))))
|