Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions src/resources/formats/html/giscus/giscus.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,25 @@ function loadGiscusWhenReady() {
document.getElementById("quarto-content").appendChild(script);
};

let observer;
const loadIfBodyReady = () => {
// Check if the body has the 'quarto-light' or 'quarto-dark' class
if (!(document.body.classList.contains('quarto-light') || document.body.classList.contains('quarto-dark'))) {
return false;
}
loadGiscus();
observer.disconnect();
return true;
};

// MutationObserver to detect when the 'quarto-light' or 'quarto-dark' class is added to the body
const observer = new MutationObserver((mutations) => {
observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
if (mutation.type === "attributes" && mutation.attributeName === "class") {
if (document.body.classList.contains('quarto-light') || document.body.classList.contains('quarto-dark')) {
loadGiscus();
observer.disconnect(); // Stop observing once Giscus is loaded
break;
if (mutation.type === "attributes" &&
mutation.attributeName === "class" &&
loadIfBodyReady()) {
break; // Stop observing if Giscus is loaded
}
}
}
});

Expand All @@ -48,6 +57,8 @@ function loadGiscusWhenReady() {
attributes: true,
attributeFilter: ["class"],
});

loadIfBodyReady(); // Initial check in case the class is already present
}
loadGiscusWhenReady();
</script>
Loading