1
1
const isEn = ( ) => location . hash . includes ( 'README_EN' ) ;
2
+
2
3
const isRoot = ( ) => [ '' , '#/' , '#/README' , '#/README_EN' ] . includes ( location . hash ) ;
4
+
3
5
const sidebar = ( ) => ( isRoot ( ) ? false : isEn ( ) ? 'summary_en.md' : 'summary.md' ) ;
6
+
4
7
const cleanedHtml = html => {
5
8
return html . replace ( / < p r e > ( [ \s \S ] * ?) < \/ p r e > / g, ( _ , group ) => {
6
9
return '<pre>' + group . replace ( / < c o d e > ( [ \s \S ] * ?) < \/ c o d e > / g, '$1' ) + '</pre>' ;
7
10
} ) ;
8
11
} ;
9
12
13
+ const getLang = ( ) => ( isEn ( ) ? 'en' : 'zh-CN' ) ;
14
+
15
+ const giscusTheme = ( ) =>
16
+ localStorage . getItem ( 'DARK_LIGHT_THEME' ) === 'light' ? 'light' : 'noborder_dark' ;
17
+
18
+ const term = ( ) => decodeURI ( location . hash . slice ( 1 , location . hash . lastIndexOf ( '/' ) ) || '/index' ) ;
19
+
10
20
window . addEventListener ( 'hashchange' , ( ) => {
11
21
window . $docsify . loadSidebar = sidebar ( ) ;
12
22
} ) ;
@@ -18,6 +28,22 @@ window.$docsify = {
18
28
'/README_EN' : 'Last updated: ' ,
19
29
'/' : '最近更新时间:' ,
20
30
} ,
31
+ giscus : {
32
+ repo : 'doocs/leetcode' ,
33
+ repoId : 'MDEwOlJlcG9zaXRvcnkxNDkwMDEzNjU' ,
34
+ category : 'Announcements' ,
35
+ categoryId : 'DIC_kwDOCOGUlc4CZmhe' ,
36
+ mapping : 'specific' ,
37
+ term : term ( ) ,
38
+ reactionsEnabled : '0' ,
39
+ strict : '1' ,
40
+ emitMetadata : '0' ,
41
+ inputPosition : 'top' ,
42
+ crossorigin : 'anonymous' ,
43
+ loading : 'lazy' ,
44
+ theme : giscusTheme ( ) ,
45
+ lang : getLang ( ) ,
46
+ } ,
21
47
logo : '/images/doocs-leetcode.png' ,
22
48
search : {
23
49
depth : 2 ,
@@ -118,6 +144,53 @@ window.$docsify = {
118
144
const footer = `<footer>Copyright © 2018-${ currentYear } <a href="https://github.com/doocs" target="_blank">Doocs</a>${ copyright } </footer>` ;
119
145
return html + footer ;
120
146
} ) ;
147
+ hook . doneEach ( ( ) => {
148
+ var giscusScript = document . createElement ( 'script' ) ;
149
+ const {
150
+ repo,
151
+ repoId,
152
+ category,
153
+ categoryId,
154
+ mapping,
155
+ reactionsEnabled,
156
+ strict,
157
+ emitMetadata,
158
+ inputPosition,
159
+ crossorigin,
160
+ loading,
161
+ theme,
162
+ } = $docsify . giscus ;
163
+ giscusScript . type = 'text/javascript' ;
164
+ giscusScript . async = true ;
165
+ giscusScript . setAttribute ( 'src' , 'https://giscus.app/client.js' ) ;
166
+ giscusScript . setAttribute ( 'data-repo' , repo ) ;
167
+ giscusScript . setAttribute ( 'data-repo-id' , repoId ) ;
168
+ giscusScript . setAttribute ( 'data-category' , category ) ;
169
+ giscusScript . setAttribute ( 'data-category-id' , categoryId ) ;
170
+ giscusScript . setAttribute ( 'data-mapping' , mapping ) ;
171
+ giscusScript . setAttribute ( 'data-reactions-enabled' , reactionsEnabled ) ;
172
+ giscusScript . setAttribute ( 'data-strict' , strict ) ;
173
+ giscusScript . setAttribute ( 'data-emit-metadata' , emitMetadata ) ;
174
+ giscusScript . setAttribute ( 'data-input-position' , inputPosition ) ;
175
+ giscusScript . setAttribute ( 'crossorigin' , crossorigin ) ;
176
+ giscusScript . setAttribute ( 'data-loading' , loading ) ;
177
+ giscusScript . setAttribute ( 'data-theme' , theme ) ;
178
+
179
+ giscusScript . setAttribute ( 'data-term' , term ( ) ) ;
180
+ giscusScript . setAttribute ( 'data-lang' , getLang ( ) ) ;
181
+
182
+ document
183
+ . getElementById ( 'main' )
184
+ . insertBefore ( giscusScript , document . getElementById ( 'main' ) . lastChild ) ;
185
+
186
+ document . getElementById ( 'docsify-darklight-theme' ) . addEventListener ( 'click' , ( ) => {
187
+ const frame = document . querySelector ( '.giscus-frame' ) ;
188
+ frame . contentWindow . postMessage (
189
+ { giscus : { setConfig : { theme } } } ,
190
+ 'https://giscus.app' ,
191
+ ) ;
192
+ } ) ;
193
+ } ) ;
121
194
} ,
122
195
] ,
123
196
} ;
0 commit comments