Skip to content
Snippets Groups Projects
Commit a1510181 authored by Thomas Wood's avatar Thomas Wood
Browse files

Fix publications ToC

parent 8f8c22e3
No related branches found
No related tags found
No related merge requests found
---
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>
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)
---
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 %}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment