diff options
author | Thibaut Girka <thib@sitedethib.com> | 2019-05-26 22:42:01 +0200 |
---|---|---|
committer | multiple creatures <dev@multiple-creature.party> | 2019-11-19 15:43:02 -0600 |
commit | 119852adab0e9347b6380446193f36ce03d8b1a3 (patch) | |
tree | b1d4866426852d22a9454d7a776055c0bd8001b8 | |
parent | c0a29cdb71536d64691fbaca0fbd03b5643dfbf7 (diff) |
Truncate long URLs while providing alt text for inline images
-rw-r--r-- | app/lib/sanitize_config.rb | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/app/lib/sanitize_config.rb b/app/lib/sanitize_config.rb index 1d336fcc3..61d5acac8 100644 --- a/app/lib/sanitize_config.rb +++ b/app/lib/sanitize_config.rb @@ -32,7 +32,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 QUERY_STRING_SANITIZER = lambda do |env| |