1
0
mirror of https://github.com/cotes2020/jekyll-theme-chirpy.git synced 2026-06-22 07:48:42 +00:00
Files
Cotes Chung 7496dd41fa feat(theme): persist user theme preferences (#2756)
- Migrate theme persistence from `sessionStorage` to `localStorage`
- Rename theme HTML attribute to `data-bs-theme` for Bootstrap compatibility
- Trim and compile CSS according to the chosen theme mode
2026-06-17 23:20:12 +08:00

66 lines
2.2 KiB
HTML

<!-- https://giscus.app/ -->
<script>
(function () {
const themeMap = Theme.newThemeMap('light', 'dark_dimmed');
const initTheme = themeMap[Theme.resolvedTheme];
let lang = '{{ site.comments.giscus.lang | default: lang }}';
{%- comment -%} https://github.com/giscus/giscus/tree/main/locales {%- endcomment -%}
if (lang.length > 2 && !lang.startsWith('zh')) {
lang = lang.slice(0, 2);
}
let giscusAttributes = {
src: 'https://giscus.app/client.js',
'data-repo': '{{ site.comments.giscus.repo}}',
'data-repo-id': '{{ site.comments.giscus.repo_id }}',
'data-category': '{{ site.comments.giscus.category }}',
'data-category-id': '{{ site.comments.giscus.category_id }}',
'data-mapping': '{{ site.comments.giscus.mapping | default: 'pathname' }}',
'data-strict' : '{{ site.comments.giscus.strict | default: '0' }}',
'data-reactions-enabled': '{{ site.comments.giscus.reactions_enabled | default: '1' }}',
'data-emit-metadata': '0',
'data-theme': initTheme,
'data-input-position': '{{ site.comments.giscus.input_position | default: 'bottom' }}',
'data-lang': lang,
'data-loading': 'lazy',
crossorigin: 'anonymous',
async: ''
};
let giscusNode = document.createElement('script');
Object.entries(giscusAttributes).forEach(([key, value]) =>
giscusNode.setAttribute(key, value)
);
const $footer = document.querySelector('footer');
$footer.insertAdjacentElement("beforebegin", giscusNode);
addEventListener('message', (event) => {
if (event.source === window && event.data && event.data.id === Theme.eventId) {
const newTheme = themeMap[Theme.resolvedTheme];
const message = {
setConfig: {
theme: newTheme
}
};
const iframe = document.querySelector('.giscus-frame');
if (!iframe) {
return;
}
if (iframe.classList.contains('giscus-frame--loading')) {
let url = new URL(iframe.src);
url.searchParams.set('theme', newTheme);
iframe.src = url.toString();
}
iframe.contentWindow.postMessage({ giscus: message }, 'https://giscus.app');
}
});
})();
</script>