https://github.com/inokawa/remark-extract-toc
remark plugin to store table of contents
https://github.com/inokawa/remark-extract-toc
markdown mdast remark remark-plugin unified
Last synced: 11 months ago
JSON representation
remark plugin to store table of contents
- Host: GitHub
- URL: https://github.com/inokawa/remark-extract-toc
- Owner: inokawa
- License: mit
- Created: 2020-05-03T18:15:52.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-12-15T20:19:10.000Z (over 2 years ago)
- Last Synced: 2025-07-04T06:09:59.956Z (12 months ago)
- Topics: markdown, mdast, remark, remark-plugin, unified
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/remark-extract-toc
- Size: 708 KB
- Stars: 8
- Watchers: 2
- Forks: 1
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# remark-extract-toc
  
[remark](https://github.com/remarkjs/remark) plugin to store table of contents.
This plugin extracts only `Heading` from [mdast](https://github.com/syntax-tree/mdast) markdown, then converts them to a nested object tree keeping the depth.
# Install
```
npm install remark-extract-toc
```
# Usage
```javascript
var unified = require("unified");
var markdown = require("remark-parse");
var extractToc = require("remark-extract-toc");
var fs = require("fs");
var text = fs.readFileSync("example.md", "utf8");
var processor = unified().use(markdown).use(extractToc);
var node = processor.parse(text);
var tree = processor.runSync(node);
console.log(tree);
```
This `example.md`
```
# Alpha
aaaa
## Bravo
bbbb
### Charlie
cccc
## Delta
dddd
```
will be converted by this library like...
```
[
{
depth: 1,
value: "Alpha",
children: [
{
depth: 2,
value: "Bravo",
children: [{ depth: 3, value: "Charlie", children: [] }],
},
{
depth: 2,
value: "Delta",
children: [],
},
],
},
]
```
# API
`remark().use(toc[, options])`
## Options
| Key | Default | Type | Description |
| ------- | ------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
| flatten | false | boolean | If true, toc is extracted as a list not nested. |
| keys | [] | string[] | Add extra field to tree object. For example, use [remark-slug](https://github.com/remarkjs/remark-slug) to add id and set `{ keys: ["data"] }`. |
# License
MIT