Skip to content

Commit

Permalink
Handle hashtags in spoiler_texts (partial fix for mastodon#699) (mast…
Browse files Browse the repository at this point in the history
…odon#2683)

* services: scan spoiler_text for hashtags (mastodon#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
  • Loading branch information
rtucker authored and Gargron committed May 10, 2017
1 parent 3a38322 commit e249168
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
11 changes: 11 additions & 0 deletions app/lib/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ def reformat(html)
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)
Expand Down
3 changes: 2 additions & 1 deletion app/services/process_hashtags_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion app/views/stream_entries/_detailed_status.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
.status__content.p-name.emojify<
- if status.spoiler_text?
%p{ style: 'margin-bottom: 0' }<
%span.p-summary> #{status.spoiler_text}&nbsp;
%span.p-summary> #{Formatter.instance.format_spoiler(status)}&nbsp;
%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)

Expand Down
2 changes: 1 addition & 1 deletion app/views/stream_entries/_simple_status.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
.status__content.p-name.emojify<
- if status.spoiler_text?
%p{ style: 'margin-bottom: 0' }<
%span.p-summary> #{status.spoiler_text}&nbsp;
%span.p-summary> #{Formatter.instance.format_spoiler(status)}&nbsp;
%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)

Expand Down

0 comments on commit e249168

Please sign in to comment.