]> git.openstreetmap.org Git - rails.git/commitdiff
Remove /wiki prefix from wiki urls in linkify
authorAnton Khorev <tony29@yandex.ru>
Fri, 28 Mar 2025 02:45:16 +0000 (05:45 +0300)
committerAnton Khorev <tony29@yandex.ru>
Fri, 28 Mar 2025 02:45:16 +0000 (05:45 +0300)
config/settings.yml
lib/rich_text.rb
test/lib/rich_text_test.rb

index 775df2c115da4e5d0a94055919d6f5f490dd7614..416fb1931f1b354c31f8392bbe553581c4494758 100644 (file)
@@ -144,6 +144,8 @@ linkify_hosts_replacement: "osm.org"
 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"
 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"
+# Regexp for wiki prefix that can be removed
+linkify_wiki_optional_path_prefix: "^/wiki(?=/[A-Z])"
 # External authentication credentials
 #google_auth_id: ""
 #google_auth_secret: ""
 # External authentication credentials
 #google_auth_id: ""
 #google_auth_secret: ""
index 1147cbc6031041beaa88ac85b6636c3259abafb6..79249730707cbfa75dfda488c614dba2a01d0b1b 100644 (file)
@@ -77,15 +77,25 @@ module RichText
       link_attr = 'rel="nofollow noopener noreferrer"'
       Rinku.auto_link(ERB::Util.html_escape(text), mode, link_attr) do |url|
         url = shorten_host(url, Settings.linkify_hosts, Settings.linkify_hosts_replacement)
       link_attr = 'rel="nofollow noopener noreferrer"'
       Rinku.auto_link(ERB::Util.html_escape(text), mode, link_attr) do |url|
         url = shorten_host(url, Settings.linkify_hosts, Settings.linkify_hosts_replacement)
-        shorten_host(url, Settings.linkify_wiki_hosts, Settings.linkify_wiki_hosts_replacement)
+        shorten_host(url, Settings.linkify_wiki_hosts, Settings.linkify_wiki_hosts_replacement) do |path|
+          path.sub(Regexp.new(Settings.linkify_wiki_optional_path_prefix || ""), "")
+        end
       end.html_safe
     end
 
     private
 
     def shorten_host(url, hosts, 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])
+      %r{^(https?://([^/]*))(.*)$}.match(url) do |m|
+        scheme_host, host, path = m.captures
+        if hosts&.include?(host)
+          path = yield(path) if block_given?
+          if hosts_replacement
+            "#{hosts_replacement}#{path}"
+          else
+            "#{scheme_host}#{path}"
+          end
+        end || url
       end || url
     end
   end
       end || url
     end
   end
index c04a4b3ae5e12c78c856db30f42b21d54a6f2458..4ec85bcf9b991968a72893634b8c61637f94a29b 100644 (file)
@@ -269,8 +269,22 @@ class RichTextTest < ActiveSupport::TestCase
     end
   end
 
     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
+  def test_text_to_html_linkify_wiki_replace_prefix
+    with_settings(:linkify_wiki_hosts => ["replace-me-wiki.example.com"], :linkify_wiki_hosts_replacement => "wiki.example.com",
+                  :linkify_wiki_optional_path_prefix => "^/wiki(?=/[A-Z])") 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/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_linkify_wiki_replace_prefix_undefined
+    with_settings(:linkify_wiki_hosts => ["replace-me-wiki.example.com"], :linkify_wiki_hosts_replacement => "wiki.example.com",
+                  :linkify_wiki_optional_path_prefix => nil) 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
       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
@@ -281,6 +295,32 @@ class RichTextTest < ActiveSupport::TestCase
     end
   end
 
     end
   end
 
+  def test_text_to_html_linkify_wiki_replace_undefined_prefix
+    with_settings(:linkify_wiki_hosts => ["replace-me-wiki.example.com"], :linkify_wiki_hosts_replacement => nil,
+                  :linkify_wiki_optional_path_prefix => "^/wiki(?=/[A-Z])") 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 => "https://replace-me-wiki.example.com/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_linkify_wiki_replace_prefix_no_match
+    with_settings(:linkify_wiki_hosts => ["replace-me-wiki.example.com"], :linkify_wiki_hosts_replacement => "wiki.example.com",
+                  :linkify_wiki_optional_path_prefix => "^/wiki(?=/[A-Z])") do
+      r = RichText.new("text", "foo https://replace-me-wiki.example.com/wiki/w bar")
+      assert_html r do
+        assert_dom "a", :count => 1, :text => "wiki.example.com/wiki/w" do
+          assert_dom "> @href", "https://replace-me-wiki.example.com/wiki/w"
+          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
   def test_text_to_html_email
     r = RichText.new("text", "foo example@example.com bar")
     assert_html r do