Skip to content

Commit

Permalink
Merge pull request #33 from allejo/feature/no-anchor-headings
Browse files Browse the repository at this point in the history
Don't render empty anchors for headings without IDs
  • Loading branch information
allejo authored Mar 31, 2020
2 parents 6b710b7 + 0c894b3 commit d03f24a
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |

<sup>*</sup> This is a required parameter

Expand Down
22 changes: 19 additions & 3 deletions _includes/toc.html
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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: '<h' %}
Expand All @@ -41,6 +43,12 @@
{% continue %}
{% endif %}

{% if skipNoIDs == true %}
{% unless node contains "id=" %}
{% continue %}
{% endunless %}
{% endif %}

{% assign headerLevel = node | replace: '"', '' | slice: 0, 1 | times: 1 %}

{% if headerLevel < minHeader or headerLevel > maxHeader %}
Expand Down Expand Up @@ -79,9 +87,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 %}
Expand Down
44 changes: 44 additions & 0 deletions _tests/noAnchorOnHeadingsWithNoIDs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
# https://github.com/allejo/jekyll-toc/issues/32
---

{% capture markdown %}
## Heading 2.1

<h2>Heading 2.2 (no link)</h2>

### Heading 2.2.1

<h2 id="heading-23" class="no_toc">Heading 2.3</h2>

## Heading 2.4

<h3 id="super-toast">Heading 2.4.1</h3>
{% endcapture %}
{% assign text = markdown | markdownify %}

{% include toc.html html=text %}

<!-- /// -->

<ul>
<li>
<a href="#heading-21">Heading 2.1</a>
</li>
<li>
Heading 2.2 (no link)
<ul>
<li>
<a href="#heading-221">Heading 2.2.1</a>
</li>
</ul>
</li>
<li>
<a href="#heading-24">Heading 2.4</a>
<ul>
<li>
<a href="#super-toast">Heading 2.4.1</a>
</li>
</ul>
</li>
</ul>
49 changes: 49 additions & 0 deletions _tests/skipHeadingsWithNoIDs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
# https://github.com/allejo/jekyll-toc/issues/32
---

{% capture markdown %}
## Sample Usage

<div>
<h1 class="page-title">My Awesome Example Page</h1>
<h2 class="page-subtitle">With an awesome subtitle</h2>
</div>

### Known Problems

Lots!

### Resources

#### Paid

#### Free
{% endcapture %}
{% assign text = markdown | markdownify %}

{% include toc.html html=text skipNoIDs=true %}

<!-- /// -->

<ul>
<li>
<a href="#sample-usage">Sample Usage</a>
<ul>
<li>
<a href="#known-problems">Known Problems</a>
</li>
<li>
<a href="#resources">Resources</a>
<ul>
<li>
<a href="#paid">Paid</a>
</li>
<li>
<a href="#free">Free</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>

0 comments on commit d03f24a

Please sign in to comment.