Skip to content

Commit fc43054

Browse files
Revert "Revert Giscus Mutation Observer logic to load correct theme"
This reverts commit 9fa377b.
1 parent a1915e4 commit fc43054

File tree

1 file changed

+52
-16
lines changed

1 file changed

+52
-16
lines changed
Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,53 @@
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>
161
<input type="hidden" id="giscus-base-theme" value="<%- giscus.baseTheme %>">
17-
<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 loadGiscusWhenReady() {
5+
// Function to get the theme based on body class
6+
const getTheme = () => {
7+
const baseTheme = document.getElementById('giscus-base-theme').value;
8+
const altTheme = document.getElementById('giscus-alt-theme').value;
9+
return document.body.classList.contains('quarto-dark') ? altTheme : baseTheme;
10+
};
11+
12+
// Create the Giscus script and add it to the desired location
13+
const loadGiscus = () => {
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+
33+
// MutationObserver to detect when the 'quarto-light' or 'quarto-dark' class is added to the body
34+
const observer = new MutationObserver((mutations) => {
35+
for (const mutation of mutations) {
36+
if (mutation.type === "attributes" && mutation.attributeName === "class") {
37+
if (document.body.classList.contains('quarto-light') || document.body.classList.contains('quarto-dark')) {
38+
loadGiscus();
39+
observer.disconnect(); // Stop observing once Giscus is loaded
40+
break;
41+
}
42+
}
43+
}
44+
});
45+
46+
// Start observing the body for class attribute changes
47+
observer.observe(document.body, {
48+
attributes: true,
49+
attributeFilter: ["class"],
50+
});
51+
}
52+
loadGiscusWhenReady();
53+
</script>

0 commit comments

Comments
 (0)