Class: Thredded::HtmlPipeline::OneboxFilter

Inherits:
HTML::Pipeline::Filter
  • Object
show all
Includes:
Utils
Defined in:
lib/thredded/html_pipeline/onebox_filter.rb

Constant Summary collapse

SANITIZE_CONFIG =
Sanitize::Config.merge(
  Sanitize::Config::ONEBOX,
  attributes: {
    'a' => Sanitize::Config::ONEBOX[:attributes]['a'] + %w[target],
  },
  add_attributes: {
    'iframe' => {
      'seamless' => 'seamless',
      'sandbox' => 'allow-same-origin allow-scripts allow-forms allow-popups allow-popups-to-escape-sandbox' \
                   ' allow-presentation',
    }
  },
  transformers: (Sanitize::Config::ONEBOX[:transformers] || []) + [
    ->(env) {
      next unless env[:node_name] == 'a'
      a_tag = env[:node]
      a_tag['href'] ||= '#'
      if a_tag['href'] =~ %r{^(?:[a-z]+:)?//}
        a_tag['target'] = '_blank'
        a_tag['rel']    = 'nofollow noopener'
      else
        a_tag.remove_attribute('target')
      end
    }
  ]
)

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.onebox_views_cacheObject

In development and test, caching is usually disabled. Regardless, always store the onebox in a file cache to enable offline development, persistence between test runs, and to improve performance.


41
42
43
# File 'lib/thredded/html_pipeline/onebox_filter.rb', line 41

def onebox_views_cache
  @onebox_views_cache
end

Instance Method Details

#callObject


48
49
50
51
52
53
54
55
56
57
58
# File 'lib/thredded/html_pipeline/onebox_filter.rb', line 48

def call
  doc.css('a').each do |element|
    url = element['href'].to_s
    next unless url.present? && url == element.content && on_its_own_line?(element)
    onebox_html = render_onebox_with_cache(url)
    next if onebox_html.empty?
    extract_block_from_paragraph! element
    element.swap onebox_html
  end
  doc
end