diff --git a/src/components/blog/GiscusComments.tsx b/src/components/blog/GiscusComments.tsx new file mode 100644 index 0000000..71286af --- /dev/null +++ b/src/components/blog/GiscusComments.tsx @@ -0,0 +1,52 @@ +import Giscus from '@giscus/react'; +import type { Lang } from '@/types/i18n'; +import { useEffect, useState } from 'react'; + +export interface GiscusCommentsProps { + lang?: Lang; +} + +export default function GiscusComments({ lang = 'en' }: GiscusCommentsProps) { + const [theme, setTheme] = useState<'light' | 'dark'>('light'); + const [term, setTerm] = useState(''); + + const giscusLang = lang === 'zh' ? 'zh-CN' : lang; + + useEffect(() => { + const getTheme = () => document.documentElement.classList.contains('dark') ? 'dark' : 'light'; + + const pathname = window.location.pathname; + const postsMatch = pathname.match(/\/posts\/([^/]+)/); + const discussionTerm = postsMatch ? postsMatch[1] : pathname; + + setTheme(getTheme()); + setTerm(discussionTerm); + + const observer = new MutationObserver(() => { + setTheme(getTheme()); + }); + + observer.observe(document.documentElement, { attributes: true, attributeFilter: ['class'] }); + + return () => observer.disconnect(); + }, []); + + + return ( + + ); +} diff --git a/src/layouts/BlogPostLayout.astro b/src/layouts/BlogPostLayout.astro index 9dafdb0..4fc3b07 100644 --- a/src/layouts/BlogPostLayout.astro +++ b/src/layouts/BlogPostLayout.astro @@ -11,6 +11,7 @@ import TableOfContents from '@/components/layout/TableOfContents.astro'; import BlogNavigation from '@/components/layout/BlogNavigation.astro'; import PostMeta from '@/components/blog/PostMeta.astro'; import Container from '../components/ui/Container'; +import GiscusComments from '@/components/blog/GiscusComments'; // Use Astro's MarkdownLayoutProps for proper type safety export type Props = MarkdownLayoutProps; @@ -82,6 +83,11 @@ const finalReadingTime = readTime ? parseInt(readTime.replace(/\D/g, '')) : unde
+ + +
+ +