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

Instance Method Summary collapse

Instance Method Details

#callObject


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/banzai/filter/external_link_filter.rb', line 5

def call
  doc.search('a').each do |node|
    link = node.attr('href')

    next unless link

    # Skip non-HTTP(S) links
    next unless link.start_with?('http')

    # Skip internal links
    next if link.start_with?(internal_url)

    node.set_attribute('rel', 'nofollow noreferrer')
  end

  doc
end