|
24 | 24 | OTHER DEALINGS IN THE SOFTWARE. |
25 | 25 | {% endcomment %} |
26 | 26 | {% comment %} |
27 | | - Version 1.0.13 |
| 27 | + Version 1.0.14 |
28 | 28 | https://github.com/allejo/jekyll-toc |
29 | 29 |
|
30 | 30 | "...like all things liquid - where there's a will, and ~36 hours to spare, there's usually a/some way" ~jaybe |
|
36 | 36 | * html (string) - the HTML of compiled markdown generated by kramdown in Jekyll |
37 | 37 |
|
38 | 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 | | - * 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 |
49 | 50 |
|
50 | 51 | Output: |
51 | 52 | An ordered or unordered list representing the table of contents of a markdown block. This snippet will only |
|
59 | 60 | {% assign maxHeader = include.h_max | default: 6 %} |
60 | 61 | {% assign nodes = include.html | split: '<h' %} |
61 | 62 | {% assign firstHeader = true %} |
| 63 | + {% assign previousLevel = 0 %} |
62 | 64 |
|
63 | 65 | {% capture listModifier %}{% if orderedList %}1.{% else %}-{% endif %}{% endcapture %} |
64 | 66 |
|
|
116 | 118 | {% capture list_item %}{{ anchor_body }}{% endcapture %} |
117 | 119 | {% endif %} |
118 | 120 |
|
| 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 | + |
119 | 136 | {% capture my_toc %}{{ my_toc }} |
120 | 137 | {{ space }}{{ listModifier }} {{ listItemClass }} {{ list_item }}{% if include.anchor_class %}{:.{{ include.anchor_class }}}{% endif %}{% endcapture %} |
| 138 | + |
| 139 | + {% assign previousLevel = indentAmount %} |
121 | 140 | {% endfor %} |
122 | 141 |
|
123 | 142 | {% if include.class and include.class != blank %} |
|
129 | 148 | {% capture my_toc %}{: #{{ include.id }}} |
130 | 149 | {{ my_toc | lstrip }}{% endcapture %} |
131 | 150 | {% 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 %} |
132 | 170 | {% endcapture %}{% assign tocWorkspace = '' %}{{ my_toc | markdownify | strip }} |
0 commit comments