Skip to content

Commit f2c6f64

Browse files
authored
Replace timeout with async while loop to increase robustness
1 parent bbef909 commit f2c6f64

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,40 @@
11
<input type="hidden" id="giscus-base-theme" value="<%- giscus.baseTheme %>">
22
<input type="hidden" id="giscus-alt-theme" value="<%- giscus.altTheme %>">
33
<script>
4+
async function loadGiscusWhenReady() {
5+
while (!document.body.classList.contains('quarto-light') && !document.body.classList.contains('quarto-dark')) {
6+
await new Promise(resolve => setTimeout(resolve, 50));
7+
}
8+
9+
// Function to get the theme based on body class
10+
const getTheme = () => {
11+
const baseTheme = document.getElementById('giscus-base-theme').value;
12+
const altTheme = document.getElementById('giscus-alt-theme').value;
13+
return document.body.classList.contains('quarto-dark') ? altTheme : baseTheme;
14+
};
15+
16+
// Create the Giscus script and add it to the desired location
17+
const script = document.createElement("script");
18+
script.src = "https://giscus.app/client.js";
19+
script.async = true;
20+
script.dataset.repo = "<%- giscus.repo %>";
21+
script.dataset.repoId = "<%- giscus['repo-id'] %>";
22+
script.dataset.category = "<%- giscus.category %>";
23+
script.dataset.categoryId = "<%- giscus['category-id'] %>";
24+
script.dataset.mapping = "<%- giscus.mapping %>";
25+
script.dataset.reactionsEnabled = "<%- giscus['reactions-enabled'] ? 1 : 0 %>";
26+
script.dataset.emitMetadata = "0";
27+
script.dataset.inputPosition = "<%- giscus['input-position'] %>";
28+
script.dataset.theme = getTheme();
29+
script.dataset.lang = "<%- giscus.language %>";
30+
script.crossOrigin = "anonymous";
31+
32+
// Append the script to the desired div instead of at the end of the body
33+
document.getElementById("quarto-content").appendChild(script);
34+
}
35+
36+
// Call the async function to start the loop
37+
loadGiscusWhenReady();
438
setTimeout(() => {
539
var bodyClass = window.document.body.classList;
640
const GetTheme = () => {

0 commit comments

Comments
 (0)