mdtocs
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

mdtocs

NPM

NPM version Build Status codecov

Markdown table of contents generator:

mdtocs(string)

This library uses regex to parse Markdown headings. As a result, edge cases like headings with links and images won't be generated correctly. Inspired by the blog post.

Example

const { mdtocs } = require('mdtocs');
mdtocs('# Hello, World!'); // '- [Hello, World!](#hello-world)'

Site | Replit | JSFiddle

Install

NPM:

npm install mdtocs --save

Yarn:

yarn add mdtocs

CDN:

<script src="https://unpkg.com/mdtocs@latest/umd/mdtocs.min.js"></script>
<script>
  window.mdtocs.mdtocs(/* string */);
</script>

Usage

Import module:

// ES Modules
import { mdtocs } from 'mdtocs';

// CommonJS
const { mdtocs } = require('mdtocs');

Generate table of contents from Markdown:

mdtocs(`
# Heading 1
## Heading 2
### Heading 3
`);

Output:

- [Heading 1](#heading-1)
  - [Heading 2](#heading-2)
    - [Heading 3](#heading-3)

If the first argument is not a string, then an error will be thrown:

mdtocs(); // TypeError: First argument must be a string

Testing

Run tests with coverage:

npm test

Run tests in watch mode:

npm run test:watch

Lint files:

npm run lint

Fix lint errors:

npm run lint:fix

Release

Release is automated with Release Please.

License

MIT

Package Sidebar

Install

npm i mdtocs

Weekly Downloads

35

Version

1.0.2

License

MIT

Unpacked Size

31.5 kB

Total Files

23

Last publish

Collaborators

  • remarkablemark