Package detail

markdown-toc-gen

thesilk-tux57.8kMIT1.2.0

Generating and updating table of contents in Markdown files which conform with prettier.

toc, markdown, generator, table of content

readme

Markdown-toc-gen

logo markdown-toc-gen

coverage pipeline npm-version npm-downloads code-style


Install

Install with npm:

npm install markdown-toc-gen

Description

markdown-toc-gen is a lightweight tool to create and update table of contents in Markdown files. The navigation of the created toc works with GitHub Flavored Markdown Spec and pandoc. The focus was on conformity with prettier. So only hyphens are allowed as bullet list.

This tool can also handle duplicate headings in a Markdown file without breaking the navigation. Code blocks will be ignored so there are no issues with code comments or Markdown in code blocks.

To use this tool you have to add the following lines in your markdown files which are separated with one or more newlines:

<-- toc -->
<-- tocstop -->

The default configuration only allows to create the toc from headings with a level from 2 (##) to 6 (######). The level 1 should only used for the markdown title which shouldn't be a part of the toc. If level 1 headings are used in your markdown markdown-toc-gen will ignore it for creating the toc. It's possible to define the maxDepth for parsing headings in the range [2,6].

Usage

Usage: markdown-toc-gen <command> [options]

Commands:
  markdown-toc-gen insert [files..]   insert/update the toc in given markdown file                     [aliases: update]
  markdown-toc-gen dry-run [files..]  returns only created markdown toc without changing given file
  markdown-toc-gen check [files..]    check if toc exists or if toc is outdated

Options:
  -d, --max-depth  max depth for header parsing (default: 6)                                                    [number]
      --version  Show version number                                                                           [boolean]
      --help     Show help                                                                                     [boolean]

Examples:
  markdown-toc-gen insert README.md   insert table of content for README.md
  markdown-toc-gen insert ./**/README.md   insert table of content for given README.md files
  markdown-toc-gen update README.md   update given table of content for README.md
  markdown-toc-gen dry-run README.md  test toc creation for given README.md
  markdown-toc-gen dry-run ./**/README.md  test toc creation for given README.md files
  markdown-toc-gen check README.md    check if toc exists or it toc is outdated
  markdown-toc-gen check ./**/README.md    validates toc for given README.md files

copyright 2021 by TheSilk
Released under MIT License

Multiple files support

It's possible to treat multiple files at once. You have to use the ./**/README.md syntax. Please notice that the node_modules directory will be ignored. In future versions it will also be possible to exclude given directories.

Insert toc

After adding the placeholders in your markdown file you can add the generated toc with

markdown-toc-gen insert README.md
markdown-toc-gen insert ./**/README.md
markdown-toc-gen insert ./**/*.md

Update toc

Updating an existing toc is also no issue. With the following command the existing toc will be updated.

markdown-toc-gen update README.md
markdown-toc-gen update ./**/README.md
markdown-toc-gen update ./**/*.md

Test toc creation

It is also possible to test the toc creation with a dry-run mode. In this mode the headings will be parsed and printed to STDOUT. There are no modifications on the given file.

markdown-toc-gen dry-run README.md
markdown-toc-gen dry-run ./**/README.md
markdown-toc-gen dry-run src/**/*.md

Toc lint

It is possible to check if a given Markdown file has a table of content or if the toc is outdated. Use cases could be a CI integration to avoid pushing Markdown files with outdated tocs.

markdown-toc-gen check README.md
markdown-toc-gen check ./**/README.md
markdown-toc-gen check ./**/*.md

Recommendation for library development

If you develop on a library with many components inside and each have an own README.md I can recommend to use markdown-toc in combination with husky and lint-staged. With these tools you can create a pre-commit hook which updates or inserts a toc to each staged markdown file. This can prevent to push an outdated table of content. An example package.json configuration could be:

{
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "lint-staged": {
    "linters": {
      "**/README.md": "markdown-toc-gen insert"
    }
  }
}

Attention

Please be aware of manipulation your markdown file with this tool should work in the most cases. There are many tests which are cover many edge cases. But sometimes it is not possible to handle all of them. So I recommend to use this tool only with files in VCS environment or in local copies. The use of this tool is at your sole risk, so I can not accept any liability for any misconduct and damaged files.

Author

Copyright 2021-2024 by TheSilk. Released under MIT License

changelog

CHANGELOG

1.2.0 (31.03.2025)

  • allow one line placeholders
  • use always first toc placeholder

Known Issue

A TOC placeholder is needed if somewhere in the markdown a placeholder in a fenced block exists.

1.1.0 (13.10.2024)

  • make markdownlint happy and don't replace _ with - for internal link creation

1.0.1 (20.09.2021)

  • remove OS restriction, windows should also be supported

1.0.0 (29.06.2021)

  • refactor toc validation with diff print
  • insert toc if no placeholder are available

0.4.4 (12.05.2021)

  • validation bugfix: ignore capitalization for link evaluation in toc
  • add unit tests to avoid this bug in the future
  • npm audit fix (lodash)

0.4.3 (04.03.2021)

  • change behavior of creating link id for PascalCase words

0.4.2 (03.03.2021)

  • escape special characters for creating link

0.4.1 (02.03.2021)

  • add empty line before tocstop placeholder

0.4.0 (01.03.2021)

  • multiple files support for using husky with lint-staged

0.3.0 (25.02.2021)

  • support windows carriage return and adding test cases

0.2.2 (24.02.2021)

  • add keywords, repository and homepage to package.json

0.2.1 (24.02.2021)

  • reorganize dependencies
  • set node shebang in index.ts

0.2.0 (24.02.2021)

  • add new logo
  • format code with prettier
  • refactor app and exclude logic in external service
  • add maxDepth option to define max parsed heading level
  • add check command to check if given toc is valid and exists

0.1.0 (13.02.2021)

  • init project