2
2
< h2 > Comments</ h2 >
3
3
< div id ="comments-giscus "> </ div >
4
4
</ section >
5
-
6
5
< script type ="text/javascript ">
7
-
8
6
function getCurrentTheme ( ) {
9
7
return document . documentElement . getAttribute ( 'data-theme' ) || document . body . classList . contains ( 'dark' ) ? 'dark' : 'light' ;
10
8
}
11
9
10
+ function setGiscusTheme ( darkMode = false ) {
11
+ const theme = darkMode ? "dark" : "light" ;
12
+ var iframe = document . querySelector ( '.giscus-frame' ) ;
13
+
14
+ if ( iframe ) {
15
+ var url = new URL ( iframe . src ) ;
16
+ url . searchParams . set ( 'theme' , theme ) ;
17
+ iframe . src = url . toString ( ) ;
18
+ }
19
+ }
20
+
12
21
function loadComment ( darkMode = false ) {
13
- const currentUrl = URL . parse ( window . location . href ) ;
14
- const paths = currentUrl . pathname . split ( '/' ) . filter ( x => x != '' ) ;
15
- const isChinese = paths . length > 0 && paths [ 0 ] == "zh" ;
22
+ const isChinese = "{{.Lang}}" == "zh" ;
16
23
const commentContainer = document . getElementById ( "comments-giscus" ) ;
24
+
25
+ // Only proceed if container exists and visibility is not hidden
17
26
if ( commentContainer !== null ) {
18
- commentContainer . innerHTML = ''
19
- const commentScript = document . createElement ( "script" ) ;
20
- commentScript . setAttribute ( "src" , "https://giscus.app/client.js" ) ;
21
- commentScript . setAttribute ( "data-repo" , "ramsayleung/comment" ) ;
22
- commentScript . setAttribute ( "data-repo-id" , "MDEwOlJlcG9zaXRvcnkzMDk2NjQ1NDk=" ) ;
23
- commentScript . setAttribute ( "data-category" , "General" ) ;
24
- commentScript . setAttribute ( "data-category-id" , "DIC_kwDOEnUbJc4Cltnz" ) ;
25
- commentScript . setAttribute ( "data-mapping" , "pathname" ) ;
26
- commentScript . setAttribute ( "data-strict" , "0" ) ;
27
- commentScript . setAttribute ( "data-reactions-enabled" , "1" ) ;
28
- commentScript . setAttribute ( "data-emit-metadata" , "0" ) ;
29
- commentScript . setAttribute ( "data-input-position" , "bottom" ) ;
30
- commentScript . setAttribute ( "data-theme" , darkMode ? "dark" : "light" ) ;
31
- commentScript . setAttribute ( "crossorigin" , "anonymous" ) ;
32
- commentScript . setAttribute ( "data-lang" , isChinese ? "zh-CN" : "en" ) ;
33
- commentScript . setAttribute ( "async" , "true" ) ;
34
- commentContainer . appendChild ( commentScript ) ;
27
+ if ( ! commentContainer . hasAttribute ( 'data-giscus-loaded' ) ) {
28
+ console . log ( 'Initial giscus load' ) ;
29
+ const commentScript = document . createElement ( "script" ) ;
30
+ commentScript . setAttribute ( "src" , "https://giscus.app/client.js" ) ;
31
+ commentScript . setAttribute ( "data-repo" , "ramsayleung/comment" ) ;
32
+ commentScript . setAttribute ( "data-repo-id" , "MDEwOlJlcG9zaXRvcnkzMDk2NjQ1NDk=" ) ;
33
+ commentScript . setAttribute ( "data-category" , "General" ) ;
34
+ commentScript . setAttribute ( "data-category-id" , "DIC_kwDOEnUbJc4Cltnz" ) ;
35
+ commentScript . setAttribute ( "data-mapping" , "pathname" ) ;
36
+ commentScript . setAttribute ( "data-strict" , "0" ) ;
37
+ commentScript . setAttribute ( "data-reactions-enabled" , "1" ) ;
38
+ commentScript . setAttribute ( "data-emit-metadata" , "0" ) ;
39
+ commentScript . setAttribute ( "data-input-position" , "bottom" ) ;
40
+ commentScript . setAttribute ( "data-theme" , darkMode ? "dark" : "light" ) ;
41
+ commentScript . setAttribute ( "crossorigin" , "anonymous" ) ;
42
+ commentScript . setAttribute ( "data-lang" , isChinese ? "zh-CN" : "en" ) ;
43
+ commentScript . setAttribute ( "async" , "true" ) ;
44
+ commentContainer . appendChild ( commentScript ) ;
45
+ commentContainer . setAttribute ( 'data-giscus-loaded' , 'true' ) ;
46
+ }
35
47
}
36
48
}
37
49
38
- const isDarkMode = getCurrentTheme ( ) === 'dark' ;
39
- loadComment ( isDarkMode ) ;
50
+ let currentTheme = getCurrentTheme ( ) ;
51
+ loadComment ( currentTheme === 'dark' ) ;
40
52
41
53
// Watch for theme changes
42
54
const themeObserver = new MutationObserver ( ( mutations ) => {
43
55
mutations . forEach ( ( mutation ) => {
44
56
if ( mutation . type === 'attributes' && mutation . attributeName === 'class' ) {
45
- const isDarkMode = getCurrentTheme ( ) === 'dark' ;
46
- loadComment ( isDarkMode ) ;
47
- console . log ( `changing theme` ) ;
57
+ const newTheme = getCurrentTheme ( ) ;
58
+ if ( newTheme !== currentTheme ) {
59
+ currentTheme = newTheme ;
60
+ setGiscusTheme ( currentTheme === 'dark' ) ;
61
+ }
48
62
}
49
63
} ) ;
50
64
} ) ;
@@ -54,5 +68,4 @@ <h2>Comments</h2>
54
68
attributes : true ,
55
69
attributeFilter : [ 'class' ]
56
70
} ) ;
57
-
58
71
</ script >
0 commit comments