I'm trying to create a filter that adds references to the TOC after each h2
heading.
Here is what I've done but it doesn't work, and each time I modify the code all the relevant pages are not recompiled.
{Project dir}/Rules:
compile '/**/*.md' do
filter :kramdown
layout '/default.*'
filter :add_ref_to_toc
filter :add_toc_french
write item.identifier.without_ext + '/index.html'
end
{Project dir}/lib/filters/add_ref_to_toc.rb:
require 'nokogiri'
class AddRefToTocFilter < Nanoc::Filter
identifier :add_ref_to_toc
def run(content, params={})
doc = Nokogiri::HTML.fragment(content)
doc.css('#contents h2') do |header|
header.add_next_sibling "<br/><a href='#toccontent'>Aller à la table des matières</a><br/><br/>"
end
doc.to_s
end
end
Page after processing:
<div id="contents">
<h2>title 1</h2>
<br/><a href='#toccontent'>Aller à la table des matières</a><br/><br/>
<h2>title 2</h2>
<br/><a href='#toccontent'>Aller à la table des matières</a><br/><br/>
<h2>title 3</h2>
<br/><a href='#toccontent'>Aller à la table des matières</a><br/><br/>
</div>