1.0.4 • Published 1 year ago
mdtocs v1.0.4
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 --saveYarn:
yarn add mdtocsCDN:
<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 stringTesting
Run tests with coverage:
npm testRun tests in watch mode:
npm run test:watchLint files:
npm run lintFix lint errors:
npm run lint:fixRelease
Release is automated with Release Please.
