2023-06-09 23:06:02 -07:00
|
|
|
import { QuartzTransformerPlugin } from "../types"
|
|
|
|
import { Root } from "mdast"
|
|
|
|
import { visit } from "unist-util-visit"
|
|
|
|
import { toString } from "mdast-util-to-string"
|
2023-08-22 22:41:50 -07:00
|
|
|
import Slugger from "github-slugger"
|
2023-06-09 23:06:02 -07:00
|
|
|
|
|
|
|
export interface Options {
|
2023-07-22 17:27:41 -07:00
|
|
|
maxDepth: 1 | 2 | 3 | 4 | 5 | 6
|
2024-02-20 18:06:53 +01:00
|
|
|
minEntries: number
|
2023-06-09 23:06:02 -07:00
|
|
|
showByDefault: boolean
|
2023-11-04 12:11:42 -07:00
|
|
|
collapseByDefault: boolean
|
2023-06-09 23:06:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const defaultOptions: Options = {
|
|
|
|
maxDepth: 3,
|
|
|
|
minEntries: 1,
|
|
|
|
showByDefault: true,
|
2023-11-04 12:11:42 -07:00
|
|
|
collapseByDefault: false,
|
2023-06-09 23:06:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
interface TocEntry {
|
2023-07-22 17:27:41 -07:00
|
|
|
depth: number
|
|
|
|
text: string
|
2023-07-15 23:02:12 -07:00
|
|
|
slug: string // this is just the anchor (#some-slug), not the canonical slug
|
2023-06-09 23:06:02 -07:00
|
|
|
}
|
|
|
|
|
2024-02-03 19:44:24 -08:00
|
|
|
const slugAnchor = new Slugger()
|
2023-07-22 17:27:41 -07:00
|
|
|
export const TableOfContents: QuartzTransformerPlugin<Partial<Options> | undefined> = (
|
|
|
|
userOpts,
|
|
|
|
) => {
|
2023-06-11 23:26:43 -07:00
|
|
|
const opts = { ...defaultOptions, ...userOpts }
|
|
|
|
return {
|
|
|
|
name: "TableOfContents",
|
|
|
|
markdownPlugins() {
|
2023-07-22 17:27:41 -07:00
|
|
|
return [
|
|
|
|
() => {
|
|
|
|
return async (tree: Root, file) => {
|
|
|
|
const display = file.data.frontmatter?.enableToc ?? opts.showByDefault
|
|
|
|
if (display) {
|
2024-02-03 19:44:24 -08:00
|
|
|
slugAnchor.reset()
|
2023-07-22 17:27:41 -07:00
|
|
|
const toc: TocEntry[] = []
|
|
|
|
let highestDepth: number = opts.maxDepth
|
|
|
|
visit(tree, "heading", (node) => {
|
|
|
|
if (node.depth <= opts.maxDepth) {
|
2024-02-04 20:57:10 -08:00
|
|
|
const text = toString(node)
|
2023-07-22 17:27:41 -07:00
|
|
|
highestDepth = Math.min(highestDepth, node.depth)
|
|
|
|
toc.push({
|
|
|
|
depth: node.depth,
|
|
|
|
text,
|
2023-08-22 22:41:50 -07:00
|
|
|
slug: slugAnchor.slug(text),
|
2023-07-22 17:27:41 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2023-06-09 23:06:02 -07:00
|
|
|
|
2024-02-20 18:06:53 +01:00
|
|
|
if (toc.length > 0 && toc.length > opts.minEntries) {
|
2023-07-22 17:27:41 -07:00
|
|
|
file.data.toc = toc.map((entry) => ({
|
|
|
|
...entry,
|
|
|
|
depth: entry.depth - highestDepth,
|
|
|
|
}))
|
2023-11-04 12:11:42 -07:00
|
|
|
file.data.collapseToc = opts.collapseByDefault
|
2023-07-22 17:27:41 -07:00
|
|
|
}
|
2023-06-09 23:06:02 -07:00
|
|
|
}
|
|
|
|
}
|
2023-07-22 17:27:41 -07:00
|
|
|
},
|
|
|
|
]
|
2023-06-11 23:26:43 -07:00
|
|
|
},
|
2023-06-09 23:06:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-22 17:27:41 -07:00
|
|
|
declare module "vfile" {
|
2023-06-09 23:06:02 -07:00
|
|
|
interface DataMap {
|
|
|
|
toc: TocEntry[]
|
2023-11-04 12:11:42 -07:00
|
|
|
collapseToc: boolean
|
2023-06-09 23:06:02 -07:00
|
|
|
}
|
|
|
|
}
|