add: support theme switch for giscus

This commit is contained in:
Junyi Hou 2024-03-18 16:58:43 +08:00
parent 3cd15e9a35
commit 74015da3e5
2 changed files with 56 additions and 3 deletions

View File

@ -4,6 +4,42 @@
{{ $category := .Site.Params.GiscusDiscussionCategory | default "Announcements" }} {{ $category := .Site.Params.GiscusDiscussionCategory | default "Announcements" }}
{{ $lazyload := .Site.Params.GiscusLazyLoad | default false }} {{ $lazyload := .Site.Params.GiscusLazyLoad | default false }}
<script>
function detectCurrentScheme() {
if (localStorage !== null && localStorage.getItem('user-color-scheme')) {
return localStorage.getItem('user-color-scheme')
}
if (defaultTheme) {
return defaultTheme
}
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
}
let giscusTheme = detectCurrentScheme();
let giscusAttributes = {
"src": "https://giscus.app/client.js",
"data-repo": "{{- .Site.Params.GiscusRepo -}}",
"data-repo-id": "{{- .Site.Params.GiscusRepoId -}}",
"data-category": "{{- $category -}}",
"data-category-id": "{{- .Site.Params.GiscusCategoryId -}}",
"data-mapping": "{{ $mapping }}",
"data-strict": "0",
"data-reactions-enabled": "1",
"data-emit-metadata": "0",
"data-input-position": "bottom",
"data-theme": giscusTheme,
"data-lang": "{{ $language }}",
"crossorigin": "anonymous",
"lazyload": "{{ $lazyload }}",
"async": true
}
let main = document.querySelector('main');
let giscusScript = document.createElement('script');
Object.entries(giscusAttributes).forEach(([key, value]) => giscusScript.setAttribute(key, value));
main.appendChild(giscusScript);
</script>
{{/*
<script src="https://giscus.app/client.js" <script src="https://giscus.app/client.js"
data-repo="{{- .Site.Params.GiscusRepo -}}" data-repo="{{- .Site.Params.GiscusRepo -}}"
data-repo-id="{{- .Site.Params.GiscusRepoId -}}" data-repo-id="{{- .Site.Params.GiscusRepoId -}}"
@ -21,4 +57,4 @@
data-loading="lazy" data-loading="lazy"
{{ end }} {{ end }}
async> async>
</script> </script> */}}

View File

@ -18,7 +18,7 @@
<script> <script>
const STORAGE_KEY = 'user-color-scheme' const STORAGE_KEY = 'user-color-scheme'
const defaultTheme = {{ $colorTheme }} const defaultTheme = "{{ $colorTheme }}"
let currentTheme let currentTheme
let switchButton let switchButton
@ -44,7 +44,7 @@
switchButton.addEventListener('click', switchTheme, false) switchButton.addEventListener('click', switchTheme, false)
} }
showContent() showContent();
}) })
function detectCurrentScheme() { function detectCurrentScheme() {
@ -61,11 +61,28 @@
currentTheme = (currentTheme === 'dark') ? 'light' : 'dark'; currentTheme = (currentTheme === 'dark') ? 'light' : 'dark';
if (localStorage) localStorage.setItem(STORAGE_KEY, currentTheme); if (localStorage) localStorage.setItem(STORAGE_KEY, currentTheme);
document.documentElement.setAttribute('data-theme', currentTheme); document.documentElement.setAttribute('data-theme', currentTheme);
changeGiscusTheme();
} }
function showContent() { function showContent() {
document.body.style.visibility = 'visible'; document.body.style.visibility = 'visible';
document.body.style.opacity = 1; document.body.style.opacity = 1;
} }
function changeGiscusTheme () {
const theme = detectCurrentScheme();
function sendMessage(message) {
const iframe = document.querySelector('iframe.giscus-frame');
if (!iframe) return;
iframe.contentWindow.postMessage({ giscus: message }, 'https://giscus.app');
}
sendMessage({
setConfig: {
theme: theme
}
});
}
</script> </script>
{{ end }} {{ end }}