Skip to content

Commit 86d35aa

Browse files
committed
feat: 加个评论
1 parent e480367 commit 86d35aa

4 files changed

Lines changed: 125 additions & 16 deletions

File tree

public/favicon/favicon.png

569 KB
Loading

src/components/misc/Giscus.astro

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
interface Props {
3+
repo: string;
4+
repoId: string;
5+
category: string;
6+
categoryId: string;
7+
mapping?: string;
8+
reactionsEnabled?: boolean;
9+
emitMetadata?: boolean;
10+
inputPosition?: 'top' | 'bottom';
11+
lang?: string;
12+
}
13+
14+
const {
15+
repo,
16+
repoId,
17+
category,
18+
categoryId,
19+
mapping = 'pathname',
20+
reactionsEnabled = true,
21+
emitMetadata = false,
22+
inputPosition = 'bottom',
23+
lang = 'zh-CN'
24+
} = Astro.props;
25+
---
26+
27+
<div id="giscus-container"></div>
28+
29+
<script define:vars={{ repo, repoId, category, categoryId, mapping, reactionsEnabled, emitMetadata, inputPosition, lang }}>
30+
function loadGiscus() {
31+
const container = document.getElementById('giscus-container');
32+
if (!container) return;
33+
34+
const isDark = document.documentElement.classList.contains('dark');
35+
const theme = isDark ? 'dark' : 'light';
36+
37+
const script = document.createElement('script');
38+
script.src = 'https://giscus.app/client.js';
39+
script.setAttribute('data-repo', repo);
40+
script.setAttribute('data-repo-id', repoId);
41+
script.setAttribute('data-category', category);
42+
script.setAttribute('data-category-id', categoryId);
43+
script.setAttribute('data-mapping', mapping);
44+
script.setAttribute('data-strict', '0');
45+
script.setAttribute('data-reactions-enabled', reactionsEnabled ? '1' : '0');
46+
script.setAttribute('data-emit-metadata', emitMetadata ? '1' : '0');
47+
script.setAttribute('data-input-position', inputPosition);
48+
script.setAttribute('data-theme', theme);
49+
script.setAttribute('data-lang', lang);
50+
script.setAttribute('data-loading', 'lazy');
51+
script.crossOrigin = 'anonymous';
52+
script.async = true;
53+
54+
container.appendChild(script);
55+
}
56+
57+
// 监听主题变化
58+
function updateGiscusTheme() {
59+
const giscusFrame = document.querySelector('iframe[src*="giscus"]');
60+
if (giscusFrame) {
61+
const isDark = document.documentElement.classList.contains('dark');
62+
const theme = isDark ? 'dark' : 'light';
63+
64+
giscusFrame.contentWindow.postMessage({
65+
giscus: {
66+
setConfig: {
67+
theme: theme
68+
}
69+
}
70+
}, 'https://giscus.app');
71+
}
72+
}
73+
74+
// 监听DOM变化来检测主题切换
75+
const observer = new MutationObserver((mutations) => {
76+
mutations.forEach((mutation) => {
77+
if (mutation.type === 'attributes' && mutation.attributeName === 'class') {
78+
updateGiscusTheme();
79+
}
80+
});
81+
});
82+
83+
// 页面加载时初始化
84+
if (document.readyState === 'loading') {
85+
document.addEventListener('DOMContentLoaded', loadGiscus);
86+
} else {
87+
loadGiscus();
88+
}
89+
90+
// 开始观察主题变化
91+
observer.observe(document.documentElement, {
92+
attributes: true,
93+
attributeFilter: ['class']
94+
});
95+
</script>

src/config.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ export const siteConfig: SiteConfig = {
3131
},
3232
favicon: [
3333
// Leave this array empty to use the default favicon
34-
// {
35-
// src: '/favicon/icon.png', // Path of the favicon, relative to the /public directory
34+
{
35+
src: '/favicon/favicon.png', // Path of the favicon, relative to the /public directory
3636
// theme: 'light', // (Optional) Either 'light' or 'dark', set only if you have different favicons for light and dark mode
3737
// sizes: '32x32', // (Optional) Size of the favicon, set only if you have favicons of different sizes
38-
// }
38+
}
3939
],
4040
};
4141

@@ -58,22 +58,27 @@ export const profileConfig: ProfileConfig = {
5858
name: "Rinai",
5959
bio: "只是一个计算机爱好者",
6060
links: [
61-
{
62-
name: "Twitter",
63-
icon: "fa6-brands:twitter", // Visit https://icones.js.org/ for icon codes
64-
// You will need to install the corresponding icon set if it's not already included
65-
// `pnpm add @iconify-json/<icon-set-name>`
66-
url: "https://twitter.com",
67-
},
68-
{
69-
name: "Steam",
70-
icon: "fa6-brands:steam",
71-
url: "https://store.steampowered.com",
72-
},
61+
// {
62+
// name: "Twitter",
63+
// icon: "fa6-brands:twitter", // Visit https://icones.js.org/ for icon codes
64+
// // You will need to install the corresponding icon set if it's not already included
65+
// // `pnpm add @iconify-json/<icon-set-name>`
66+
// url: "https://twitter.com",
67+
// },
68+
// {
69+
// name: "Steam",
70+
// icon: "fa6-brands:steam",
71+
// url: "https://store.steampowered.com",
72+
// },
7373
{
7474
name: "GitHub",
7575
icon: "fa6-brands:github",
76-
url: "https://github.com/saicaca/fuwari",
76+
url: "https://github.com/Rinai-R",
77+
},
78+
{
79+
name: "Mail",
80+
icon: "material-symbols:alternate-email",
81+
url: "mailto:whrinai@outlook.com",
7782
},
7883
],
7984
};

src/pages/posts/[...slug].astro

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import path from "node:path";
33
import License from "@components/misc/License.astro";
44
import Markdown from "@components/misc/Markdown.astro";
5+
import Giscus from "../../components/misc/Giscus.astro";
56
import I18nKey from "@i18n/i18nKey";
67
import { i18n } from "@i18n/translation";
78
import MainGridLayout from "@layouts/MainGridLayout.astro";
@@ -108,6 +109,14 @@ const jsonLd = {
108109

109110
{licenseConfig.enable && <License title={entry.data.title} slug={entry.slug} pubDate={entry.data.published} class="mb-6 rounded-xl license-container onload-animation"></License>}
110111

112+
113+
<Giscus
114+
repo="Rinai-R/blog-comments"
115+
repoId="R_kgDOQPDBJg"
116+
category="Announcements"
117+
categoryId="DIC_kwDOQPDBJs4CxbUb"
118+
lang="zh-CN"
119+
/>
111120
</div>
112121
</div>
113122

0 commit comments

Comments
 (0)