about summary refs log tree commit diff
path: root/app/lib/formatter.rb
diff options
context:
space:
mode:
authorFire Demon <firedemon@creature.cafe>2020-08-16 03:24:47 -0500
committerFire Demon <firedemon@creature.cafe>2020-08-30 05:45:18 -0500
commit6e28a99c8e46295dd049f7af45565d4bea97c725 (patch)
tree1091cdf9ef010e4edc61e188835f8a691077a6ff /app/lib/formatter.rb
parentd7a405e5e7df0d792a75f27fa404e76ab46aad66 (diff)
[Feature] Full article support
Diffstat (limited to 'app/lib/formatter.rb')
-rw-r--r--app/lib/formatter.rb20
1 files changed, 18 insertions, 2 deletions
diff --git a/app/lib/formatter.rb b/app/lib/formatter.rb
index d85bbf0e0..bf99701b7 100644
--- a/app/lib/formatter.rb
+++ b/app/lib/formatter.rb
@@ -39,8 +39,14 @@ class Formatter
       prepend_reblog = false
     end
 
+    summary = nil
     raw_content = status.text
 
+    if status.title.present?
+      summary = status.spoiler_text.presence || status.text
+      raw_content = options[:article_content] ? status.text : summary
+    end
+
     if options[:inline_poll_options] && status.preloadable_poll
       raw_content = raw_content + "\n\n" + status.preloadable_poll.options.map { |title| "[ ] #{title}" }.join("\n")
     end
@@ -50,6 +56,7 @@ class Formatter
     unless status.local?
       html = reformat(raw_content)
       html = encode_custom_emojis(html, status.emojis, options[:autoplay]) if options[:custom_emojify]
+      html = format_article_content(summary, html) if options[:article_content] && summary.present?
       return html.html_safe # rubocop:disable Rails/OutputSafety
     end
 
@@ -68,6 +75,7 @@ class Formatter
       html = html.delete("\n")
     end
 
+    html = format_article_content(summary, html) if options[:article_content] && summary.present?
     html.html_safe # rubocop:disable Rails/OutputSafety
   end
 
@@ -77,10 +85,14 @@ class Formatter
   end
 
   def format_article(text)
-    text = text.gsub(/>[\r\n]+</, "><")
+    text = text.gsub(/>[\r\n]+</, '><')
     text.html_safe # rubocop:disable Rails/OutputSafety
   end
 
+  def format_article_content(summary, html)
+    "<blockquote data-name=\"summary\">#{format_summary(summary, html)}</blockquote>#{html}"
+  end
+
   def reformat(html, outgoing = false)
     sanitize(html, Sanitize::Config::MASTODON_STRICT.merge(outgoing: outgoing))
   rescue ArgumentError
@@ -108,8 +120,12 @@ class Formatter
     Sanitize.fragment(html, config)
   end
 
+  def format_summary(summary, fallback)
+    summary&.strip.presence || fallback[/(?:<p>.*?<\/p>)/im].presence || '🗎❓'
+  end
+
   def format_spoiler(status, **options)
-    html = encode(status.spoiler_text)
+    html = encode(status.title.presence || status.spoiler_text)
     html = encode_custom_emojis(html, status.emojis, options[:autoplay])
     html.html_safe # rubocop:disable Rails/OutputSafety
   end