Skip to content

Commit

Permalink
Adds rel nofollow to external links (home-assistant#4918)
Browse files Browse the repository at this point in the history
* ✨ Adds rel nofollow to external links

Fixes home-assistant#4583

Signed-off-by: Franck Nijhof <[email protected]>

* 🎀 Adds support for appending nofollow to existing external links
  • Loading branch information
frenck authored and balloob committed Mar 15, 2018
1 parent e62bb85 commit 23d593c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ group :jekyll_plugins do
end

gem 'sinatra', '~> 1.4.2'
gem 'nokogiri'
6 changes: 5 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ GEM
rb-inotify (~> 0.9, >= 0.9.7)
mercenary (0.3.6)
method_source (0.8.2)
mini_portile2 (2.3.0)
nokogiri (1.8.2)
mini_portile2 (~> 2.3.0)
octopress (3.0.11)
jekyll (>= 2.0)
mercenary (~> 0.3.2)
Expand Down Expand Up @@ -100,6 +103,7 @@ DEPENDENCIES
jekyll-redirect-from
jekyll-sitemap
jekyll-time-to-read
nokogiri
octopress (~> 3.0)
octopress-include-tag
pry
Expand All @@ -113,4 +117,4 @@ RUBY VERSION
ruby 2.4.1p111

BUNDLED WITH
1.15.4
1.16.1
33 changes: 33 additions & 0 deletions plugins/no_follow.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Jekyll Auto Nofollow Plugin
# Automatically adds rel='external nofollow' to outgoing links.

require 'jekyll'
require 'nokogiri'

module Jekyll
module NoFollow
def nofollow(content)
dom = Nokogiri::HTML.fragment(content)

# Find all links
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
dom.to_s
end
end
end

Liquid::Template.register_filter(Jekyll::NoFollow)
2 changes: 1 addition & 1 deletion source/_layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<div class="grid__item two-thirds lap-one-whole palm-one-whole">
{% endif %}

{{ content }}
{{ content | nofollow }}

</div>

Expand Down

0 comments on commit 23d593c

Please sign in to comment.