Skip to content

Commit 448cd03

Browse files
committed
添加giscus评论功能
1 parent e030adf commit 448cd03

File tree

5 files changed

+108
-166
lines changed

5 files changed

+108
-166
lines changed

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
"author": "yunnysunny <[email protected]>",
1515
"license": "MIT",
1616
"devDependencies": {
17-
"@whyun/vuepress-plugin-pdf-export": "^1.2.0",
17+
"@vuepress/plugin-active-header-links": "^1.9.10",
18+
"@vuepress/plugin-back-to-top": "^1.9.10",
19+
"@vuepress/plugin-google-analytics": "^1.9.10",
20+
"@vuepress/plugin-nprogress": "^1.9.10",
1821
"cross-env": "^7.0.3",
1922
"eslint": "^8.56.0",
2023
"markdown-it-disable-url-encode": "^1.0.1",

text/.vuepress/config.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,13 @@ module.exports = {
6161
// 排除无实际内容的页面
6262
exclude: ["/404.html"]
6363
},
64-
// 'vuepress-plugin-export': {
65-
66-
// },
64+
'@vuepress/google-analytics': {
65+
ga: 'G-33K8GJ2J4R',
66+
},
67+
'@vuepress/active-header-links': {},
68+
'@vuepress/back-to-top': {},
69+
'@vuepress/nprogress': {},
70+
// 'vuepress-plugin-nuggets-style-copy': {},
6771
'@whyun/vuepress-plugin-pdf-export': {
6872
puppeteerLaunchOptions: {
6973
args: ['--no-sandbox', '--disable-setuid-sandbox']

text/.vuepress/enhanceApp.js

+63
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,48 @@
1+
function addGiscus () {
2+
const script = document.createElement('script')
3+
// use local file
4+
// script.src = 'script.js';
5+
script.src =
6+
'https://giscus.app/client.js';
7+
script.async = true;
8+
script.crossOrigin = 'anonymous';
9+
/**
10+
* data-repo="yunnysunny/nodebook"
11+
data-repo-id="MDEwOlJlcG9zaXRvcnk0MTAyNjM2Ng=="
12+
data-category="Q&A"
13+
data-category-id="MDE4OkRpc2N1c3Npb25DYXRlZ29yeTMyMTU4NDA2"
14+
data-mapping="pathname"
15+
data-strict="0"
16+
data-reactions-enabled="1"
17+
data-emit-metadata="0"
18+
data-input-position="bottom"
19+
data-theme="preferred_color_scheme"
20+
data-lang="zh-CN"
21+
*/
22+
const attrs = {
23+
'data-repo': 'yunnysunny/nodebook',
24+
'data-repo-id': 'MDEwOlJlcG9zaXRvcnk0MTAyNjM2Ng==',
25+
'data-category': 'Q&A',
26+
'data-category-id': 'MDE4OkRpc2N1c3Npb25DYXRlZ29yeTMyMTU4NDA2',
27+
'data-mapping': 'pathname',
28+
'data-strict': '0',
29+
'data-reactions-enabled': '1',
30+
'data-emit-metadata': '0',
31+
'data-input-position': 'bottom',
32+
'data-theme': 'preferred_color_scheme',
33+
'data-lang': 'zh-CN',
34+
}
35+
for (const key in attrs) {
36+
script.setAttribute(key, attrs[key])
37+
}
38+
script.onload = () => {
39+
console.log('Script loaded successfuly');
40+
};
41+
script.onerror = () => {
42+
console.log('Error occurred while loading script');
43+
};
44+
document.querySelector('.page').appendChild(script)
45+
}
146
export default ({
247
Vue, // VuePress 正在使用的 Vue 构造函数
348
options, // 附加到根实例的一些选项
@@ -44,4 +89,22 @@ export default ({
4489
}
4590
}
4691
}
92+
// setTimeout(() => {
93+
// addGiscus()
94+
// }, 3000)
95+
// 选择一个要监听的节点
96+
const targetNode = document.body
97+
98+
// 创建一个新的 MutationObserver
99+
const observer = new MutationObserver(() => {
100+
if (document.querySelector('.page')) {
101+
addGiscus();
102+
observer.disconnect(); // 销毁监视者
103+
}
104+
})
105+
106+
const config = { childList: true, subtree: true } // 对哪些更改做出反应
107+
108+
// 绑定目标节点并启动监视者
109+
observer.observe(targetNode, config)
47110
}

text/.vuepress/styles/index.styl

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.giscus {
2+
max-width: 740px;
3+
margin: 0 auto;
4+
}

0 commit comments

Comments
 (0)