-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
29 lines (23 loc) · 788 Bytes
/
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
// noop
const visit = require("unist-util-visit");
const toString = require("mdast-util-to-string");
const slugify = require("@sindresorhus/slugify");
module.exports = async ({markdownAST}, pluginOptions) => {
visit(markdownAST, "heading", (node) => {
let {depth} = node;
const headings = pluginOptions?.headings ?? [];
// Skip if not in headings options
if (headings.length > 0 && !headings.includes(`h${depth}`)) return;
let text = toString(node);
let slug = slugify(text.replace(/<[^>]*>/g, ""));
const html = `
<h${depth} class="heading-container" id="${slug}">
<a class="heading-text" href="#${slug}">${text}</a>
</h${depth}>
`;
node.type = "html";
node.children = undefined;
node.value = html;
});
return markdownAST;
};