Skip to content

Commit 7398c88

Browse files
committed
Adding TOC include
1 parent 25e96c8 commit 7398c88

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html
99

1010
* 'is something wrong' and 'back to top' footer includes with optional footer spacer when used with footer content
1111
* Governance statement for use in footers
12+
* Table of Contents (TOC) generic include
1213

1314
### Changed [BREAKING!]
1415

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ All includes are namespaced using a `bas-style-kit/` directory - i.e. the includ
8686

8787
* [`head`](/docs/include/head.md)
8888
* [`body`](/docs/include/body.md)
89+
* [`toc`](/docs/include/toc.md)
8990

9091
#### Style Kit specific includes
9192

_includes/bas-style-kit/toc.html

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
{% capture tocWorkspace %}
2+
{% comment %}
3+
Version 1.0.4
4+
https://github.com/allejo/jekyll-toc
5+
6+
"...like all things liquid - where there's a will, and ~36 hours to spare, there's usually a/some way" ~jaybe
7+
8+
Usage:
9+
{% include toc.html html=content sanitize=true class="inline_toc" id="my_toc" h_min=2 h_max=3 %}
10+
11+
Parameters:
12+
* html (string) - the HTML of compiled markdown generated by kramdown in Jekyll
13+
14+
Optional Parameters:
15+
* sanitize (bool) : false - when set to true, the headers will be stripped of any HTML in the TOC
16+
* class (string) : '' - a CSS class assigned to the TOC
17+
* id (string) : '' - an ID to assigned to the TOC
18+
* h_min (int) : 1 - the minimum TOC header level to use; any header lower than this value will be ignored
19+
* h_max (int) : 6 - the maximum TOC header level to use; any header greater than this value will be ignored
20+
* ordered (bool) : false - when set to true, an ordered list will be outputted instead of an unordered list
21+
* item_class (string) : '' - add custom class for each list item; has support for '%level%' placeholder, which is the current heading level
22+
23+
Output:
24+
An ordered or unordered list representing the table of contents of a markdown block. This snippet will only generate the table of contents and will NOT output the markdown given to it
25+
{% endcomment %}
26+
27+
{% capture my_toc %}{% endcapture %}
28+
{% assign orderedList = include.ordered | default: false %}
29+
{% assign minHeader = include.h_min | default: 1 %}
30+
{% assign maxHeader = include.h_max | default: 6 %}
31+
{% assign nodes = include.html | split: '<h' %}
32+
{% assign firstHeader = true %}
33+
34+
{% capture listModifier %}{% if orderedList %}1.{% else %}-{% endif %}{% endcapture %}
35+
36+
{% for node in nodes %}
37+
{% if node == "" %}
38+
{% continue %}
39+
{% endif %}
40+
41+
{% assign headerLevel = node | replace: '"', '' | slice: 0, 1 | times: 1 %}
42+
43+
{% if headerLevel < minHeader or headerLevel > maxHeader %}
44+
{% continue %}
45+
{% endif %}
46+
47+
{% if firstHeader %}
48+
{% assign firstHeader = false %}
49+
{% assign minHeader = headerLevel %}
50+
{% endif %}
51+
52+
{% assign indentAmount = headerLevel | minus: minHeader | add: 1 %}
53+
{% assign _workspace = node | split: '</h' %}
54+
55+
{% assign _idWorkspace = _workspace[0] | split: 'id="' %}
56+
{% assign _idWorkspace = _idWorkspace[1] | split: '"' %}
57+
{% assign html_id = _idWorkspace[0] %}
58+
59+
{% capture _hAttrToStrip %}{{ _workspace[0] | split: '>' | first }}>{% endcapture %}
60+
{% assign header = _workspace[0] | replace: _hAttrToStrip, '' %}
61+
62+
{% assign space = '' %}
63+
{% for i in (1..indentAmount) %}
64+
{% assign space = space | prepend: ' ' %}
65+
{% endfor %}
66+
67+
{% unless include.item_class == blank %}
68+
{% capture listItemClass %}{:.{{ include.item_class | replace: '%level%', headerLevel }}}{% endcapture %}
69+
{% endunless %}
70+
71+
{% capture my_toc %}{{ my_toc }}
72+
{{ space }}{{ listModifier }} {{ listItemClass }} [{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}](#{{ html_id }}){% endcapture %}
73+
74+
{% endfor %}
75+
76+
{% if include.class %}
77+
{% capture my_toc %}{:.{{ include.class }}}
78+
{{ my_toc | lstrip }}{% endcapture %}
79+
{% endif %}
80+
81+
{% if include.id %}
82+
{% capture my_toc %}{: #{{ include.id }}}
83+
{{ my_toc | lstrip }}{% endcapture %}
84+
{% endif %}
85+
{% endcapture %}{% assign tocWorkspace = '' %}{{ my_toc | markdownify | strip }}

docs/include/toc.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# BAS Style Kit Jekyll Theme
2+
3+
## Table of Contents (TOC) include
4+
5+
Implements the third-party [jekyll-toc](https://github.com/allejo/jekyll-toc) include.
6+
7+
This is used over Karmadown's in-built TOC support where the TOC is not part of the page output, but part of a generic
8+
layout.
9+
10+
There are [various parameters](https://github.com/allejo/jekyll-toc#parameters) which can be used with this plugin.
11+
They can be set whenever this include is used.
12+
13+
This include is not designed to be overridden.

0 commit comments

Comments
 (0)