diff options
| author | Asko Nõmm <asko@bien.ee> | 2021-12-04 17:34:04 -0300 |
|---|---|---|
| committer | Asko Nõmm <asko@bien.ee> | 2021-12-04 17:34:04 -0300 |
| commit | 17f3b8c45ab395b5fe03d2358defe01f38ee727d (patch) | |
| tree | f24571587efcd565194fe1b7e7142a2b267f9a25 /src/clarktown/parsers | |
| parent | 8d1c6e52760686faf3907f2696eff54755cf04e2 (diff) | |
Add link and image renderer
Diffstat (limited to 'src/clarktown/parsers')
| -rw-r--r-- | src/clarktown/parsers/link_and_image.clj | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/clarktown/parsers/link_and_image.clj b/src/clarktown/parsers/link_and_image.clj new file mode 100644 index 0000000..5ef8857 --- /dev/null +++ b/src/clarktown/parsers/link_and_image.clj @@ -0,0 +1,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)))))) |
