2022-03-26 01:53:34 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module FormattingHelper
|
2024-10-15 14:12:54 +00:00
|
|
|
SYNDICATED_EMOJI_STYLES = <<~CSS.squish
|
|
|
|
height: 1.1em;
|
|
|
|
margin: -.2ex .15em .2ex;
|
|
|
|
object-fit: contain;
|
|
|
|
vertical-align: middle;
|
|
|
|
width: 1.1em;
|
|
|
|
CSS
|
|
|
|
|
2022-03-26 01:53:34 +00:00
|
|
|
def html_aware_format(text, local, options = {})
|
|
|
|
HtmlAwareFormatter.new(text, local, options).to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def linkify(text, options = {})
|
|
|
|
TextFormatter.new(text, options).to_s
|
|
|
|
end
|
|
|
|
|
2023-10-20 13:32:16 +00:00
|
|
|
def url_for_preview_card(preview_card)
|
|
|
|
preview_card.url
|
|
|
|
end
|
|
|
|
|
2022-03-27 23:17:17 +00:00
|
|
|
def extract_status_plain_text(status)
|
|
|
|
PlainTextFormatter.new(status.text, status.local?).to_s
|
2022-03-26 01:53:34 +00:00
|
|
|
end
|
2022-03-30 22:49:24 +00:00
|
|
|
module_function :extract_status_plain_text
|
2022-03-26 01:53:34 +00:00
|
|
|
|
|
|
|
def status_content_format(status)
|
2024-10-29 10:10:17 +00:00
|
|
|
MastodonOTELTracer.in_span('HtmlAwareFormatter rendering') do |span|
|
|
|
|
span.add_attributes(
|
|
|
|
'app.formatter.content.type' => 'status',
|
|
|
|
'app.formatter.content.origin' => status.local? ? 'local' : 'remote'
|
|
|
|
)
|
|
|
|
|
|
|
|
html_aware_format(status.text, status.local?, preloaded_accounts: [status.account] + (status.respond_to?(:active_mentions) ? status.active_mentions.map(&:account) : []))
|
|
|
|
end
|
2022-03-26 01:53:34 +00:00
|
|
|
end
|
2022-03-27 23:17:17 +00:00
|
|
|
|
2022-05-09 05:43:08 +00:00
|
|
|
def rss_status_content_format(status)
|
|
|
|
prerender_custom_emojis(
|
2024-10-15 14:12:54 +00:00
|
|
|
wrapped_status_content_format(status),
|
2022-05-09 05:43:08 +00:00
|
|
|
status.emojis,
|
2024-10-15 14:12:54 +00:00
|
|
|
style: SYNDICATED_EMOJI_STYLES
|
2022-05-09 05:43:08 +00:00
|
|
|
).to_str
|
|
|
|
end
|
|
|
|
|
2022-03-27 23:17:17 +00:00
|
|
|
def account_bio_format(account)
|
2024-10-29 10:10:17 +00:00
|
|
|
MastodonOTELTracer.in_span('HtmlAwareFormatter rendering') do |span|
|
|
|
|
span.add_attributes(
|
|
|
|
'app.formatter.content.type' => 'account_bio',
|
|
|
|
'app.formatter.content.origin' => account.local? ? 'local' : 'remote'
|
|
|
|
)
|
|
|
|
|
|
|
|
html_aware_format(account.note, account.local?)
|
|
|
|
end
|
2022-03-27 23:17:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def account_field_value_format(field, with_rel_me: true)
|
2023-07-06 13:06:49 +00:00
|
|
|
if field.verified? && !field.account.local?
|
|
|
|
TextFormatter.shortened_link(field.value_for_verification)
|
|
|
|
else
|
|
|
|
html_aware_format(field.value, field.account.local?, with_rel_me: with_rel_me, with_domains: true, multiline: false)
|
|
|
|
end
|
2022-03-27 23:17:17 +00:00
|
|
|
end
|
2024-10-15 14:12:54 +00:00
|
|
|
|
2024-12-09 10:04:46 +00:00
|
|
|
def markdown(text)
|
|
|
|
Redcarpet::Markdown.new(Redcarpet::Render::HTML, escape_html: true, no_images: true).render(text).html_safe # rubocop:disable Rails/OutputSafety
|
|
|
|
end
|
|
|
|
|
2024-10-15 14:12:54 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def wrapped_status_content_format(status)
|
|
|
|
safe_join [
|
|
|
|
rss_content_preroll(status),
|
|
|
|
status_content_format(status),
|
|
|
|
rss_content_postroll(status),
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
def rss_content_preroll(status)
|
|
|
|
if status.spoiler_text?
|
|
|
|
safe_join [
|
|
|
|
tag.p { spoiler_with_warning(status) },
|
|
|
|
tag.hr,
|
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def spoiler_with_warning(status)
|
|
|
|
safe_join [
|
|
|
|
tag.strong { I18n.t('rss.content_warning', locale: available_locale_or_nil(status.language) || I18n.default_locale) },
|
|
|
|
status.spoiler_text,
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
def rss_content_postroll(status)
|
|
|
|
if status.preloadable_poll
|
|
|
|
tag.p do
|
|
|
|
poll_option_tags(status)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def poll_option_tags(status)
|
|
|
|
safe_join(
|
|
|
|
status.preloadable_poll.options.map do |option|
|
|
|
|
tag.send(status.preloadable_poll.multiple? ? 'checkbox' : 'radio', option, disabled: true)
|
|
|
|
end,
|
|
|
|
tag.br
|
|
|
|
)
|
|
|
|
end
|
2022-03-26 01:53:34 +00:00
|
|
|
end
|