Generating a Table of Contents from Markdown with Pandoc

How Pandoc builds a table of contents from Markdown headings, what the --toc option changes, and where anchors, links and accessibility can break.

Pandoc can build a table of contents from the headings in a Markdown file and insert it into the converted document. This review covers what that generation step changes, when it suits a workflow, and where the result can stop working after it is copied, published or opened in another tool. The description reflects Pandoc's documented behavior. No hands-on test results were supplied for this review, so behavior in specific renderers, editors or assistive technologies is flagged as unverified.

Is Pandoc's table of contents suitable for your use?

Pandoc fits well when Markdown is the single source and the deliverable is a generated format such as HTML, PDF, Word or EPUB. Its main benefit is that the contents list is rebuilt from the headings on each conversion, so it cannot drift out of sync with the document as long as you regenerate it.

The decisive tradeoff is link portability. The table of contents points to heading identifiers that Pandoc generates under its own rules. Those identifiers are reliable inside Pandoc's output, but they may not match the anchors another platform creates when it renders the same Markdown. If you plan to write the table of contents back into a Markdown file and publish it on a code hosting site or wiki, check whether the links resolve there before relying on them.

A plain-text outline, or no generated table of contents at all, is preferable when the document is short, when the destination already builds its own navigation from headings, when the file will be edited by hand by people who will not rerun Pandoc, or when the text will be pasted into email, chat or forms that drop links.

What the transformation actually changes

This is a structural formatting step, not a character-level change. Pandoc does not reverse, mirror or restyle any text, and it does not substitute Unicode characters. It reads the heading structure, assigns an identifier to each heading, and emits a nested list of links to those identifiers in the output document.

Your source Markdown is not modified. The table of contents exists only in the output file. In HTML it is real markup stored in the file, typically a nav element containing nested lists of links. In LaTeX and PDF output, Pandoc relies on the LaTeX table of contents mechanism. In Word output it inserts a table of contents field that Word itself populates, which is why Word may ask to update fields when the document is opened. EPUB readers also use their own navigation document, so the visible in-book contents page and the reader's navigation menu are separate things.

The core options are:

  • --toc (or --table-of-contents): include a generated table of contents.
  • --toc-depth=N: limit which heading levels appear; the documented default is 3.
  • -s (--standalone): produce a complete document; the table of contents is placed through the output template, so it generally requires standalone output.
  • --number-sections: number headings, which is reflected in the table of contents.
  • --toc-title, or the toc-title metadata variable: set a heading for the contents list in formats whose templates support it.
pandoc input.md -s --toc --toc-depth=2 -o output.html
pandoc input.md -s --toc -o output.pdf
pandoc input.md -s --toc -o output.docx
pandoc input.md -s --toc -t markdown -o output.md

Individual headings can be adjusted with attributes. A heading marked with the unnumbered class is left out of section numbering, and in recent Pandoc versions the unlisted class keeps a heading out of the table of contents. You can also assign an explicit identifier to a heading so that its anchor stays stable even if the heading text changes. Check the documentation for your installed version, since attribute support has expanded over time.

# Introduction {#intro}

# Changelog {.unnumbered .unlisted}

How heading identifiers are built

By default, Pandoc's identifier rules remove formatting, links and footnotes from the heading text, remove punctuation other than underscores, hyphens and periods, replace spaces with hyphens, convert letters to lowercase, and drop everything before the first letter. If nothing remains, the identifier becomes section. Duplicate identifiers receive numeric suffixes.

Under these rules, a heading such as "Getting Started: Step 1" should produce an identifier like getting-started-step-1. That is a worked example of the documented rules, not output captured from a test run.

Two extensions change this behavior. The gfm_auto_identifiers extension switches to rules modeled on GitHub's anchor style, which matters if a Markdown table of contents is meant to work on that platform. The ascii_identifiers extension converts identifiers to ASCII. Without it, letters from other scripts are kept, so a heading in Cyrillic produces a Cyrillic identifier. Such identifiers work as anchors in browsers but are often percent-encoded when a link is copied, which makes shared URLs long and hard to read.

Will the result stay readable and usable after copying or sharing?

Inside the generated file, the table of contents is ordinary text and links in normal reading order, so it does not disturb how the document reads from top to bottom. The usability risks come from moving or re-rendering it.

  • Copying from a rendered page: pasting into a plain-text destination keeps the heading text but loses the links, leaving a static outline.
  • Re-rendering Markdown elsewhere: a table of contents written into a Markdown file works only where the destination generates matching heading anchors. Mismatched rules lead to links that silently go nowhere.
  • Editing after generation: renaming a heading changes its automatic identifier, so any saved table of contents or external link to the old anchor breaks until you regenerate. Explicit identifiers avoid this.
  • Word documents: the contents depend on Word updating the field. If fields are not updated, the table can appear empty or stale. Other word processors may handle the field differently; this was not tested.
  • Search: a generated contents list adds no new information to a page, so it should not be expected to change how the document is indexed. No evidence was supplied on how specific search engines treat it.

For accessibility, a nested list of links inside a navigation landmark is a conventional structure that assistive technologies are designed to handle, and keeping real headings in the body remains the more important factor, since screen reader users often navigate by heading directly. No documentation or scoped test with a specific screen reader was supplied for this review, so claims about how a particular assistive technology announces Pandoc's output cannot be confirmed here. If accessibility is a requirement, test the actual output with the tools your readers use.

A plain-text equivalent is needed when the contents list will travel through channels that strip links or markup, such as email bodies, chat messages, ticket systems or form fields. In those cases, a short typed outline of section names is clearer than a pasted list of broken links.

Gaps in the available evidence

This review relies on Pandoc's documented behavior and general principles of document structure. It does not include version-specific test runs, rendering comparisons across hosting platforms, results from Word alternatives, or screen reader testing. Before adopting Pandoc's table of contents in a publishing workflow, run a conversion with your own Pandoc version and confirm that the links resolve in the place where readers will actually open the document.