Skip to content

Commit 6ce0832

Browse files
feat: add giscus comments to all docs automatically
1 parent 51ef3c8 commit 6ce0832

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

docusaurus.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { themes as prismThemes } from "prism-react-renderer";
22
import type { Config } from "@docusaurus/types";
33
import type * as Preset from "@docusaurus/preset-classic";
44
import * as dotenv from "dotenv";
5+
import giscusInjector from "./src/plugins/giscus-injector";
56
dotenv.config();
67

78
// This runs in Node.js - Don't use client-side code here (browser APIs, JSX...)
@@ -53,6 +54,7 @@ const config: Config = {
5354
sidebarPath: require.resolve("./sidebars.ts"),
5455
editUrl: ({ docPath }) =>
5556
`https://github.com/recodehive/recode-website/tree/main/docs/${docPath}`,
57+
remarkPlugins: [giscusInjector],
5658
},
5759
blog: {
5860
showReadingTime: true,

src/plugins/giscus-injector.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const { visit } = require("unist-util-visit");
2+
3+
/**
4+
* Remark plugin to automatically inject GiscusComments component
5+
* at the end of MDX files that don't already have it
6+
*/
7+
export default function giscusInjector() {
8+
return (tree) => {
9+
let hasGiscus = false;
10+
11+
// Check if GiscusComments already exists
12+
visit(tree, (node) => {
13+
// Check JSX elements
14+
if (
15+
node.type === "mdxJsxFlowElement" ||
16+
node.type === "mdxJsxTextElement"
17+
) {
18+
if (node.name === "GiscusComments") {
19+
hasGiscus = true;
20+
}
21+
}
22+
23+
// Check raw JSX strings
24+
if (
25+
node.type === "jsx" &&
26+
node.value &&
27+
node.value.includes("GiscusComments")
28+
) {
29+
hasGiscus = true;
30+
}
31+
});
32+
33+
// Only inject if not present
34+
if (!hasGiscus) {
35+
// Add the component as an MDX JSX element
36+
tree.children.push({
37+
type: "mdxJsxFlowElement",
38+
name: "GiscusComments",
39+
attributes: [],
40+
children: [],
41+
});
42+
}
43+
};
44+
}

0 commit comments

Comments
 (0)