Skip to content

Commit

Permalink
add jekyll extlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
tidusIO committed Jan 3, 2025
1 parent 1da8c7d commit c2751ae
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 2 deletions.
4 changes: 4 additions & 0 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ autopages:
collections:
enabled: false

extlinks:
attributes: {rel: "noreferrer noopener", target: _blank}
rel_exclude: ['angular.de', 'vuejs.de', 'reactjs.de', 'workshops.de', 'webdave.de', 'ng-de.org', 'conf.vuejs.de']

# Build settings
markdown: kramdown
sass:
Expand Down
2 changes: 1 addition & 1 deletion _layouts/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ <h1 class="header-post-title">{{ page.title }}</h1>
</p>

<div class="c-content">
{{ content }}
{{ content | extlinks }}
</div>

<div class="text-end">
Expand Down
76 changes: 76 additions & 0 deletions _plugins/jekyll-extlinks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Jekyll ExtLinks Plugin
# Adds custom attributes to external links (rel="nofollow", target="_blank", etc.)
#
# Configuration example in _config.yml (notice the indentation matters):
#
# extlinks:
# attributes: {rel: nofollow, target: _blank}
# rel_exclude: ['host1.com', 'host2.net']
#
# (attributes are required - at least one of them, rel_exclude is optional)
# Relative links will not be processed.
# Links to hosts listed in rel_exclude will not have the 'rel' attribute set.
# Links which have the 'rel' attribute already will keep it unchanged, like
# this one in Markdown:
# [Link text](http://someurl.com){:rel="dofollow"}
#
# Using in layouts: {{ content | extlinks }}
#
# Developed by Dmitry Ogarkov - http://ogarkov.com/jekyll/plugins/extlinks/
# Based on http://dev.mensfeld.pl/2014/12/rackrails-middleware-that-will-ensure-relnofollow-for-all-your-links/

require 'jekyll'
require 'nokogiri'

module Jekyll
module ExtLinks
# Access plugin config in _config.yml
def config
@context.registers[:site].config['extlinks']
end

# Checks if str contains any fragment of the fragments array
def contains_any(str, fragments)
return false unless Regexp.union(fragments) =~ str
true
end

def extlinks(content)
# Process configured link attributes and whitelisted hosts
if config
if config['attributes']
attributes = Array(config['attributes'])
end
if config['rel_exclude']
rel_exclude = Array(config['rel_exclude'])
end
end
# Stop if no attributes were specified
return content unless attributes

doc = Nokogiri::HTML.fragment(content)
# Stop if we could't parse with HTML
return content unless doc

doc.css('a').each do |a|
# If this is a local link don't change it
next unless a.get_attribute('href') =~ /\Ahttp/i

attributes.each do |attr, value|
if attr.downcase == 'rel'
# If there's a rel already don't change it
next unless !a.get_attribute('rel') || a.get_attribute('rel').empty?
# Skip whitelisted hosts for the 'rel' attribute
next if rel_exclude && contains_any(a.get_attribute('href'), rel_exclude)
end
a.set_attribute(attr, value)
end
end

doc.to_s
end

end
end

Liquid::Template.register_filter(Jekyll::ExtLinks)
2 changes: 1 addition & 1 deletion shared

0 comments on commit c2751ae

Please sign in to comment.