FIX: correctly escape regex url

FIX: correctly escape regex url

duplicate link detection could fail in certain cases

diff --git a/lib/plain_text_to_markdown.rb b/lib/plain_text_to_markdown.rb
index ae32557..33aa3c3 100644
--- a/lib/plain_text_to_markdown.rb
+++ b/lib/plain_text_to_markdown.rb
@@ -157,7 +157,8 @@ class PlainTextToMarkdown
       .keep_if { |_, urls | urls.length > 1 }
       .keys.each do |url|
 
-      text.gsub!(Regexp.new(%Q|#{url}(\s*[()\\[\\]<>ยซยป'"โ€œโ€โ€˜โ€™]?#{url}[()\\[\\]<>ยซยป'"โ€œโ€โ€˜โ€™]?)|, Regexp::IGNORECASE), url)
+      escaped = Regexp.escape(url)
+      text.gsub!(Regexp.new(%Q|#{escaped}(\s*[()\\[\\]<>ยซยป'"โ€œโ€โ€˜โ€™]?#{escaped}[()\\[\\]<>ยซยป'"โ€œโ€โ€˜โ€™]?)|, Regexp::IGNORECASE), url)
     end
 
     text
diff --git a/spec/components/plain_text_to_markdown_spec.rb b/spec/components/plain_text_to_markdown_spec.rb
index afccb0f..724b3c1 100644
--- a/spec/components/plain_text_to_markdown_spec.rb
+++ b/spec/components/plain_text_to_markdown_spec.rb
@@ -160,6 +160,9 @@ describe PlainTextToMarkdown do
 
   context "links" do
     it "removes duplicate links" do
+      expect(to_markdown("foo https://www.example.com/foo.html?a=1 <https://www.example.com/foo.html?a=1> bar"))
+        .to eq("foo https://www.example.com/foo.html?a=1 bar")
+
       expect(to_markdown("foo https://www.example.com/foo.html <https://www.example.com/foo.html> bar"))
         .to eq("foo https://www.example.com/foo.html bar")

GitHub sha: 6300d978

1 Like