| | |
| | | { |
| | | giscus: { |
| | | setConfig: { |
| | | theme: theme, |
| | | theme: getThemeUrl(getThemeName(theme)), |
| | | }, |
| | | }, |
| | | }, |
| | |
| | | ) |
| | | } |
| | | |
| | | const getThemeName = (theme: string) => { |
| | | if (theme !== "dark" && theme !== "light") { |
| | | return theme |
| | | } |
| | | const giscusContainer = document.querySelector(".giscus") as GiscusElement |
| | | if (!giscusContainer) { |
| | | return theme |
| | | } |
| | | const darkGiscus = giscusContainer.dataset.darkTheme ?? "dark" |
| | | const lightGiscus = giscusContainer.dataset.lightTheme ?? "light" |
| | | return theme === "dark" ? darkGiscus : lightGiscus |
| | | } |
| | | |
| | | const getThemeUrl = (theme: string) => { |
| | | const giscusContainer = document.querySelector(".giscus") as GiscusElement |
| | | if (!giscusContainer) { |
| | | return `https://giscus.app/themes/${theme}.css` |
| | | } |
| | | return `${giscusContainer.dataset.themeUrl ?? "https://giscus.app/themes"}/${theme}.css` |
| | | } |
| | | |
| | | type GiscusElement = Omit<HTMLElement, "dataset"> & { |
| | | 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 |
| | |
| | | |
| | | const theme = document.documentElement.getAttribute("saved-theme") |
| | | if (theme) { |
| | | giscusScript.setAttribute("data-theme", theme) |
| | | giscusScript.setAttribute("data-theme", getThemeUrl(getThemeName(theme))) |
| | | } |
| | | |
| | | giscusContainer.appendChild(giscusScript) |