From a2a2e04fcd623e515d3f106038d8d46f827d4c79 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Sun, 28 Apr 2024 12:28:40 +0200 Subject: fix table of contents --- _plugins/toc.rb | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 _plugins/toc.rb (limited to '_plugins/toc.rb') diff --git a/_plugins/toc.rb b/_plugins/toc.rb new file mode 100644 index 0000000..216341d --- /dev/null +++ b/_plugins/toc.rb @@ -0,0 +1,62 @@ +require 'nokogiri' + +module Jekyll + class TOC < Liquid::Tag + def render context + # load HTML into nokogiri + html = context.registers[:page]['content'] + doc = Nokogiri::HTML(html) + + # enumerate over all h1-4 headings + @els = doc.css("h1, h2, h3, h4") + return '
%s
' % [ output_toc ] + end + + def output_toc + # empty toc (this check prevents crash) + return "" if @els.length == 0 + + output = '' + + return output + end + + def el_level el + return Integer(el.name[1..]) + end + end +end + +Liquid::Template.register_tag('toc', Jekyll::TOC) + -- cgit v1.2.3