11{% capture tocWorkspace %}
22 {% comment %}
3- Version 1.0.6
3+ Version 1.0.11
44 https://github.com/allejo/jekyll-toc
55
66 "...like all things liquid - where there's a will, and ~36 hours to spare, there's usually a/some way" ~jaybe
2121 * item_class (string) : '' - add custom class(es) for each list item; has support for '%level%' placeholder, which is the current heading level
2222 * baseurl (string) : '' - add a base url to the TOC links for when your TOC is on another page than the actual content
2323 * anchor_class (string) : '' - add custom class(es) for each anchor element
24+ * skipNoIDs (bool) : false - skip headers that do not have an `id` attribute
2425
2526 Output:
2627 An ordered or unordered list representing the table of contents of a markdown block. This snippet will only
2930
3031 {% capture my_toc %}{% endcapture %}
3132 {% assign orderedList = include.ordered | default: false %}
33+ {% assign skipNoIDs = include.skipNoIDs | default: false %}
3234 {% assign minHeader = include.h_min | default: 1 %}
3335 {% assign maxHeader = include.h_max | default: 6 %}
3436 {% assign nodes = include.html | split: '< h ' %}
4143 {% continue %}
4244 {% endif %}
4345
46+ {% if skipNoIDs == true %}
47+ {% unless node contains "id=" %}
48+ {% continue %}
49+ {% endunless %}
50+ {% endif %}
51+
4452 {% assign headerLevel = node | replace: '"', '' | slice: 0, 1 | times: 1 %}
4553
4654 {% if headerLevel < minHeader or headerLevel > maxHeader %}
5260 {% assign minHeader = headerLevel %}
5361 {% endif %}
5462
55- {% assign indentAmount = headerLevel | minus: minHeader | add: 1 %}
63+ {% assign indentAmount = headerLevel | minus: minHeader %}
5664 {% assign _workspace = node | split: '</ h' %}
5765
5866 {% assign _idWorkspace = _workspace[0] | split: 'id="' %}
5967 {% assign _idWorkspace = _idWorkspace[1] | split: '"' %}
6068 {% assign html_id = _idWorkspace[0] %}
6169
70+ {% assign _classWorkspace = _workspace[0] | split: 'class="' %}
71+ {% assign _classWorkspace = _classWorkspace[1] | split: '"' %}
72+ {% assign html_class = _classWorkspace[0] %}
73+
74+ {% if html_class contains "no_toc" %}
75+ {% continue %}
76+ {% endif %}
77+
6278 {% capture _hAttrToStrip %}{{ _workspace[0] | split: '> ' | first }}> {% endcapture %}
6379 {% assign header = _workspace[0] | replace: _hAttrToStrip, '' %}
6480
6783 {% assign space = space | prepend: ' ' %}
6884 {% endfor %}
6985
70- {% unless include.item_class = = blank %}
86+ {% if include.item_class and include.item_class ! = blank %}
7187 {% capture listItemClass %}{:.{{ include.item_class | replace: '%level%', headerLevel }}}{% endcapture %}
72- {% endunless %}
88+ {% endif %}
89+
90+ {% capture anchor_body %}{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}{% endcapture %}
91+ {% capture anchor_body %}{{ anchor_body | replace: "|", "\|" }}{% endcapture %}
92+
93+ {% if html_id %}
94+ {% capture list_item %}[{{ anchor_body }}]({% if include.baseurl %}{{ include.baseurl }}{% endif %}#{{ html_id }}){% endcapture %}
95+ {% else %}
96+ {% capture list_item %}{{ anchor_body }}{% endcapture %}
97+ {% endif %}
7398
7499 {% capture my_toc %}{{ my_toc }}
75- {{ space }}{{ listModifier }} {{ listItemClass }} [{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}]({% if include.baseurl %}{{ include.baseurl }}{% endif %}#{{ html_id }}) {% if include.anchor_class %}{:.{{ include.anchor_class }}}{% endif %}{% endcapture %}
100+ {{ space }}{{ listModifier }} {{ listItemClass }} {{ list_item }} {% if include.anchor_class %}{:.{{ include.anchor_class }}}{% endif %}{% endcapture %}
76101 {% endfor %}
77102
78- {% if include.class %}
103+ {% if include.class and include.class != blank %}
79104 {% capture my_toc %}{:.{{ include.class }}}
80105{{ my_toc | lstrip }}{% endcapture %}
81106 {% endif %}
84109 {% capture my_toc %}{: #{{ include.id }}}
85110{{ my_toc | lstrip }}{% endcapture %}
86111 {% endif %}
87- {% endcapture %}{% assign tocWorkspace = '' %}{{ my_toc | markdownify | strip }}
112+ {% endcapture %}{% assign tocWorkspace = '' %}{{ my_toc | markdownify | strip }}
0 commit comments