Class: Thredded::HtmlPipeline::OneboxFilter

Inherits:
HTML::Pipeline::Filter
  • Object
show all
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] || []) + [
    lambda do |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
    end
  ]
)

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.onebox_data_cacheObject

Returns the value of attribute onebox_data_cache.



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

def onebox_data_cache
  @onebox_data_cache
end

.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.



39
40
41
# File 'lib/thredded/html_pipeline/onebox_filter.rb', line 39

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?
    fixup_paragraph! element
    element.swap onebox_html
  end
  doc
end