remark-extract-toc
v2.0.0
Published
remark plugin to store table of contents
Maintainers
Readme
remark-extract-toc
remark plugin to store table of contents.
This plugin extracts only Heading from mdast markdown, then converts them to a nested object tree keeping the depth.
Install
npm install remark-extract-tocUsage
import unified from "unified";
import markdown from "remark-parse";
import extractToc from "remark-extract-toc";
import * as fs from "fs";
const text = fs.readFileSync("example.md", "utf8");
const processor = unified().use(markdown).use(extractToc);
const res = processor.processSync(text);
console.log(res.result);This example.md
# Alpha
aaaa
## Bravo
bbbb
### Charlie
cccc
## Delta
ddddwill 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: [],
},
],
},
]Documentation
License
MIT
