forked from home-assistant/home-assistant.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds rel nofollow to external links (home-assistant#4918)
* ✨ 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
Showing
4 changed files
with
40 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,4 @@ group :jekyll_plugins do | |
end | ||
|
||
gem 'sinatra', '~> 1.4.2' | ||
gem 'nokogiri' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters