The bmannconsulting.com website
1# If the configuration sets `open_external_links_in_new_tab` to a truthy value, 2# add 'target=_blank' to anchor tags that don't have `internal-link` class 3 4# frozen_string_literal: true 5require 'nokogiri' 6 7Jekyll::Hooks.register [:notes], :post_convert do |doc| 8 convert_links(doc) 9end 10 11Jekyll::Hooks.register [:pages], :post_convert do |doc| 12 # jekyll considers anything at the root as a page, 13 # we only want to consider actual pages 14 next unless doc.path.start_with?('_pages/') 15 convert_links(doc) 16end 17 18def convert_links(doc) 19 open_external_links_in_new_tab = !!doc.site.config["open_external_links_in_new_tab"] 20 21 if open_external_links_in_new_tab 22 parsed_doc = Nokogiri::HTML(doc.content) 23 parsed_doc.css("a:not(.internal-link):not(.footnote):not(.reversefootnote)").each do |link| 24 link.set_attribute('target', '_blank') 25 end 26 doc.content = parsed_doc.inner_html 27 end 28end