diff options
Diffstat (limited to 'app/lib/html_aware_formatter.rb')
-rw-r--r-- | app/lib/html_aware_formatter.rb | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/app/lib/html_aware_formatter.rb b/app/lib/html_aware_formatter.rb new file mode 100644 index 000000000..64edba09b --- /dev/null +++ b/app/lib/html_aware_formatter.rb @@ -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 |