home-assistant.io/plugins/output_modder.rb
Bram Kragten 630f456aed
Merge current into next (#40176)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: ecspiegel <87676266+ecspiegel@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: c0ffeeca7 <38767475+c0ffeeca7@users.noreply.github.com>
Co-authored-by: alawadhi3000 <5523980+alawadhi3000@users.noreply.github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Franck Nijhof <git@frenck.dev>
Co-authored-by: Marcel van der Veldt <m.vanderveldt@outlook.com>
Co-authored-by: staticdev <staticdev-support@proton.me>
Co-authored-by: tronikos <tronikos@users.noreply.github.com>
Co-authored-by: AlCalzone <dominic.griesel@nabucasa.com>
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
Co-authored-by: essys <essys@users.noreply.github.com>
Co-authored-by: Abílio Costa <abmantis@users.noreply.github.com>
Co-authored-by: Gord <31004434+googanhiem@users.noreply.github.com>
Co-authored-by: Darren Griffin <darren.griffin@live.co.uk>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: hanwg <han.wuguang@gmail.com>
Co-authored-by: Adam W <35391288+A-damW@users.noreply.github.com>
Co-authored-by: Hessel <hesselonline@users.noreply.github.com>
Co-authored-by: Damien Sorel <mistic@strangeplanet.fr>
Co-authored-by: Tempura San <tempura.san@gmail.com>
Co-authored-by: Manu <4445816+tr4nt0r@users.noreply.github.com>
Co-authored-by: J. Diego Rodríguez Royo <jdrr1998@hotmail.com>
Co-authored-by: lexitus <38081592+lexitus@users.noreply.github.com>
Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com>
Co-authored-by: Evgeny Sureev <u@litka.ru>
Co-authored-by: Stefan Agner <stefan@agner.ch>
Co-authored-by: TomArm <TomArm@users.noreply.github.com>
Co-authored-by: starkillerOG <starkiller.og@gmail.com>
Co-authored-by: Maciej Bieniek <bieniu@users.noreply.github.com>
Co-authored-by: Guillaume Rischard <github@stereo.lu>
Co-authored-by: Geoff <85890024+Thulrus@users.noreply.github.com>
Co-authored-by: GhoweVege <85890024+GhoweVege@users.noreply.github.com>
Co-authored-by: Logan Rosen <loganrosen@gmail.com>
Co-authored-by: Robert Resch <robert@resch.dev>
Co-authored-by: RanTheLab <RanTheLab@users.noreply.github.com>
Co-authored-by: Jan Bouwhuis <jbouwh@users.noreply.github.com>
Co-authored-by: John Hess <john@h3ss.com>
Co-authored-by: Marco <cdrfun@cdrfun.eu>
Co-authored-by: robthebold <38596885+robthebold@users.noreply.github.com>
Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
Co-authored-by: Pieter Rautenbach <parautenbach@gmail.com>
Co-authored-by: Tsvi Mostovicz <ttmost@gmail.com>
Co-authored-by: Michael <35783820+mib1185@users.noreply.github.com>
Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
Co-authored-by: Galorhallen <12990764+Galorhallen@users.noreply.github.com>
Co-authored-by: Jan-Philipp Benecke <jan-philipp@bnck.me>
Co-authored-by: Jan Čermák <sairon@users.noreply.github.com>
Co-authored-by: Norbert Rittel <norbert@rittel.de>
Co-authored-by: Mr. Snyds <41122989+mrsnyds@users.noreply.github.com>
Co-authored-by: Allen Porter <allen.porter@gmail.com>
Co-authored-by: Ludovic BOUÉ <lboue@users.noreply.github.com>
Co-authored-by: threatdisplay <ajbriones@gmail.com>
Co-authored-by: Kamil Breguła <mik-laj@users.noreply.github.com>
Co-authored-by: Simone Chemelli <simone.chemelli@gmail.com>
Co-authored-by: Timothy <6560631+TimoPtr@users.noreply.github.com>
Co-authored-by: Joris Pelgröm <jpelgrom@users.noreply.github.com>
Co-authored-by: Wendelin <12148533+wendevlin@users.noreply.github.com>
Co-authored-by: Arie Catsman <120491684+catsmanac@users.noreply.github.com>
Co-authored-by: Marko Dimjašević <marko@dimjasevic.net>
Co-authored-by: karwosts <32912880+karwosts@users.noreply.github.com>
Co-authored-by: Guido Schmitz <Shutgun@users.noreply.github.com>
2025-07-28 16:45:52 +02:00

74 lines
2.6 KiB
Ruby

# Jekyll Out Modder - Allows for mangling/modding the HTML output
#
# This is combined in a single plugin/filter to reduce the NokoGiri dom
# parsing to just once per page/content.
#
# - Automatically adds rel='external nofollow' to outgoing links.
# - Automatically make headers linkable
#
require 'jekyll'
require 'nokogiri'
module Jekyll
module OutputModder
def output_modder(content)
dom = Nokogiri::HTML.fragment(content)
# Find all links, make all external links rel='external nofollow'
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
# Append an external link icon, if there isn't an icon already
next if link.get_attribute('href') =~ /\Ahttps?:\/\/\w*.?home-assistant.io/i
next if link.css('iconify-icon').any?
icon = Nokogiri::XML::Node.new('iconify-icon', dom)
icon['inline'] = true
icon['icon'] = 'tabler:external-link'
icon['class'] = 'external-link'
link.add_child(icon)
# Play nice with our own links
next if link.get_attribute('href') =~ /\Ahttps?:\/\/(?:\w+\.)?(?:home-assistant\.io|esphome\.io|nabucasa\.com|openhomefoundation\.org)/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
# Find all headers, make them linkable with unique slug names
used_slugs = {}
dom.css('h2,h3,h4,h5,h6,h7,h8').each do |header|
# Skip linked headers
next if header.at_css('a')
title = header.content
# Clean the title to create a slug
base_slug = title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
# Make slug unique by adding counter if needed
if used_slugs[base_slug]
used_slugs[base_slug] += 1
slug = "#{base_slug}-#{used_slugs[base_slug] - 1}"
else
used_slugs[base_slug] = 1
slug = base_slug
end
header.children = "#{title} <a class='title-link' name='#{slug}' href='\##{slug}'></a>"
end
dom.to_s
end
end
end
Liquid::Template.register_filter(Jekyll::OutputModder)