-
-
Notifications
You must be signed in to change notification settings - Fork 377
fix(giscus): theme and language #783
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
e4ddf5e
c0f4a4f
a6d73cf
fb4506c
271ffa7
9bc5242
d4a94b0
1331e2a
2f587f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
imfing marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| 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 () { | ||
|
|
@@ -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> | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. curious where is this id from?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. The problem happens because the page contains 2 elements with the id
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| {{- else -}} | ||
| {{ warnf "giscus is not configured" }} | ||
| {{- end -}} | ||

Uh oh!
There was an error while loading. Please reload this page.