Search code examples
jekyllliquidkramdown

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.


Solution

  • 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.