diff options
| author | Asko Nomm <asko@bien.ee> | 2022-04-09 19:32:05 +0200 |
|---|---|---|
| committer | Asko Nomm <asko@bien.ee> | 2022-04-09 19:32:05 +0200 |
| commit | 9fae4b6dfabc7485a8a2debf108d8190761590f5 (patch) | |
| tree | f880342b263fd281850ba70b011ca5e13bb11360 | |
| parent | aad4d251568125c7f3938237a8fc5d44552bbea1 (diff) | |
Fix #8: Links break textual checkboxes
| -rw-r--r-- | src/clarktown/parsers/link_and_image.clj | 6 | ||||
| -rw-r--r-- | test/clarktown/parsers/link_and_image_test.clj | 20 |
2 files changed, 23 insertions, 3 deletions
diff --git a/src/clarktown/parsers/link_and_image.clj b/src/clarktown/parsers/link_and_image.clj index dd8c9d0..3dbbac7 100644 --- a/src/clarktown/parsers/link_and_image.clj +++ b/src/clarktown/parsers/link_and_image.clj @@ -7,15 +7,15 @@ "Renders all occurring links and images." [block _] (loop [block block - matches (-> (re-seq #"\!?\[(.*?)\]\((.*?)\)" block) + matches (-> (re-seq #"\!?\[(\w+( \w+|\.|\,)*)\]\((.*?)\)" block) distinct)] (if (empty? matches) block - (let [[whole-match label href] (first matches) + (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)))))) + (drop 1 matches))))))
\ No newline at end of file diff --git a/test/clarktown/parsers/link_and_image_test.clj b/test/clarktown/parsers/link_and_image_test.clj new file mode 100644 index 0000000..eadfc58 --- /dev/null +++ b/test/clarktown/parsers/link_and_image_test.clj @@ -0,0 +1,20 @@ +(ns clarktown.parsers.link-and-image-test + (:require + [clojure.test :refer [deftest testing is]] + [clarktown.parsers.link-and-image :as link-and-image])) + + +(deftest link-test + (testing "Creating a link" + (is (= (link-and-image/render "[This is a link.](https://example.com)" nil) + "<a href=\"https://example.com\">This is a link.</a>")) + + (is (= (link-and-image/render "[x] [label](link)" nil) + "[x] <a href=\"link\">label</a>")) + + (is (= (link-and-image/render "[ ] [label](link)" nil) + "[ ] <a href=\"link\">label</a>"))) + + (testing "Creating an image" + (is (= (link-and-image/render "" nil) + "<img src=\"https://example.com\" alt=\"This is an image\">"))))
\ No newline at end of file |
