Skip to content

Commit 6264f56

Browse files
authored
fix: comments on spa should work (closes #1296) (#1298)
* fix comments on spa * fix giscus
1 parent e1a9661 commit 6264f56

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

quartz/components/Comments.tsx

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,26 @@ function boolToStringBool(b: boolean): string {
2020

2121
export default ((opts: Options) => {
2222
const Comments: QuartzComponent = (_props: QuartzComponentProps) => <div class="giscus"></div>
23+
2324
Comments.afterDOMLoaded = `
25+
const changeTheme = (e) => {
26+
const theme = e.detail.theme
27+
const iframe = document.querySelector('iframe.giscus-frame')
28+
if (!iframe) {
29+
return
30+
}
31+
32+
iframe.contentWindow.postMessage({
33+
giscus: {
34+
setConfig: {
35+
theme: theme
36+
}
37+
}
38+
}, 'https://giscus.app')
39+
}
40+
41+
document.addEventListener("nav", () => {
42+
const giscusContainer = document.querySelector(".giscus")
2443
const giscusScript = document.createElement("script")
2544
giscusScript.src = "https://giscus.app/client.js"
2645
giscusScript.async = true
@@ -38,29 +57,11 @@ export default ((opts: Options) => {
3857
3958
const theme = document.documentElement.getAttribute("saved-theme")
4059
giscusScript.setAttribute("data-theme", theme)
41-
document.head.appendChild(giscusScript)
42-
43-
const changeTheme = (e) => {
44-
const theme = e.detail.theme
45-
const iframe = document.querySelector('iframe.giscus-frame')
46-
if (!iframe) {
47-
return
48-
}
49-
50-
iframe.contentWindow.postMessage({
51-
giscus: {
52-
setConfig: {
53-
theme: theme
54-
}
55-
}
56-
}, 'https://giscus.app')
57-
}
60+
giscusContainer.appendChild(giscusScript)
5861
59-
document.addEventListener("nav", () => {
60-
document.addEventListener("themechange", changeTheme)
61-
window.addCleanup(() => document.removeEventListener("themechange", changeTheme))
62-
})
63-
`
62+
document.addEventListener("themechange", changeTheme)
63+
window.addCleanup(() => document.removeEventListener("themechange", changeTheme))
64+
})`
6465

6566
return Comments
6667
}) satisfies QuartzComponentConstructor<Options>

quartz/components/scripts/graph.inline.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ContentDetails, ContentIndex } from "../../plugins/emitters/contentIndex"
1+
import type { ContentDetails } from "../../plugins/emitters/contentIndex"
22
import * as d3 from "d3"
33
import { registerEscapeHandler, removeAllChildren } from "./util"
44
import { FullSlug, SimpleSlug, getFullSlug, resolveRelative, simplifySlug } from "../../util/path"

quartz/plugins/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ export function getStaticResourcesFromPlugins(ctx: BuildCtx) {
2828
loadTime: "afterDOMReady",
2929
contentType: "inline",
3030
script: `
31-
const socket = new WebSocket('${wsUrl}')
32-
// reload(true) ensures resources like images and scripts are fetched again in firefox
33-
socket.addEventListener('message', () => document.location.reload(true))
34-
`,
31+
const socket = new WebSocket('${wsUrl}')
32+
// reload(true) ensures resources like images and scripts are fetched again in firefox
33+
socket.addEventListener('message', () => document.location.reload(true))
34+
`,
3535
})
3636
}
3737

0 commit comments

Comments
 (0)