npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

markdown-toc-gen

v1.1.0

Published

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

Downloads

11,168

Readme

Markdown-toc-gen


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