-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathgiscus-injector.ts
More file actions
44 lines (40 loc) · 1.01 KB
/
giscus-injector.ts
File metadata and controls
44 lines (40 loc) · 1.01 KB
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
const { visit } = require("unist-util-visit");
/**
* Remark plugin to automatically inject GiscusComments component
* at the end of MDX files that don't already have it
*/
export default function giscusInjector() {
return (tree) => {
let hasGiscus = false;
// Check if GiscusComments already exists
visit(tree, (node) => {
// Check JSX elements
if (
node.type === "mdxJsxFlowElement" ||
node.type === "mdxJsxTextElement"
) {
if (node.name === "GiscusComments") {
hasGiscus = true;
}
}
// Check raw JSX strings
if (
node.type === "jsx" &&
node.value &&
node.value.includes("GiscusComments")
) {
hasGiscus = true;
}
});
// Only inject if not present
if (!hasGiscus) {
// Add the component as an MDX JSX element
tree.children.push({
type: "mdxJsxFlowElement",
name: "GiscusComments",
attributes: [],
children: [],
});
}
};
}