kvz
kvz

Reputation: 5885

Automatic numbering of generated ToC in Jekyll

I'm using Kramdown's ToC generation in Jekyll and am wondering I can make it somehow support automatic numbering, so that this markdown:

## Header A

### Subheader

## Header B

Gets turned into this HTML:

<h2>1 Header A</h2>
<h3>1.1 Subheader</h3>
<h2>2 Header B</h2>

Apparently this could be done in CSS or JavaScript, but I'm looking for a Markdown->HTML only solution.

Upvotes: 1

Views: 1359

Answers (3)

JohnShape
JohnShape

Reputation: 101

You can use https://github.com/A4Vision/enumerate-markdown

pip install enumerate-markdown
markdown-enum filename.md <minimal level> filenameoutput.md

Minimal level can be 1,2,3 etc depending on which header level you like to assign #1 to.

The github mentions no minimal level option as required, but in my case it only worked providing a level.

Example - input

# header 1
text
## header 2
text
# header 3
text

Output

# 1.  header 1
text
## 1.1  header 2
text
# 2.  header 3
text

As stated in Are numbered headings in Markdown / Rdiscount possible?

As headers are now numbered, they will be numbered in TOC and in your headers.

Upvotes: 0

zesaver
zesaver

Reputation: 578

Pay attention to this part in kramdown documentation:

All attributes applied to the original list will also be applied to the generated TOC list and it will get an ID of markdown-toc if no ID was set.

So, simply replace

- This list will contain the toc (it doesn't matter what you write here)
{:toc}

with

1. This list will contain the toc (it doesn't matter what you write here)
{:toc}

to get numbered ToC instead of unnumbered.

Upvotes: 2

David Jacquel
David Jacquel

Reputation: 52809

As seen in kramdown config no way to get this natively. The plugin way can solve this issue.

Upvotes: 0

Related Questions