]> cat aescling's git repositories - mastodon.git/commitdiff
Translate incoming remote img tags by a link
authorThibaut Girka <thib@sitedethib.com>
Sun, 26 May 2019 13:10:33 +0000 (15:10 +0200)
committerThibG <thib@sitedethib.com>
Tue, 28 May 2019 17:33:43 +0000 (19:33 +0200)
app/lib/sanitize_config.rb

index 75c916485ce937dc93114e3ae9c19396e853db59..a9a2351a4d8e914e5b2c4479ab605b1958920b36 100644 (file)
@@ -19,6 +19,17 @@ class Sanitize
       node['class'] = class_list.join(' ')
     end
 
+    IMG_TAG_TRANSFORMER = lambda do |env|
+      node = env[:node]
+
+      return unless env[:node_name] == 'img'
+
+      node.name = 'a'
+
+      node['href'] = node['src']
+      node.content = "[🖼 #{node['alt'] || node['href']}]"
+    end
+
     MASTODON_STRICT ||= freeze_config(
       elements: %w(p br span a abbr del pre blockquote code b strong u sub i em h1 h2 h3 h4 h5 ul ol li),
 
@@ -43,6 +54,7 @@ class Sanitize
 
       transformers: [
         CLASS_WHITELIST_TRANSFORMER,
+        IMG_TAG_TRANSFORMER,
       ]
     )