2025-08-24 23:06:14 +02:00
|
|
|
{{- $lang := site.Language.Lang | default `en` -}}
|
|
|
|
{{- if hasPrefix $lang "zh" -}}
|
|
|
|
{{- /* See: https://github.com/giscus/giscus/tree/main/locales */}}
|
|
|
|
{{- $lang = site.Language.LanguageCode | default `zh-CN` -}}
|
|
|
|
{{- end -}}
|
2023-09-27 04:47:22 +09:00
|
|
|
|
2023-09-26 23:33:27 +01:00
|
|
|
{{- with site.Params.comments.giscus -}}
|
2023-09-27 04:47:22 +09:00
|
|
|
<script>
|
2024-12-26 21:29:21 +09:00
|
|
|
function getGiscusTheme() {
|
2025-08-24 23:06:14 +02:00
|
|
|
const giscusTheme = '{{ .theme }}';
|
|
|
|
if (giscusTheme === 'light' || giscusTheme === 'dark') {
|
|
|
|
return giscusTheme;
|
|
|
|
}
|
|
|
|
|
|
|
|
const hugoTheme = localStorage.getItem("color-theme");
|
|
|
|
if (hugoTheme === 'light' || hugoTheme === 'dark') {
|
|
|
|
return hugoTheme;
|
2025-08-06 08:01:00 +08:00
|
|
|
}
|
2025-08-24 23:06:14 +02:00
|
|
|
|
|
|
|
if (hugoTheme === 'system') {
|
|
|
|
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
|
|
|
}
|
|
|
|
|
|
|
|
const defaultTheme = '{{ site.Params.theme.default }}';
|
|
|
|
if (defaultTheme === 'light' || defaultTheme === 'dark') {
|
|
|
|
return defaultTheme;
|
2024-12-26 21:29:21 +09:00
|
|
|
}
|
2025-08-24 23:06:14 +02:00
|
|
|
|
|
|
|
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
2024-12-26 21:29:21 +09:00
|
|
|
}
|
2023-09-27 04:47:22 +09:00
|
|
|
|
|
|
|
function setGiscusTheme() {
|
2025-08-24 23:06:14 +02:00
|
|
|
const iframe = document.querySelector('iframe.giscus-frame');
|
|
|
|
if (!iframe) return;
|
|
|
|
|
|
|
|
const msg = {
|
|
|
|
giscus: {
|
|
|
|
setConfig: {
|
|
|
|
theme: getGiscusTheme(),
|
|
|
|
},
|
2023-09-27 04:47:22 +09:00
|
|
|
},
|
2025-08-24 23:06:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
iframe.contentWindow.postMessage(msg, 'https://giscus.app');
|
2023-09-27 04:47:22 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
document.addEventListener('DOMContentLoaded', function () {
|
|
|
|
const giscusAttributes = {
|
|
|
|
"src": "https://giscus.app/client.js",
|
|
|
|
"data-repo": "{{ .repo }}",
|
|
|
|
"data-repo-id": "{{ .repoId }}",
|
|
|
|
"data-category": "{{ .category }}",
|
|
|
|
"data-category-id": "{{ .categoryId }}",
|
|
|
|
"data-mapping": "{{ .mapping | default `pathname` }}",
|
|
|
|
"data-strict": "{{ (string .strict) | default 0 }}",
|
|
|
|
"data-reactions-enabled": "{{ (string .reactionsEnabled) | default 1 }}",
|
|
|
|
"data-emit-metadata": "{{ (string .emitMetadata) | default 0 }}",
|
|
|
|
"data-input-position": "{{ .inputPosition | default `top` }}",
|
|
|
|
"data-theme": getGiscusTheme(),
|
|
|
|
"data-lang": "{{ .lang | default $lang }}",
|
|
|
|
"crossorigin": "anonymous",
|
|
|
|
"async": "",
|
|
|
|
};
|
|
|
|
|
|
|
|
// Dynamically create script tag
|
|
|
|
const giscusScript = document.createElement("script");
|
|
|
|
Object.entries(giscusAttributes).forEach(([key, value]) => giscusScript.setAttribute(key, value));
|
2025-08-24 23:06:14 +02:00
|
|
|
// Random hash id to avoid conflicts with titles inside pages.
|
|
|
|
document.getElementById('giscus-hextra-bb112b9f807c37c1752e5da6a1652a29').appendChild(giscusScript);
|
|
|
|
|
|
|
|
// Listen for system theme changes
|
|
|
|
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", setGiscusTheme);
|
2023-09-27 04:47:22 +09:00
|
|
|
|
|
|
|
// Update giscus theme when theme switcher is clicked
|
2025-08-24 23:06:14 +02:00
|
|
|
const themeToggleOptions = document.querySelectorAll(".hextra-theme-toggle-options p");
|
|
|
|
if (themeToggleOptions) {
|
|
|
|
themeToggleOptions.forEach(toggle => toggle.addEventListener('click', setGiscusTheme));
|
2023-09-27 04:47:22 +09:00
|
|
|
}
|
|
|
|
});
|
|
|
|
</script>
|
2023-09-26 22:12:38 +01:00
|
|
|
|
2025-08-24 23:06:14 +02:00
|
|
|
<div id="giscus-hextra-bb112b9f807c37c1752e5da6a1652a29"></div>
|
2023-09-26 23:33:27 +01:00
|
|
|
{{- else -}}
|
|
|
|
{{ warnf "giscus is not configured" }}
|
2023-09-27 04:47:22 +09:00
|
|
|
{{- end -}}
|