From d4fd8d01eb9b57f1ee7b770ca24fc5e121b49edc Mon Sep 17 00:00:00 2001 From: Asko Nomm Date: Sun, 10 Apr 2022 05:16:34 +0200 Subject: Fix #9: Underscores in links get rendered into --- resources/test/core.md | 2 ++ resources/test/core_result.html | 2 ++ src/clarktown/parsers.clj | 12 ++++++------ src/clarktown/parsers/link_and_image.clj | 12 +++++++++--- 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 @@

Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.

+

There's a link here.

+
  1. List item
  2. Another list item
    1. Sub list item
    2. Another sub list item
      1. Sub sub list item
    3. Continuing sub list item
  3. Continuing list item
// 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 "\""")
-            link (str "" label "")]
+            image (str "\""")
+            link (str "" label "")]
         (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)
-           "This is a link."))
+    (is (= (link-and-image/render "[This is a link](https://example.com)" nil)
+           "This is a link"))
 
     (is (= (link-and-image/render "[x] [label](link)" nil)
            "[x] label"))
-- 
cgit v1.2.3