Generate a Table of Contents in Markdown Preview Enhanced
How Markdown Preview Enhanced builds a table of contents, what each method changes in your file, and where the result holds up after sharing.
Markdown Preview Enhanced, the preview extension most commonly used in Visual Studio Code, can build a table of contents from your document's headings. It offers more than one way to do this, and the methods differ in one important respect: some change only what you see in the preview, while others write new text into your Markdown file. That difference decides whether the table of contents still works after you copy the file elsewhere, publish it on a code hosting site, or hand it to someone using a different editor.
This review covers the extension's documented behavior. No independent test results, version numbers or release dates were available for this article, so check details against the current extension documentation before relying on them.
Is it the right approach for your document?
For long Markdown files that you mainly read, edit and export inside Markdown Preview Enhanced, the generated table of contents is a good fit. It saves you from maintaining a list of headings by hand and keeps that list in sync as headings change.
The main tradeoff is portability. A table of contents that exists only in the preview disappears when the file is opened anywhere else. One written into the file survives, but its anchor links depend on how each destination turns headings into link targets, and those rules are not identical across renderers.
A hand-written list, or no table of contents at all, is the better choice for short documents, for files whose main audience reads them on a platform that generates its own navigation, and for content that will be pasted into tools that do not support in-page links.
What each method actually changes
Generating a table of contents is a structural transformation, not a character-level one. It does not reverse, mirror or restyle any text, and it does not substitute Unicode look-alike characters. Your heading text stays exactly as written. What changes is either the rendered view, the file contents, or both, depending on the method.
| Method | What changes | Where the TOC appears |
| [TOC] marker in the source | One line of text added to the file; the list itself is built at render time | Markdown Preview Enhanced preview and its exports only |
| Create Toc command | A hidden import comment plus a real Markdown list written into the file | Any Markdown renderer, since the list is ordinary text |
| Sidebar TOC in the preview | Nothing in the file; a navigation panel is shown alongside the preview | The preview window only |
The [TOC] marker
Placing [TOC] on its own line tells the extension to render a table of contents at that spot in the preview. The file itself only contains the marker. CommonMark has no table of contents syntax, so a renderer that does not recognize the marker will display the bracketed text literally or treat it as an unresolved link.
The Create Toc command
Running the extension's Create Toc command from the command palette inserts an HTML comment that holds the settings. When the file is saved, the extension writes a nested Markdown list of links between two output markers. The extension's documentation has stated that the preview needs to be open for the list to refresh on save; confirm this for the version you use.
A typical result looks like the snippet below. The heading slugs follow the common convention of lowercasing the text and replacing spaces with hyphens.
<!-- @import "[TOC]" {cmd="toc" depthFrom=2 depthTo=3 orderedList=false} -->
<!-- code_chunk_output -->
- [Installation](#installation)
- [Usage](#usage)
- [Basic options](#basic-options)
<!-- /code_chunk_output -->
## Installation
## Usage
### Basic options
In this example, depthFrom and depthTo limit the list to second- and third-level headings, so a top-level document title would not appear. Setting orderedList to true produces a numbered list instead of bullets.
Excluding individual headings
The documentation describes adding {ignore=true} after a heading to keep it out of the generated list, for example on a changelog or license section. This attribute is extension-specific. Other renderers may show it as literal text in the heading, so it trades portability for control.
The sidebar view
The preview can also show a table of contents in a side panel. This is presentation only: nothing is written to the file, and nothing carries over when the Markdown is shared. It is useful for navigating while editing, not for producing a deliverable.
Will the table of contents keep working after sharing?
The answer depends on the method and the destination. The points below follow from how Markdown and HTML behave in general; they are not results of tests on specific platforms.
- Other Markdown renderers: a list written by Create Toc displays as normal links. The HTML comments around it are hidden wherever HTML comments are respected, which is standard browser behavior.
- Anchor links: each link points to a slug derived from a heading. Renderers differ in how they handle punctuation, non-English characters, emoji and repeated heading names, so some links can break even though the list looks correct. Test the links on the destination you actually publish to.
- The [TOC] marker: outside Markdown Preview Enhanced it produces no list. Readers on other platforms see the marker text or nothing useful.
- Exported HTML or PDF: exports made from the preview include the rendered table of contents, because the export captures what the preview shows. Whether links remain clickable in a PDF depends on the export route used.
- Plain-text pastes: pasting into email, chat or a word processor usually keeps the visible heading names but drops or breaks the in-page links, since those destinations have no matching anchors.
Editing and maintenance
Text between the output markers is regenerated on save, so manual edits to the list are overwritten. Make changes to the headings or the settings comment instead. If collaborators edit the file in a different tool, the list will not update for them, and it can drift out of date until someone saves it again in Markdown Preview Enhanced.
Accessibility
A table of contents built as a nested list of links is a familiar structure for assistive technology, because screen readers generally let users move through lists and links directly. Its usefulness depends on the underlying headings: a skipped level or a vague heading name produces an equally confusing table of contents. No testing with specific screen readers was available for this article, so behavior in a particular reader and browser combination remains unverified.
When a plain-text equivalent is the better choice
- The file will be read mainly on a platform that already builds its own heading navigation.
- The document is short enough that readers can scan it without a list.
- The content will be pasted into tools without in-page link support, where a simple list of section names is clearer than broken links.
- Collaborators do not use Markdown Preview Enhanced and would otherwise see stale lists or literal extension syntax.
- Headings contain characters that different renderers slug inconsistently, making link breakage likely.
Gaps to check before relying on it
This review did not have confirmed version details, test results on named publishing platforms, or accessibility testing. Before committing to a workflow, confirm in the current extension documentation how and when the list refreshes, which settings the import comment accepts, and how custom heading identifiers are handled. Then open the saved file on your real destination and click every link in the generated list.