diff options
| author | Asko Nomm <asko@bien.ee> | 2022-04-10 05:16:34 +0200 |
|---|---|---|
| committer | Asko Nomm <asko@bien.ee> | 2022-04-10 05:16:34 +0200 |
| commit | d4fd8d01eb9b57f1ee7b770ca24fc5e121b49edc (patch) | |
| tree | 0170a42d350401bd897a8ca26269ea68928c8c94 | |
| parent | 9fae4b6dfabc7485a8a2debf108d8190761590f5 (diff) | |
Fix #9: Underscores in links get rendered into
| -rw-r--r-- | resources/test/core.md | 2 | ||||
| -rw-r--r-- | resources/test/core_result.html | 2 | ||||
| -rw-r--r-- | src/clarktown/parsers.clj | 12 | ||||
| -rw-r--r-- | src/clarktown/parsers/link_and_image.clj | 12 | ||||
| -rw-r--r-- | test/clarktown/parsers/link_and_image_test.clj | 4 |
5 files changed, 21 insertions, 11 deletions
diff --git a/resources/test/core.md b/resources/test/core.md index 6c476a9..619ab5e 100644 --- a/resources/test/core.md +++ b/resources/test/core.md @@ -1,5 +1,7 @@ Lorem ipsum dolor **sit** amet. Lorem ipsum *dolor* _sit_ __amet__. +There's a [link here](https://example.com/that_has_things?!???!#in-it). + 1. List item 2. Another list item 1. Sub list item diff --git a/resources/test/core_result.html b/resources/test/core_result.html index bf9cc55..59ca907 100644 --- a/resources/test/core_result.html +++ b/resources/test/core_result.html @@ -1,5 +1,7 @@ <p>Lorem ipsum dolor <strong>sit</strong> amet. Lorem ipsum <em>dolor</em> <em>sit</em> <strong>amet</strong>.</p> +<p>There's a <a href="https://example.com/that_has_things?!???!#in-it">link here</a>.</p> + <ol><li>List item</li><li>Another list item<ol><li>Sub list item</li><li>Another sub list item<ol><li>Sub sub list item</li></ol></li><li>Continuing sub list item</li></ol></li><li>Continuing list item</li></ol> <pre><code class="language-javascript">// Detect horizontal line block diff --git a/src/clarktown/parsers.clj b/src/clarktown/parsers.clj index b5b6132..cd909b3 100644 --- a/src/clarktown/parsers.clj +++ b/src/clarktown/parsers.clj @@ -20,26 +20,26 @@ {:matcher horizontal-line-block/is? :renderers [horizontal-line-block/render]} {:matcher heading-block/is? - :renderers [bold/render + :renderers [link-and-image/render + bold/render italic/render inline-code/render strikethrough/render - link-and-image/render heading-block/render]} {:matcher quote-block/is? :renderers [quote-block/render]} {:matcher code-block/is? :renderers [code-block/render]} {:matcher list-block/is? - :renderers [bold/render + :renderers [link-and-image/render + bold/render italic/render inline-code/render strikethrough/render - link-and-image/render list-block/render]} - {:renderers [bold/render + {:renderers [link-and-image/render + bold/render italic/render inline-code/render strikethrough/render - link-and-image/render paragraph-block/render]}]) diff --git a/src/clarktown/parsers/link_and_image.clj b/src/clarktown/parsers/link_and_image.clj index 3dbbac7..fb8ad31 100644 --- a/src/clarktown/parsers/link_and_image.clj +++ b/src/clarktown/parsers/link_and_image.clj @@ -3,18 +3,24 @@ [clojure.string :as string])) +(defn encode-href + [href] + (-> href + (string/replace "_" "_"))) + + (defn render "Renders all occurring links and images." [block _] (loop [block block - matches (-> (re-seq #"\!?\[(\w+( \w+|\.|\,)*)\]\((.*?)\)" 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>")] + image (str "<img src=\"" (encode-href href) "\" alt=\"" label "\">") + link (str "<a href=\"" (encode-href href) "\">" label "</a>")] (recur (if image? (string/replace block whole-match image) (string/replace block whole-match link)) diff --git a/test/clarktown/parsers/link_and_image_test.clj b/test/clarktown/parsers/link_and_image_test.clj index eadfc58..5630306 100644 --- a/test/clarktown/parsers/link_and_image_test.clj +++ b/test/clarktown/parsers/link_and_image_test.clj @@ -6,8 +6,8 @@ (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 "[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>")) |
