Skip to content

Commit

Permalink
Add support for new flat_toc parameter
Browse files Browse the repository at this point in the history
Closes #70
  • Loading branch information
allejo committed Nov 17, 2023
1 parent 3b4c816 commit 2ab431a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ This snippet is highly customizable. Here are the available parameters to change
| `base_url` | 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 |
| `skip_no_ids` | bool | false | skip headers that do not have an `id` attribute |
| `flat_toc` | bool | false | when set to true, the TOC will be a single level list |

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

Expand Down
1 change: 1 addition & 0 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ exclude:
- Gemfile.lock
- LICENSE.BSD3.md
- LICENSE.MIT.md
- plutils/
- README.md
- tests.py
- vendor
Expand Down
15 changes: 11 additions & 4 deletions _includes/toc.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
* base_url (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
* skip_no_ids (bool) : false - skip headers that do not have an `id` attribute
* flat_toc (bool) : false - when set to true, the TOC will be a single level list
Output:
An ordered or unordered list representing the table of contents of a markdown block. This snippet will only
Expand All @@ -69,6 +70,7 @@

{% capture jekyll_toc %}{% endcapture %}
{% assign orderedList = include.ordered | default: false %}
{% assign flatToc = include.flat_toc | default: false %}
{% assign baseURL = include.base_url | default: include.baseurl | default: '' %}
{% assign skipNoIDs = include.skip_no_ids | default: include.skipNoIDs | default: false %}
{% assign minHeader = include.h_min | default: 1 %}
Expand Down Expand Up @@ -138,9 +140,9 @@
{% capture listItem %}{{ anchorBody }}{% endcapture %}
{% endif %}

{% if currLevel > lastLevel %}
{% if currLevel > lastLevel and flatToc == false %}
{% capture jekyll_toc %}{{ jekyll_toc }}<{{ listModifier }}{{ subMenuClass }}>{% endcapture %}
{% elsif currLevel < lastLevel %}
{% elsif currLevel < lastLevel and flatToc == false %}
{% assign repeatCount = lastLevel | minus: currLevel %}

{% for i in (1..repeatCount) %}
Expand All @@ -158,8 +160,13 @@
{% assign firstHeader = false %}
{% endfor %}

{% assign repeatCount = minHeader | minus: 1 %}
{% assign repeatCount = lastLevel | minus: repeatCount %}
{% if flatToc == true %}
{% assign repeatCount = 1 %}
{% else %}
{% assign repeatCount = minHeader | minus: 1 %}
{% assign repeatCount = lastLevel | minus: repeatCount %}
{% endif %}

{% for i in (1..repeatCount) %}
{% capture jekyll_toc %}{{ jekyll_toc }}</li></{{ listModifier }}>{% endcapture %}
{% endfor %}
Expand Down
25 changes: 25 additions & 0 deletions _tests/flatToc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
# https://github.com/allejo/jekyll-toc/issues/70
---

{% capture markdown %}
# Heading 1

## Heading 2

### Heading 3

## Heading 4
{% endcapture %}
{% assign text = markdown | markdownify %}

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

<!-- /// -->

<ul>
<li><a href="#heading-1">Heading 1</a></li>
<li><a href="#heading-2">Heading 2</a></li>
<li><a href="#heading-3">Heading 3</a></li>
<li><a href="#heading-4">Heading 4</a></li>
</ul>

0 comments on commit 2ab431a

Please sign in to comment.