Top Level Namespace

Defined Under Namespace

Modules: Jekyll

Instance Method Summary collapse

Instance Method Details



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/jekyll-external-links/external_links.rb', line 24

def mark_links_in_page_or_document(page_or_document)
  site_hostname = URI(page_or_document.site.config['url']).host

  ext_link_config = page_or_document.site.config['external_links'] || {}

  # The link is marked as external by:
  # (1) setting the rel attribute to external and
  # (2) appending specified marker HTML.
  # Default marker is Font Awesome icon.
  marker_html = ext_link_config['marker_html'] || "<span class='ico-ext'><i class='fas fa-external-link-square-alt'></i></span>"

  # Determines which links to mark. E.g., usually we don’t want to mark navigational links.
  link_selector = ext_link_config['selector'] || 'main a'

  # Determining which links to ignore. For example, links comprised entirely from images
  # may look incorrectly with external marker. 
  unmarked_link_selectors = ext_link_config['ignored_selectors'] || [
    'a[href*=travis]',
    'a[href*=coverity]',
    'a[href*=codecov]',
  ]

  unless page_or_document.respond_to?(:asset_file?) and page_or_document.asset_file?
    page_or_document.output = process_content(
      site_hostname,
      page_or_document.output,
      marker_html,
      link_selector,
      unmarked_link_selectors)
  end
end

#matches_one_of(node, selectors) ⇒ Object

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



58
59
60
61
62
63
64
65
# 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 = []) ⇒ Object

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.



11
12
13
14
15
16
17
18
19
20
21
22
# 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