diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Gemfile | 7 | ||||
-rw-r--r-- | _includes/toc.html | 24 | ||||
-rw-r--r-- | _layouts/post.html | 2 | ||||
-rw-r--r-- | _plugins/toc.rb | 62 | ||||
-rw-r--r-- | css/navbar.css | 1 | ||||
-rw-r--r-- | makefile | 2 |
7 files changed, 72 insertions, 27 deletions
@@ -4,3 +4,4 @@ _site .DS_Store +Gemfile.lock @@ -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 @@ -<div class="chapterChildren"> - <ul> - <li class="stub"> - <details open> - <summary><a href="#">1.</a></summary> - <ul> - <li><a href="#">1.1. a really really really long title here</a></li> - <li><a href="#">1.2.</a></li> - <li class="stub"> - <details open> - <summary><a href="#">1.3.</a></summary> - <ul> - <li><a href="#">1.3.1.</a></li> - <li><a href="#">1.3.2.</a></li> - </ul> - </details> - </li> - <li><a href="#">1.4.</a></li> - </ul> - </details> - </li> - </ul> -</div> - 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 @@ <div class='navAreaWrapper'> <div class='sticky'> {% include navbar.html page="post" %} - {% include toc.html content=content %} + {% toc %} </div> </div> <!-- <MobileNavbar /> --> 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) + 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; @@ -1,5 +1,5 @@ build: - jekyll build --quiet + bundle exec jekyll build --quiet clean: $(RM) -r _site |