Autolinking shouldn't include final punctuation.

This commit is contained in:
Mike Dalessio
2025-06-02 14:10:04 -04:00
parent 01f286b2a6
commit 1302fe9da2
2 changed files with 34 additions and 1 deletions
+10 -1
View File
@@ -34,7 +34,16 @@ module HtmlHelper
def auto_link_urls(linked_content)
linked_content.gsub!(URL_REGEXP) do |match|
%(<a href="#{match}" rel="noreferrer">#{match}</a>)
url, trailing_punct = extract_url_and_punctuation(match)
%(<a href="#{url}" rel="noreferrer">#{url}</a>#{trailing_punct})
end
end
def extract_url_and_punctuation(url_match)
if url_match.end_with?(".", "?", ",", ":")
[ url_match[..-2], url_match[-1] ]
else
[ url_match, "" ]
end
end
+24
View File
@@ -5,6 +5,30 @@ class HtmlHelperTest < ActionView::TestCase
assert_equal_html \
%(<p>Check this: <a href="https://example.com" rel="noreferrer">https://example.com</a></p>),
format_html("<p>Check this: https://example.com</p>")
assert_equal_html \
%(<p>Check this: <a href="https://example.com/" rel="noreferrer">https://example.com/</a></p>),
format_html("<p>Check this: https://example.com/</p>")
end
test "does not include punctuation in URL autolinking" do
assert_equal_html \
%(<p>Check this: <a href="https://example.com" rel="noreferrer">https://example.com</a>!</p>),
format_html("<p>Check this: https://example.com!</p>")
assert_equal_html \
%(<p>Check this: <a href="https://example.com" rel="noreferrer">https://example.com</a>.</p>),
format_html("<p>Check this: https://example.com.</p>")
assert_equal_html \
%(<p>Check this: <a href="https://example.com" rel="noreferrer">https://example.com</a>?</p>),
format_html("<p>Check this: https://example.com?</p>")
assert_equal_html \
%(<p>Check this: <a href="https://example.com" rel="noreferrer">https://example.com</a>,</p>),
format_html("<p>Check this: https://example.com,</p>")
assert_equal_html \
%(<p>Check this: <a href="https://example.com" rel="noreferrer">https://example.com</a>:</p>),
format_html("<p>Check this: https://example.com:</p>")
assert_equal_html \
%(<p>Check this: <a href="https://example.com" rel="noreferrer">https://example.com</a>;</p>),
format_html("<p>Check this: https://example.com;</p>")
end
test "respect existing links" do