~cytrogen/masto-fe

ref: 53f5b27bd10e5f471b59cd0597d67ea12587c95a masto-fe/app/lib/html_aware_formatter.rb -rw-r--r-- 886 bytes
53f5b27b — Claire Merge commit '640421f661ee4d7e76a2aab607e7b15687940b6f' into glitch-soc/merge-upstream 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# frozen_string_literal: true

class HtmlAwareFormatter
  STATUS_MIME_TYPES = %w(text/plain text/markdown text/html).freeze

  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
    if %w(text/markdown text/html).include?(@options[:content_type])
      AdvancedTextFormatter.new(text, options).to_s
    else
      TextFormatter.new(text, options).to_s
    end
  end
end