Skip to content

Commit c42a477

Browse files
cscheidgordonwoodhull
authored andcommitted
giscus - load immediately if light or dark mode classes present
1 parent fc43054 commit c42a477

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/resources/formats/html/giscus/giscus.ejs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,25 @@ function loadGiscusWhenReady() {
3030
document.getElementById("quarto-content").appendChild(script);
3131
};
3232
33+
let observer;
34+
const loadIfBodyReady = () => {
35+
// Check if the body has the 'quarto-light' or 'quarto-dark' class
36+
if (!(document.body.classList.contains('quarto-light') || document.body.classList.contains('quarto-dark'))) {
37+
return false;
38+
}
39+
loadGiscus();
40+
observer.disconnect();
41+
return true;
42+
};
43+
3344
// MutationObserver to detect when the 'quarto-light' or 'quarto-dark' class is added to the body
34-
const observer = new MutationObserver((mutations) => {
45+
observer = new MutationObserver((mutations) => {
3546
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;
47+
if (mutation.type === "attributes" &&
48+
mutation.attributeName === "class" &&
49+
loadIfBodyReady()) {
50+
break; // Stop observing if Giscus is loaded
4151
}
42-
}
4352
}
4453
});
4554
@@ -48,6 +57,8 @@ function loadGiscusWhenReady() {
4857
attributes: true,
4958
attributeFilter: ["class"],
5059
});
60+
61+
loadIfBodyReady(); // Initial check in case the class is already present
5162
}
5263
loadGiscusWhenReady();
5364
</script>

0 commit comments

Comments
 (0)