From 1302fe9da299a8906d598e1f15977411dc0b0ec8 Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Mon, 2 Jun 2025 14:10:04 -0400 Subject: [PATCH] Autolinking shouldn't include final punctuation. --- app/helpers/html_helper.rb | 11 ++++++++++- test/helpers/html_helper_test.rb | 24 ++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) 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