Skip to content

Commit 92d9645

Browse files
committed
Add option to generate an ordered list instead
1 parent 457b682 commit 92d9645

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
_site
22
.sass-cache
33
.jekyll-metadata
4-
Gemfile.lock
4+
Gemfile.lock
5+
vendor
6+
.ruby-version
7+
.bundle

_includes/toc.html

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{% capture tocWorkspace %}
22
{% comment %}
3-
Version 1.0.2
3+
Version 1.0.3
4+
https://github.com/allejo/jekyll-toc
45

56
"...like all things liquid - where there's a will, and ~36 hours to spare, there's usually a/some way" ~jaybe
67

@@ -16,17 +17,21 @@
1617
* id (string) : '' - an ID to assigned to the TOC
1718
* h_min (int) : 1 - the minimum TOC header level to use; any header lower than this value will be ignored
1819
* h_max (int) : 6 - the maximum TOC header level to use; any header greater than this value will be ignored
20+
* ordered (bool) : false - when set to true, an ordered list will be outputted instead of an unordered list
1921

2022
Output:
21-
An unordered list representing the table of contents of a markdown block. This snippet will only generate the table of contents and will NOT output the markdown given to it
23+
An ordered or unordered list representing the table of contents of a markdown block. This snippet will only generate the table of contents and will NOT output the markdown given to it
2224
{% endcomment %}
2325

2426
{% capture my_toc %}{% endcapture %}
27+
{% assign orderedList = include.ordered | default: false %}
2528
{% assign minHeader = include.h_min | default: 1 %}
2629
{% assign maxHeader = include.h_max | default: 6 %}
2730
{% assign nodes = include.html | split: '<h' %}
2831
{% assign firstHeader = true %}
2932

33+
{% capture listModifier %}{% if orderedList %}1.{% else %}-{% endif %}{% endcapture %}
34+
3035
{% for node in nodes %}
3136
{% if node == "" %}
3237
{% continue %}
@@ -55,11 +60,11 @@
5560

5661
{% assign space = '' %}
5762
{% for i in (1..indentAmount) %}
58-
{% assign space = space | prepend: ' ' %}
63+
{% assign space = space | prepend: ' ' %}
5964
{% endfor %}
6065

6166
{% capture my_toc %}{{ my_toc }}
62-
{{ space }}- [{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}](#{{ html_id }}){% endcapture %}
67+
{{ space }}{{ listModifier }} [{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}](#{{ html_id }}){% endcapture %}
6368

6469
{% endfor %}
6570

_tests/tocWithOrderedList.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
# Given `ordered` is set to true, an ordered list should be output instead of an unordered list
3+
---
4+
5+
{% capture markdown %}
6+
# Heading 1
7+
8+
## Heading 2.1
9+
10+
### Heading 3
11+
12+
#### Heading 4
13+
14+
## Heading 2.2
15+
{% endcapture %}
16+
{% assign text = markdown | markdownify %}
17+
18+
{% include toc.html html=text ordered=true %}
19+
20+
<!-- /// -->
21+
22+
<ol>
23+
<li>
24+
<a href="#heading-1">Heading 1</a>
25+
<ol>
26+
<li>
27+
<a href="#heading-21">Heading 2.1</a>
28+
<ol>
29+
<li>
30+
<a href="#heading-3">Heading 3</a>
31+
<ol>
32+
<li><a href="#heading-4">Heading 4</a></li>
33+
</ol>
34+
</li>
35+
</ol>
36+
</li>
37+
<li><a href="#heading-22">Heading 2.2</a></li>
38+
</ol>
39+
</li>
40+
</ol>

0 commit comments

Comments
 (0)