From 3df8234b91a7c1d6c14669a58ebe6c7d9d67fdf2 Mon Sep 17 00:00:00 2001 From: Marwin Hochfelsner <50826859+hlfan@users.noreply.github.com> Date: Mon, 1 Dec 2025 23:57:57 +0100 Subject: [PATCH] Refactor linkify method --- lib/rich_text.rb | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/rich_text.rb b/lib/rich_text.rb index 197df06df..f1885f03d 100644 --- a/lib/rich_text.rb +++ b/lib/rich_text.rb @@ -87,17 +87,25 @@ module RichText end def linkify(text, mode = :urls) - link_attr = 'rel="nofollow noopener noreferrer" dir="auto"' - 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) do |path| - path.sub(Regexp.new(Settings.linkify_wiki_optional_path_prefix || ""), "") - end - end.html_safe + ERB::Util.html_escape(text) + .then { |html| auto_link(html, mode) } + .html_safe end private + def auto_link(text, mode) + link_attr = 'rel="nofollow noopener noreferrer" dir="auto"' + Rinku.auto_link(text, mode, link_attr) { |url| format_link_text(url) } + end + + def format_link_text(url) + url = shorten_host(url, Settings.linkify_hosts, Settings.linkify_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 + def shorten_host(url, hosts, hosts_replacement) %r{^(https?://([^/]*))(.*)$}.match(url) do |m| scheme_host, host, path = m.captures -- 2.39.5