Skip to content

Commit c7e5b26

Browse files
committed
feat: integrate giscus
1 parent be3a29f commit c7e5b26

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/components/BaseHead.astro

+54
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const socialImageURL = new URL(ogImage ? ogImage : "/social-card.png", Astro.url
1616
---
1717

1818
<meta charset="utf-8" />
19+
<meta name="viewport" content="width=device-width" />
1920
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no" />
2021
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
2122
<title>{siteTitle}</title>
@@ -74,3 +75,56 @@ const socialImageURL = new URL(ogImage ? ogImage : "/social-card.png", Astro.url
7475
href="https://fonts.googleapis.com/css2?family=Inter:[email protected]&display=swap"
7576
rel="stylesheet"
7677
/>
78+
79+
<!-- Giscus -->
80+
<script>
81+
document.addEventListener("DOMContentLoaded", function () {
82+
function getGiscusTheme() {
83+
const bodyThemeIsDark = document.documentElement.classList.contains("dark");
84+
return bodyThemeIsDark ? "dark" : "light";
85+
}
86+
87+
function setGiscusTheme() {
88+
function sendMessage(message) {
89+
const iframe = document.querySelector("iframe.giscus-frame");
90+
if (!iframe) return;
91+
iframe.contentWindow.postMessage({ giscus: message }, "https://giscus.app");
92+
}
93+
sendMessage({
94+
setConfig: {
95+
theme: getGiscusTheme(),
96+
},
97+
});
98+
}
99+
100+
const giscusAttributes = {
101+
src: "https://giscus.app/client.js",
102+
"data-repo": "oxwazz/oxwazz.github.io",
103+
"data-repo-id": "R_kgDOKOXRsQ",
104+
"data-category": "Announcements",
105+
"data-category-id": "DIC_kwDOKOXRsc4CeZDU",
106+
"data-mapping": "pathname",
107+
"data-strict": "0",
108+
"data-reactions-enabled": "1",
109+
"data-emit-metadata": "0",
110+
"data-input-position": "top",
111+
"data-theme": getGiscusTheme(),
112+
"data-lang": "en",
113+
crossorigin: "anonymous",
114+
async: "",
115+
};
116+
117+
// Dynamically create script tag
118+
const giscusScript = document.createElement("script");
119+
Object.entries(giscusAttributes).forEach(([key, value]) =>
120+
giscusScript.setAttribute(key, value),
121+
);
122+
document.querySelector("head").appendChild(giscusScript);
123+
124+
// Update giscus theme when theme switcher is clicked
125+
const toggle = document.querySelector("theme-toggle");
126+
if (toggle) {
127+
toggle.addEventListener("click", setGiscusTheme);
128+
}
129+
});
130+
</script>

src/layouts/Base.astro

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const {
2929
<Header />
3030
<main id="main" class="w-full max-w-2xl">
3131
<slot />
32+
<div class="giscus mt-8"></div>
3233
<Footer />
3334
</main>
3435
</body>

0 commit comments

Comments
 (0)