From e3d1091d2f46dbe6d0df81cb060b8dcecf40b2c9 Mon Sep 17 00:00:00 2001 From: gandalf3 Date: Thu, 14 Mar 2019 00:11:27 -0700 Subject: [PATCH] Add a simple way to customize anchor injection --- README.md | 25 +++++++++++++++++++++++++ lib/jekyll-toc.rb | 2 +- lib/table_of_contents/parser.rb | 18 ++++++++++++++---- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 2f065d0..423064f 100644 --- a/README.md +++ b/README.md @@ -201,3 +201,28 @@ The general `toc-entry` is applied to all `li` elements in the `ul.section-nav`. Depending on the heading level each specific entry refers to, it has a second CSS class `toc-XX`, where `XX` is the HTML heading tag name. For example, the TOC entry linking to a heading `

...

` (a single `#` in Markdown) will get the CSS class `toc-h1`. + +#### Anchor injection + +Anchor injection can be customized with a liquid template: + +```yml +toc: + injection_template: 'path/to/template.html' +``` + +The template is supplied with the original text content (`heading_text`) and id (`heading_id`) for each heading element: + +```liquid + +{{ heading_text }} +``` + +The above template, when injected on an `

`, yeilds html like the following: + +```html +

+ + This is a heading +

+``` diff --git a/lib/jekyll-toc.rb b/lib/jekyll-toc.rb index 6a7d70c..2ea1067 100644 --- a/lib/jekyll-toc.rb +++ b/lib/jekyll-toc.rb @@ -24,7 +24,7 @@ def toc_only(html) def inject_anchors(html) return html unless toc_enabled? - ::Jekyll::TableOfContents::Parser.new(html, toc_config).inject_anchors_into_html + ::Jekyll::TableOfContents::Parser.new(html, toc_config).inject_anchors_into_html @context end def toc(html) diff --git a/lib/table_of_contents/parser.rb b/lib/table_of_contents/parser.rb index bbc4eb8..09b07e7 100644 --- a/lib/table_of_contents/parser.rb +++ b/lib/table_of_contents/parser.rb @@ -13,6 +13,7 @@ class Parser 'max_level' => 6, 'list_class' => 'section-nav', 'sublist_class' => '', + 'injection_template' => '', 'item_class' => 'toc-entry', 'item_prefix' => 'toc-' }.freeze @@ -23,6 +24,7 @@ def initialize(html, options = {}) @toc_levels = options['min_level']..options['max_level'] @no_toc_section_class = options['no_toc_section_class'] @list_class = options['list_class'] + @injection_template = options['injection_template'] @sublist_class = options['sublist_class'] @item_class = options['item_class'] @item_prefix = options['item_prefix'] @@ -37,11 +39,19 @@ def build_toc %() end - def inject_anchors_into_html + def inject_anchors_into_html(context) @entries.each do |entry| - entry[:content_node].add_previous_sibling( - %() - ) + + if @injection_template == '' + entry[:content_node].add_previous_sibling( + %() + ) + else + template = File.read(@injection_template) + context.merge({'heading_id' => entry[:id].value, 'heading_text' => entry[:content_node].content}) + inject_markup = Liquid::Template.parse(template).render(context) + entry[:content_node].replace(inject_markup) + end end @doc.inner_html