From 9487676bd823e38464e97b50b9b7ff6f93e25312 Mon Sep 17 00:00:00 2001 From: Maxime Vaillancourt Date: Sun, 14 Mar 2021 20:33:05 -0400 Subject: [PATCH] Skip non-pages for external links detection --- _plugins/open_external_links_in_new_tab.rb | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/_plugins/open_external_links_in_new_tab.rb b/_plugins/open_external_links_in_new_tab.rb index 847687b..fc00afe 100644 --- a/_plugins/open_external_links_in_new_tab.rb +++ b/_plugins/open_external_links_in_new_tab.rb @@ -1,9 +1,21 @@ +# If the configuration sets `open_external_links_in_new_tab` to a truthy value, +# add 'target=_blank' to anchor tags that don't have `internal-link` class + # frozen_string_literal: true require 'nokogiri' -# If the configuration sets `open_external_links_in_new_tab` to a truthy value, -# add 'target=_blank' to anchor tags that don't have `internal-link` class -Jekyll::Hooks.register [:pages, :notes], :post_convert do |doc| +Jekyll::Hooks.register [:notes], :post_convert do |doc| + convert_links(doc) +end + +Jekyll::Hooks.register [:pages], :post_convert do |doc| + # jekyll considers anything at the root as a page, + # we only want to consider actual pages + next unless doc.path.start_with?('_pages/') + convert_links(doc) +end + +def convert_links(doc) open_external_links_in_new_tab = !!doc.site.config["open_external_links_in_new_tab"] if open_external_links_in_new_tab