Skip to content

Commit 88301e8

Browse files
committed
feat: Add giscus
1 parent f048a5f commit 88301e8

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

app/[slug]/page.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { notFound } from 'next/navigation';
44
import { useMDXComponent } from 'next-contentlayer/hooks';
55

66
import Code from '@/components/Code';
7+
import Giscus from '@/components/Giscus';
78
import { HomeIcon } from '@/components/icons';
89
import TableOfContent from '@/components/TableOfContent';
910
import { allBlogPosts, parseToc } from '@/libs/post';
@@ -83,6 +84,7 @@ export default function PostPage({ params }: PostPageProps) {
8384
<div data-animate className="prose prose-neutral">
8485
<MDXContent components={mdxComponents} />
8586
</div>
87+
<Giscus />
8688
</main>
8789
);
8890
}

components/Giscus.tsx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use client';
2+
import { usePathname } from 'next/navigation';
3+
import { useEffect, useRef } from 'react';
4+
5+
export default function Giscus() {
6+
const ref = useRef<HTMLDivElement>(null);
7+
const pathname = usePathname();
8+
9+
useEffect(() => {
10+
if (!ref.current || ref.current.hasChildNodes()) return;
11+
12+
const scriptElement = document.createElement('script');
13+
scriptElement.src = 'https://giscus.app/client.js';
14+
scriptElement.async = true;
15+
scriptElement.crossOrigin = 'anonymous';
16+
17+
scriptElement.setAttribute('data-repo', 'yuhwan-park/yuhwan-park.github.io');
18+
scriptElement.setAttribute('data-repo-id', 'R_kgDONC_0rQ');
19+
scriptElement.setAttribute('data-category', 'Announcements');
20+
scriptElement.setAttribute('data-category-id', 'DIC_kwDONC_0rc4Cjwdo');
21+
scriptElement.setAttribute('data-mapping', 'pathname');
22+
scriptElement.setAttribute('data-strict', '0');
23+
scriptElement.setAttribute('data-reactions-enabled', '1');
24+
scriptElement.setAttribute('data-emit-metadata', '0');
25+
scriptElement.setAttribute('data-input-position', 'bottom');
26+
scriptElement.setAttribute('data-theme', 'light');
27+
scriptElement.setAttribute('data-lang', 'en');
28+
29+
ref.current.appendChild(scriptElement);
30+
}, []);
31+
32+
useEffect(() => {
33+
const iframe = document.querySelector<HTMLIFrameElement>('iframe.giscus-frame');
34+
iframe?.contentWindow?.postMessage(
35+
{ giscus: { setConfig: { term: pathname } } },
36+
'https://giscus.app',
37+
);
38+
}, [pathname]);
39+
40+
return <section className="mt-32" ref={ref} />;
41+
}

0 commit comments

Comments
 (0)