| | |
| | | import { Root } from "mdast" |
| | | import { visit } from "unist-util-visit" |
| | | import { toString } from "mdast-util-to-string" |
| | | import { slug as slugAnchor } from "github-slugger" |
| | | import Slugger from "github-slugger" |
| | | |
| | | export interface Options { |
| | | maxDepth: 1 | 2 | 3 | 4 | 5 | 6 |
| | |
| | | return async (tree: Root, file) => { |
| | | const display = file.data.frontmatter?.enableToc ?? opts.showByDefault |
| | | if (display) { |
| | | const slugAnchor = new Slugger() |
| | | const toc: TocEntry[] = [] |
| | | let highestDepth: number = opts.maxDepth |
| | | visit(tree, "heading", (node) => { |
| | |
| | | toc.push({ |
| | | depth: node.depth, |
| | | text, |
| | | slug: slugAnchor(text), |
| | | slug: slugAnchor.slug(text), |
| | | }) |
| | | } |
| | | }) |