-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (58 loc) · 1.74 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { toc } from "mdast-util-toc";
import { filter } from "unist-util-filter";
import { modifyChildren } from "unist-util-modify-children";
import { load } from "js-yaml";
const defaultOptions = {
className: "toc",
};
// Default options for mdast-util-toc
const defaultTOCOptions = {
ordered: false,
tight: true,
};
const modify = (options) =>
modifyChildren(function (node, index, parent) {
// we have a TOC
if (node.type === "code" && node.lang === "toc") {
const config = load(node.value);
// For XSS safety, we only allow basic css names
// this is taken from `gatsby-remark-table-of-contents`
if (!options?.className?.match(/^[ a-zA-Z0-9_-]*$/)) {
options.className = defaultOptions.className;
}
// support both 'from-heading' and 'from'
let from = config["from-heading"] || config.from;
let to = config["to-heading"] || config.to;
// only keep the headings whose depth is greater than our minimum
// removes the headings < (from +1), so never include h1
const treeWithoutHeadings = filter(
parent,
(node) => node.type !== "heading" || node.depth >= from + 1
);
const result = toc(treeWithoutHeadings, {
maxDepth: to,
...defaultTOCOptions,
...options,
});
parent.children.splice(
index,
1,
{
type: "html",
value: `<div class="${options.className}">`,
},
result.map,
{
type: "html",
value: "</div>",
}
);
return index + result.map.length + 2;
}
});
export default function selectiveTOC(options = defaultOptions) {
return function (tree, file) {
modify(options)(tree);
return tree;
};
}