diff --git a/README.md b/README.md index 66c009c..1825f5f 100644 --- a/README.md +++ b/README.md @@ -47,19 +47,20 @@ Alright, so how do you use it? This snippet is highly customizable. Here are the available parameters to change the behavior of the snippet. -| Parameter | Type | Default | Description | -| -------------- | :----: | :-----: | ----------- | -| `html` | string | * | the HTML of compiled markdown generated by kramdown in Jekyll | -| `sanitize` | bool | false | when set to true, the headers will be stripped of any HTML in the TOC | -| `class` | string | '' | a CSS class assigned to the TOC; concat multiple classes with '.' | -| `id` | string | '' | an ID to be assigned to the TOC | -| `h_min` | int | 1 | the minimum TOC header level to use; any heading lower than this value will be ignored | -| `h_max` | int | 6 | the maximum TOC header level to use; any heading greater than this value will be ignored | -| `ordered` | bool | false | when set to true, an ordered list will be outputted instead of an unordered list | -| `item_class` | string | '' | add custom class for each list item; has support for `%level%` placeholder, which is the current heading level | -| `baseurl` | string | '' | add a base url to the TOC links for when your TOC is on another page than the actual content | -| `anchor_class` | string | '' | add custom class(es) for each anchor element | -| `skipNoIDs` | bool | false | skip headers that do not have an `id` attribute | +| Parameter | Type | Default | Description | +| --------------- | :----: | :-----: | ----------- | +| `html` | string | * | the HTML of compiled markdown generated by kramdown in Jekyll | +| `sanitize` | bool | false | when set to true, the headers will be stripped of any HTML in the TOC | +| `class` | string | '' | a CSS class assigned to the TOC; concat multiple classes with '.' | +| `id` | string | '' | an ID to be assigned to the TOC | +| `h_min` | int | 1 | the minimum TOC header level to use; any heading lower than this value will be ignored | +| `h_max` | int | 6 | the maximum TOC header level to use; any heading greater than this value will be ignored | +| `ordered` | bool | false | when set to true, an ordered list will be outputted instead of an unordered list | +| `item_class` | string | '' | add custom class for each list item; has support for `%level%` placeholder, which is the current heading level | +| `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 | +| `baseurl` | string | '' | add a base url to the TOC links for when your TOC is on another page than the actual content | +| `anchor_class` | string | '' | add custom class(es) for each anchor element | +| `skipNoIDs` | bool | false | skip headers that do not have an `id` attribute | * This is a required parameter diff --git a/_includes/toc.html b/_includes/toc.html index 628b221..e6c1cb4 100644 --- a/_includes/toc.html +++ b/_includes/toc.html @@ -24,7 +24,7 @@ OTHER DEALINGS IN THE SOFTWARE. {% endcomment %} {% comment %} - Version 1.0.13 + Version 1.0.14 https://github.com/allejo/jekyll-toc "...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 @@ * html (string) - the HTML of compiled markdown generated by kramdown in Jekyll Optional Parameters: - * sanitize (bool) : false - when set to true, the headers will be stripped of any HTML in the TOC - * class (string) : '' - a CSS class assigned to the TOC - * id (string) : '' - an ID to assigned to the TOC - * h_min (int) : 1 - the minimum TOC header level to use; any header lower than this value will be ignored - * h_max (int) : 6 - the maximum TOC header level to use; any header greater than this value will be ignored - * ordered (bool) : false - when set to true, an ordered list will be outputted instead of an unordered list - * item_class (string) : '' - add custom class(es) for each list item; has support for '%level%' placeholder, which is the current heading level - * baseurl (string) : '' - add a base url to the TOC links for when your TOC is on another page than the actual content - * anchor_class (string) : '' - add custom class(es) for each anchor element - * skipNoIDs (bool) : false - skip headers that do not have an `id` attribute + * sanitize (bool) : false - when set to true, the headers will be stripped of any HTML in the TOC + * class (string) : '' - a CSS class assigned to the TOC + * id (string) : '' - an ID to assigned to the TOC + * h_min (int) : 1 - the minimum TOC header level to use; any header lower than this value will be ignored + * h_max (int) : 6 - the maximum TOC header level to use; any header greater than this value will be ignored + * ordered (bool) : false - when set to true, an ordered list will be outputted instead of an unordered list + * item_class (string) : '' - add custom class(es) for each list item; has support for '%level%' placeholder, which is the current heading level + * 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 + * baseurl (string) : '' - add a base url to the TOC links for when your TOC is on another page than the actual content + * anchor_class (string) : '' - add custom class(es) for each anchor element + * skipNoIDs (bool) : false - skip headers that do not have an `id` attribute Output: An ordered or unordered list representing the table of contents of a markdown block. This snippet will only @@ -59,6 +60,7 @@ {% assign maxHeader = include.h_max | default: 6 %} {% assign nodes = include.html | split: ' + {% if include.submenu_class and previousLevel > indentAmount %} + + {% assign submenuIndentation = space | prepend: ' ' %} + + {% capture my_toc %}{{ my_toc }} +{{ submenuIndentation }}{:.{{ include.submenu_class | replace: '%level%', previousLevel }}}{% endcapture %} + {% endif %} + {% capture my_toc %}{{ my_toc }} {{ space }}{{ listModifier }} {{ listItemClass }} {{ list_item }}{% if include.anchor_class %}{:.{{ include.anchor_class }}}{% endif %}{% endcapture %} + + {% assign previousLevel = indentAmount %} {% endfor %} {% if include.class and include.class != blank %} @@ -129,4 +148,23 @@ {% capture my_toc %}{: #{{ include.id }}} {{ my_toc | lstrip }}{% endcapture %} {% endif %} + + + {% if include.submenu_class != blank %} + + {% for i in (1..previousLevel) %} + {% assign lvl = previousLevel | plus: 1 | minus: i %} + {% assign closingSpace = '' %} + + {% for i in (1..lvl) %} + {% assign closingSpace = closingSpace | prepend: ' ' %} + {% endfor %} + + {% capture my_toc %}{{ my_toc }} +{{ closingSpace }}{:.{{ include.submenu_class | replace: '%level%', lvl }}}{% endcapture %} + {% endfor %} + {% endif %} {% endcapture %}{% assign tocWorkspace = '' %}{{ my_toc | markdownify | strip }} diff --git a/_tests/sanitizeDisabled.md b/_tests/sanitizeDisabled.md index 8ab3114..fca3773 100644 --- a/_tests/sanitizeDisabled.md +++ b/_tests/sanitizeDisabled.md @@ -5,7 +5,7 @@ {% capture markdown %} # Heading 1 -## `Heading` 2.1 +## Heading 2.1 ### Heading **3** @@ -24,7 +24,7 @@ Heading 1