Merge. Bug fixes for <code> and ThemeSwitch for Giscus

Feature main
This commit is contained in:
Junyi 2024-03-18 16:59:26 +08:00 committed by GitHub
commit 05d41821bb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 75 additions and 18 deletions

View File

@ -17,7 +17,7 @@
--nav-link-color: #b6b6b6;
--pre-color: #f8f8f2;
--pre-bg-color: rgba(175, 184, 193, 0.1);
--pre-bg-color: #292b2d;
--pre-border-color: rgba(175, 184, 193, 0.3);
--bq-color: #ccc;
@ -55,7 +55,7 @@ html[data-theme='dark'] {
--nav-link-color: #b6b6b6;
--pre-color: #f8f8f2;
--pre-bg-color: rgba(175, 184, 193, 0.1);
--pre-bg-color: #292b2d;
--pre-border-color: rgba(175, 184, 193, 0.3);
--bq-color: #ccc;

View File

@ -16,8 +16,8 @@ html[data-theme='light'] {
--nav-link-color: #696969;
--pre-color: rgb(31, 35, 40);
--pre-bg-color: rgba(175, 184, 193, 0.2);
--pre-border-color: rgba(175, 184, 193, 0.3);
--pre-bg-color: #eff1f2;
--pre-border-color: #e1e5e9;
--bq-color: #ccc;
--hr-color: #ccc;

View File

@ -188,6 +188,7 @@ kbd {
pre code, pre kbd {
color: inherit;
background-color: inherit;
border: none;
}
/* Styles */

View File

@ -4,6 +4,42 @@
{{ $category := .Site.Params.GiscusDiscussionCategory | default "Announcements" }}
{{ $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"
data-repo="{{- .Site.Params.GiscusRepo -}}"
data-repo-id="{{- .Site.Params.GiscusRepoId -}}"
@ -21,4 +57,4 @@
data-loading="lazy"
{{ end }}
async>
</script>
</script> */}}

View File

@ -18,7 +18,7 @@
<script>
const STORAGE_KEY = 'user-color-scheme'
const defaultTheme = {{ $colorTheme }}
const defaultTheme = "{{ $colorTheme }}"
let currentTheme
let switchButton
@ -44,7 +44,7 @@
switchButton.addEventListener('click', switchTheme, false)
}
showContent()
showContent();
})
function detectCurrentScheme() {
@ -61,11 +61,28 @@
currentTheme = (currentTheme === 'dark') ? 'light' : 'dark';
if (localStorage) localStorage.setItem(STORAGE_KEY, currentTheme);
document.documentElement.setAttribute('data-theme', currentTheme);
changeGiscusTheme();
}
function showContent() {
document.body.style.visibility = 'visible';
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>
{{ end }}

View File

@ -17,16 +17,19 @@
<script>
document.querySelector(".toc").addEventListener("click", function () {
if (event.target.tagName !== "A") {
event.preventDefault();
if (this.open) {
this.open = false;
this.classList.remove("expanded");
} else {
this.open = true;
this.classList.add("expanded");
var toc = document.querySelector(".toc");
if (toc) {
toc.addEventListener("click", function () {
if (event.target.tagName !== "A") {
event.preventDefault();
if (this.open) {
this.open = false;
this.classList.remove("expanded");
} else {
this.open = true;
this.classList.add("expanded");
}
}
}
});
});
}
</script>