https://github.com/remarkablemark/mdtocs
📝 Markdown table of contents generator.
https://github.com/remarkablemark/mdtocs
javascript markdown mdtocs nodejs npm parser table-of-contents typescript
Last synced: 3 months ago
JSON representation
📝 Markdown table of contents generator.
- Host: GitHub
- URL: https://github.com/remarkablemark/mdtocs
- Owner: remarkablemark
- License: mit
- Created: 2020-12-20T04:11:57.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2025-03-12T16:20:23.000Z (over 1 year ago)
- Last Synced: 2025-03-17T10:51:42.908Z (over 1 year ago)
- Topics: javascript, markdown, mdtocs, nodejs, npm, parser, table-of-contents, typescript
- Language: TypeScript
- Homepage: https://b.remarkabl.org/mdtocs
- Size: 3.61 MB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# mdtocs
[](https://www.npmjs.com/package/mdtocs)
[](https://www.npmjs.com/package/mdtocs)
[](https://github.com/remarkablemark/mdtocs/actions/workflows/build.yml)
[](https://codecov.io/gh/remarkablemark/mdtocs)
[Markdown](https://wikipedia.org/wiki/Markdown) table of contents generator:
```
mdtocs(string)
```
This library uses [regex](https://developer.mozilla.org/docs/Web/JavaScript/Guide/Regular_Expressions) to parse [Markdown headings](https://www.markdownguide.org/basic-syntax/#headings). As a result, edge cases like headings with links and images won't be generated correctly. Inspired by the [blog post](https://b.remarkabl.org/3rgdgCk).
#### Example
```js
const { mdtocs } = require('mdtocs');
mdtocs('# Hello, World!'); // '- [Hello, World!](#hello-world)'
```
[Site](https://b.remarkabl.org/mdtocs) | [JSFiddle](https://jsfiddle.net/remarkablemark/dr03pLxn/)
## Install
[NPM](https://www.npmjs.com/package/mdtocs):
```sh
npm install mdtocs --save
```
[Yarn](https://yarnpkg.com/package/mdtocs):
```sh
yarn add mdtocs
```
[CDN](https://unpkg.com/mdtocs/):
```html
window.mdtocs.mdtocs(/* string */);
```
## Usage
Import with ES Modules:
```js
import { mdtocs } from 'mdtocs';
```
Or require with CommonJS:
```js
const { mdtocs } = require('mdtocs');
```
Generate table of contents from Markdown:
```js
mdtocs(`
# Heading 1
## Heading 2
### Heading 3
`);
```
Output:
```md
- [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:
```js
mdtocs(); // TypeError: First argument must be a string
```
## Testing
Run tests with coverage:
```sh
npm test
```
Run tests in watch mode:
```sh
npm run test:watch
```
Lint files:
```sh
npm run lint
```
Fix lint errors:
```sh
npm run lint:fix
```
## Release
Release is automated with [Release Please](https://github.com/googleapis/release-please).
## License
[MIT](https://github.com/remarkablemark/mdtocs/blob/master/LICENSE)