Skip to content

Commit faf86f5

Browse files
authoredDec 10, 2020
Update allejo/jekyll-toc to v1.1.0, skip headings without an ID (mmistakes#2752)
* Update allejo/jekyll-toc to v1.1.0, skip headings without an ID https://github.com/allejo/jekyll-toc/releases/tag/v1.1.0 * Update CHANGELOG and history
1 parent e61476d commit faf86f5

File tree

5 files changed

+86
-72
lines changed

5 files changed

+86
-72
lines changed
 

‎CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
- Update Indonesian localized UI text strings. [#2731](https://github.com/mmistakes/minimal-mistakes/pull/2731)
1111
- Update remote theme documentation. [#2734](https://github.com/mmistakes/minimal-mistakes/pull/2734)
12+
- Update allejo/jekyll-toc to v1.1.0, skip headings without an ID. [#2752](https://github.com/mmistakes/minimal-mistakes/pull/2752)
1213

1314
## [4.21.0](https://github.com/mmistakes/minimal-mistakes/releases/tag/4.21.0)
1415

@@ -17,7 +18,7 @@
1718
- Fix greedy navigation by improving reliability of remaining space for visible links. [#2664](https://github.com/mmistakes/minimal-mistakes/issues/2664)
1819
- Collapse white-space in `figure` helper to fix issues when used in Markdown ordered and unordered lists. [#2697](https://github.com/mmistakes/minimal-mistakes/pull/2697)
1920
- Fix dead link to CI services in documentation. [#2635](https://github.com/mmistakes/minimal-mistakes/issues/2635) [#2692](https://github.com/mmistakes/minimal-mistakes/pull/2692)
20-
- Fix a small type in documentation. [#2718](https://github.com/mmistakes/minimal-mistakes/pull/2718)
21+
- Fix a small typo in documentation. [#2718](https://github.com/mmistakes/minimal-mistakes/pull/2718)
2122

2223
### Enhancements
2324

‎_includes/toc.html

+80-68
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.14
27+
Version 1.1.0
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
@@ -44,127 +44,139 @@
4444
* ordered (bool) : false - when set to true, an ordered list will be outputted instead of an unordered list
4545
* item_class (string) : '' - add custom class(es) for each list item; has support for '%level%' placeholder, which is the current heading level
4646
* 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
47+
* base_url (string) : '' - add a base url to the TOC links for when your TOC is on another page than the actual content
4848
* anchor_class (string) : '' - add custom class(es) for each anchor element
49-
* skipNoIDs (bool) : false - skip headers that do not have an `id` attribute
49+
* skip_no_ids (bool) : false - skip headers that do not have an `id` attribute
5050

5151
Output:
5252
An ordered or unordered list representing the table of contents of a markdown block. This snippet will only
5353
generate the table of contents and will NOT output the markdown given to it
5454
{% endcomment %}
5555

56-
{% capture my_toc %}{% endcapture %}
56+
{% capture newline %}
57+
{% endcapture %}
58+
{% assign newline = newline | rstrip %} <!-- Remove the extra spacing but preserve the newline -->
59+
60+
{% capture deprecation_warnings %}{% endcapture %}
61+
62+
{% if include.baseurl %}
63+
{% capture deprecation_warnings %}{{ deprecation_warnings }}<!-- jekyll-toc :: "baseurl" has been deprecated, use "base_url" instead -->{{ newline }}{% endcapture %}
64+
{% endif %}
65+
66+
{% if include.skipNoIDs %}
67+
{% capture deprecation_warnings %}{{ deprecation_warnings }}<!-- jekyll-toc :: "skipNoIDs" has been deprecated, use "skip_no_ids" instead -->{{ newline }}{% endcapture %}
68+
{% endif %}
69+
70+
{% capture jekyll_toc %}{% endcapture %}
5771
{% assign orderedList = include.ordered | default: false %}
58-
{% assign skipNoIDs = include.skipNoIDs | default: false %}
72+
{% assign baseURL = include.base_url | default: include.baseurl | default: '' %}
73+
{% assign skipNoIDs = include.skip_no_ids | default: include.skipNoIDs | default: false %}
5974
{% assign minHeader = include.h_min | default: 1 %}
6075
{% assign maxHeader = include.h_max | default: 6 %}
61-
{% assign nodes = include.html | split: '<h' %}
76+
{% assign nodes = include.html | strip | split: '<h' %}
77+
6278
{% assign firstHeader = true %}
63-
{% assign previousLevel = 0 %}
79+
{% assign currLevel = 0 %}
80+
{% assign lastLevel = 0 %}
6481

65-
{% capture listModifier %}{% if orderedList %}1.{% else %}-{% endif %}{% endcapture %}
82+
{% capture listModifier %}{% if orderedList %}ol{% else %}ul{% endif %}{% endcapture %}
6683

6784
{% for node in nodes %}
6885
{% if node == "" %}
6986
{% continue %}
7087
{% endif %}
7188

72-
{% assign headerLevel = node | replace: '"', '' | slice: 0, 1 | times: 1 %}
89+
{% assign currLevel = node | replace: '"', '' | slice: 0, 1 | times: 1 %}
7390

74-
{% if headerLevel < minHeader or headerLevel > maxHeader %}
91+
{% if currLevel < minHeader or currLevel > maxHeader %}
7592
{% continue %}
7693
{% endif %}
7794

7895
{% assign _workspace = node | split: '</h' %}
7996

8097
{% assign _idWorkspace = _workspace[0] | split: 'id="' %}
8198
{% assign _idWorkspace = _idWorkspace[1] | split: '"' %}
82-
{% assign html_id = _idWorkspace[0] %}
99+
{% assign htmlID = _idWorkspace[0] %}
83100

84101
{% assign _classWorkspace = _workspace[0] | split: 'class="' %}
85102
{% assign _classWorkspace = _classWorkspace[1] | split: '"' %}
86-
{% assign html_class = _classWorkspace[0] %}
103+
{% assign htmlClass = _classWorkspace[0] %}
87104

88-
{% if html_class contains "no_toc" %}
105+
{% if htmlClass contains "no_toc" %}
89106
{% continue %}
90107
{% endif %}
91108

92109
{% if firstHeader %}
93-
{% assign firstHeader = false %}
94-
{% assign minHeader = headerLevel %}
110+
{% assign minHeader = currLevel %}
95111
{% endif %}
96112

97113
{% capture _hAttrToStrip %}{{ _workspace[0] | split: '>' | first }}>{% endcapture %}
98114
{% assign header = _workspace[0] | replace: _hAttrToStrip, '' %}
99115

100-
{% assign indentAmount = headerLevel | minus: minHeader %}
101-
{% assign space = '' %}
102-
{% for i in (1..indentAmount) %}
103-
{% assign space = space | prepend: ' ' %}
104-
{% endfor %}
105-
106116
{% if include.item_class and include.item_class != blank %}
107-
{% capture listItemClass %}{:.{{ include.item_class | replace: '%level%', headerLevel }}}{% endcapture %}
117+
{% capture listItemClass %} class="{{ include.item_class | replace: '%level%', currLevel | split: '.' | join: ' ' }}"{% endcapture %}
118+
{% endif %}
119+
120+
{% if include.submenu_class and include.submenu_class != blank %}
121+
{% assign subMenuLevel = currLevel | minus: 1 %}
122+
{% capture subMenuClass %} class="{{ include.submenu_class | replace: '%level%', subMenuLevel | split: '.' | join: ' ' }}"{% endcapture %}
108123
{% endif %}
109124

110-
{% capture anchor_body %}{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}{% endcapture %}
111-
{% capture anchor_body %}{{ anchor_body | replace: "|", "\|" }}{% endcapture %}
125+
{% capture anchorBody %}{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}{% endcapture %}
126+
127+
{% if htmlID %}
128+
{% capture anchorAttributes %} href="{% if baseURL %}{{ baseURL }}{% endif %}#{{ htmlID }}"{% endcapture %}
129+
130+
{% if include.anchor_class %}
131+
{% capture anchorAttributes %}{{ anchorAttributes }} class="{{ include.anchor_class | split: '.' | join: ' ' }}"{% endcapture %}
132+
{% endif %}
112133

113-
{% if html_id %}
114-
{% capture list_item %}[{{ anchor_body }}]({% if include.baseurl %}{{ include.baseurl }}{% endif %}#{{ html_id }}){% endcapture %}
134+
{% capture listItem %}<a{{ anchorAttributes }}>{{ anchorBody }}</a>{% endcapture %}
115135
{% elsif skipNoIDs == true %}
116136
{% continue %}
117137
{% else %}
118-
{% capture list_item %}{{ anchor_body }}{% endcapture %}
138+
{% capture listItem %}{{ anchorBody }}{% endcapture %}
119139
{% endif %}
120140

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 %}
141+
{% if currLevel > lastLevel %}
142+
{% capture jekyll_toc %}{{ jekyll_toc }}<{{ listModifier }}{{ subMenuClass }}>{% endcapture %}
143+
{% elsif currLevel < lastLevel %}
144+
{% assign repeatCount = lastLevel | minus: currLevel %}
145+
146+
{% for i in (1..repeatCount) %}
147+
{% capture jekyll_toc %}{{ jekyll_toc }}</li></{{ listModifier }}>{% endcapture %}
148+
{% endfor %}
149+
150+
{% capture jekyll_toc %}{{ jekyll_toc }}</li>{% endcapture %}
151+
{% else %}
152+
{% capture jekyll_toc %}{{ jekyll_toc }}</li>{% endcapture %}
134153
{% endif %}
135154

136-
{% capture my_toc %}{{ my_toc }}
137-
{{ space }}{{ listModifier }} {{ listItemClass }} {{ list_item }}{% if include.anchor_class %}{:.{{ include.anchor_class }}}{% endif %}{% endcapture %}
155+
{% capture jekyll_toc %}{{ jekyll_toc }}<li{{ listItemClass }}>{{ listItem }}{% endcapture %}
138156

139-
{% assign previousLevel = indentAmount %}
157+
{% assign lastLevel = currLevel %}
158+
{% assign firstHeader = false %}
140159
{% endfor %}
141160

142-
{% if include.class and include.class != blank %}
143-
{% capture my_toc %}{:.{{ include.class }}}
144-
{{ my_toc | lstrip }}{% endcapture %}
145-
{% endif %}
161+
{% assign repeatCount = minHeader | minus: 1 %}
162+
{% assign repeatCount = lastLevel | minus: repeatCount %}
163+
{% for i in (1..repeatCount) %}
164+
{% capture jekyll_toc %}{{ jekyll_toc }}</li></{{ listModifier }}>{% endcapture %}
165+
{% endfor %}
146166

147-
{% if include.id %}
148-
{% capture my_toc %}{: #{{ include.id }}}
149-
{{ my_toc | lstrip }}{% endcapture %}
150-
{% endif %}
167+
{% if jekyll_toc != '' %}
168+
{% assign rootAttributes = '' %}
169+
{% if include.class and include.class != blank %}
170+
{% capture rootAttributes %} class="{{ include.class | split: '.' | join: ' ' }}"{% endcapture %}
171+
{% endif %}
151172

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 %}
173+
{% if include.id and include.id != blank %}
174+
{% capture rootAttributes %}{{ rootAttributes }} id="{{ include.id }}"{% endcapture %}
175+
{% endif %}
165176

166-
{% capture my_toc %}{{ my_toc }}
167-
{{ closingSpace }}{:.{{ include.submenu_class | replace: '%level%', lvl }}}{% endcapture %}
168-
{% endfor %}
177+
{% if rootAttributes %}
178+
{% assign nodes = jekyll_toc | split: '>' %}
179+
{% capture jekyll_toc %}<{{ listModifier }}{{ rootAttributes }}>{{ nodes | shift | join: '>' }}>{% endcapture %}
180+
{% endif %}
169181
{% endif %}
170-
{% endcapture %}{% assign tocWorkspace = '' %}{{ my_toc | markdownify | strip }}
182+
{% endcapture %}{% assign tocWorkspace = '' %}{{ deprecation_warnings }}{{ jekyll_toc }}

‎_layouts/single.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<aside class="sidebar__right {% if page.toc_sticky %}sticky{% endif %}">
3737
<nav class="toc">
3838
<header><h4 class="nav__title"><i class="fas fa-{{ page.toc_icon | default: 'file-alt' }}"></i> {{ page.toc_label | default: site.data.ui-text[site.locale].toc_label | default: "On this page" }}</h4></header>
39-
{% include toc.html sanitize=true html=content h_min=1 h_max=6 class="toc__menu" %}
39+
{% include toc.html sanitize=true html=content h_min=1 h_max=6 class="toc__menu" skip_no_ids=true %}
4040
</nav>
4141
</aside>
4242
{% endif %}

‎docs/_docs/14-helpers.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ To embed the following Bilibili video at url `https://www.bilibili.com/video/BV1
249249
{% raw %}{% include video id="BV1E7411e7hC" provider="bilibili" %}{% endraw %}
250250
```
251251

252-
If you want to enable danmaku (弹幕) for the embedded video, which is disabled by default, you can supply an additional parameter `danmaku="1"` as shown below:
252+
If you want to enable danmaku (弹幕) for the embedded video, which is disabled by default, you can supply an additional parameter `danmaku="1"` as shown below:
253253

254254
```liquid
255255
{% raw %}{% include video id="BV1E7411e7hC" provider="bilibili" danmaku="1" %}{% endraw %}

‎docs/_docs/18-history.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ toc: false
2020

2121
- Update Indonesian localized UI text strings. [#2731](https://github.com/mmistakes/minimal-mistakes/pull/2731)]
2222
- Update remote theme documentation. [#2734](https://github.com/mmistakes/minimal-mistakes/pull/2734)
23+
- Update allejo/jekyll-toc to v1.1.0, skip headings without an ID. [#2752](https://github.com/mmistakes/minimal-mistakes/pull/2752)
2324

2425
## [4.21.0](https://github.com/mmistakes/minimal-mistakes/releases/tag/4.21.0)
2526

@@ -28,7 +29,7 @@ toc: false
2829
- Fix greedy navigation by improving reliability of remaining space for visible links. [#2664](https://github.com/mmistakes/minimal-mistakes/issues/2664)
2930
- Collapse white-space in `figure` helper to fix issues when used in Markdown ordered and unordered lists. [#2697](https://github.com/mmistakes/minimal-mistakes/pull/2697)
3031
- Fix dead link to CI services in documentation. [#2635](https://github.com/mmistakes/minimal-mistakes/issues/2635) [#2692](https://github.com/mmistakes/minimal-mistakes/pull/2692)
31-
- Fix a small type in documentation. [#2718](https://github.com/mmistakes/minimal-mistakes/pull/2718)
32+
- Fix a small typo in documentation. [#2718](https://github.com/mmistakes/minimal-mistakes/pull/2718)
3233

3334
### Enhancements
3435

0 commit comments

Comments
 (0)
Please sign in to comment.