1

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.

kvz
  • 5,517
  • 1
  • 42
  • 33

3 Answers3

2

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.

zesaver
  • 578
  • 4
  • 6
  • This will only apply to the ToC, not on the headers itself. Is there a way to apply auto numbering to headers as the TS is asking? – JohnShape Jun 22 '21 at 23:06
0

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

David Jacquel
  • 51,670
  • 6
  • 121
  • 147
0

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.

JohnShape
  • 101
  • 2