Add option to open external links in new tab
This commit is contained in:
16
_plugins/open_external_links_in_new_tab.rb
Normal file
16
_plugins/open_external_links_in_new_tab.rb
Normal file
@@ -0,0 +1,16 @@
|
||||
# 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|
|
||||
open_external_links_in_new_tab = !!doc.site.config["open_external_links_in_new_tab"]
|
||||
|
||||
if open_external_links_in_new_tab
|
||||
parsed_doc = Nokogiri::HTML(doc.content)
|
||||
parsed_doc.css("a:not(.internal-link)").each do |link|
|
||||
link.set_attribute('target', 'blank')
|
||||
end
|
||||
doc.content = parsed_doc.to_html
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user