|
| 1 | +<!-- https://giscus.app/ --> |
| 2 | +<script type="text/javascript"> |
| 3 | + (function () { |
| 4 | + const origin = 'https://giscus.app'; |
| 5 | + const lightTheme = 'light'; |
| 6 | + const darkTheme = 'dark_dimmed'; |
| 7 | + |
| 8 | + let initTheme = lightTheme; |
| 9 | + const html = document.documentElement; |
| 10 | + |
| 11 | + if ( |
| 12 | + (html.hasAttribute('data-mode') && |
| 13 | + html.getAttribute('data-mode') === 'dark') || |
| 14 | + (!html.hasAttribute('data-mode') && |
| 15 | + window.matchMedia('(prefers-color-scheme: dark)').matches) |
| 16 | + ) { |
| 17 | + initTheme = darkTheme; |
| 18 | + } |
| 19 | + |
| 20 | + let lang = '{{ site.comments.giscus.lang | default: lang }}'; |
| 21 | + {%- comment -%} https://github.com/giscus/giscus/tree/main/locales {%- endcomment -%} |
| 22 | + if (lang.length > 2 && !lang.startsWith('zh')) { |
| 23 | + lang = lang.slice(0, 2); |
| 24 | + } |
| 25 | + |
| 26 | + let giscusAttributes = { |
| 27 | + src: 'https://giscus.app/client.js', |
| 28 | + 'data-repo': '{{ site.comments.giscus.repo}}', |
| 29 | + 'data-repo-id': '{{ site.comments.giscus.repo_id }}', |
| 30 | + 'data-category': '{{ site.comments.giscus.category }}', |
| 31 | + 'data-category-id': '{{ site.comments.giscus.category_id }}', |
| 32 | + 'data-mapping': '{{ site.comments.giscus.mapping | default: 'pathname' }}', |
| 33 | + 'data-strict' : '{{ site.comments.giscus.strict | default: '0' }}', |
| 34 | + 'data-reactions-enabled': '{{ site.comments.giscus.reactions_enabled | default: '1' }}', |
| 35 | + 'data-emit-metadata': '0', |
| 36 | + 'data-theme': initTheme, |
| 37 | + 'data-input-position': '{{ site.comments.giscus.input_position | default: 'bottom' }}', |
| 38 | + 'data-lang': lang, |
| 39 | + 'data-loading': 'lazy', |
| 40 | + crossorigin: 'anonymous', |
| 41 | + async: '' |
| 42 | + }; |
| 43 | + |
| 44 | + let giscusScript = document.createElement('script'); |
| 45 | + Object.entries(giscusAttributes).forEach(([key, value]) => |
| 46 | + giscusScript.setAttribute(key, value) |
| 47 | + ); |
| 48 | + document.getElementById('tail-wrapper').appendChild(giscusScript); |
| 49 | + |
| 50 | + addEventListener('message', (event) => { |
| 51 | + if ( |
| 52 | + event.source === window && |
| 53 | + event.data && |
| 54 | + event.data.direction === ModeToggle.ID |
| 55 | + ) { |
| 56 | + {%- comment -%} global theme mode changed {%- endcomment -%} |
| 57 | + const mode = event.data.message; |
| 58 | + const theme = mode === ModeToggle.DARK_MODE ? darkTheme : lightTheme; |
| 59 | + |
| 60 | + const message = { |
| 61 | + setConfig: { |
| 62 | + theme: theme |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + const giscus = document.getElementsByClassName('giscus-frame')[0].contentWindow; |
| 67 | + giscus.postMessage({ giscus: message }, origin); |
| 68 | + } |
| 69 | + }); |
| 70 | + })(); |
| 71 | +</script> |
0 commit comments