]> git.openstreetmap.org Git - rails.git/commitdiff
Merge remote-tracking branch 'upstream/pull/5844'
authorTom Hughes <tom@compton.nu>
Tue, 25 Mar 2025 19:09:44 +0000 (19:09 +0000)
committerTom Hughes <tom@compton.nu>
Tue, 25 Mar 2025 19:09:44 +0000 (19:09 +0000)
config/settings.yml
lib/rich_text.rb
test/lib/rich_text_test.rb

index 51f4444c4802edce9b6cc2c5869a8f0c4f8a33ed..b98e77c92bc9f2ebfcb31d67d4963951fbe8681c 100644 (file)
@@ -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: ""
index c20f973b12286164b56fcfc9f8eb47ac2fe0645d..d9c799611a7752f24a54dd582123449d0d8d4216 100644 (file)
@@ -76,11 +76,13 @@ module RichText
     end
 
     def linkify(text, mode = :urls)
-      if text.html_safe?
-        Rinku.auto_link(text, mode, tag_builder.tag_options(:rel => "nofollow noopener noreferrer")).html_safe
-      else
-        Rinku.auto_link(text, mode, tag_builder.tag_options(:rel => "nofollow noopener noreferrer"))
-      end
+      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
 
index 63c70b0996f59520e9cde2d4702c5f1a3b16f064..aa9e7008517fb0ef892b273dd0b1f1ecfdff5e84 100644 (file)
@@ -221,19 +221,62 @@ class RichTextTest < ActiveSupport::TestCase
     assert_equal 50, r.spam_score.round
   end
 
-  def test_text_to_html
-    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
+  def test_text_to_html_linkify
+    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=te<st&limit=20>10#result12 bar")
+      assert_html r do
+        assert_dom "a", :count => 1, :text => "repl.example.com/some/path?query=te<st&limit=20>10#result12" do
+          assert_dom "> @href", "https://replace-me.example.com/some/path?query=te<st&limit=20>10#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=te<st&limit=20>10#result12 bar")
+      assert_html r do
+        assert_dom "a", :count => 1, :text => "ftp://replace-me.example.com/some/path?query=te<st&limit=20>10#result12" do
+          assert_dom "> @href", "ftp://replace-me.example.com/some/path?query=te<st&limit=20>10#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=te<st&limit=20>10#result12 bar")
+      assert_html r do
+        assert_dom "a", :count => 1, :text => "https://replace-me.example.com/some/path?query=te<st&limit=20>10#result12" do
+          assert_dom "> @href", "https://replace-me.example.com/some/path?query=te<st&limit=20>10#result12"
+          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
       assert_select "a", 0
     end
+  end
 
+  def test_text_to_html_escape
     r = RichText.new("text", "foo < bar & baz > qux")
     assert_html r do
       assert_select "p", "foo < bar & baz > qux"