FIX: correctly escape regex url

duplicate link detection could fail in certain cases
This commit is contained in:
Sam Saffron 2019-06-04 16:20:40 +10:00
parent 6428aa5b1f
commit 6300d978e2
2 changed files with 5 additions and 1 deletions

View File

@ -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

View File

@ -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")