Skip to content

Commit 79d01a9

Browse files
thinkasanyyanglbme
andauthored
feat: add giscus plugin (#1678)
--------- Co-authored-by: Yang Libin <contact@yanglibin.info>
1 parent 6b8a81d commit 79d01a9

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

index.html

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<link rel="stylesheet" href="https://cdn-doocs.oss-cn-shenzhen.aliyuncs.com/npm/docsify@4.13.0/lib/themes/vue.css">
1414
<link rel="stylesheet" href="https://cdn-doocs.oss-cn-shenzhen.aliyuncs.com/npm/docsify-darklight-theme@latest/dist/style.min.css">
1515
<link rel="stylesheet" href="https://cdn-doocs.oss-cn-shenzhen.aliyuncs.com/npm/katex@latest/dist/katex.min.css">
16+
<link rel="stylesheet" href="https://cdn-doocs.oss-cn-shenzhen.aliyuncs.com/npm/docsify-giscus@1.0.0/dist/giscus.css">
1617
<link rel="icon" type="image/png" sizes="32x32" href="images/favicon-32x32.png">
1718
<link rel="icon" type="image/png" sizes="16x16" href="images/favicon-16x16.png">
1819
<style>
@@ -52,6 +53,7 @@
5253
<script src="https://cdn-doocs.oss-cn-shenzhen.aliyuncs.com/npm/docsify-darklight-theme@latest/dist/index.min.js"></script>
5354
<script src="https://cdn-doocs.oss-cn-shenzhen.aliyuncs.com/npm/docsify-contributors@latest/dist/index.min.js"></script>
5455
<script src="https://cdn-doocs.oss-cn-shenzhen.aliyuncs.com/npm/docsify-katex@latest/dist/docsify-katex.js"></script>
56+
<script src='https://cdn-doocs.oss-cn-shenzhen.aliyuncs.com/npm/docsify-giscus@1.0.0/dist/docsify-giscus.min.js'></script>
5557
</body>
5658

5759
<!--

main.js

+73
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
const isEn = () => location.hash.includes('README_EN');
2+
23
const isRoot = () => ['', '#/', '#/README', '#/README_EN'].includes(location.hash);
4+
35
const sidebar = () => (isRoot() ? false : isEn() ? 'summary_en.md' : 'summary.md');
6+
47
const cleanedHtml = html => {
58
return html.replace(/<pre>([\s\S]*?)<\/pre>/g, (_, group) => {
69
return '<pre>' + group.replace(/<code>([\s\S]*?)<\/code>/g, '$1') + '</pre>';
710
});
811
};
912

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+
1020
window.addEventListener('hashchange', () => {
1121
window.$docsify.loadSidebar = sidebar();
1222
});
@@ -18,6 +28,22 @@ window.$docsify = {
1828
'/README_EN': 'Last updated: ',
1929
'/': '最近更新时间:',
2030
},
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+
},
2147
logo: '/images/doocs-leetcode.png',
2248
search: {
2349
depth: 2,
@@ -118,6 +144,53 @@ window.$docsify = {
118144
const footer = `<footer>Copyright © 2018-${currentYear} <a href="https://github.com/doocs" target="_blank">Doocs</a>${copyright}</footer>`;
119145
return html + footer;
120146
});
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+
});
121194
},
122195
],
123196
};

0 commit comments

Comments
 (0)