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 --- .gitignore | 1 + Gemfile | 7 ++++++ _includes/toc.html | 24 --------------------- _layouts/post.html | 2 +- _plugins/toc.rb | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ css/navbar.css | 1 - makefile | 2 +- 7 files changed, 72 insertions(+), 27 deletions(-) create mode 100644 Gemfile delete mode 100644 _includes/toc.html create mode 100644 _plugins/toc.rb diff --git a/.gitignore b/.gitignore index ca77d87..487f1af 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ _site .DS_Store +Gemfile.lock diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..2f7ad5d --- /dev/null +++ b/Gemfile @@ -0,0 +1,7 @@ +ruby '~> 3.0' +source 'https://rubygems.org' + +gem 'jekyll', '~> 4.3' +gem 'json', '~> 2.7' +gem 'nokogiri', '~> 1.16' + diff --git a/_includes/toc.html b/_includes/toc.html deleted file mode 100644 index 311864b..0000000 --- a/_includes/toc.html +++ /dev/null @@ -1,24 +0,0 @@ -
- -
- diff --git a/_layouts/post.html b/_layouts/post.html index 7f035af..4597623 100644 --- a/_layouts/post.html +++ b/_layouts/post.html @@ -15,7 +15,7 @@ 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) + diff --git a/css/navbar.css b/css/navbar.css index b6612d5..10933a0 100644 --- a/css/navbar.css +++ b/css/navbar.css @@ -143,7 +143,6 @@ .chapterChildren li, .chapterChildren summary { - display: block; min-height: 24px; padding-left: 32px; position: relative; diff --git a/makefile b/makefile index a8578d4..ddbddcd 100644 --- a/makefile +++ b/makefile @@ -1,5 +1,5 @@ build: - jekyll build --quiet + bundle exec jekyll build --quiet clean: $(RM) -r _site -- cgit v1.2.3