Introducing toc-css

1 Introducing toc-css

Pandoc Lua Filter to add floating table of contents to HTML

Michael Cysouw cysouw@mac.com

1.1 Pandoc

Pandoc is a fabulous system to prepare text and export it to (almost) every format of your liking. Pandoc includes an option to add a table of contents, but by default this is just added at the top of the document. This Lua filter adds some better layout of the table of contents for HTML documents.

The filter adds a bit CSS and vanilla Javascript to the HTML output as created by the Pandoc defaults. The actual html is not changed in any way. If you have not added a title for the table of contents then the title “Contents” is automatically added.

Please note that I am not a very proficient CSS/Javascript programmer. I welcome suggestions and improvements! The current version was specifically made for a long book with a very complex structure, the current version can be found here as an example.

1.2 Usage

As an example, consider the following command to convert this README.md into README.html:

pandoc README.md \
    --to html \
    --output docs/README.html \
    --lua-filter toc-css.lua \
    --standalone \
    --toc \
    --number-sections \
    --metadata toc-title="Table of Contents" \
    --metadata title="Introducing toc-css"

1.3 Functionality

2 Another first level heading

The remaining headers in this readme are only added to showcase the result in the HTML file.

2.1 With subheading

2.1.1 Going deeper

2.1.2 More

2.1.3 Let’s try a completely long and superfluous title

2.2 Another subheading

2.2.1 Example 1

2.2.2 Example 2

2.2.3 Example 3

3 Next Chapter

3.1 Next Subheading

3.2 Another very long title that will not fit