Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion exampleSite/hugo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ languages:
title: هگزترا
ja:
languageName: 日本語
languageCode: ja
languageCode: ja-JP
weight: 3
title: Hextra
zh-cn:
Expand Down
74 changes: 42 additions & 32 deletions layouts/_partials/components/giscus.html
Original file line number Diff line number Diff line change
@@ -1,40 +1,47 @@
{{- $lang := site.Language.LanguageCode | default `en` -}}
{{- $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 -}}

{{- with site.Params.comments.giscus -}}
<script>
/*
* "preferred color scheme" theme in giscus works using "prefers-color-scheme" in media query.
* but, hugo's theme switch function works by using "color-theme" in local storage.
* This solution was created with reference to:
* https://github.com/giscus/giscus/issues/336#issuecomment-1214366281
*/
function getHugoTheme() {
return localStorage.getItem("color-theme");
}

function getGiscusTheme() {
let giscusTheme = "{{ (string .theme) | default `light` }}";
let hugoTheme = getHugoTheme();
if(hugoTheme == 'light') {
return giscusTheme.replace('dark', 'light');
const giscussTheme = '{{ .theme }}';
if (giscussTheme === 'light' || giscussTheme === 'dark') {
return giscussTheme;
}

const hugoTheme = localStorage.getItem("color-theme");
if (hugoTheme === 'light' || hugoTheme === 'dark') {
return hugoTheme;
}
if(hugoTheme == 'dark') {
return giscusTheme.replace('light', 'dark');

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;
}
return giscusTheme;

return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
}

function setGiscusTheme() {
function sendMessage(message) {
const iframe = document.querySelector('iframe.giscus-frame');
if (!iframe) return;
iframe.contentWindow.postMessage({ giscus: message }, 'https://giscus.app');
}
sendMessage({
setConfig: {
theme: getGiscusTheme(),
const iframe = document.querySelector('iframe.giscus-frame');
if (!iframe) return;

const msg = {
giscus: {
setConfig: {
theme: getGiscusTheme(),
},
},
});
}

iframe.contentWindow.postMessage(msg, 'https://giscus.app');
}

document.addEventListener('DOMContentLoaded', function () {
Expand All @@ -58,17 +65,20 @@
// Dynamically create script tag
const giscusScript = document.createElement("script");
Object.entries(giscusAttributes).forEach(([key, value]) => giscusScript.setAttribute(key, value));
document.getElementById('giscus').appendChild(giscusScript);
document.getElementById('giscus-hextra-bb112b9f807c37c1752e5da6a1652a29').appendChild(giscusScript);

// Listen for system theme changes
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", setGiscusTheme);

// Update giscus theme when theme switcher is clicked
const toggles = document.querySelectorAll(".hextra-theme-toggle");
if (toggles) {
toggles.forEach(toggle => toggle.addEventListener('click', setGiscusTheme));
const themeToggleOptions = document.querySelectorAll(".hextra-theme-toggle-options p");
if (themeToggleOptions) {
themeToggleOptions.forEach(toggle => toggle.addEventListener('click', setGiscusTheme));
}
});
</script>

<div id="giscus"></div>
<div id="giscus-hextra-bb112b9f807c37c1752e5da6a1652a29"></div>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curious where is this id from?

Copy link
Contributor Author

@ldez ldez Aug 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a random hash to be sure to have a unique ID.
Currently, without my PR, if you enable Giscuss and go to the page http://localhost:1313/docs/advanced/comments/ you will see the problem.

The problem happens because the page contains 2 elements with the id giscuss: a title inside the page and the Giscuss div at the bottom of the page.

Details Screenshot 2025-08-24 at 22-50-17 Comments System – Hextra

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thanks for the explanation. Might worth adding a line of comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

{{- else -}}
{{ warnf "giscus is not configured" }}
{{- end -}}