Skip to content

Commit 74a48ba

Browse files
simple dynamic load of giscus
generate script tag based on user preference at page load no need for mutation observer because class is already set
1 parent 73f8602 commit 74a48ba

File tree

1 file changed

+32
-36
lines changed

1 file changed

+32
-36
lines changed
Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,33 @@
1-
<script src="https://giscus.app/client.js"
2-
data-repo="<%- giscus.repo %>"
3-
data-repo-id="<%- giscus['repo-id'] %>"
4-
data-category="<%- giscus.category %>"
5-
data-category-id="<%- giscus['category-id'] %>"
6-
data-mapping="<%- giscus.mapping %>"
7-
data-reactions-enabled="<%- giscus['reactions-enabled'] ? 1 : 0 %>"
8-
data-emit-metadata="0"
9-
data-input-position="<%- giscus['input-position'] %>"
10-
data-theme="<%- giscus.theme %>"
11-
data-lang="<%- giscus.language %>"
12-
crossorigin="anonymous"
13-
<%- giscus.loading ? `data-loading=${giscus.loading}` : '' %>
14-
async>
15-
</script>
16-
<script type="application/javascript">
17-
const giscusIframeObserver = new MutationObserver(function (mutations) {
18-
mutations.forEach(function (mutation) {
19-
mutation.addedNodes.forEach(function (addedNode) {
20-
if (addedNode.matches && addedNode.matches('div.giscus')) {
21-
const giscusIframe = addedNode.querySelector('iframe.giscus-frame');
22-
if(giscusIframe) {
23-
giscusIframe.addEventListener("load", function() {
24-
window.setTimeout(() => {
25-
toggleGiscusIfUsed(hasAlternateSentinel(), authorPrefersDark);
26-
}, 100);
27-
});
28-
giscusIframeObserver.disconnect();
29-
}
30-
}
31-
});
32-
});
33-
});
34-
giscusIframeObserver.observe(document.body, { childList: true, subtree: true });
35-
</script>
361
<input type="hidden" id="giscus-base-theme" value="<%- giscus.baseTheme %>">
37-
<input type="hidden" id="giscus-alt-theme" value="<%- giscus.altTheme %>">
2+
<input type="hidden" id="giscus-alt-theme" value="<%- giscus.altTheme %>">
3+
<script>
4+
function loadGiscus() {
5+
// Function to get the theme based on body class
6+
const getTheme = () => {
7+
let baseTheme = document.getElementById('giscus-base-theme').value;
8+
let altTheme = document.getElementById('giscus-alt-theme').value;
9+
if (authorPrefersDark) {
10+
[baseTheme, altTheme] = [altTheme, baseTheme];
11+
}
12+
return document.body.classList.contains('quarto-dark') ? altTheme : baseTheme;
13+
};
14+
const script = document.createElement("script");
15+
script.src = "https://giscus.app/client.js";
16+
script.async = true;
17+
script.dataset.repo = "<%- giscus.repo %>";
18+
script.dataset.repoId = "<%- giscus['repo-id'] %>";
19+
script.dataset.category = "<%- giscus.category %>";
20+
script.dataset.categoryId = "<%- giscus['category-id'] %>";
21+
script.dataset.mapping = "<%- giscus.mapping %>";
22+
script.dataset.reactionsEnabled = "<%- giscus['reactions-enabled'] ? 1 : 0 %>";
23+
script.dataset.emitMetadata = "0";
24+
script.dataset.inputPosition = "<%- giscus['input-position'] %>";
25+
script.dataset.theme = getTheme();
26+
script.dataset.lang = "<%- giscus.language %>";
27+
script.crossOrigin = "anonymous";
28+
29+
// Append the script to the desired div instead of at the end of the body
30+
document.getElementById("quarto-content").appendChild(script);
31+
}
32+
loadGiscus();
33+
</script>

0 commit comments

Comments
 (0)