forked from home-assistant/home-assistant.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
output_modder.rb
50 lines (39 loc) · 1.66 KB
/
output_modder.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
# Jekyll Out Modder - Allows for mangling/modding the HTML output
#
# This is combined in a single plugin/filter to reduce the NokoGiri dom
# parsing to just once per page/content.
#
# - Automatically adds rel='external nofollow' to outgoing links.
# - Automatically make headers linkable
#
require 'jekyll'
require 'nokogiri'
module Jekyll
module OutputModder
def output_modder(content)
dom = Nokogiri::HTML.fragment(content)
# Find all links, make all external links rel='external nofollow'
dom.css('a').each do |link|
rel = ['external', 'nofollow']
# All external links start with 'http', skip when this one does not
next unless link.get_attribute('href') =~ /\Ahttp/i
# Play nice with our own links
next if link.get_attribute('href') =~ /\Ahttps?:\/\/\w*.?home-assistant.io/i
# Play nice with links that already have a rel attribute set
rel.unshift(link.get_attribute('rel'))
# Add rel attribute to link
link.set_attribute('rel', rel.join(' ').strip)
end
# Find all headers, make them linkable
dom.css('h2,h3,h4,h5,h6,h7,h8').each do |header|
# Skip linked headers
next if header.at_css('a')
title = header.content
slug = title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
header.children = "<a class='title-link' name='#{slug}' href='\##{slug}'></a> #{title}"
end
dom.to_s
end
end
end
Liquid::Template.register_filter(Jekyll::OutputModder)