-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(comments): move script to files (#1308)
* refactor(comments): move script to files for LSP, treesitter, and the whole galore. Signed-off-by: Aaron Pham <[email protected]> * fix(type): support removeEventListener with CustomEventMap Signed-off-by: Aaron Pham <[email protected]> * fix: parse bool to string first Signed-off-by: Aaron Pham <[email protected]> * chore: address comments and test on branch Signed-off-by: Aaron Pham <[email protected]> * revert: remove comments section from main quartz pages Signed-off-by: Aaron Pham <[email protected]> --------- Signed-off-by: Aaron Pham <[email protected]>
- Loading branch information
Showing
3 changed files
with
90 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
const changeTheme = (e: CustomEventMap["themechange"]) => { | ||
const theme = e.detail.theme | ||
const iframe = document.querySelector("iframe.giscus-frame") as HTMLIFrameElement | ||
if (!iframe) { | ||
return | ||
} | ||
|
||
if (!iframe.contentWindow) { | ||
return | ||
} | ||
|
||
iframe.contentWindow.postMessage( | ||
{ | ||
giscus: { | ||
setConfig: { | ||
theme: theme, | ||
}, | ||
}, | ||
}, | ||
"https://giscus.app", | ||
) | ||
} | ||
|
||
type GiscusElement = Omit<HTMLElement, "dataset"> & { | ||
dataset: DOMStringMap & { | ||
repo: `${string}/${string}` | ||
repoId: string | ||
category: string | ||
categoryId: string | ||
mapping: "url" | "title" | "og:title" | "specific" | "number" | "pathname" | ||
strict: string | ||
reactionsEnabled: string | ||
inputPosition: "top" | "bottom" | ||
} | ||
} | ||
|
||
document.addEventListener("nav", () => { | ||
const giscusContainer = document.querySelector(".giscus") as GiscusElement | ||
if (!giscusContainer) { | ||
return | ||
} | ||
|
||
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", giscusContainer.dataset.repo) | ||
giscusScript.setAttribute("data-repo-id", giscusContainer.dataset.repoId) | ||
giscusScript.setAttribute("data-category", giscusContainer.dataset.category) | ||
giscusScript.setAttribute("data-category-id", giscusContainer.dataset.categoryId) | ||
giscusScript.setAttribute("data-mapping", giscusContainer.dataset.mapping) | ||
giscusScript.setAttribute("data-strict", giscusContainer.dataset.strict) | ||
giscusScript.setAttribute("data-reactions-enabled", giscusContainer.dataset.reactionsEnabled) | ||
giscusScript.setAttribute("data-input-position", giscusContainer.dataset.inputPosition) | ||
|
||
const theme = document.documentElement.getAttribute("saved-theme") | ||
if (theme) { | ||
giscusScript.setAttribute("data-theme", theme) | ||
} | ||
|
||
giscusContainer.appendChild(giscusScript) | ||
|
||
document.addEventListener("themechange", changeTheme) | ||
window.addCleanup(() => document.removeEventListener("themechange", changeTheme)) | ||
}) |