|
1 | 1 | {% capture tocWorkspace %}
|
2 |
| - {% comment %} |
3 |
| - Copyright (c) 2017 Vladimir "allejo" Jimenez |
4 |
| - |
5 |
| - Permission is hereby granted, free of charge, to any person |
6 |
| - obtaining a copy of this software and associated documentation |
7 |
| - files (the "Software"), to deal in the Software without |
8 |
| - restriction, including without limitation the rights to use, |
9 |
| - copy, modify, merge, publish, distribute, sublicense, and/or sell |
10 |
| - copies of the Software, and to permit persons to whom the |
11 |
| - Software is furnished to do so, subject to the following |
12 |
| - conditions: |
13 |
| - |
14 |
| - The above copyright notice and this permission notice shall be |
15 |
| - included in all copies or substantial portions of the Software. |
16 |
| - |
17 |
| - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
18 |
| - EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
19 |
| - OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
20 |
| - NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
21 |
| - HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
22 |
| - WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
23 |
| - FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
24 |
| - OTHER DEALINGS IN THE SOFTWARE. |
25 |
| - {% endcomment %} |
26 |
| - {% comment %} |
27 |
| - Version 1.2.0 |
28 |
| - https://github.com/allejo/jekyll-toc |
29 |
| - |
30 |
| - "...like all things liquid - where there's a will, and ~36 hours to spare, there's usually a/some way" ~jaybe |
31 |
| - |
32 |
| - Usage: |
33 |
| - {% include jekyll-toc.html html=content sanitize=true class="inline_toc" id="my_toc" h_min=2 h_max=3 %} |
34 |
| - |
35 |
| - Parameters: |
36 |
| - * html (string) - the HTML of compiled markdown generated by kramdown in Jekyll |
37 |
| - |
38 |
| - 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 |
| - * 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 |
| - * base_url (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 |
| - * skip_no_ids (bool) : false - skip headers that do not have an `id` attribute |
50 |
| - |
51 |
| - Output: |
52 |
| - An ordered or unordered list representing the table of contents of a markdown block. This snippet will only |
53 |
| - generate the table of contents and will NOT output the markdown given to it |
54 |
| - {% endcomment %} |
55 |
| - |
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 %} |
| 2 | +{% comment %} |
| 3 | +Copyright (c) 2017 Vladimir "allejo" Jimenez |
| 4 | + |
| 5 | +Permission is hereby granted, free of charge, to any person |
| 6 | +obtaining a copy of this software and associated documentation |
| 7 | +files (the "Software"), to deal in the Software without |
| 8 | +restriction, including without limitation the rights to use, |
| 9 | +copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +copies of the Software, and to permit persons to whom the |
| 11 | +Software is furnished to do so, subject to the following |
| 12 | +conditions: |
| 13 | + |
| 14 | +The above copyright notice and this permission notice shall be |
| 15 | +included in all copies or substantial portions of the Software. |
| 16 | + |
| 17 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 19 | +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 20 | +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 21 | +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 22 | +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 23 | +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 24 | +OTHER DEALINGS IN THE SOFTWARE. |
| 25 | +{% endcomment %} |
| 26 | +{% comment %} |
| 27 | +Version 1.2.0 |
| 28 | +https://github.com/allejo/jekyll-toc |
| 29 | + |
| 30 | +"...like all things liquid - where there's a will, and ~36 hours to spare, there's usually a/some way" ~jaybe |
| 31 | + |
| 32 | +Usage: |
| 33 | +{% include jekyll-toc.html html=content sanitize=true class="inline_toc" id="my_toc" h_min=2 h_max=3 %} |
| 34 | + |
| 35 | +Parameters: |
| 36 | +* html (string) - the HTML of compiled markdown generated by kramdown in Jekyll |
| 37 | + |
| 38 | +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 |
| 46 | +the current heading level |
| 47 | +* submenu_class (string) : '' - add custom class(es) for each child group of headings; has support for '%level%' |
| 48 | +placeholder which is the current "submenu" heading level |
| 49 | +* base_url (string) : '' - add a base url to the TOC links for when your TOC is on another page than the actual content |
| 50 | +* anchor_class (string) : '' - add custom class(es) for each anchor element |
| 51 | +* skip_no_ids (bool) : false - skip headers that do not have an `id` attribute |
| 52 | + |
| 53 | +Output: |
| 54 | +An ordered or unordered list representing the table of contents of a markdown block. This snippet will only |
| 55 | +generate the table of contents and will NOT output the markdown given to it |
| 56 | +{% endcomment %} |
| 57 | + |
| 58 | +{% capture newline %} |
| 59 | +{% endcapture %} |
| 60 | +{% assign newline = newline | rstrip %} |
| 61 | +<!-- Remove the extra spacing but preserve the newline --> |
| 62 | + |
| 63 | +{% capture deprecation_warnings %}{% endcapture %} |
| 64 | + |
| 65 | +{% if include.baseurl %} |
| 66 | +{% capture deprecation_warnings %}{{ deprecation_warnings }} |
| 67 | +<!-- jekyll-toc :: "baseurl" has been deprecated, use "base_url" instead -->{{ newline }}{% endcapture %} |
| 68 | +{% endif %} |
| 69 | + |
| 70 | +{% if include.skipNoIDs %} |
| 71 | +{% capture deprecation_warnings %}{{ deprecation_warnings }} |
| 72 | +<!-- jekyll-toc :: "skipNoIDs" has been deprecated, use "skip_no_ids" instead -->{{ newline }}{% endcapture %} |
| 73 | +{% endif %} |
| 74 | + |
| 75 | +{% capture jekyll_toc %}{% endcapture %} |
| 76 | +{% assign orderedList = include.ordered | default: false %} |
| 77 | +{% assign baseURL = include.base_url | default: include.baseurl | default: '' %} |
| 78 | +{% assign skipNoIDs = include.skip_no_ids | default: include.skipNoIDs | default: false %} |
| 79 | +{% assign minHeader = include.h_min | default: 1 %} |
| 80 | +{% assign maxHeader = include.h_max | default: 6 %} |
| 81 | +{% assign nodes = include.html | strip | split: '<h' %} {% assign firstHeader=true %} {% assign currLevel=0 %} {% assign |
| 82 | + lastLevel=0 %} {% capture listModifier %}{% if orderedList %}ol{% else %}ul{% endif %}{% endcapture %} {% for node |
| 83 | + in nodes %} {% if node=="" %} {% continue %} {% endif %} {% if forloop.last==true %} {% continue %} {% endif %} {% |
| 84 | + assign currLevel=node | replace: '"' , '' | slice: 0, 1 | times: 1 %} {% if currLevel < minHeader or currLevel> |
| 85 | + maxHeader %} |
| 86 | + {% continue %} |
64 | 87 | {% endif %}
|
65 | 88 |
|
66 |
| - {% if include.skipNoIDs %} |
67 |
| - {% capture deprecation_warnings %}{{ deprecation_warnings }}<!-- jekyll-toc :: "skipNoIDs" has been deprecated, use "skip_no_ids" instead -->{{ newline }}{% endcapture %} |
| 89 | + {% assign _workspace = node | split: '</h' %} {% assign _idWorkspace=_workspace[0] | split: 'id="' %} {% assign |
| 90 | + _idWorkspace=_idWorkspace[1] | split: '"' %} {% assign htmlID=_idWorkspace[0] %} {% assign |
| 91 | + _classWorkspace=_workspace[0] | split: 'class="' %} {% assign _classWorkspace=_classWorkspace[1] | split: '"' %} {% |
| 92 | + assign htmlClass=_classWorkspace[0] %} {% if htmlClass contains "no_toc" %} {% continue %} {% endif %} {% if |
| 93 | + firstHeader %} {% assign minHeader=currLevel %} {% endif %} {% capture _hAttrToStrip %}{{ _workspace[0] | split: '>' |
| 94 | + | first }}>{% endcapture %} |
| 95 | +{% assign header = _workspace[0] | replace: _hAttrToStrip, '' %} |
| 96 | + |
| 97 | +{% if include.item_class and include.item_class != blank %} |
| 98 | +{% capture listItemClass %} class="{{ include.item_class | replace: '%level%', currLevel | split: '.' | join: ' ' }}"{% |
| 99 | +endcapture %} |
| 100 | +{% endif %} |
| 101 | + |
| 102 | +{% if include.submenu_class and include.submenu_class != blank %} |
| 103 | +{% assign subMenuLevel = currLevel | minus: 1 %} |
| 104 | +{% capture subMenuClass %} class="{{ include.submenu_class | replace: '%level%', subMenuLevel | split: '.' | join: ' ' |
| 105 | +}}"{% endcapture %} |
| 106 | +{% endif %} |
| 107 | + |
| 108 | +{% capture anchorBody %}{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}{% endcapture |
| 109 | +%} |
| 110 | + |
| 111 | +{% if htmlID %} |
| 112 | +{% capture anchorAttributes %} href="{% if baseURL %}{{ baseURL }}{% endif %}#{{ htmlID }}"{% endcapture %} |
| 113 | + |
| 114 | +{% if include.anchor_class %} |
| 115 | +{% capture anchorAttributes %}{{ anchorAttributes }} class="{{ include.anchor_class | split: '.' | join: ' ' }}"{% |
| 116 | +endcapture %} |
| 117 | +{% endif %} |
| 118 | + |
| 119 | +{% capture listItem %}<a{{ anchorAttributes }}>{{ anchorBody }}</a>{% endcapture %} |
| 120 | + {% elsif skipNoIDs == true %} |
| 121 | + {% continue %} |
| 122 | + {% else %} |
| 123 | + {% capture listItem %}{{ anchorBody }}{% endcapture %} |
68 | 124 | {% endif %}
|
69 | 125 |
|
70 |
| - {% capture jekyll_toc %}{% endcapture %} |
71 |
| - {% assign orderedList = include.ordered | default: false %} |
72 |
| - {% assign baseURL = include.base_url | default: include.baseurl | default: '' %} |
73 |
| - {% assign skipNoIDs = include.skip_no_ids | default: include.skipNoIDs | default: false %} |
74 |
| - {% assign minHeader = include.h_min | default: 1 %} |
75 |
| - {% assign maxHeader = include.h_max | default: 6 %} |
76 |
| - {% assign nodes = include.html | strip | split: '<h' %} |
77 |
| - |
78 |
| - {% assign firstHeader = true %} |
79 |
| - {% assign currLevel = 0 %} |
80 |
| - {% assign lastLevel = 0 %} |
81 |
| - |
82 |
| - {% capture listModifier %}{% if orderedList %}ol{% else %}ul{% endif %}{% endcapture %} |
83 |
| - |
84 |
| - {% for node in nodes %} |
85 |
| - {% if node == "" %} |
86 |
| - {% continue %} |
87 |
| - {% endif %} |
88 |
| - |
89 |
| - {% if forloop.last == true %} |
90 |
| - {% continue %} |
91 |
| - {% endif %} |
92 |
| - |
93 |
| - {% assign currLevel = node | replace: '"', '' | slice: 0, 1 | times: 1 %} |
94 |
| - |
95 |
| - {% if currLevel < minHeader or currLevel > maxHeader %} |
96 |
| - {% continue %} |
97 |
| - {% endif %} |
98 |
| - |
99 |
| - {% assign _workspace = node | split: '</h' %} |
100 |
| - |
101 |
| - {% assign _idWorkspace = _workspace[0] | split: 'id="' %} |
102 |
| - {% assign _idWorkspace = _idWorkspace[1] | split: '"' %} |
103 |
| - {% assign htmlID = _idWorkspace[0] %} |
104 |
| - |
105 |
| - {% assign _classWorkspace = _workspace[0] | split: 'class="' %} |
106 |
| - {% assign _classWorkspace = _classWorkspace[1] | split: '"' %} |
107 |
| - {% assign htmlClass = _classWorkspace[0] %} |
108 |
| - |
109 |
| - {% if htmlClass contains "no_toc" %} |
110 |
| - {% continue %} |
111 |
| - {% endif %} |
112 |
| - |
113 |
| - {% if firstHeader %} |
114 |
| - {% assign minHeader = currLevel %} |
115 |
| - {% endif %} |
116 |
| - |
117 |
| - {% capture _hAttrToStrip %}{{ _workspace[0] | split: '>' | first }}>{% endcapture %} |
118 |
| - {% assign header = _workspace[0] | replace: _hAttrToStrip, '' %} |
119 |
| - |
120 |
| - {% if include.item_class and include.item_class != blank %} |
121 |
| - {% capture listItemClass %} class="{{ include.item_class | replace: '%level%', currLevel | split: '.' | join: ' ' }}"{% endcapture %} |
122 |
| - {% endif %} |
123 |
| - |
124 |
| - {% if include.submenu_class and include.submenu_class != blank %} |
125 |
| - {% assign subMenuLevel = currLevel | minus: 1 %} |
126 |
| - {% capture subMenuClass %} class="{{ include.submenu_class | replace: '%level%', subMenuLevel | split: '.' | join: ' ' }}"{% endcapture %} |
127 |
| - {% endif %} |
128 |
| - |
129 |
| - {% capture anchorBody %}{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}{% endcapture %} |
130 |
| - |
131 |
| - {% if htmlID %} |
132 |
| - {% capture anchorAttributes %} href="{% if baseURL %}{{ baseURL }}{% endif %}#{{ htmlID }}"{% endcapture %} |
133 |
| - |
134 |
| - {% if include.anchor_class %} |
135 |
| - {% capture anchorAttributes %}{{ anchorAttributes }} class="{{ include.anchor_class | split: '.' | join: ' ' }}"{% endcapture %} |
136 |
| - {% endif %} |
137 |
| - |
138 |
| - {% capture listItem %}<a{{ anchorAttributes }}>{{ anchorBody }}</a>{% endcapture %} |
139 |
| - {% elsif skipNoIDs == true %} |
140 |
| - {% continue %} |
141 |
| - {% else %} |
142 |
| - {% capture listItem %}{{ anchorBody }}{% endcapture %} |
143 |
| - {% endif %} |
144 |
| - |
145 |
| - {% if currLevel > lastLevel %} |
146 |
| - {% capture jekyll_toc %}{{ jekyll_toc }}<{{ listModifier }}{{ subMenuClass }}>{% endcapture %} |
147 |
| - {% elsif currLevel < lastLevel %} |
148 |
| - {% assign repeatCount = lastLevel | minus: currLevel %} |
149 |
| - |
150 |
| - {% for i in (1..repeatCount) %} |
151 |
| - {% capture jekyll_toc %}{{ jekyll_toc }}</li></{{ listModifier }}>{% endcapture %} |
152 |
| - {% endfor %} |
| 126 | + {% if currLevel > lastLevel %} |
| 127 | + {% capture jekyll_toc %}{{ jekyll_toc }}<{{ listModifier }}{{ subMenuClass }}>{% endcapture %} |
| 128 | + {% elsif currLevel < lastLevel %} {% assign repeatCount=lastLevel | minus: currLevel %} {% for i in |
| 129 | + (1..repeatCount) %} {% capture jekyll_toc %}{{ jekyll_toc }}</li> |
| 130 | + </{{ listModifier }}>{% endcapture %} |
| 131 | + {% endfor %} |
153 | 132 |
|
154 |
| - {% capture jekyll_toc %}{{ jekyll_toc }}</li>{% endcapture %} |
155 |
| - {% else %} |
156 |
| - {% capture jekyll_toc %}{{ jekyll_toc }}</li>{% endcapture %} |
157 |
| - {% endif %} |
| 133 | + {% capture jekyll_toc %}{{ jekyll_toc }}</li>{% endcapture %} |
| 134 | + {% else %} |
| 135 | + {% capture jekyll_toc %}{{ jekyll_toc }}</li>{% endcapture %} |
| 136 | + {% endif %} |
158 | 137 |
|
159 |
| - {% capture jekyll_toc %}{{ jekyll_toc }}<li{{ listItemClass }}>{{ listItem }}{% endcapture %} |
| 138 | + {% capture jekyll_toc %}{{ jekyll_toc }}<li{{ listItemClass }}>{{ listItem }}{% endcapture %} |
160 | 139 |
|
161 | 140 | {% assign lastLevel = currLevel %}
|
162 | 141 | {% assign firstHeader = false %}
|
163 |
| - {% endfor %} |
| 142 | + {% endfor %} |
164 | 143 |
|
165 |
| - {% assign repeatCount = minHeader | minus: 1 %} |
166 |
| - {% assign repeatCount = lastLevel | minus: repeatCount %} |
167 |
| - {% for i in (1..repeatCount) %} |
168 |
| - {% capture jekyll_toc %}{{ jekyll_toc }}</li></{{ listModifier }}>{% endcapture %} |
169 |
| - {% endfor %} |
| 144 | + {% assign repeatCount = minHeader | minus: 1 %} |
| 145 | + {% assign repeatCount = lastLevel | minus: repeatCount %} |
| 146 | + {% for i in (1..repeatCount) %} |
| 147 | + {% capture jekyll_toc %}{{ jekyll_toc }}</li> |
| 148 | + </{{ listModifier }}>{% endcapture %} |
| 149 | + {% endfor %} |
170 | 150 |
|
171 |
| - {% if jekyll_toc != '' %} |
| 151 | + {% if jekyll_toc != '' %} |
172 | 152 | {% assign rootAttributes = '' %}
|
173 | 153 | {% if include.class and include.class != blank %}
|
174 |
| - {% capture rootAttributes %} class="{{ include.class | split: '.' | join: ' ' }}"{% endcapture %} |
| 154 | + {% capture rootAttributes %} class="{{ include.class | split: '.' | join: ' ' }}"{% endcapture %} |
175 | 155 | {% endif %}
|
176 | 156 |
|
177 | 157 | {% if include.id and include.id != blank %}
|
178 |
| - {% capture rootAttributes %}{{ rootAttributes }} id="{{ include.id }}"{% endcapture %} |
| 158 | + {% capture rootAttributes %}{{ rootAttributes }} id="{{ include.id }}"{% endcapture %} |
179 | 159 | {% endif %}
|
180 | 160 |
|
181 | 161 | {% if rootAttributes %}
|
182 |
| - {% assign nodes = jekyll_toc | split: '>' %} |
183 |
| - {% capture jekyll_toc %}<{{ listModifier }}{{ rootAttributes }}>{{ nodes | shift | join: '>' }}>{% endcapture %} |
184 |
| - {% endif %} |
185 |
| - {% endif %} |
186 |
| -{% endcapture %}{% assign tocWorkspace = '' %}{{ deprecation_warnings }}{{ jekyll_toc -}} |
| 162 | + {% assign nodes = jekyll_toc | split: '>' %} |
| 163 | + {% capture jekyll_toc %}<{{ listModifier }}{{ rootAttributes }}>{{ nodes | shift | join: '>' }}>{% endcapture %} |
| 164 | + {% endif %} |
| 165 | + {% endif %} |
| 166 | + {% endcapture %}{% assign tocWorkspace = '' %}{{ deprecation_warnings }}{{ jekyll_toc -}} |
0 commit comments