diff --git a/app/helpers/html_helper.rb b/app/helpers/html_helper.rb index f61da26c1..82a18722d 100644 --- a/app/helpers/html_helper.rb +++ b/app/helpers/html_helper.rb @@ -34,7 +34,16 @@ module HtmlHelper def auto_link_urls(linked_content) linked_content.gsub!(URL_REGEXP) do |match| - %(#{match}) + url, trailing_punct = extract_url_and_punctuation(match) + %(#{url}#{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 diff --git a/test/helpers/html_helper_test.rb b/test/helpers/html_helper_test.rb index 818ffa57b..b2536b5c4 100644 --- a/test/helpers/html_helper_test.rb +++ b/test/helpers/html_helper_test.rb @@ -5,6 +5,30 @@ class HtmlHelperTest < ActionView::TestCase assert_equal_html \ %(
Check this: https://example.com
), format_html("Check this: https://example.com
") + assert_equal_html \ + %(Check this: https://example.com/
), + format_html("Check this: https://example.com/
") + end + + test "does not include punctuation in URL autolinking" do + assert_equal_html \ + %(Check this: https://example.com!
), + format_html("Check this: https://example.com!
") + assert_equal_html \ + %(Check this: https://example.com.
), + format_html("Check this: https://example.com.
") + assert_equal_html \ + %(Check this: https://example.com?
), + format_html("Check this: https://example.com?
") + assert_equal_html \ + %(Check this: https://example.com,
), + format_html("Check this: https://example.com,
") + assert_equal_html \ + %(Check this: https://example.com:
), + format_html("Check this: https://example.com:
") + assert_equal_html \ + %(Check this: https://example.com;
), + format_html("Check this: https://example.com;
") end test "respect existing links" do