-
Notifications
You must be signed in to change notification settings - Fork 432
Expand file tree
/
Copy pathgiscus.ejs
More file actions
64 lines (58 loc) · 2.41 KB
/
giscus.ejs
File metadata and controls
64 lines (58 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<input type="hidden" id="giscus-base-theme" value="<%- giscus.baseTheme %>">
<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);
};
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
observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
if (mutation.type === "attributes" &&
mutation.attributeName === "class" &&
loadIfBodyReady()) {
break; // Stop observing if Giscus is loaded
}
}
});
// Start observing the body for class attribute changes
observer.observe(document.body, {
attributes: true,
attributeFilter: ["class"],
});
loadIfBodyReady(); // Initial check in case the class is already present
}
loadGiscusWhenReady();
</script>