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

@zeusdeux/md-toc

v1.0.5

Published

Generate a GitHub compatible table of contents from headings in a markdown file

Downloads

567

Readme

md-toc

Generate a GitHub compatible table of contents from headings in a markdown file.

Table of contents

Motivation

I wanted something for myself that does exactly what I need it to do and exposes all the levers that I need. Also, I specifically wanted a CLI that can run in a pre-commit hook (via husky + lint-staged) before prettier --write for markdown files so that I never have another piece of documentation without a table of contents!

Installation

npm install -D @zeusdeux/md-toc

Usage

Usage with lint-staged

Add the following line to the "lint-staged" config

"lint-staged": {
  "*.md": "md-toc --write",
}

It is recommended that you run prettier after md-toc. For example —

"lint-staged": {
  "*.md": "md-toc --write",
  "*.{js,cjs,mjs,ts,json,yaml,md}": "prettier --write"
}

Usage as a CLI

Usage: md-toc [options] <file or stdin>

Options:
  -w, --write        Write changes to the input file  [boolean] [default: false]
  -a, --insert-under Heading to insert the table of contents under      [string]
  -d, --debug        Print debug logs to stderr       [boolean] [default: false]
  -v, --version      Show cli version                                  [boolean]
  -h, --help         Show help                                         [boolean]

Examples:
  md-toc --write Readme.md           Generate a table of contents from headings
                                     in Readme.md, insert them under a heading
                                     name "Table of Contents", "toc" or
                                     "table-of-contents" (all case insensitive)
                                     in Readme.md and write the file to disk
  md-toc Readme.md                   Same as the --write option but the output
                                     is written to stdout and Readme.md is left
                                     as is
  md-toc -a "Contents" -w Readme.md  Same as --write but the table of contents
                                     is inserted under the first heading named
                                     "Contents"

Debugging

If you run into problems and want to open an issue here, please run the cli with --debug and put the output in the issue. Do redact PII from the debug info before pasting into the issue.

Contributing

The package manager used is pnpm so please use that and don't commit a package-lock.json. Also, before starting development, run nvm use in the folder where you clone this repository.

Other than that, it is a fairly tiny codebase. Feel free to clone, edit and open a PR.

To test your changes locally, run pnpm link --global and assuming you are using nvm, this should make md-toc available as a command in your shell.