Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update giscus.ejs to fix giscus color on load #11047

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
68 changes: 52 additions & 16 deletions src/resources/formats/html/giscus/giscus.ejs
Original file line number Diff line number Diff line change
@@ -1,17 +1,53 @@
<script src="https://giscus.app/client.js"
data-repo="<%- giscus.repo %>"
data-repo-id="<%- giscus['repo-id'] %>"
data-category="<%- giscus.category %>"
data-category-id="<%- giscus['category-id'] %>"
data-mapping="<%- giscus.mapping %>"
data-reactions-enabled="<%- giscus['reactions-enabled'] ? 1 : 0 %>"
data-emit-metadata="0"
data-input-position="<%- giscus['input-position'] %>"
data-theme="<%- giscus.theme %>"
data-lang="<%- giscus.language %>"
crossorigin="anonymous"
<%- giscus.loading ? `data-loading=${giscus.loading}` : '' %>
async>
</script>
<input type="hidden" id="giscus-base-theme" value="<%- giscus.baseTheme %>">
<input type="hidden" id="giscus-alt-theme" value="<%- giscus.altTheme %>">
<input type="hidden" id="giscus-alt-theme" value="<%- giscus.altTheme %>">
<script>
function loadGiscusWhenReady() {
// Function to get the theme based on body class
const getTheme = () => {
const baseTheme = document.getElementById('giscus-base-theme').value;
const altTheme = document.getElementById('giscus-alt-theme').value;
return document.body.classList.contains('quarto-dark') ? altTheme : baseTheme;
};

// Create the Giscus script and add it to the desired location
const loadGiscus = () => {
const script = document.createElement("script");
script.src = "https://giscus.app/client.js";
script.async = true;
script.dataset.repo = "<%- giscus.repo %>";
script.dataset.repoId = "<%- giscus['repo-id'] %>";
script.dataset.category = "<%- giscus.category %>";
script.dataset.categoryId = "<%- giscus['category-id'] %>";
script.dataset.mapping = "<%- giscus.mapping %>";
script.dataset.reactionsEnabled = "<%- giscus['reactions-enabled'] ? 1 : 0 %>";
script.dataset.emitMetadata = "0";
script.dataset.inputPosition = "<%- giscus['input-position'] %>";
script.dataset.theme = getTheme();
script.dataset.lang = "<%- giscus.language %>";
script.crossOrigin = "anonymous";

// Append the script to the desired div instead of at the end of the body
document.getElementById("quarto-content").appendChild(script);
};

// MutationObserver to detect when the 'quarto-light' or 'quarto-dark' class is added to the body
const 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;
}
}
}
});

// Start observing the body for class attribute changes
observer.observe(document.body, {
attributes: true,
attributeFilter: ["class"],
});
}
loadGiscusWhenReady();
</script>
Loading