unified

Project: inokawa/remark-extract-toc

Package: remark-extract-toc@1.1.0

  1. remark plugin to store table of contents
  1. unist 117
  2. remark 115
  3. markdown 100
  4. rehype 86
  5. hast 74
  6. mdast 64
  7. retext 41
  8. nlcst 15
  9. toc 4
  10. table 4
  11. of 3
  12. 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