mdtocs
v1.0.4
Published
Markdown table of contents generator.
Downloads
304
Maintainers
Readme
mdtocs
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)'
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 with ES Modules:
import { mdtocs } from 'mdtocs';
Or require with 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.