Skip to content

Commit f80c5cc

Browse files
committed
CHORE: 让discus跟随网站主题
1 parent cd54043 commit f80c5cc

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{{- with .Site.Params.comments.giscus -}}
2+
<div class="giscus-container"
3+
data-repo="{{- .repo -}}"
4+
data-repo-id="{{- .repoID -}}"
5+
data-category="{{- .category -}}"
6+
data-category-id="{{- .categoryID -}}"
7+
data-mapping="{{- default `title` .mapping -}}"
8+
data-strict="{{- default 0 .strict -}}"
9+
data-reactions-enabled="{{- default 1 .reactionsEnabled -}}"
10+
data-emit-metadata="{{- default 0 .emitMetadata -}}"
11+
data-input-position="{{- default `top` .inputPosition -}}"
12+
data-theme-light="{{- default `light` .lightTheme -}}"
13+
data-theme-dark="{{- default `dark` .darkTheme -}}"
14+
data-lang="{{- default `en` .lang -}}"
15+
data-loading="{{- .loading -}}"
16+
></div>
17+
{{- end -}}

layouts/partials/footer/custom.html

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,61 @@
125125

126126
// Re-render KaTeX
127127
renderKaTeX();
128+
129+
// Re-load Giscus
130+
loadGiscus();
128131
})
129132

133+
// --- Giscus Logic ---
134+
function loadGiscus() {
135+
const container = document.querySelector('.giscus-container');
136+
if (!container) return;
137+
138+
const script = document.createElement('script');
139+
script.src = "https://giscus.app/client.js";
140+
141+
// Determine current theme
142+
const isDark = document.documentElement.dataset.scheme === 'dark';
143+
const theme = isDark ? container.dataset.themeDark : container.dataset.themeLight;
144+
145+
for (const attr of container.attributes) {
146+
if (attr.name.startsWith('data-')) {
147+
script.setAttribute(attr.name, attr.value);
148+
}
149+
}
150+
151+
// Set dynamic theme
152+
script.setAttribute('data-theme', theme);
153+
154+
script.crossOrigin = "anonymous";
155+
script.async = true;
156+
157+
container.innerHTML = '';
158+
container.appendChild(script);
159+
}
160+
161+
// Listen for theme changes
162+
window.addEventListener('onColorSchemeChange', (e) => {
163+
const container = document.querySelector('.giscus-container');
164+
if (!container) return;
165+
166+
const newTheme = e.detail === 'dark' ? container.dataset.themeDark : container.dataset.themeLight;
167+
168+
const iframe = document.querySelector('iframe.giscus-frame');
169+
if (iframe) {
170+
iframe.contentWindow.postMessage({
171+
giscus: {
172+
setConfig: {
173+
theme: newTheme
174+
}
175+
}
176+
}, 'https://giscus.app');
177+
}
178+
});
179+
180+
// Initial load
181+
loadGiscus();
182+
130183
// --- KaTeX Logic ---
131184
async function renderKaTeX() {
132185
let katex = document.querySelector(".math-katex");

0 commit comments

Comments
 (0)