Jiangnan Li
2025-06-08 cdd0be2aa8c6e0a6d97779f3db3072952b54bf13
quartz/components/Comments.tsx
@@ -1,4 +1,7 @@
import { QuartzComponent, QuartzComponentConstructor, QuartzComponentProps } from "./types"
import { classNames } from "../util/lang"
// @ts-ignore
import script from "./scripts/comments.inline"
type Options = {
  provider: "giscus"
@@ -7,10 +10,14 @@
    repoId: string
    category: string
    categoryId: string
    themeUrl?: string
    lightTheme?: string
    darkTheme?: string
    mapping?: "url" | "title" | "og:title" | "specific" | "number" | "pathname"
    strict?: boolean
    reactionsEnabled?: boolean
    inputPosition?: "top" | "bottom"
    lang?: string
  }
}
@@ -19,48 +26,37 @@
}
export default ((opts: Options) => {
  const Comments: QuartzComponent = (_props: QuartzComponentProps) => <div class="giscus"></div>
  Comments.afterDOMLoaded = `
      const giscusScript = document.createElement("script")
      giscusScript.src = "https://giscus.app/client.js"
      giscusScript.async = true
      giscusScript.crossOrigin = "anonymous"
      giscusScript.setAttribute("data-loading", "lazy")
      giscusScript.setAttribute("data-emit-metadata", "0")
      giscusScript.setAttribute("data-repo", "${opts.options.repo}")
      giscusScript.setAttribute("data-repo-id", "${opts.options.repoId}")
      giscusScript.setAttribute("data-category", "${opts.options.category}")
      giscusScript.setAttribute("data-category-id", "${opts.options.categoryId}")
      giscusScript.setAttribute("data-mapping", "${opts.options.mapping ?? "url"}")
      giscusScript.setAttribute("data-strict", "${boolToStringBool(opts.options.strict ?? true)}")
      giscusScript.setAttribute("data-reactions-enabled", "${boolToStringBool(opts.options.reactionsEnabled ?? true)}")
      giscusScript.setAttribute("data-input-position", "${opts.options.inputPosition ?? "bottom"}")
  const Comments: QuartzComponent = ({ displayClass, fileData, cfg }: QuartzComponentProps) => {
    // check if comments should be displayed according to frontmatter
    const disableComment: boolean =
      typeof fileData.frontmatter?.comments !== "undefined" &&
      (!fileData.frontmatter?.comments || fileData.frontmatter?.comments === "false")
    if (disableComment) {
      return <></>
    }
      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
    return (
      <div
        class={classNames(displayClass, "giscus")}
        data-repo={opts.options.repo}
        data-repo-id={opts.options.repoId}
        data-category={opts.options.category}
        data-category-id={opts.options.categoryId}
        data-mapping={opts.options.mapping ?? "url"}
        data-strict={boolToStringBool(opts.options.strict ?? true)}
        data-reactions-enabled={boolToStringBool(opts.options.reactionsEnabled ?? true)}
        data-input-position={opts.options.inputPosition ?? "bottom"}
        data-light-theme={opts.options.lightTheme ?? "light"}
        data-dark-theme={opts.options.darkTheme ?? "dark"}
        data-theme-url={
          opts.options.themeUrl ?? `https://${cfg.baseUrl ?? "example.com"}/static/giscus`
        }
        data-lang={opts.options.lang ?? "en"}
      ></div>
    )
  }
        iframe.contentWindow.postMessage({
          giscus: {
            setConfig: {
              theme: theme
            }
          }
        }, 'https://giscus.app')
      }
      document.addEventListener("nav", () => {
        document.addEventListener("themechange", changeTheme)
        window.addCleanup(() => document.removeEventListener("themechange", changeTheme))
      })
  `
  Comments.afterDOMLoaded = script
  return Comments
}) satisfies QuartzComponentConstructor<Options>