aboutsummaryrefslogtreecommitdiff
path: root/_plugins/toc.rb
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2024-04-28 12:28:40 +0200
committerlonkaars <loek@pipeframe.xyz>2024-04-28 12:28:40 +0200
commita2a2e04fcd623e515d3f106038d8d46f827d4c79 (patch)
tree0611cb8b64bd8dd3d3b679b204f096c9ea2f2252 /_plugins/toc.rb
parentc171f4e5b83abdb3dd8b3ece6063a5b482ad3b6d (diff)
fix table of contents
Diffstat (limited to '_plugins/toc.rb')
-rw-r--r--_plugins/toc.rb62
1 files changed, 62 insertions, 0 deletions
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 '<div class="chapterChildren">%s</div>' % [ output_toc ]
+ end
+
+ def output_toc
+ # empty toc (this check prevents crash)
+ return "" if @els.length == 0
+
+ output = '<ul>'
+
+ current_level = el_level(@els[0])
+
+ while @els.length > 0
+ el = @els[0]
+ el_next = @els[1]
+ level = el_level(el)
+ level_next = el_level(el_next || el) # || el to prevent crash on end of list
+
+ if level >= level_next
+ output += '<li>'
+ else
+ output += '<li class="stub"><details open>'
+ output += '<summary>'
+ end
+
+ output += '<a href="#%s">%s</a>' % [ el['id'], el.inner_html ]
+ @els.shift()
+
+ if level >= level_next
+ output += '</li>'
+ else
+ output += '</summary>'
+ output += output_toc
+ output += '</details></li>'
+ end
+
+ break if level_next < level
+ end
+
+ output += '</ul>'
+
+ return output
+ end
+
+ def el_level el
+ return Integer(el.name[1..])
+ end
+ end
+end
+
+Liquid::Template.register_tag('toc', Jekyll::TOC)
+