diff --git a/_layouts/toc.html b/_layouts/toc.html new file mode 100644 index 0000000000000000000000000000000000000000..a8090d9fb27285af17c7353d6a40caabc1fbeac0 --- /dev/null +++ b/_layouts/toc.html @@ -0,0 +1,16 @@ +--- +layout: page +dataspy: "#sidenav" +toc: "true" +--- +<div class="row stickyrow"> + <div class="col-sm-9"> + {{ content | parse_toc }} + </div> + <div class="col-sm-3"> + <nav class="sidenav fixedsticky" id="sidenav"><ul class="nav list-group"> + {% for i in page.toc %}<li class="nav-item list-group-item"><a href="#{{i.id}}" class="nav-link">{{i.name}}</a></li> +{% endfor %} + </ul></nav> + </div> +</div> diff --git a/_plugins/jekyll-toc.rb b/_plugins/jekyll-toc.rb new file mode 100644 index 0000000000000000000000000000000000000000..6c3aa58a34a96d43516fd605d284143424e664f1 --- /dev/null +++ b/_plugins/jekyll-toc.rb @@ -0,0 +1,34 @@ +require 'nokogiri' + +module Jekyll + # parse logic is from html-pipeline toc_filter + # https://github.com/jch/html-pipeline/blob/v1.1.0/lib/html/pipeline/toc_filter.rb + module TableOfContentsFilter + PUNCTUATION_REGEXP = RUBY_VERSION > "1.9" ? /[^\p{Word}\- ]/u : /[^\w\- ]/ + + def parse_toc(html) + toc = [] + doc = Nokogiri::HTML::DocumentFragment.parse(html) + headers = Hash.new(0) + + doc.css('h1, h2, h3, h4, h5, h6').each do |node| + text = node.text + id = text.downcase + id.gsub!(PUNCTUATION_REGEXP, '') # remove punctuation + id.gsub!(' ', '-') # replace spaces with dash + + uniq = (headers[id] > 0) ? "-#{headers[id]}" : '' + uniqid = "#{id}#{uniq}" + headers[id] += 1 + + toc << { 'id' => uniqid, 'name' => text, 'level' => node.name } + node['id'] = uniqid + end + @context.environments.first['page']['toc'] = toc + + doc.inner_html + end + end +end + +Liquid::Template.register_filter(Jekyll::TableOfContentsFilter) diff --git a/publications/index.html b/publications/index.html index f7fea3656c951d3adea7df2887c808852b82dee5..11718aa50ff77d6440c036e32b91b00a5a03f6e3 100644 --- a/publications/index.html +++ b/publications/index.html @@ -1,16 +1,6 @@ --- title: Publications -dataspy: "#sidenav" menu: true +layout: toc --- -<div class="row stickyrow"> - <div class="col-sm-9"> - {% bibliography -g year %} - </div> - <div class="col-sm-3"> - <nav class="sidenav fixedsticky" id="sidenav"><ul class="nav list-group"> - {% for group in page.bibliography_groups %}<li class="nav-item list-group-item"><a href="#{{group[0]}}" class="nav-link">{{group[0]}}</a></li> -{% endfor %} - </ul></nav> - </div> -</div> +{% bibliography -g year %}