From 3e217c68dc12b76fb604f4c9a3016204384eecbc Mon Sep 17 00:00:00 2001 From: Vladimir Jimenez Date: Fri, 20 Mar 2020 18:13:02 -0700 Subject: [PATCH 1/2] Don't render empty anchors for headings without IDs --- _includes/toc.html | 14 +++++++-- _tests/noAnchorOnHeadingsWithNoIDs.md | 44 +++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 _tests/noAnchorOnHeadingsWithNoIDs.md diff --git a/_includes/toc.html b/_includes/toc.html index 56ac8e4..b87e437 100644 --- a/_includes/toc.html +++ b/_includes/toc.html @@ -1,6 +1,6 @@ {% capture tocWorkspace %} {% comment %} - Version 1.0.10 + Version 1.0.11 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 @@ -79,9 +79,17 @@ {% capture listItemClass %}{:.{{ include.item_class | replace: '%level%', headerLevel }}}{% endcapture %} {% endif %} - {% capture heading_body %}{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}{% endcapture %} + {% capture anchor_body %}{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}{% endcapture %} + {% capture anchor_body %}{{ anchor_body | replace: "|", "\|" }}{% endcapture %} + + {% if html_id %} + {% capture list_item %}[{{ anchor_body }}]({% if include.baseurl %}{{ include.baseurl }}{% endif %}#{{ html_id }}){% endcapture %} + {% else %} + {% capture list_item %}{{ anchor_body }}{% endcapture %} + {% endif %} + {% capture my_toc %}{{ my_toc }} -{{ space }}{{ listModifier }} {{ listItemClass }} [{{ heading_body | replace: "|", "\|" }}]({% if include.baseurl %}{{ include.baseurl }}{% endif %}#{{ html_id }}){% if include.anchor_class %}{:.{{ include.anchor_class }}}{% endif %}{% endcapture %} +{{ space }}{{ listModifier }} {{ listItemClass }} {{ list_item }}{% if include.anchor_class %}{:.{{ include.anchor_class }}}{% endif %}{% endcapture %} {% endfor %} {% if include.class and include.class != blank %} diff --git a/_tests/noAnchorOnHeadingsWithNoIDs.md b/_tests/noAnchorOnHeadingsWithNoIDs.md new file mode 100644 index 0000000..913f97f --- /dev/null +++ b/_tests/noAnchorOnHeadingsWithNoIDs.md @@ -0,0 +1,44 @@ +--- +# https://github.com/allejo/jekyll-toc/issues/32 +--- + +{% capture markdown %} +## Heading 2.1 + +

Heading 2.2 (no link)

+ +### Heading 2.2.1 + +

Heading 2.3

+ +## Heading 2.4 + +

Heading 2.4.1

+{% endcapture %} +{% assign text = markdown | markdownify %} + +{% include toc.html html=text %} + + + + From 0c894b3c546ea83f798382853f74263b1f45d750 Mon Sep 17 00:00:00 2001 From: Vladimir Jimenez Date: Tue, 31 Mar 2020 00:37:05 -0700 Subject: [PATCH 2/2] Add new 'skipNoIDs' parameter --- README.md | 1 + _includes/toc.html | 8 ++++++ _tests/skipHeadingsWithNoIDs.md | 49 +++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 _tests/skipHeadingsWithNoIDs.md diff --git a/README.md b/README.md index 53bb633..0d717a0 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,7 @@ This snippet is highly customizable. Here are the available parameters to change | `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 | * This is a required parameter diff --git a/_includes/toc.html b/_includes/toc.html index b87e437..6971739 100644 --- a/_includes/toc.html +++ b/_includes/toc.html @@ -21,6 +21,7 @@ * 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 Output: An ordered or unordered list representing the table of contents of a markdown block. This snippet will only @@ -29,6 +30,7 @@ {% capture my_toc %}{% endcapture %} {% assign orderedList = include.ordered | default: false %} + {% assign skipNoIDs = include.skipNoIDs | default: false %} {% assign minHeader = include.h_min | default: 1 %} {% assign maxHeader = include.h_max | default: 6 %} {% assign nodes = include.html | split: ' maxHeader %} diff --git a/_tests/skipHeadingsWithNoIDs.md b/_tests/skipHeadingsWithNoIDs.md new file mode 100644 index 0000000..7084c80 --- /dev/null +++ b/_tests/skipHeadingsWithNoIDs.md @@ -0,0 +1,49 @@ +--- +# https://github.com/allejo/jekyll-toc/issues/32 +--- + +{% capture markdown %} +## Sample Usage + +
+

My Awesome Example Page

+

With an awesome subtitle

+
+ +### Known Problems + +Lots! + +### Resources + +#### Paid + +#### Free +{% endcapture %} +{% assign text = markdown | markdownify %} + +{% include toc.html html=text skipNoIDs=true %} + + + +