Class: Banzai::Filter::ExternalLinkFilter

Inherits:
HTML::Pipeline::Filter
  • Object
show all
Defined in:
lib/banzai/filter/external_link_filter.rb

Overview

HTML Filter to modify the attributes of external links

Constant Summary collapse

SCHEMES =
['http', 'https', nil].freeze
RTLO =
"\u202E"
ENCODED_RTLO =
'%E2%80%AE'

Instance Method Summary collapse

Instance Method Details

#callObject


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/banzai/filter/external_link_filter.rb', line 11

def call
  links.each do |node|
    # URI.parse does stricter checking on the url than Addressable,
    # such as on `mailto:` links. Since we've been using it, do an
    # initial parse for validity and then use Addressable
    # for IDN support, etc
    uri = uri_strict(node_src(node))
    if uri
      node.set_attribute(node_src_attribute(node), uri.to_s)
      addressable_uri = addressable_uri(node_src(node))
    else
      addressable_uri = nil
    end

    unless internal_url?(addressable_uri)
      punycode_autolink_node!(addressable_uri, node)
      sanitize_link_text!(node)
      add_malicious_tooltip!(addressable_uri, node)
      add_nofollow!(addressable_uri, node)
    end
  end

  doc
end