Skip to content

Commit

Permalink
fix: comments on spa should work (closes jackyzha0#1296) (jackyzha0#1298
Browse files Browse the repository at this point in the history
)

* fix comments on spa

* fix giscus
  • Loading branch information
jackyzha0 authored Jul 30, 2024
1 parent e1a9661 commit 6264f56
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 27 deletions.
45 changes: 23 additions & 22 deletions quartz/components/Comments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,26 @@ function boolToStringBool(b: boolean): string {

export default ((opts: Options) => {
const Comments: QuartzComponent = (_props: QuartzComponentProps) => <div class="giscus"></div>

Comments.afterDOMLoaded = `
const changeTheme = (e) => {
const theme = e.detail.theme
const iframe = document.querySelector('iframe.giscus-frame')
if (!iframe) {
return
}
iframe.contentWindow.postMessage({
giscus: {
setConfig: {
theme: theme
}
}
}, 'https://giscus.app')
}
document.addEventListener("nav", () => {
const giscusContainer = document.querySelector(".giscus")
const giscusScript = document.createElement("script")
giscusScript.src = "https://giscus.app/client.js"
giscusScript.async = true
Expand All @@ -38,29 +57,11 @@ export default ((opts: Options) => {
const theme = document.documentElement.getAttribute("saved-theme")
giscusScript.setAttribute("data-theme", theme)
document.head.appendChild(giscusScript)
const changeTheme = (e) => {
const theme = e.detail.theme
const iframe = document.querySelector('iframe.giscus-frame')
if (!iframe) {
return
}
iframe.contentWindow.postMessage({
giscus: {
setConfig: {
theme: theme
}
}
}, 'https://giscus.app')
}
giscusContainer.appendChild(giscusScript)
document.addEventListener("nav", () => {
document.addEventListener("themechange", changeTheme)
window.addCleanup(() => document.removeEventListener("themechange", changeTheme))
})
`
document.addEventListener("themechange", changeTheme)
window.addCleanup(() => document.removeEventListener("themechange", changeTheme))
})`

return Comments
}) satisfies QuartzComponentConstructor<Options>
2 changes: 1 addition & 1 deletion quartz/components/scripts/graph.inline.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ContentDetails, ContentIndex } from "../../plugins/emitters/contentIndex"
import type { ContentDetails } from "../../plugins/emitters/contentIndex"
import * as d3 from "d3"
import { registerEscapeHandler, removeAllChildren } from "./util"
import { FullSlug, SimpleSlug, getFullSlug, resolveRelative, simplifySlug } from "../../util/path"
Expand Down
8 changes: 4 additions & 4 deletions quartz/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ export function getStaticResourcesFromPlugins(ctx: BuildCtx) {
loadTime: "afterDOMReady",
contentType: "inline",
script: `
const socket = new WebSocket('${wsUrl}')
// reload(true) ensures resources like images and scripts are fetched again in firefox
socket.addEventListener('message', () => document.location.reload(true))
`,
const socket = new WebSocket('${wsUrl}')
// reload(true) ensures resources like images and scripts are fetched again in firefox
socket.addEventListener('message', () => document.location.reload(true))
`,
})
}

Expand Down

0 comments on commit 6264f56

Please sign in to comment.