diff options
| author | Alejandro Soto <alejandro@34project.org> | 2025-09-13 15:01:06 -0600 |
|---|---|---|
| committer | Alejandro Soto <alejandro@34project.org> | 2025-09-13 15:01:06 -0600 |
| commit | f4fcda54638685899c730b3fa90a87d80d6dbef5 (patch) | |
| tree | 0737e627cce304c3a9c4e757bc5f6571a7456091 /sysret.org/themes/tabi-lean/static/js/giscus.js | |
| parent | d8b9cf1f61cc07d625f1c37ccc28adfd58918416 (diff) | |
| parent | 2c13119932765c6d788f08fb53abc244407c0d80 (diff) | |
Merge commit '6a7d3111b31e73fc66af5360149d41f690fbcaa4'
Diffstat (limited to 'sysret.org/themes/tabi-lean/static/js/giscus.js')
| -rw-r--r-- | sysret.org/themes/tabi-lean/static/js/giscus.js | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/sysret.org/themes/tabi-lean/static/js/giscus.js b/sysret.org/themes/tabi-lean/static/js/giscus.js new file mode 100644 index 0000000..1fbe837 --- /dev/null +++ b/sysret.org/themes/tabi-lean/static/js/giscus.js @@ -0,0 +1,81 @@ +function setGiscusTheme(newTheme) { + // Get the giscus iframe. + const frame = document.querySelector('iframe.giscus-frame'); + + if (frame) { + // If the iframe exists, send a message to set the theme. + frame.contentWindow.postMessage( + { giscus: { setConfig: { theme: newTheme } } }, + 'https://giscus.app' + ); + } +} + +// Function to initialize Giscus. This function is run when the window loads. +function initGiscus() { + // Get the div that will contain the comments. + const commentsDiv = document.querySelector('.comments'); + if (commentsDiv) { + // Get the various settings from data attributes on the div. + const repo = commentsDiv.getAttribute('data-repo'); + const repoId = commentsDiv.getAttribute('data-repo-id'); + const category = commentsDiv.getAttribute('data-category'); + const categoryId = commentsDiv.getAttribute('data-category-id'); + const strictTitleMatching = commentsDiv.getAttribute('data-strict'); + const term = commentsDiv.getAttribute('data-term'); + const reactionsEnabled = commentsDiv.getAttribute('data-reactions-enabled'); + const inputPosition = commentsDiv.getAttribute('data-input-position'); + const lightTheme = commentsDiv.getAttribute('data-light-theme'); + const darkTheme = commentsDiv.getAttribute('data-dark-theme'); + const lang = commentsDiv.getAttribute('data-lang'); + const lazyLoading = commentsDiv.getAttribute('data-lazy-loading'); + + // Create a new script tag that will load the Giscus script. + const script = document.createElement('script'); + script.src = 'https://giscus.app/client.js'; + script.async = true; + + // Set the various settings as data attributes on the script tag. + script.setAttribute('data-repo', repo); + script.setAttribute('data-repo-id', repoId); + script.setAttribute('data-category', category); + script.setAttribute('data-category-id', categoryId); + script.setAttribute('data-term', term); + script.setAttribute('data-strict', strictTitleMatching); + script.setAttribute('data-reactions-enabled', reactionsEnabled); + script.setAttribute('data-emit-metadata', '0'); + script.setAttribute('data-input-position', inputPosition); + script.setAttribute('data-lang', lang); + script.setAttribute('crossorigin', 'anonymous'); + + // Set the mapping if it is provided. + const mapping = commentsDiv.getAttribute('data-mapping'); + if (mapping) { + script.setAttribute('data-mapping', mapping); + } + + // Choose the correct theme based on the current theme of the document. + const currentTheme = + document.documentElement.getAttribute('data-theme') || 'light'; + const selectedTheme = currentTheme === 'dark' ? darkTheme : lightTheme; + script.setAttribute('data-theme', selectedTheme); + + // Set the loading attribute if lazy loading is enabled. + if (lazyLoading === 'true') { + script.setAttribute('data-loading', 'lazy'); + } + + // Add the script tag to the div. + commentsDiv.appendChild(script); + + // Listen for theme changes and update the Giscus theme when they occur. + window.addEventListener('themeChanged', (event) => { + const selectedTheme = + event.detail.theme === 'dark' ? darkTheme : lightTheme; + setGiscusTheme(selectedTheme); + }); + } +} + +// Initialize Giscus. +initGiscus(); |
