From e2491680e696d2c285a798ec4c66b26d2748df66 Mon Sep 17 00:00:00 2001 From: R Tucker Date: Tue, 9 May 2017 22:47:25 -0400 Subject: [PATCH] Handle hashtags in spoiler_texts (partial fix for #699) (#2683) * services: scan spoiler_text for hashtags (#699) * views: link hashtags from spoiler_texts This covers linking hashtags from within the spoiler text on the server-generated pages. * services: fix string concat going into hashtag RE Cleaner Ruby syntax, may handle immutable strings better --- app/lib/formatter.rb | 11 +++++++++++ app/services/process_hashtags_service.rb | 3 ++- app/views/stream_entries/_detailed_status.html.haml | 2 +- app/views/stream_entries/_simple_status.html.haml | 2 +- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/app/lib/formatter.rb b/app/lib/formatter.rb index 0f2989a81f..7b5b8bab45 100644 --- a/app/lib/formatter.rb +++ b/app/lib/formatter.rb @@ -25,6 +25,17 @@ class Formatter sanitize(html, Sanitize::Config::MASTODON_STRICT).html_safe # rubocop:disable Rails/OutputSafety end + def format_spoiler(status) + return reformat(status.spoiler_text) unless status.local? + + html = status.spoiler_text + html = encode(html) + html = html.delete("\n") + html = link_hashtags(html) + + html.html_safe # rubocop:disable Rails/OutputSafety + end + def plaintext(status) return status.text if status.local? strip_tags(status.text) diff --git a/app/services/process_hashtags_service.rb b/app/services/process_hashtags_service.rb index 617a381590..eab033d50d 100644 --- a/app/services/process_hashtags_service.rb +++ b/app/services/process_hashtags_service.rb @@ -2,7 +2,8 @@ class ProcessHashtagsService < BaseService def call(status, tags = []) - tags = status.text.scan(Tag::HASHTAG_RE).map(&:first) if status.local? + text = [status.text, status.spoiler_text].reject(&:empty?).join(' ') + tags = text.scan(Tag::HASHTAG_RE).map(&:first) if status.local? tags.map { |str| str.mb_chars.downcase }.uniq(&:to_s).each do |tag| status.tags << Tag.where(name: tag).first_or_initialize(name: tag) diff --git a/app/views/stream_entries/_detailed_status.html.haml b/app/views/stream_entries/_detailed_status.html.haml index d22afb0822..4cf94c83cf 100644 --- a/app/views/stream_entries/_detailed_status.html.haml +++ b/app/views/stream_entries/_detailed_status.html.haml @@ -10,7 +10,7 @@ .status__content.p-name.emojify< - if status.spoiler_text? %p{ style: 'margin-bottom: 0' }< - %span.p-summary> #{status.spoiler_text}  + %span.p-summary> #{Formatter.instance.format_spoiler(status)}  %a.status__content__spoiler-link{ href: '#' }= t('statuses.show_more') .e-content{ lang: status.language, style: "display: #{status.spoiler_text? ? 'none' : 'block'}; direction: #{rtl?(status.content) ? 'rtl' : 'ltr'}" }= Formatter.instance.format(status) diff --git a/app/views/stream_entries/_simple_status.html.haml b/app/views/stream_entries/_simple_status.html.haml index 3fa347f74d..583bb24f36 100644 --- a/app/views/stream_entries/_simple_status.html.haml +++ b/app/views/stream_entries/_simple_status.html.haml @@ -16,7 +16,7 @@ .status__content.p-name.emojify< - if status.spoiler_text? %p{ style: 'margin-bottom: 0' }< - %span.p-summary> #{status.spoiler_text}  + %span.p-summary> #{Formatter.instance.format_spoiler(status)}  %a.status__content__spoiler-link{ href: '#' }= t('statuses.show_more') .e-content{ lang: status.language, style: "display: #{status.spoiler_text? ? 'none' : 'block'}; direction: #{rtl?(status.content) ? 'rtl' : 'ltr'}" }= Formatter.instance.format(status)