File size: 1,809 Bytes
96dd062 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | ---
import { commentConfig } from "@/config";
if (!commentConfig || !commentConfig.giscus) {
throw new Error("Giscus comments are not configured");
}
const giscus = commentConfig.giscus;
const lightTheme = "light";
const darkTheme = "dark";
---
<script is:inline type="module" src="https://esm.sh/giscus"></script>
<!--
Giscus 官方Web Component用法,兼容Astro静态输出,无需import包,无需NPM依赖!
参考:https://giscus.app/ 详情配置说明
-->
<giscus-widget
id="comments"
repo={giscus.repo}
repoId={giscus.repoId}
category={giscus.category}
categoryId={giscus.categoryId}
mapping={giscus.mapping}
strict={giscus.strict}
reactionsEnabled={giscus.reactionsEnabled}
emitMetadata={giscus.emitMetadata}
inputPosition={giscus.inputPosition}
theme={lightTheme}
lang={giscus.lang}
loading={giscus.loading}
/>
<script is:inline define:vars={{ lightTheme, darkTheme }}>
function updateGiscusTheme() {
const isDark = document.documentElement.classList.contains('dark');
const theme = isDark ? darkTheme : lightTheme;
const widget = document.querySelector('giscus-widget');
if (widget) {
widget.setAttribute('theme', theme);
}
}
// Initial update
updateGiscusTheme();
// Clean up previous observer if exists
if (window.giscusThemeObserver) {
window.giscusThemeObserver.disconnect();
}
// Create new observer
window.giscusThemeObserver = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.type === 'attributes' && mutation.attributeName === 'class') {
updateGiscusTheme();
}
});
});
window.giscusThemeObserver.observe(document.documentElement, { attributes: true });
</script>
|