| | |
| | | import { QuartzComponent, QuartzComponentConstructor, QuartzComponentProps } from "./types" |
| | | import { classNames } from "../util/lang" |
| | | // @ts-ignore |
| | | import script from "./scripts/comments.inline" |
| | | |
| | | type Options = { |
| | | provider: "giscus" |
| | |
| | | } |
| | | |
| | | 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 |
| | | const Comments: QuartzComponent = ({ displayClass, cfg }: QuartzComponentProps) => { |
| | | 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"} |
| | | ></div> |
| | | ) |
| | | } |
| | | |
| | | 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 |
| | | 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 theme = document.documentElement.getAttribute("saved-theme") |
| | | giscusScript.setAttribute("data-theme", theme) |
| | | giscusContainer.appendChild(giscusScript) |
| | | |
| | | document.addEventListener("themechange", changeTheme) |
| | | window.addCleanup(() => document.removeEventListener("themechange", changeTheme)) |
| | | })` |
| | | Comments.afterDOMLoaded = script |
| | | |
| | | return Comments |
| | | }) satisfies QuartzComponentConstructor<Options> |