From 90a92f2dc23be4f5ee1064d11ecd196085cbeb2f Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Thu, 15 Mar 2018 00:17:12 +0100 Subject: [PATCH 1/2] :sparkles: Adds rel nofollow to external links Fixes #4583 Signed-off-by: Franck Nijhof --- Gemfile | 1 + Gemfile.lock | 6 +++++- plugins/no_follow.rb | 32 ++++++++++++++++++++++++++++++++ source/_layouts/default.html | 2 +- 4 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 plugins/no_follow.rb diff --git a/Gemfile b/Gemfile index d7b8fe661d03..1b5ab49ea5a2 100644 --- a/Gemfile +++ b/Gemfile @@ -24,3 +24,4 @@ group :jekyll_plugins do end gem 'sinatra', '~> 1.4.2' +gem 'nokogiri' diff --git a/Gemfile.lock b/Gemfile.lock index 9b9a87a02c02..051fb09fdb52 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -100,6 +103,7 @@ DEPENDENCIES jekyll-redirect-from jekyll-sitemap jekyll-time-to-read + nokogiri octopress (~> 3.0) octopress-include-tag pry @@ -113,4 +117,4 @@ RUBY VERSION ruby 2.4.1p111 BUNDLED WITH - 1.15.4 + 1.16.1 diff --git a/plugins/no_follow.rb b/plugins/no_follow.rb new file mode 100644 index 000000000000..1ab62685cb8c --- /dev/null +++ b/plugins/no_follow.rb @@ -0,0 +1,32 @@ +# 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| + + # All external links start with 'http', skip when this one does not + next unless link.get_attribute('href') =~ /\Ahttp/i + + # Play nice with links that already have a rel attribute set, skip it + next if link.get_attribute('rel') + + # Play nice with our own links + next if link.get_attribute('href') =~ /\Ahttps?:\/\/\w*.?home-assistant.io/i + + # Add rel attribute to link + link.set_attribute('rel', 'external nofollow') + end + dom.to_s + end + end +end + +Liquid::Template.register_filter(Jekyll::NoFollow) diff --git a/source/_layouts/default.html b/source/_layouts/default.html index 5c8e47d068f3..ca7e0199c2b2 100644 --- a/source/_layouts/default.html +++ b/source/_layouts/default.html @@ -24,7 +24,7 @@
{% endif %} - {{ content }} + {{ content | nofollow }}
From d494f0d04a707f32c253a2abf7daef767fd5db44 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Thu, 15 Mar 2018 01:19:54 +0100 Subject: [PATCH 2/2] :ribbon: Adds support for appending nofollow to existing external links --- plugins/no_follow.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/plugins/no_follow.rb b/plugins/no_follow.rb index 1ab62685cb8c..27da6c90f25a 100644 --- a/plugins/no_follow.rb +++ b/plugins/no_follow.rb @@ -11,18 +11,19 @@ def nofollow(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 links that already have a rel attribute set, skip it - next if link.get_attribute('rel') - # 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', 'external nofollow') + link.set_attribute('rel', rel.join(' ').strip) end dom.to_s end