From 7fb3fa16dd03a80d0ce5d3a30ab60e9b49be72a8 Mon Sep 17 00:00:00 2001 From: Thibaut Girka Date: Sun, 26 May 2019 22:42:01 +0200 Subject: [PATCH] Truncate long URLs while providing alt text for inline images --- app/lib/sanitize_config.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/lib/sanitize_config.rb b/app/lib/sanitize_config.rb index a9a2351a4d..e6e861eb9c 100644 --- a/app/lib/sanitize_config.rb +++ b/app/lib/sanitize_config.rb @@ -27,7 +27,15 @@ class Sanitize node.name = 'a' node['href'] = node['src'] - node.content = "[🖼 #{node['alt'] || node['href']}]" + if node['alt'].present? + node.content = "[🖼 #{node['alt']}]" + else + url = node['href'] + prefix = url.match(/\Ahttps?:\/\/(www\.)?/).to_s + text = url[prefix.length, 30] + text = text + "…" if url[prefix.length..-1].length > 30 + node.content = "[🖼 #{text}]" + end end MASTODON_STRICT ||= freeze_config(