fix comments on spa

This commit is contained in:
Jacky Zhao 2024-07-29 11:52:02 -07:00
parent 34fde07cf8
commit 52e31600a6

View File

@ -19,8 +19,12 @@ function boolToStringBool(b: boolean): string {
} }
export default ((opts: Options) => { export default ((opts: Options) => {
const Comments: QuartzComponent = (_props: QuartzComponentProps) => <div class="giscus"></div> const Comments: QuartzComponent = (props: QuartzComponentProps) => {
Comments.afterDOMLoaded = ` props.externalResources.js.push({
loadTime: "afterDOMReady",
spaPreserve: true,
contentType: "inline",
script: `
const giscusScript = document.createElement("script") const giscusScript = document.createElement("script")
giscusScript.src = "https://giscus.app/client.js" giscusScript.src = "https://giscus.app/client.js"
giscusScript.async = true giscusScript.async = true
@ -57,10 +61,21 @@ export default ((opts: Options) => {
} }
document.addEventListener("nav", () => { document.addEventListener("nav", () => {
iframe.contentWindow.postMessage({
giscus: {
setConfig: {
term: window.document.body.dataset.slug
},
},
}, 'https://giscus.app')
document.addEventListener("themechange", changeTheme) document.addEventListener("themechange", changeTheme)
window.addCleanup(() => document.removeEventListener("themechange", changeTheme)) window.addCleanup(() => document.removeEventListener("themechange", changeTheme))
}) })`,
` })
return <div class="giscus"></div>
}
return Comments return Comments
}) satisfies QuartzComponentConstructor<Options> }) satisfies QuartzComponentConstructor<Options>