diff --git a/lib/plain_text_to_markdown.rb b/lib/plain_text_to_markdown.rb index ae32557c132..33aa3c3d7d6 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 afccb0f8793..724b3c14f7a 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 bar")) + .to eq("foo https://www.example.com/foo.html?a=1 bar") + expect(to_markdown("foo https://www.example.com/foo.html bar")) .to eq("foo https://www.example.com/foo.html bar")