Skip to content

Commit b9656ed

Browse files
committed
add comment section and fix empty folder issue
1 parent 98bd9b4 commit b9656ed

6 files changed

Lines changed: 12075 additions & 4 deletions

File tree

app/components/GiscusComments.tsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"use client";
2+
3+
import { useEffect, useRef } from "react";
4+
5+
export function GiscusComments() {
6+
const containerRef = useRef<HTMLDivElement>(null);
7+
8+
useEffect(() => {
9+
const container = containerRef.current;
10+
if (!container) {
11+
return;
12+
}
13+
14+
const script = document.createElement("script");
15+
script.src = "https://giscus.app/client.js";
16+
script.async = true;
17+
script.crossOrigin = "anonymous";
18+
script.setAttribute("data-repo", "InvolutionHell/involutionhell.github.io");
19+
script.setAttribute("data-repo-id", "R_kgDOPuD_8A");
20+
script.setAttribute("data-category", "Comments");
21+
script.setAttribute("data-category-id", "DIC_kwDOPuD_8M4Cvip8");
22+
script.setAttribute("data-mapping", "pathname");
23+
script.setAttribute("data-strict", "0");
24+
script.setAttribute("data-reactions-enabled", "1");
25+
script.setAttribute("data-emit-metadata", "0");
26+
script.setAttribute("data-input-position", "bottom");
27+
script.setAttribute("data-theme", "preferred_color_scheme");
28+
script.setAttribute("data-lang", "en");
29+
30+
container.appendChild(script);
31+
32+
return () => {
33+
while (container.firstChild) {
34+
container.removeChild(container.firstChild);
35+
}
36+
};
37+
}, []);
38+
39+
return <div ref={containerRef} className="giscus" />;
40+
}

app/docs/[...slug]/page.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { DocsPage, DocsBody } from "fumadocs-ui/page";
33
import { notFound } from "next/navigation";
44
import type { Metadata } from "next";
55
import { getMDXComponents } from "@/mdx-components";
6+
import { GiscusComments } from "@/app/components/GiscusComments";
67

78
interface Param {
89
params: Promise<{
@@ -27,6 +28,9 @@ export default async function DocPage({ params }: Param) {
2728
{page.data.title}
2829
</h1>
2930
<Mdx components={getMDXComponents()} />
31+
<section className="mt-16">
32+
<GiscusComments />
33+
</section>
3034
</DocsBody>
3135
</DocsPage>
3236
);

app/docs/ai/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ tags:
2626
- 方法论学习:科研指南、论文阅读
2727
- 杂项工具:开发工具、平台使用
2828

29-
> 注意:论文合集可邀请到 Zotero 协作;“TODO” 代表待施工项目。
29+
> 注意:论文合集可邀请到 Zotero 协作;

app/docs/layout.tsx

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,66 @@ import { DocsLayout } from "fumadocs-ui/layouts/docs";
33
import { baseOptions } from "@/lib/layout.shared";
44
import type { ReactNode } from "react";
55
import { DocsRouteFlag } from "@/app/components/RouteFlags";
6+
import type { PageTree } from "fumadocs-core/server";
7+
8+
function pruneEmptyFolders(root: PageTree.Root): PageTree.Root {
9+
const transformNode = (node: PageTree.Node): PageTree.Node | null => {
10+
if (node.type === "folder") {
11+
const transformedChildren = node.children
12+
.map(transformNode)
13+
.filter((child): child is PageTree.Node => child !== null);
14+
15+
const index = node.index ? { ...node.index } : undefined;
16+
17+
if (transformedChildren.length > 0) {
18+
return {
19+
...node,
20+
index,
21+
children: transformedChildren,
22+
};
23+
}
24+
25+
if (index) {
26+
return { ...index };
27+
}
28+
29+
return null;
30+
}
31+
32+
if (node.type === "separator") {
33+
return { ...node };
34+
}
35+
36+
return { ...node };
37+
};
38+
39+
const transformRoot = (node: PageTree.Root): PageTree.Root => {
40+
const children = node.children
41+
.map(transformNode)
42+
.filter((child): child is PageTree.Node => child !== null);
43+
44+
return {
45+
...node,
46+
children,
47+
fallback: node.fallback ? transformRoot(node.fallback) : undefined,
48+
};
49+
};
50+
51+
return transformRoot(root);
52+
}
653

754
export default function Layout({ children }: { children: ReactNode }) {
55+
const tree = pruneEmptyFolders(source.pageTree);
56+
857
return (
958
<>
1059
{/* Add a class on <html> while in docs to adjust global backgrounds */}
1160
<DocsRouteFlag />
1261
<DocsLayout
13-
tree={source.pageTree}
62+
tree={tree}
1463
{...baseOptions()}
1564
sidebar={{
16-
// 第一屏仅显示目录,不展开子目录
65+
// Only show top-level items on first load
1766
defaultOpenLevel: 0,
1867
}}
1968
>

0 commit comments

Comments
 (0)