Refactor formatter (#17828)
* Refactor formatter * Move custom emoji pre-rendering logic to view helpers * Move more methods out of Formatter * Fix code style issues * Remove Formatter * Add inline poll options to RSS feeds * Remove unused helper method * Fix code style issues * Various fixes and improvements * Fix test
This commit is contained in:
parent
2dd30804b6
commit
cefa526c6d
44 changed files with 932 additions and 1024 deletions
38
app/lib/html_aware_formatter.rb
Normal file
38
app/lib/html_aware_formatter.rb
Normal file
|
@ -0,0 +1,38 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class HtmlAwareFormatter
|
||||
attr_reader :text, :local, :options
|
||||
|
||||
alias local? local
|
||||
|
||||
# @param [String] text
|
||||
# @param [Boolean] local
|
||||
# @param [Hash] options
|
||||
def initialize(text, local, options = {})
|
||||
@text = text
|
||||
@local = local
|
||||
@options = options
|
||||
end
|
||||
|
||||
def to_s
|
||||
return ''.html_safe if text.blank?
|
||||
|
||||
if local?
|
||||
linkify
|
||||
else
|
||||
reformat.html_safe # rubocop:disable Rails/OutputSafety
|
||||
end
|
||||
rescue ArgumentError
|
||||
''.html_safe
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def reformat
|
||||
Sanitize.fragment(text, Sanitize::Config::MASTODON_STRICT)
|
||||
end
|
||||
|
||||
def linkify
|
||||
TextFormatter.new(text, options).to_s
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue