From: Anton Khorev Date: Thu, 27 Mar 2025 14:23:12 +0000 (+0300) Subject: Shorten matching wiki urls in linkify X-Git-Tag: live~29^2 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/459daa030e3c562ecb5e0633c3166a547e61a161?hp=-c Shorten matching wiki urls in linkify --- 459daa030e3c562ecb5e0633c3166a547e61a161 diff --git a/config/settings.yml b/config/settings.yml index b98e77c92..775df2c11 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -140,6 +140,10 @@ fossgis_valhalla_url: "https://valhalla1.openstreetmap.de/route" 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" +# Wiki website hosts to match in linkify +linkify_wiki_hosts: ["wiki.openstreetmap.org", "wiki.osm.org", "wiki.openstreetmap.com", "wiki.openstreetmaps.org", "osm.wiki", "www.osm.wiki", "wiki.osm.wiki"] +# Shorter host to replace wiki hosts +linkify_wiki_hosts_replacement: "osm.wiki" # External authentication credentials #google_auth_id: "" #google_auth_secret: "" diff --git a/lib/rich_text.rb b/lib/rich_text.rb index d9c799611..453f3d23a 100644 --- a/lib/rich_text.rb +++ b/lib/rich_text.rb @@ -78,12 +78,18 @@ module RichText def linkify(text, mode = :urls) 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 + url = shorten_host(url, Settings.linkify_hosts, Settings.linkify_hosts_replacement) + shorten_host(url, Settings.linkify_wiki_hosts, Settings.linkify_wiki_hosts_replacement) end.html_safe end + + private + + def shorten_host(url, hosts, hosts_replacement) + %r{^https?://([^/]*)(.*)$}.match(url) do |m| + "#{hosts_replacement}#{m[2]}" if hosts_replacement && hosts&.include?(m[1]) + end || url + end end class HTML < Base diff --git a/test/lib/rich_text_test.rb b/test/lib/rich_text_test.rb index aa9e70085..c04a4b3ae 100644 --- a/test/lib/rich_text_test.rb +++ b/test/lib/rich_text_test.rb @@ -269,6 +269,18 @@ class RichTextTest < ActiveSupport::TestCase end end + def test_text_to_html_linkify_wiki_replace + with_settings(:linkify_wiki_hosts => ["replace-me-wiki.example.com"], :linkify_wiki_hosts_replacement => "wiki.example.com") do + r = RichText.new("text", "foo https://replace-me-wiki.example.com/wiki/Tag:surface%3Dmetal bar") + assert_html r do + assert_dom "a", :count => 1, :text => "wiki.example.com/wiki/Tag:surface%3Dmetal" do + assert_dom "> @href", "https://replace-me-wiki.example.com/wiki/Tag:surface%3Dmetal" + assert_dom "> @rel", "nofollow noopener noreferrer" + end + end + end + end + def test_text_to_html_email r = RichText.new("text", "foo example@example.com bar") assert_html r do