Module: JekyllMiscellaneous::Hooks::ExternalLinks

Defined in:
lib/jekyll_miscellaneous/hooks/external_links.rb

Class Method Summary collapse

Class Method Details

.process(resource) ⇒ Object

Calls process_content on the resource’s content. If the resource is an asset file, does nothing.

Parameters:

resource

The resource to process.

Returns:

The processed resource.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/jekyll_miscellaneous/hooks/external_links.rb', line 18

def self.process(resource)
  return if resource.data['layout'].nil?

  site_hostname = URI(resource.site.config['base_url']).host
  link_selector = 'body a'

  return if resource.respond_to?(:asset_file?) && resource.asset_file?

  resource.output = process_content(
    site_hostname,
    resource.output,
    link_selector
  )
end

.process_content(site_hostname, content, link_selector) ⇒ Object

Processes a string of content with Nokogiri. The content is processed with the given link selector. The links are checked to see if they match the site hostname. If they do not, the link is modified to include rel=“external” and target=“_blank”.

The links are modified to include (a unicode character) to indicate that they are external. Except:

  • When the link is a relative URL.

  • When the link contains an image.

  • When the link has a the class skip-external.

Parameters:

site_hostname

The hostname of the site.

content

The content to process.

link_selector

The CSS selector used to find links.

Returns

site_hostname

The hostname of the site.

content

The content to process.

link_selector

The CSS selector for the links.

Returns:

The content with instances of a HTML tags with new attributes.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/jekyll_miscellaneous/hooks/external_links.rb', line 64

def self.process_content(site_hostname, content, link_selector)
  content = Nokogiri::HTML(content)
  content.css(link_selector).each do |a|
    next unless /\Ahttp/i.match?(a.get_attribute('href'))
    next if %r{\Ahttp(s)?://#{site_hostname}}i.match?(a.get_attribute('href'))

    a.set_attribute('target', '_blank')
    a.set_attribute('rel', 'noopener')

    next if a.children.size.positive? && a.children.map(&:name).include?('img')
    next if a.get_attribute('class')&.include?('skip-external')

    a.content = "#{a.content}"
  end
  content.to_s
end