diff --git a/docs/features/comments.md b/docs/features/comments.md index 92ea754b1..13faffbfd 100644 --- a/docs/features/comments.md +++ b/docs/features/comments.md @@ -63,6 +63,15 @@ type Options = { category: string categoryId: string + // defaults to 'https://giscus.app' + themeUrl?: string + + // defaults to 'light' + lightTheme?: string + + // defaults to 'dark' + darkTheme?: string + // how to map pages -> discussions // defaults to 'url' mapping?: "url" | "title" | "og:title" | "specific" | "number" | "pathname" diff --git a/quartz/components/Comments.tsx b/quartz/components/Comments.tsx index 8e4494026..4004d7c7e 100644 --- a/quartz/components/Comments.tsx +++ b/quartz/components/Comments.tsx @@ -10,6 +10,9 @@ type Options = { repoId: string category: string categoryId: string + themeUrl?: string + lightTheme?: string + darkTheme?: string mapping?: "url" | "title" | "og:title" | "specific" | "number" | "pathname" strict?: boolean reactionsEnabled?: boolean diff --git a/quartz/components/scripts/comments.inline.ts b/quartz/components/scripts/comments.inline.ts index 4ab29f087..038d12d05 100644 --- a/quartz/components/scripts/comments.inline.ts +++ b/quartz/components/scripts/comments.inline.ts @@ -13,20 +13,45 @@ const changeTheme = (e: CustomEventMap["themechange"]) => { { giscus: { setConfig: { - theme: theme, + theme: getThemeName(theme), }, }, }, - "https://giscus.app", + getThemeUrl(), ) } +const getThemeName = (theme: "light" | "dark") => { + const giscusContainer = document.querySelector(".giscus") as GiscusElement + if (!giscusContainer) { + return theme + } + const darkGiscus = + giscusContainer.dataset.darkTheme !== "" ? giscusContainer.dataset.darkTheme : "dark" + const lightGiscus = + giscusContainer.dataset.lightTheme !== "" ? giscusContainer.dataset.lightTheme : "light" + return theme === "dark" ? darkGiscus : lightGiscus +} + +const getThemeUrl = () => { + const giscusContainer = document.querySelector(".giscus") as GiscusElement + if (!giscusContainer) { + return "https://giscus.app" + } + return giscusContainer.dataset.themeUrl !== "" + ? giscusContainer.dataset.themeUrl + : "https://giscus.app" +} + type GiscusElement = Omit & { dataset: DOMStringMap & { repo: `${string}/${string}` repoId: string category: string categoryId: string + themeUrl: string + lightTheme: string + darkTheme: string mapping: "url" | "title" | "og:title" | "specific" | "number" | "pathname" strict: string reactionsEnabled: string