unified

Project: inokawa/remark-extract-toc

Package: remark-extract-toc@1.1.0

  1. remark plugin to store table of contents
  1. remark 196
  2. unist 117
  3. markdown 114
  4. rehype 89
  5. hast 74
  6. mdast 68
  7. retext 42
  8. nlcst 15
  9. list 10
  10. table 8
  11. toc 4
  12. of 3
  13. contents 3

remark-extract-toc

npm npm check

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-toc

Usage

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

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: [],
      },
    ],
  },
]

Documentation

License

MIT