Skip to content

Commit 67742fc

Browse files
committed
修复 giscus 主题没有自动适配的问题
1 parent 0aa21e7 commit 67742fc

1 file changed

Lines changed: 54 additions & 48 deletions

File tree

src/components/Comment.astro

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,64 +7,70 @@ const giscus = config.integ.giscus
77
{
88
giscus && giscus.enable && (
99
<div class='giscus-container'>
10-
<script
11-
is:inline
12-
src='https://giscus.app/client.js'
13-
data-repo={giscus.repo}
14-
data-repo-id={giscus.repoId}
15-
data-category={giscus.category}
16-
data-category-id={giscus.categoryId}
17-
data-mapping={giscus.mapping || 'pathname'}
18-
data-strict={giscus.strict || '0'}
19-
data-reactions-enabled={giscus.reactionsEnabled || '1'}
20-
data-emit-metadata={giscus.emitMetadata || '0'}
21-
data-input-position={giscus.inputPosition || 'top'}
22-
data-theme={giscus.theme}
23-
data-lang={giscus.lang || 'zh-CN'}
24-
data-loading='lazy'
25-
crossorigin='anonymous'
26-
async
27-
/>
10+
<script is:inline data-giscus-config={JSON.stringify(giscus)}>
11+
// Immediately-invoked function to avoid polluting the global scope
12+
;(() => {
13+
const getGiscusTheme = () => {
14+
let theme = localStorage.getItem('theme')
15+
if (!theme || theme === 'system') {
16+
theme =
17+
window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
18+
}
19+
// Use the 'noborder_' prefix as required by the user's setup
20+
return `noborder_${theme}`
21+
}
22+
23+
// Create a new script element
24+
const giscusScript = document.createElement('script')
25+
26+
// Get the Giscus config from the data attribute of this script
27+
const config = JSON.parse(document.currentScript.getAttribute('data-giscus-config'))
28+
29+
// Set all the necessary attributes for Giscus
30+
Object.entries({
31+
src: 'https://giscus.app/client.js',
32+
'data-repo': config.repo,
33+
'data-repo-id': config.repoId,
34+
'data-category': config.category,
35+
'data-category-id': config.categoryId,
36+
'data-mapping': config.mapping || 'pathname',
37+
'data-strict': config.strict || '0',
38+
'data-reactions-enabled': config.reactionsEnabled || '1',
39+
'data-emit-metadata': config.emitMetadata || '0',
40+
'data-input-position': config.inputPosition || 'top',
41+
'data-lang': config.lang || 'zh-CN',
42+
'data-loading': 'lazy',
43+
crossorigin: 'anonymous',
44+
async: true,
45+
// Set the theme dynamically!
46+
'data-theme': getGiscusTheme()
47+
}).forEach(([key, value]) => giscusScript.setAttribute(key, String(value)))
48+
49+
// Append the script to the container, which will trigger it to load.
50+
document.querySelector('.giscus-container').appendChild(giscusScript)
51+
})()
52+
</script>
2853
</div>
2954
)
3055
}
3156

57+
{/* This separate script handles theme changes after the initial load */}
3258
<script is:inline>
3359
;(() => {
34-
const getEffectiveTheme = () => {
35-
const root = document.documentElement
36-
const theme = root.getAttribute('data-theme')
37-
if (theme === 'system') {
38-
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
39-
}
40-
return theme || 'light'
41-
}
42-
43-
const updateGiscus = (attempt = 0) => {
44-
const giscusScript = document.querySelector('script[src^="https://giscus.app"]');
45-
const iframe = document.querySelector('iframe.giscus-frame');
60+
const updateGiscusTheme = () => {
61+
const iframe = document.querySelector('iframe.giscus-frame')
62+
if (!iframe || !iframe.contentWindow) return
4663

47-
if (!iframe || !giscusScript) {
48-
if (attempt < 10) setTimeout(() => updateGiscus(attempt + 1), 300);
49-
return;
50-
}
51-
52-
const effectiveTheme = getEffectiveTheme();
53-
54-
// 始终使用 noborder 主题
55-
const newGiscusTheme = `noborder_${effectiveTheme}`;
64+
const effectiveTheme =
65+
document.documentElement.classList.contains('dark') ? 'dark' : 'light'
66+
const newGiscusTheme = `noborder_${effectiveTheme}`
5667

5768
iframe.contentWindow.postMessage(
5869
{ giscus: { setConfig: { theme: newGiscusTheme } } },
5970
'https://giscus.app'
60-
);
71+
)
6172
}
6273

63-
// 初始化
64-
document.addEventListener('DOMContentLoaded', updateGiscus);
65-
window.addEventListener('load', updateGiscus);
66-
67-
// 监听主题切换完成事件
68-
document.addEventListener('theme-transition-complete', updateGiscus);
69-
})();
70-
</script>
74+
document.addEventListener('theme-transition-complete', updateGiscusTheme)
75+
})()
76+
</script>

0 commit comments

Comments
 (0)