1
1
import { QuartzComponent , QuartzComponentConstructor , QuartzComponentProps } from "./types"
2
+ import { classNames } from "../util/lang"
3
+ // @ts -ignore
4
+ import script from "./scripts/comments.inline"
2
5
3
6
type Options = {
4
7
provider : "giscus"
@@ -19,49 +22,23 @@ function boolToStringBool(b: boolean): string {
19
22
}
20
23
21
24
export default ( ( opts : Options ) => {
22
- const Comments : QuartzComponent = ( _props : QuartzComponentProps ) => < div class = "giscus" > </ div >
23
-
24
- Comments . afterDOMLoaded = `
25
- const changeTheme = (e) => {
26
- const theme = e.detail.theme
27
- const iframe = document.querySelector('iframe.giscus-frame')
28
- if (!iframe) {
29
- return
30
- }
31
-
32
- iframe.contentWindow.postMessage({
33
- giscus: {
34
- setConfig: {
35
- theme: theme
36
- }
37
- }
38
- }, 'https://giscus.app')
39
- }
40
-
41
- document.addEventListener("nav", () => {
42
- const giscusContainer = document.querySelector(".giscus")
43
- const giscusScript = document.createElement("script")
44
- giscusScript.src = "https://giscus.app/client.js"
45
- giscusScript.async = true
46
- giscusScript.crossOrigin = "anonymous"
47
- giscusScript.setAttribute("data-loading", "lazy")
48
- giscusScript.setAttribute("data-emit-metadata", "0")
49
- giscusScript.setAttribute("data-repo", "${ opts . options . repo } ")
50
- giscusScript.setAttribute("data-repo-id", "${ opts . options . repoId } ")
51
- giscusScript.setAttribute("data-category", "${ opts . options . category } ")
52
- giscusScript.setAttribute("data-category-id", "${ opts . options . categoryId } ")
53
- giscusScript.setAttribute("data-mapping", "${ opts . options . mapping ?? "url" } ")
54
- giscusScript.setAttribute("data-strict", "${ boolToStringBool ( opts . options . strict ?? true ) } ")
55
- giscusScript.setAttribute("data-reactions-enabled", "${ boolToStringBool ( opts . options . reactionsEnabled ?? true ) } ")
56
- giscusScript.setAttribute("data-input-position", "${ opts . options . inputPosition ?? "bottom" } ")
57
-
58
- const theme = document.documentElement.getAttribute("saved-theme")
59
- giscusScript.setAttribute("data-theme", theme)
60
- giscusContainer.appendChild(giscusScript)
25
+ const Comments : QuartzComponent = ( { displayClass, cfg } : QuartzComponentProps ) => {
26
+ return (
27
+ < div
28
+ class = { classNames ( displayClass , "giscus" ) }
29
+ data-repo = { opts . options . repo }
30
+ data-repo-id = { opts . options . repoId }
31
+ data-category = { opts . options . category }
32
+ data-category-id = { opts . options . categoryId }
33
+ data-mapping = { opts . options . mapping ?? "url" }
34
+ data-strict = { boolToStringBool ( opts . options . strict ?? true ) }
35
+ data-reactions-enabled = { boolToStringBool ( opts . options . reactionsEnabled ?? true ) }
36
+ data-input-position = { opts . options . inputPosition ?? "bottom" }
37
+ > </ div >
38
+ )
39
+ }
61
40
62
- document.addEventListener("themechange", changeTheme)
63
- window.addCleanup(() => document.removeEventListener("themechange", changeTheme))
64
- })`
41
+ Comments . afterDOMLoaded = script
65
42
66
43
return Comments
67
44
} ) satisfies QuartzComponentConstructor < Options >
0 commit comments