From: Anton Khorev Date: Tue, 25 Mar 2025 01:04:46 +0000 (+0300) Subject: Shorten matching urls in linkify X-Git-Tag: live~39^2 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/cc06ce4c8fcc7cd686d1991c12bf3638247a6fae?hp=-c Shorten matching urls in linkify --- cc06ce4c8fcc7cd686d1991c12bf3638247a6fae diff --git a/config/settings.yml b/config/settings.yml index 51f4444c4..b98e77c92 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -136,6 +136,10 @@ overpass_credentials: false graphhopper_url: "https://graphhopper.com/api/1/route" fossgis_osrm_url: "https://routing.openstreetmap.de/" fossgis_valhalla_url: "https://valhalla1.openstreetmap.de/route" +# Main website hosts to match in linkify +linkify_hosts: ["www.openstreetmap.org", "www.osm.org", "www.openstreetmap.com", "openstreetmap.org", "osm.org", "openstreetmap.com"] +# Shorter host to replace main hosts +linkify_hosts_replacement: "osm.org" # External authentication credentials #google_auth_id: "" #google_auth_secret: "" diff --git a/lib/rich_text.rb b/lib/rich_text.rb index d6df214d1..d9c799611 100644 --- a/lib/rich_text.rb +++ b/lib/rich_text.rb @@ -76,7 +76,13 @@ module RichText end def linkify(text, mode = :urls) - Rinku.auto_link(ERB::Util.html_escape(text), mode, tag_builder.tag_options(:rel => "nofollow noopener noreferrer")).html_safe + link_attr = tag_builder.tag_options(:rel => "nofollow noopener noreferrer") + Rinku.auto_link(ERB::Util.html_escape(text), mode, link_attr) do |url| + %r{^https?://([^/]*)(.*)$}.match(url) do |m| + "#{Settings.linkify_hosts_replacement}#{m[2]}" if Settings.linkify_hosts_replacement && + Settings.linkify_hosts&.include?(m[1]) + end || url + end.html_safe end end diff --git a/test/lib/rich_text_test.rb b/test/lib/rich_text_test.rb index d454c4728..aa9e70085 100644 --- a/test/lib/rich_text_test.rb +++ b/test/lib/rich_text_test.rb @@ -222,11 +222,50 @@ class RichTextTest < ActiveSupport::TestCase end def test_text_to_html_linkify - r = RichText.new("text", "foo http://example.com/ bar") - assert_html r do - assert_select "a", 1 - assert_select "a[href='http://example.com/']", 1 - assert_select "a[rel='nofollow noopener noreferrer']", 1 + with_settings(:linkify_hosts => ["replace-me.example.com"], :linkify_hosts_replacement => "repl.example.com") do + r = RichText.new("text", "foo http://example.com/ bar") + assert_html r do + assert_dom "a", :count => 1, :text => "http://example.com/" do + assert_dom "> @href", "http://example.com/" + assert_dom "> @rel", "nofollow noopener noreferrer" + end + end + end + end + + def test_text_to_html_linkify_replace + with_settings(:linkify_hosts => ["replace-me.example.com"], :linkify_hosts_replacement => "repl.example.com") do + r = RichText.new("text", "foo https://replace-me.example.com/some/path?query=te10#result12 bar") + assert_html r do + assert_dom "a", :count => 1, :text => "repl.example.com/some/path?query=te10#result12" do + assert_dom "> @href", "https://replace-me.example.com/some/path?query=te10#result12" + assert_dom "> @rel", "nofollow noopener noreferrer" + end + end + end + end + + def test_text_to_html_linkify_replace_other_scheme + with_settings(:linkify_hosts => ["replace-me.example.com"], :linkify_hosts_replacement => "repl.example.com") do + r = RichText.new("text", "foo ftp://replace-me.example.com/some/path?query=te10#result12 bar") + assert_html r do + assert_dom "a", :count => 1, :text => "ftp://replace-me.example.com/some/path?query=te10#result12" do + assert_dom "> @href", "ftp://replace-me.example.com/some/path?query=te10#result12" + assert_dom "> @rel", "nofollow noopener noreferrer" + end + end + end + end + + def test_text_to_html_linkify_replace_undefined + with_settings(:linkify_hosts => ["replace-me.example.com"], :linkify_hosts_replacement => nil) do + r = RichText.new("text", "foo https://replace-me.example.com/some/path?query=te10#result12 bar") + assert_html r do + assert_dom "a", :count => 1, :text => "https://replace-me.example.com/some/path?query=te10#result12" do + assert_dom "> @href", "https://replace-me.example.com/some/path?query=te10#result12" + assert_dom "> @rel", "nofollow noopener noreferrer" + end + end end end