Skip to content

Commit

Permalink
Add option to generate an ordered list instead
Browse files Browse the repository at this point in the history
  • Loading branch information
allejo committed Jan 10, 2018
1 parent 457b682 commit 92d9645
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
_site
.sass-cache
.jekyll-metadata
Gemfile.lock
Gemfile.lock
vendor
.ruby-version
.bundle
13 changes: 9 additions & 4 deletions _includes/toc.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{% capture tocWorkspace %}
{% comment %}
Version 1.0.2
Version 1.0.3
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 @@ -16,17 +17,21 @@
* 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

Output:
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
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
{% endcomment %}

{% capture my_toc %}{% endcapture %}
{% assign orderedList = include.ordered | default: false %}
{% assign minHeader = include.h_min | default: 1 %}
{% assign maxHeader = include.h_max | default: 6 %}
{% assign nodes = include.html | split: '<h' %}
{% assign firstHeader = true %}

{% capture listModifier %}{% if orderedList %}1.{% else %}-{% endif %}{% endcapture %}

{% for node in nodes %}
{% if node == "" %}
{% continue %}
Expand Down Expand Up @@ -55,11 +60,11 @@

{% assign space = '' %}
{% for i in (1..indentAmount) %}
{% assign space = space | prepend: ' ' %}
{% assign space = space | prepend: ' ' %}
{% endfor %}

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

{% endfor %}

Expand Down
40 changes: 40 additions & 0 deletions _tests/tocWithOrderedList.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
# Given `ordered` is set to true, an ordered list should be output instead of an unordered list
---

{% capture markdown %}
# Heading 1

## Heading 2.1

### Heading 3

#### Heading 4

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

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

<!-- /// -->

<ol>
<li>
<a href="#heading-1">Heading 1</a>
<ol>
<li>
<a href="#heading-21">Heading 2.1</a>
<ol>
<li>
<a href="#heading-3">Heading 3</a>
<ol>
<li><a href="#heading-4">Heading 4</a></li>
</ol>
</li>
</ol>
</li>
<li><a href="#heading-22">Heading 2.2</a></li>
</ol>
</li>
</ol>

0 comments on commit 92d9645

Please sign in to comment.