]> cat aescling's git repositories - mastodon.git/commitdiff
Truncate long URLs while providing alt text for inline images
authorThibaut Girka <thib@sitedethib.com>
Sun, 26 May 2019 20:42:01 +0000 (22:42 +0200)
committerThibG <thib@sitedethib.com>
Tue, 28 May 2019 17:33:43 +0000 (19:33 +0200)
app/lib/sanitize_config.rb

index a9a2351a4d8e914e5b2c4479ab605b1958920b36..e6e861eb9cb99117fe0fce38ecd1ea85e8800f87 100644 (file)
@@ -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(