class Object

Public Instance Methods

matches_one_of(node, selectors) click to toggle source

Returns true if Nokogiri’s Node matches one of selectors, otherwise return false

# File lib/jekyll-external-links/external_links.rb, line 58
def matches_one_of(node, selectors)
  for selector in selectors
    if node.matches? selector
      return true
    end
  end
  return false
end
process_content(site_hostname, content, marker_html, link_selector, exclude_selectors=[]) click to toggle source

Given hostname and content, updates any found <a> elements as follows:

  • Adds `rel` attribute

  • Appends inner markup for external link icon

Only processes external links where `href` starts with “http” and target host does not start with given site hostname.

# File lib/jekyll-external-links/external_links.rb, line 11
def process_content(site_hostname, content, marker_html, link_selector, exclude_selectors=[])
  content = Nokogiri::HTML(content)
  content.css(link_selector).each do |a|
    next if matches_one_of(a, exclude_selectors)
    next unless a.get_attribute('href') =~ /\Ahttp/i
    next if a.get_attribute('href') =~ /\Ahttp(s)?:\/\/#{site_hostname}\//i
    next if a.inner_html.include? "ico-ext"
    a.set_attribute('rel', 'external')
    a.inner_html = "#{a.inner_html}#{marker_html}"
  end
  return content.to_s
end