diff options
| author | Eltrac <hi@guhub.cn> | 2025-04-17 18:23:50 +0800 |
|---|---|---|
| committer | Eltrac <hi@guhub.cn> | 2025-04-17 18:23:50 +0800 |
| commit | 7505950e10ba70193bef86b1876df1e5522de1b9 (patch) | |
| tree | 4f74f1108372a5810de9731cec1493268ee1ace6 | |
| parent | 45faa5d31fb781a5adc02a75be68cfd217c6f4ab (diff) | |
feat: use noborder theme for giscus
add scripts sending message to giscus to dynamically change themes for different color schemes
| -rw-r--r-- | hugo.yaml | 2 | ||||
| -rw-r--r-- | layouts/partials/components/comment.html | 3 | ||||
| -rw-r--r-- | layouts/partials/scripts.html | 41 |
3 files changed, 44 insertions, 2 deletions
@@ -107,7 +107,7 @@ params: reactionsEnabled: 0 emitMetadata: 0 inputPosition: top - theme: preferred_color_scheme + theme: noborder_light services: rss: limit: 10 diff --git a/layouts/partials/components/comment.html b/layouts/partials/components/comment.html index b0b4ed47..cfdfa886 100644 --- a/layouts/partials/components/comment.html +++ b/layouts/partials/components/comment.html @@ -29,7 +29,8 @@ <p style="text-align:center">你也可以加入本站的 <a href="https://t.me/teaparty418" target="_blank">Telegram 群组</a>参与讨论</p> {{ end }} <!-- giscus script --> - <script src="https://giscus.app/client.js" + <script id="giscus-script" + src="https://giscus.app/client.js" data-repo="{{ .Site.Params.giscus.repo }}" data-repo-id="{{ .Site.Params.giscus.repoId }}" data-category="{{ .Site.Params.giscus.category }}" diff --git a/layouts/partials/scripts.html b/layouts/partials/scripts.html index dbf6aee0..90f99630 100644 --- a/layouts/partials/scripts.html +++ b/layouts/partials/scripts.html @@ -20,4 +20,45 @@ littlefoot.littlefoot({ dismissOnUnhover: true }); + + /* == auto-switch giscus theme == */ + + // Function to send the theme change to the Giscus iframe + function changeGiscusTheme() { + const theme = document.documentElement.getAttribute('data-theme') === 'noborder_dark' ? 'noborder_dark' : 'noborder_light'; + + function sendMessage(message) { + const iframe = document.querySelector('iframe.giscus-frame'); + if (!iframe) return; + iframe.contentWindow.postMessage({ giscus: message }, 'https://giscus.app'); + } + + // Send the theme change to Giscus iframe + sendMessage({ + setConfig: { + theme: theme + } + }); + } + + // Detect the system's color scheme (light or dark) + function updateTheme() { + const prefersDarkScheme = window.matchMedia("(prefers-color-scheme: dark)").matches; + + // Update the theme of the page (this affects your custom styles) + if (prefersDarkScheme) { + document.documentElement.setAttribute('data-theme', 'noborder_dark'); + } else { + document.documentElement.setAttribute('data-theme', 'noborder_light'); + } + + // Change the Giscus iframe theme accordingly + changeGiscusTheme(); + } + + // Run the theme update function on page load + window.addEventListener('load', updateTheme); + + // Re-run when the system theme changes + window.matchMedia("(prefers-color-scheme: dark)").addEventListener('change', updateTheme); </script> |
