feat(comments): add lang option for giscus (#498)

This commit is contained in:
zhixuan 2022-03-03 05:31:44 +09:00 committed by GitHub
parent 98923b1dd7
commit 24cd07589b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 17 deletions

View file

@ -95,6 +95,7 @@ params:
darkTheme: darkTheme:
reactionsEnabled: 1 reactionsEnabled: 1
emitMetadata: 0 emitMetadata: 0
lang:
gitalk: gitalk:
owner: owner:

View file

@ -9,41 +9,41 @@
data-reactions-enabled="{{- default 1 .reactionsEnabled -}}" data-reactions-enabled="{{- default 1 .reactionsEnabled -}}"
data-emit-metadata="{{- default 0 .emitMetadata -}}" data-emit-metadata="{{- default 0 .emitMetadata -}}"
data-theme="{{- default `light` .lightTheme -}}" data-theme="{{- default `light` .lightTheme -}}"
data-lang="{{- default `en` .lang -}}"
crossorigin="anonymous" crossorigin="anonymous"
async async
></script> ></script>
<script> <script>
function setGiscusTheme(theme) { function setGiscusTheme(theme) {
let giscus = document.querySelector('iframe.giscus-frame'); let giscus = document.querySelector("iframe.giscus-frame");
if (giscus) { if (giscus) {
giscus.contentWindow.postMessage( giscus.contentWindow.postMessage(
{ {
giscus: { giscus: {
setConfig: { setConfig: {
theme: theme theme: theme,
} },
} },
}, },
"https://giscus.app" "https://giscus.app"
); );
}; }
}; }
(function(){ (function () {
addEventListener('message', (e) => { addEventListener("message", (e) => {
if (event.origin !== 'https://giscus.app') return; if (event.origin !== "https://giscus.app") return;
handler() handler();
}); });
window.addEventListener('onColorSchemeChange', handler); window.addEventListener("onColorSchemeChange", handler);
function handler() { function handler() {
if (document.documentElement.dataset.scheme === "light") { if (document.documentElement.dataset.scheme === "light") {
setGiscusTheme('{{- default "light" .lightTheme -}}'); setGiscusTheme('{{- default "light" .lightTheme -}}');
} else { } else {
setGiscusTheme('{{- default "dark_dimmed" .darkTheme -}}'); setGiscusTheme('{{- default "dark_dimmed" .darkTheme -}}');
}; }
}; }
}()); })();
</script> </script>
{{- end -}} {{- end -}}