Skip to content

Commit 80782d7

Browse files
authored
Merge pull request #43 from allejo/feature/submenu-classes
2 parents 60ad45e + ac56595 commit 80782d7

File tree

5 files changed

+153
-26
lines changed

5 files changed

+153
-26
lines changed

README.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,20 @@ Alright, so how do you use it?
4747

4848
This snippet is highly customizable. Here are the available parameters to change the behavior of the snippet.
4949

50-
| Parameter | Type | Default | Description |
51-
| -------------- | :----: | :-----: | ----------- |
52-
| `html` | string | <sup>*</sup> | the HTML of compiled markdown generated by kramdown in Jekyll |
53-
| `sanitize` | bool | false | when set to true, the headers will be stripped of any HTML in the TOC |
54-
| `class` | string | '' | a CSS class assigned to the TOC; concat multiple classes with '.' |
55-
| `id` | string | '' | an ID to be assigned to the TOC |
56-
| `h_min` | int | 1 | the minimum TOC header level to use; any heading lower than this value will be ignored |
57-
| `h_max` | int | 6 | the maximum TOC header level to use; any heading greater than this value will be ignored |
58-
| `ordered` | bool | false | when set to true, an ordered list will be outputted instead of an unordered list |
59-
| `item_class` | string | '' | add custom class for each list item; has support for `%level%` placeholder, which is the current heading level |
60-
| `baseurl` | string | '' | add a base url to the TOC links for when your TOC is on another page than the actual content |
61-
| `anchor_class` | string | '' | add custom class(es) for each anchor element |
62-
| `skipNoIDs` | bool | false | skip headers that do not have an `id` attribute |
50+
| Parameter | Type | Default | Description |
51+
| --------------- | :----: | :-----: | ----------- |
52+
| `html` | string | <sup>*</sup> | the HTML of compiled markdown generated by kramdown in Jekyll |
53+
| `sanitize` | bool | false | when set to true, the headers will be stripped of any HTML in the TOC |
54+
| `class` | string | '' | a CSS class assigned to the TOC; concat multiple classes with '.' |
55+
| `id` | string | '' | an ID to be assigned to the TOC |
56+
| `h_min` | int | 1 | the minimum TOC header level to use; any heading lower than this value will be ignored |
57+
| `h_max` | int | 6 | the maximum TOC header level to use; any heading greater than this value will be ignored |
58+
| `ordered` | bool | false | when set to true, an ordered list will be outputted instead of an unordered list |
59+
| `item_class` | string | '' | add custom class for each list item; has support for `%level%` placeholder, which is the current heading level |
60+
| `submenu_class` | string | '' | add custom class(es) for each child group of headings; has support for '%level%' placeholder which is the current "submenu" heading level |
61+
| `baseurl` | string | '' | add a base url to the TOC links for when your TOC is on another page than the actual content |
62+
| `anchor_class` | string | '' | add custom class(es) for each anchor element |
63+
| `skipNoIDs` | bool | false | skip headers that do not have an `id` attribute |
6364

6465
<sup>*</sup> This is a required parameter
6566

_includes/toc.html

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
OTHER DEALINGS IN THE SOFTWARE.
2525
{% endcomment %}
2626
{% comment %}
27-
Version 1.0.13
27+
Version 1.0.14
2828
https://github.com/allejo/jekyll-toc
2929

3030
"...like all things liquid - where there's a will, and ~36 hours to spare, there's usually a/some way" ~jaybe
@@ -36,16 +36,17 @@
3636
* html (string) - the HTML of compiled markdown generated by kramdown in Jekyll
3737

3838
Optional Parameters:
39-
* sanitize (bool) : false - when set to true, the headers will be stripped of any HTML in the TOC
40-
* class (string) : '' - a CSS class assigned to the TOC
41-
* id (string) : '' - an ID to assigned to the TOC
42-
* h_min (int) : 1 - the minimum TOC header level to use; any header lower than this value will be ignored
43-
* h_max (int) : 6 - the maximum TOC header level to use; any header greater than this value will be ignored
44-
* ordered (bool) : false - when set to true, an ordered list will be outputted instead of an unordered list
45-
* item_class (string) : '' - add custom class(es) for each list item; has support for '%level%' placeholder, which is the current heading level
46-
* baseurl (string) : '' - add a base url to the TOC links for when your TOC is on another page than the actual content
47-
* anchor_class (string) : '' - add custom class(es) for each anchor element
48-
* skipNoIDs (bool) : false - skip headers that do not have an `id` attribute
39+
* sanitize (bool) : false - when set to true, the headers will be stripped of any HTML in the TOC
40+
* class (string) : '' - a CSS class assigned to the TOC
41+
* id (string) : '' - an ID to assigned to the TOC
42+
* h_min (int) : 1 - the minimum TOC header level to use; any header lower than this value will be ignored
43+
* h_max (int) : 6 - the maximum TOC header level to use; any header greater than this value will be ignored
44+
* ordered (bool) : false - when set to true, an ordered list will be outputted instead of an unordered list
45+
* item_class (string) : '' - add custom class(es) for each list item; has support for '%level%' placeholder, which is the current heading level
46+
* submenu_class (string) : '' - add custom class(es) for each child group of headings; has support for '%level%' placeholder which is the current "submenu" heading level
47+
* baseurl (string) : '' - add a base url to the TOC links for when your TOC is on another page than the actual content
48+
* anchor_class (string) : '' - add custom class(es) for each anchor element
49+
* skipNoIDs (bool) : false - skip headers that do not have an `id` attribute
4950

5051
Output:
5152
An ordered or unordered list representing the table of contents of a markdown block. This snippet will only
@@ -59,6 +60,7 @@
5960
{% assign maxHeader = include.h_max | default: 6 %}
6061
{% assign nodes = include.html | split: '<h' %}
6162
{% assign firstHeader = true %}
63+
{% assign previousLevel = 0 %}
6264

6365
{% capture listModifier %}{% if orderedList %}1.{% else %}-{% endif %}{% endcapture %}
6466

@@ -116,8 +118,25 @@
116118
{% capture list_item %}{{ anchor_body }}{% endcapture %}
117119
{% endif %}
118120

121+
<!--
122+
If we have a submenu class and we're unindenting, then we need to add a "closing" class to this group of bullet
123+
points
124+
-->
125+
{% if include.submenu_class and previousLevel > indentAmount %}
126+
<!--
127+
`space` is the current indentation, so we if want to close off the previous grouping, we need to add one
128+
more level of indentation
129+
-->
130+
{% assign submenuIndentation = space | prepend: ' ' %}
131+
132+
{% capture my_toc %}{{ my_toc }}
133+
{{ submenuIndentation }}{:.{{ include.submenu_class | replace: '%level%', previousLevel }}}{% endcapture %}
134+
{% endif %}
135+
119136
{% capture my_toc %}{{ my_toc }}
120137
{{ space }}{{ listModifier }} {{ listItemClass }} {{ list_item }}{% if include.anchor_class %}{:.{{ include.anchor_class }}}{% endif %}{% endcapture %}
138+
139+
{% assign previousLevel = indentAmount %}
121140
{% endfor %}
122141

123142
{% if include.class and include.class != blank %}
@@ -129,4 +148,23 @@
129148
{% capture my_toc %}{: #{{ include.id }}}
130149
{{ my_toc | lstrip }}{% endcapture %}
131150
{% endif %}
151+
152+
<!--
153+
If we have a submenu class, we need to close off all the remaining list item groups so that submenu classes are
154+
applied correctly to them
155+
-->
156+
{% if include.submenu_class != blank %}
157+
<!-- The last level of indentation that we were at, we need to work backwards from there closing each group -->
158+
{% for i in (1..previousLevel) %}
159+
{% assign lvl = previousLevel | plus: 1 | minus: i %} <!-- Invert the indent level based on `i` -->
160+
{% assign closingSpace = '' %}
161+
162+
{% for i in (1..lvl) %}
163+
{% assign closingSpace = closingSpace | prepend: ' ' %}
164+
{% endfor %}
165+
166+
{% capture my_toc %}{{ my_toc }}
167+
{{ closingSpace }}{:.{{ include.submenu_class | replace: '%level%', lvl }}}{% endcapture %}
168+
{% endfor %}
169+
{% endif %}
132170
{% endcapture %}{% assign tocWorkspace = '' %}{{ my_toc | markdownify | strip }}

_tests/sanitizeDisabled.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
{% capture markdown %}
66
# Heading 1
77

8-
## `Heading` 2.1
8+
## Heading 2.1
99

1010
### Heading **3**
1111

@@ -24,7 +24,7 @@
2424
<a href="#heading-1">Heading 1</a>
2525
<ul>
2626
<li>
27-
<a href="#heading-21"><code class="highlighter-rouge">Heading</code> 2.1</a>
27+
<a href="#heading-21">Heading 2.1</a>
2828
<ul>
2929
<li>
3030
<a href="#heading-3">Heading <strong>3</strong></a>

_tests/subMenuClass.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
# https://github.com/allejo/jekyll-toc/issues/40
3+
---
4+
5+
{% capture markdown %}
6+
# Heading 1
7+
8+
## Heading 2.1
9+
10+
### Heading 3.1
11+
12+
### Heading 3.2
13+
14+
## Heading 2.2
15+
16+
### Heading 3.3
17+
18+
### Heading 3.4
19+
{% endcapture %}
20+
{% assign text = markdown | markdownify %}
21+
22+
{% include toc.html html=text submenu_class="submenu-%level%" %}
23+
24+
<!-- /// -->
25+
26+
<ul>
27+
<li>
28+
<a href="#heading-1">Heading 1</a>
29+
<ul class="submenu-1">
30+
<li>
31+
<a href="#heading-21">Heading 2.1</a>
32+
<ul class="submenu-2">
33+
<li><a href="#heading-31">Heading 3.1</a></li>
34+
<li><a href="#heading-32">Heading 3.2</a></li>
35+
</ul>
36+
</li>
37+
<li>
38+
<a href="#heading-22">Heading 2.2</a>
39+
<ul class="submenu-2">
40+
<li><a href="#heading-33">Heading 3.3</a></li>
41+
<li><a href="#heading-34">Heading 3.4</a></li>
42+
</ul>
43+
</li>
44+
</ul>
45+
</li>
46+
</ul>

_tests/subMenuClassSkipNoIDs.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
# https://github.com/allejo/jekyll-toc/issues/40
3+
---
4+
5+
{% capture markdown %}
6+
# Heading 1
7+
8+
## Heading 2.1
9+
10+
<h3>Heading 3.1</h3>
11+
12+
<h3>Heading 3.2</h3>
13+
14+
## Heading 2.2
15+
16+
### Heading 3.3
17+
18+
### Heading 3.4
19+
{% endcapture %}
20+
{% assign text = markdown | markdownify %}
21+
22+
{% include toc.html html=text skipNoIDs=true submenu_class="submenu-%level%" %}
23+
24+
<!-- /// -->
25+
26+
<ul>
27+
<li>
28+
<a href="#heading-1">Heading 1</a>
29+
<ul class="submenu-1">
30+
<li>
31+
<a href="#heading-21">Heading 2.1</a>
32+
</li>
33+
<li>
34+
<a href="#heading-22">Heading 2.2</a>
35+
<ul class="submenu-2">
36+
<li><a href="#heading-33">Heading 3.3</a></li>
37+
<li><a href="#heading-34">Heading 3.4</a></li>
38+
</ul>
39+
</li>
40+
</ul>
41+
</li>
42+
</ul>

0 commit comments

Comments
 (0)