From af064f9332ff6a0cfdc1043dd325c73bb5fb031d Mon Sep 17 00:00:00 2001 From: Vladimir Jimenez Date: Wed, 29 Jul 2020 17:00:01 -0700 Subject: [PATCH 1/6] Add preliminary support for `submenu_class` arg --- _includes/toc.html | 27 +++++++++++++++++++++++++ _tests/subMenuClass.md | 46 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 _tests/subMenuClass.md diff --git a/_includes/toc.html b/_includes/toc.html index 628b221..5d5aa95 100644 --- a/_includes/toc.html +++ b/_includes/toc.html @@ -59,6 +59,7 @@ {% assign maxHeader = include.h_max | default: 6 %} {% assign nodes = include.html | split: ' indentAmount %} + {% assign submenu_space = '' %} + {% for i in (0..indentAmount) %} + {% assign submenu_space = submenu_space | prepend: ' ' %} + {% endfor %} + + {% capture my_toc %}{{ my_toc }} +{{ submenu_space }}{:.{{ include.submenu_class | replace: '%level%', previous_level }}}{% endcapture %} + {% endif %} + {% capture my_toc %}{{ my_toc }} {{ space }}{{ listModifier }} {{ listItemClass }} {{ list_item }}{% if include.anchor_class %}{:.{{ include.anchor_class }}}{% endif %}{% endcapture %} + + {% assign previous_level = indentAmount %} {% endfor %} {% if include.class and include.class != blank %} @@ -129,4 +142,18 @@ {% capture my_toc %}{: #{{ include.id }}} {{ my_toc | lstrip }}{% endcapture %} {% endif %} + + {% if include.submenu_class != blank %} + {% for i in (1..previous_level) %} + {% assign lvl = previous_level | plus: 1 | minus: i %} + {% assign closing_space = '' %} + + {% for i in (1..lvl) %} + {% assign closing_space = closing_space | prepend: ' ' %} + {% endfor %} + + {% capture my_toc %}{{ my_toc }} +{{ closing_space }}{:.{{ include.submenu_class | replace: '%level%', lvl }}}{% endcapture %} + {% endfor %} + {% endif %} {% endcapture %}{% assign tocWorkspace = '' %}{{ my_toc | markdownify | strip }} diff --git a/_tests/subMenuClass.md b/_tests/subMenuClass.md new file mode 100644 index 0000000..628e822 --- /dev/null +++ b/_tests/subMenuClass.md @@ -0,0 +1,46 @@ +--- +# https://github.com/allejo/jekyll-toc/issues/40 +--- + +{% capture markdown %} +# Heading 1 + +## Heading 2.1 + +### Heading 3.1 + +### Heading 3.2 + +## Heading 2.2 + +### Heading 3.3 + +### Heading 3.4 +{% endcapture %} +{% assign text = markdown | markdownify %} + +{% include toc.html html=text submenu_class="submenu-%level%" %} + + + + From be14a7bdb1bb0bce51f7a2e591808cd5af16a7c7 Mon Sep 17 00:00:00 2001 From: Vladimir Jimenez Date: Wed, 29 Jul 2020 20:13:03 -0700 Subject: [PATCH 2/6] Update documentation in snippet + README --- README.md | 27 ++++++++++++++------------- _includes/toc.html | 21 +++++++++++---------- 2 files changed, 25 insertions(+), 23 deletions(-) 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 5d5aa95..ee596c1 100644 --- a/_includes/toc.html +++ b/_includes/toc.html @@ -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 From 3ef2d623c4bb70e1ee0db9258dc367f078c74eb4 Mon Sep 17 00:00:00 2001 From: Vladimir Jimenez Date: Wed, 29 Jul 2020 20:13:24 -0700 Subject: [PATCH 3/6] Add comments in code + rename variable names --- _includes/toc.html | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/_includes/toc.html b/_includes/toc.html index ee596c1..8151600 100644 --- a/_includes/toc.html +++ b/_includes/toc.html @@ -60,7 +60,7 @@ {% assign maxHeader = include.h_max | default: 6 %} {% assign nodes = include.html | split: ' indentAmount %} - {% assign submenu_space = '' %} - {% for i in (0..indentAmount) %} - {% assign submenu_space = submenu_space | prepend: ' ' %} - {% endfor %} + + {% if include.submenu_class and previousLevel > indentAmount %} + + {% assign submenuIndentation = space | prepend: ' ' %} {% capture my_toc %}{{ my_toc }} -{{ submenu_space }}{:.{{ include.submenu_class | replace: '%level%', previous_level }}}{% endcapture %} +{{ 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 previous_level = indentAmount %} + {% assign previousLevel = indentAmount %} {% endfor %} {% if include.class and include.class != blank %} @@ -144,17 +149,22 @@ {{ my_toc | lstrip }}{% endcapture %} {% endif %} + {% if include.submenu_class != blank %} - {% for i in (1..previous_level) %} - {% assign lvl = previous_level | plus: 1 | minus: i %} - {% assign closing_space = '' %} + + {% for i in (1..previousLevel) %} + {% assign lvl = previousLevel | plus: 1 | minus: i %} + {% assign closingSpace = '' %} {% for i in (1..lvl) %} - {% assign closing_space = closing_space | prepend: ' ' %} + {% assign closingSpace = closingSpace | prepend: ' ' %} {% endfor %} {% capture my_toc %}{{ my_toc }} -{{ closing_space }}{:.{{ include.submenu_class | replace: '%level%', lvl }}}{% endcapture %} +{{ closingSpace }}{:.{{ include.submenu_class | replace: '%level%', lvl }}}{% endcapture %} {% endfor %} {% endif %} {% endcapture %}{% assign tocWorkspace = '' %}{{ my_toc | markdownify | strip }} From f57e156e9dae1402a64a592f2786a34357f99696 Mon Sep 17 00:00:00 2001 From: Vladimir Jimenez Date: Wed, 29 Jul 2020 20:20:16 -0700 Subject: [PATCH 4/6] Add unit test for headings without IDs + submenu cls --- _tests/subMenuClassSkipNoIDs.md | 42 +++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 _tests/subMenuClassSkipNoIDs.md diff --git a/_tests/subMenuClassSkipNoIDs.md b/_tests/subMenuClassSkipNoIDs.md new file mode 100644 index 0000000..7125ca4 --- /dev/null +++ b/_tests/subMenuClassSkipNoIDs.md @@ -0,0 +1,42 @@ +--- +# https://github.com/allejo/jekyll-toc/issues/40 +--- + +{% capture markdown %} +# Heading 1 + +## Heading 2.1 + +

Heading 3.1

+ +

Heading 3.2

+ +## Heading 2.2 + +### Heading 3.3 + +### Heading 3.4 +{% endcapture %} +{% assign text = markdown | markdownify %} + +{% include toc.html html=text skipNoIDs=true submenu_class="submenu-%level%" %} + + + + From fe1b6f6afd5e37c365ae88cf16c465c2141681f8 Mon Sep 17 00:00:00 2001 From: Vladimir Jimenez Date: Wed, 29 Jul 2020 20:20:30 -0700 Subject: [PATCH 5/6] Version bump to 1.0.14 --- _includes/toc.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_includes/toc.html b/_includes/toc.html index 8151600..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 From ac5659591e02d6f3040af2f1112f49970fbdbb9b Mon Sep 17 00:00:00 2001 From: Vladimir Jimenez Date: Wed, 29 Jul 2020 20:28:09 -0700 Subject: [PATCH 6/6] Remove code tag in sanitize unit test --- _tests/sanitizeDisabled.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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