Skip to content

Commit 027acbf

Browse files
simple dynamic load of giscus
generate script tag based on user preference at page load no need for any mutation observers, because class is already set
1 parent 95b4ac3 commit 027acbf

File tree

2 files changed

+33
-36
lines changed

2 files changed

+33
-36
lines changed

news/changelog-1.7.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## In this release
44

55
- ([#12625](https://github.com/quarto-dev/quarto-cli/pull/12625)): Fire resize event on window when light/dark toggle is clicked, to tell widgets to resize.
6+
- ([#12657](https://github.com/quarto-dev/quarto-cli/pull/12657)): Load Giscus in generated script tag, to avoid wrong-theming in Chrome.
67

78
# v1.7 changes
89

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,33 @@
1-
<script src="https://giscus.app/client.js"
2-
data-repo="<%- giscus.repo %>"
3-
data-repo-id="<%- giscus['repo-id'] %>"
4-
data-category="<%- giscus.category %>"
5-
data-category-id="<%- giscus['category-id'] %>"
6-
data-mapping="<%- giscus.mapping %>"
7-
data-reactions-enabled="<%- giscus['reactions-enabled'] ? 1 : 0 %>"
8-
data-emit-metadata="0"
9-
data-input-position="<%- giscus['input-position'] %>"
10-
data-theme="<%- giscus.theme %>"
11-
data-lang="<%- giscus.language %>"
12-
crossorigin="anonymous"
13-
<%- giscus.loading ? `data-loading=${giscus.loading}` : '' %>
14-
async>
15-
</script>
16-
<script type="application/javascript">
17-
const giscusIframeObserver = new MutationObserver(function (mutations) {
18-
mutations.forEach(function (mutation) {
19-
mutation.addedNodes.forEach(function (addedNode) {
20-
if (addedNode.matches && addedNode.matches('div.giscus')) {
21-
const giscusIframe = addedNode.querySelector('iframe.giscus-frame');
22-
if(giscusIframe) {
23-
giscusIframe.addEventListener("load", function() {
24-
window.setTimeout(() => {
25-
toggleGiscusIfUsed(hasAlternateSentinel(), authorPrefersDark);
26-
}, 100);
27-
});
28-
giscusIframeObserver.disconnect();
29-
}
30-
}
31-
});
32-
});
33-
});
34-
giscusIframeObserver.observe(document.body, { childList: true, subtree: true });
35-
</script>
361
<input type="hidden" id="giscus-base-theme" value="<%- giscus.baseTheme %>">
37-
<input type="hidden" id="giscus-alt-theme" value="<%- giscus.altTheme %>">
2+
<input type="hidden" id="giscus-alt-theme" value="<%- giscus.altTheme %>">
3+
<script>
4+
function loadGiscus() {
5+
// Function to get the theme based on body class
6+
const getTheme = () => {
7+
let baseTheme = document.getElementById('giscus-base-theme').value;
8+
let altTheme = document.getElementById('giscus-alt-theme').value;
9+
if (authorPrefersDark) {
10+
[baseTheme, altTheme] = [altTheme, baseTheme];
11+
}
12+
return document.body.classList.contains('quarto-dark') ? altTheme : baseTheme;
13+
};
14+
const script = document.createElement("script");
15+
script.src = "https://giscus.app/client.js";
16+
script.async = true;
17+
script.dataset.repo = "<%- giscus.repo %>";
18+
script.dataset.repoId = "<%- giscus['repo-id'] %>";
19+
script.dataset.category = "<%- giscus.category %>";
20+
script.dataset.categoryId = "<%- giscus['category-id'] %>";
21+
script.dataset.mapping = "<%- giscus.mapping %>";
22+
script.dataset.reactionsEnabled = "<%- giscus['reactions-enabled'] ? 1 : 0 %>";
23+
script.dataset.emitMetadata = "0";
24+
script.dataset.inputPosition = "<%- giscus['input-position'] %>";
25+
script.dataset.theme = getTheme();
26+
script.dataset.lang = "<%- giscus.language %>";
27+
script.crossOrigin = "anonymous";
28+
29+
// Append the script to the desired div instead of at the end of the body
30+
document.getElementById("quarto-content").appendChild(script);
31+
}
32+
loadGiscus();
33+
</script>

0 commit comments

Comments
 (0)