Skip to content

Commit d68bb90

Browse files
author
gandalf3
committed
Add a simple way to customize anchor injection
1 parent 09c8e16 commit d68bb90

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,28 @@ The general `toc-entry` is applied to all `li` elements in the `ul.section-nav`.
201201

202202
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 `<h1>...</h1>` (a single
203203
`#` in Markdown) will get the CSS class `toc-h1`.
204+
205+
#### Anchor injection
206+
207+
Anchor injection can be customized with a liquid template:
208+
209+
```yml
210+
toc:
211+
injection_template: 'path/to/template.html'
212+
```
213+
214+
The template is supplied with the original text content (`heading_text`) and id (`heading_id`) for each heading element:
215+
216+
```liquid
217+
<!-- Append an anchor pilcrow after the heading text, Sphinx style: -->
218+
{{ heading_text }} <a class="anchor" aria-hidden="true" href="{{ page.url }}{{ heading_id }}">¶</a>
219+
```
220+
221+
The above template, when injected on an `<h1>`, yeilds html like the following:
222+
223+
```html
224+
<h1 id="this-is-a-heading">
225+
<!-- Append an anchor pilcrow after the heading text, Sphinx style: -->
226+
This is a heading <a class="anchor" aria-hidden="true" href="example.com/this/page.html#this-is-a-heading">¶</a>
227+
</h1>
228+
```

lib/jekyll-toc.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def toc_only(html)
2424
def inject_anchors(html)
2525
return html unless toc_enabled?
2626

27-
::Jekyll::TableOfContents::Parser.new(html, toc_config).inject_anchors_into_html
27+
::Jekyll::TableOfContents::Parser.new(html, toc_config).inject_anchors_into_html @context
2828
end
2929

3030
def toc(html)

lib/table_of_contents/parser.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Parser
1313
'max_level' => 6,
1414
'list_class' => 'section-nav',
1515
'sublist_class' => '',
16+
'injection_template' => '',
1617
'item_class' => 'toc-entry',
1718
'item_prefix' => 'toc-'
1819
}.freeze
@@ -23,6 +24,7 @@ def initialize(html, options = {})
2324
@toc_levels = options['min_level']..options['max_level']
2425
@no_toc_section_class = options['no_toc_section_class']
2526
@list_class = options['list_class']
27+
@injection_template = options['injection_template']
2628
@sublist_class = options['sublist_class']
2729
@item_class = options['item_class']
2830
@item_prefix = options['item_prefix']
@@ -37,11 +39,19 @@ def build_toc
3739
%(<ul class="#{@list_class}">\n#{build_toc_list(@entries)}</ul>)
3840
end
3941

40-
def inject_anchors_into_html
42+
def inject_anchors_into_html(context)
4143
@entries.each do |entry|
42-
entry[:content_node].add_previous_sibling(
43-
%(<a id="#{entry[:id]}#{entry[:uniq]}" class="anchor" href="##{entry[:id]}#{entry[:uniq]}" aria-hidden="true"><span class="octicon octicon-link"></span></a>)
44-
)
44+
45+
if @injection_template == ''
46+
entry[:content_node].add_previous_sibling(
47+
%(<a id="#{entry[:id]}#{entry[:uniq]}" class="anchor" href="##{entry[:id]}#{entry[:uniq]}" aria-hidden="true"><span class="octicon octicon-link"></span></a>)
48+
)
49+
else
50+
template = File.read(@injection_template)
51+
context.merge({'heading_id' => entry[:id].value, 'heading_text' => entry[:content_node].content})
52+
inject_markup = Liquid::Template.parse(template).render(context)
53+
entry[:content_node].replace(inject_markup)
54+
end
4555
end
4656

4757
@doc.inner_html

0 commit comments

Comments
 (0)