]> git.openstreetmap.org Git - rails.git/blobdiff - lib/rich_text.rb
Shorten matching wiki urls in linkify
[rails.git] / lib / rich_text.rb
index d9c799611a7752f24a54dd582123449d0d8d4216..453f3d23a4a5fda0f553754a3aeb4d5e8d18968f 100644 (file)
@@ -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