Skip to content

Commit bde4e78

Browse files
committed
feat: support comments module
1 parent 09ae79d commit bde4e78

File tree

7 files changed

+131
-32
lines changed

7 files changed

+131
-32
lines changed

README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,23 @@ abbrlink: s31w9gd1
4040
4141
## 主题
4242

43-
create-react-doc 提供了官方默认主题 [crd-seed](https://github.com/MuYunyun/create-react-doc/tree/main/packages/crd-seed)该主题支持以下特性:
43+
create-react-doc 提供了默认主题 [crd-seed](https://github.com/MuYunyun/create-react-doc/tree/main/packages/crd-seed)
4444

45-
* 适配移动、PC 端展示。
46-
* 支持暗黑模式。
47-
* 支持文档内嵌展示 codepen、codesandbox 案例。
48-
* 支持与 GitHub 文档编辑能力联动。
49-
* 支持配置展示标签以自定义聚合文章内容。
45+
该主题支持以下特性:
5046

51-
[笔者博客](http://muyunyun.cn/blog)使用的是 [crd-seed](https://github.com/MuYunyun/create-react-doc/tree/main/packages/crd-seed) 主题搭建的。
47+
- [x] 适配网页/移动端展示。
48+
- [x] 支持暗黑模式。
49+
- [x] 支持标签页自定义聚合文章内容。
50+
- [x] 内置评论模块。
51+
- [x] 支持内嵌展示 codepen、codesandbox 案例。
52+
- [x] 支持从文档页快速跳转到对应的 Github 文档页进行在线编辑。
53+
54+
该主题效果可以参考[笔者博客](http://muyunyun.cn/blog)
5255

5356
![](http://with.muyunyun.cn/90d3e357a31649b9466a828a92b6d88d.jpg)
5457
![](http://with.muyunyun.cn/2e7440e4256debda2d73a4e6392c7146.jpg-300)
5558

56-
如果您想定制化或者分享个人主题, 可以参考[自定义主题](https://muyunyun.cn/create-react-doc/9f41fc98)
59+
如果您想定制化或者分享个人主题可以参考[自定义主题](https://muyunyun.cn/create-react-doc/9f41fc98)章节
5760

5861
## 快速上手
5962

config.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,11 @@ inject: injectLogic/index.js
4343
# Use abbrlink
4444
abbrlink: true
4545

46-
# show Tags in head
46+
# Show Tags in head
4747
tags: true
48+
49+
# Config comment section
50+
comment:
51+
# Giscus Config, The config parameter that's supported can be seen in [giscus-component](https://github.com/giscus/giscus-component#documentation).
52+
GiscusConfig:
53+
id: MDEwOlJlcG9zaXRvcnkyNjgwMTE4MzA=

docs/主题/默认主题.md

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ abbrlink: 85li8wdd
44

55
## 默认主题
66

7-
create-react-doc 的默认主题为 [crd-seed](https://github.com/MuYunyun/create-react-doc/tree/main/packages/crd-seed)该主题有以下特性:
7+
create-react-doc 的默认主题为 [crd-seed](https://github.com/MuYunyun/create-react-doc/tree/main/packages/crd-seed)
88

9-
* 适配移动、PC 多端展示。
10-
* 支持暗黑模式。
11-
* 文档支持内嵌 codepen、codesandbox。
12-
* GitHub 联动。
9+
该主题支持以下特性:
10+
11+
- [x] 适配网页/移动端展示。
12+
- [x] 支持暗黑模式。
13+
- [x] 支持标签页自定义聚合文章内容。
14+
- [x] 内置评论模块。
15+
- [x] 支持内嵌展示 codepen、codesandbox 案例。
16+
- [x] 支持从文档页快速跳转到对应的 Github 文档页进行在线编辑。
1317

1418
使用该主题搭建的项目有:
1519

@@ -26,18 +30,19 @@ create-react-doc 的默认主题为 [crd-seed](https://github.com/MuYunyun/creat
2630

2731
它支持配置的属性如下:
2832

29-
| 属性名 | 作用 | 类型 | 默认 |
30-
| :------------: | :----------------------------------: | :------------------: | :------: |
31-
| title | 站点名 | string | |
32-
| menu | 作为站点菜单的文件/文件夹路径 | string[] | |
33-
| menuOpenKeys | 默认展开菜单的文件夹路径 | string | |
34-
| user | Github 用户名 | string | |
35-
| repo | Github 项目名 | string | |
36-
| language | 站点语言 | en \| zh-cn | en |
37-
| github-ribbons | 是否在右上角显示 github 丝带 | boolean | false |
38-
| theme | 使用主题 | string | crd-seed |
39-
| devTheme | 开发自定义主题时, 需设置其为 true | string | ./index |
40-
| seo | 是否开启 SEO 优化 | { google?: boolean } | |
41-
| domain | SEO 优化的站点域名, 用于生成 sitemap | string | |
33+
| 属性名 | 作用 | 类型 | 默认 |
34+
| :------------: | :----------------------------------: | :---------------------------------------------------------------------------------: | :------: |
35+
| title | 站点名 | string | |
36+
| menu | 作为站点菜单的文件/文件夹路径 | string[] | |
37+
| menuOpenKeys | 默认展开菜单的文件夹路径 | string | |
38+
| user | Github 用户名 | string | |
39+
| repo | Github 项目名 | string | |
40+
| language | 站点语言 | en \| zh-cn | en |
41+
| github-ribbons | 是否在右上角显示 github 丝带 | boolean | false |
42+
| theme | 使用主题 | string | crd-seed |
43+
| devTheme | 开发自定义主题时, 需设置其为 true | string | ./index |
44+
| seo | 是否开启 SEO 优化 | { google?: boolean } | |
45+
| domain | SEO 优化的站点域名, 用于生成 sitemap | string | |
46+
| comment | 开启评论区,并进行相关配置 | { GiscusConfig: [Props](https://github.com/giscus/giscus-component#documentation) } | |
4247

4348
详细用法可以参考 [config.yml](https://github.com/MuYunyun/blog/blob/main/config.yml)

packages/crd-seed/layout/index.js

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as React from 'react'
22
import { Routes, Link, Route, Navigate, useLocation } from 'react-router-dom'
33
import cx from 'classnames'
44
import { ifDev, ifProd, ifPrerender } from 'crd-client-utils'
5+
const Giscus = require('@giscus/react')
56
import Menu from '../component/Menu'
67
import Icon from '../component/Icon'
78
import Affix from '../component/Affix'
@@ -15,7 +16,7 @@ import logo from '../crd.logo.svg'
1516
import styles from './index.less'
1617
import '../style/mobile.less'
1718

18-
const { useState, useEffect } = React
19+
const { useState, useEffect, useMemo } = React
1920
const SubMenu = Menu.SubMenu
2021

2122
function BasicLayout({
@@ -30,8 +31,10 @@ function BasicLayout({
3031
branch = 'main',
3132
language = 'en',
3233
menuOpenKeys,
33-
tags
34+
tags,
35+
comment
3436
} = DOCSCONFIG || {}
37+
3538
const [inlineCollapsed, setInlineCollapsed] = useState(true)
3639
const [selectedKey, setSelectedKey] = useState('')
3740
const curOpenKeys = getOpenSubMenuKeys({
@@ -150,7 +153,7 @@ function BasicLayout({
150153
)
151154
}
152155
/**
153-
* this section is to show article's relevant information
156+
* This section is to show article's relevant information
154157
* such as edit in github and so on.
155158
*/
156159
const renderPageHeader = () => {
@@ -176,8 +179,34 @@ function BasicLayout({
176179
</div>
177180
)
178181
}
182+
183+
/**
184+
* This section is to render comment area.
185+
* Every pathname should has its own comment module.
186+
*/
187+
const renderComment = useMemo(() => {
188+
return <Giscus
189+
key={pathname}
190+
id="comments"
191+
repo={`${user}/${repo}`}
192+
category="General"
193+
categoryId="DIC_kwDOD_mJNs4CSd1W"
194+
mapping="pathname"
195+
strict="0"
196+
reactionsEnabled="1"
197+
emitMetadata="0"
198+
inputPosition="top"
199+
theme="preferred_color_scheme"
200+
lang="en"
201+
loading="lazy"
202+
crossorigin="anonymous"
203+
async
204+
{...comment?.GiscusConfig}
205+
/>
206+
}, [pathname])
207+
179208
/**
180-
* this section is to show article's relevant information
209+
* This section is to show article's relevant information
181210
* such as edit in created time、edited time and so on.
182211
*/
183212
const renderPageFooter = () => {
@@ -283,6 +312,7 @@ function BasicLayout({
283312
{/* Todo: follow up how to use Redirect to back up the rest of route. */}
284313
{/* <Redirect path='/' to={ifAddPrefix ? `/${repo}/404` : `/404`} /> */}
285314
</Routes>
315+
{comment?.GiscusConfig ? renderComment : null}
286316
{renderPageFooter()}
287317
</div>
288318
)

packages/crd-seed/layout/index.less

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,10 @@
117117

118118
.contentNoMenu {
119119
min-height: 400px;
120-
}
120+
}
121+
122+
giscus-widget {
123+
display: flex;
124+
margin: auto;
125+
max-width: 940px;
126+
}

packages/crd-seed/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "Default Theme with Create React Doc",
55
"main": "index.js",
66
"dependencies": {
7+
"@giscus/react": "^2.2.2",
78
"crd-client-utils": "^1.8.2",
89
"react-router-dom": "^6.3.0",
910
"react-switch": "^5.0.1"

yarn.lock

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,6 +1254,13 @@
12541254
unique-filename "^1.1.1"
12551255
which "^1.3.1"
12561256

1257+
"@giscus/react@^2.2.2":
1258+
version "2.2.2"
1259+
resolved "https://registry.npmjs.org/@giscus/react/-/react-2.2.2.tgz#3b06c03bea6965ccbc6d26bd3abe038d91fb4cfc"
1260+
integrity sha512-UXU2pzJEnsjEh36//UlqmWd7j3FJTGL7ijZIaijwbrsERtzkPkRhOkJ7eqRytCOdpcJSji3XX07/5rPGknmm3w==
1261+
dependencies:
1262+
giscus "^1.2.2"
1263+
12571264
"@inquirer/core@^0.0.13-alpha.0":
12581265
version "0.0.13-alpha.0"
12591266
resolved "https://registry.npmjs.org/@inquirer/core/-/core-0.0.13-alpha.0.tgz#374e901cc2aff56fc3f565c2fde5d5053145035f"
@@ -1962,6 +1969,11 @@
19621969
npmlog "^4.1.2"
19631970
write-file-atomic "^2.3.0"
19641971

1972+
"@lit/reactive-element@^1.3.0", "@lit/reactive-element@^1.4.0":
1973+
version "1.4.2"
1974+
resolved "https://registry.npmjs.org/@lit/reactive-element/-/reactive-element-1.4.2.tgz#65d659e89d1a7e2f4f2b547182aef0a224f4adb7"
1975+
integrity sha512-VMOxsWh/QDwrxPsgkSQnuZ+8mfNy1OTjzzUdLBvvZtpahwPTHTeVZ51RZRqO4xfKVrR+btIPA8D01IL3xeG66w==
1976+
19651977
"@mdx-js/loader@^1.6.22":
19661978
version "1.6.22"
19671979
resolved "https://registry.npmjs.org/@mdx-js/loader/-/loader-1.6.22.tgz#d9e8fe7f8185ff13c9c8639c048b123e30d322c4"
@@ -2315,6 +2327,11 @@
23152327
resolved "https://registry.npmjs.org/@types/tapable/-/tapable-1.0.6.tgz#a9ca4b70a18b270ccb2bc0aaafefd1d486b7ea74"
23162328
integrity sha512-W+bw9ds02rAQaMvaLYxAbJ6cvguW/iJXNT6lTssS1ps6QdrMKttqEAMEG/b5CR8TZl3/L7/lH0ZV5nNR1LXikA==
23172329

2330+
"@types/trusted-types@^2.0.2":
2331+
version "2.0.2"
2332+
resolved "https://registry.npmjs.org/@types/trusted-types/-/trusted-types-2.0.2.tgz#fc25ad9943bcac11cceb8168db4f275e0e72e756"
2333+
integrity sha512-F5DIZ36YVLE+PN+Zwws4kJogq47hNgX3Nx6WyDJ3kcplxyke3XIzB8uK5n/Lpm1HBsbGzd6nmGehL8cPekP+Tg==
2334+
23182335
"@types/uglify-js@*":
23192336
version "3.11.1"
23202337
resolved "https://registry.npmjs.org/@types/uglify-js/-/uglify-js-3.11.1.tgz#97ff30e61a0aa6876c270b5f538737e2d6ab8ceb"
@@ -5943,6 +5960,13 @@ gh-pages@^3.1.0:
59435960
fs-extra "^8.1.0"
59445961
globby "^6.1.0"
59455962

5963+
giscus@^1.2.2:
5964+
version "1.2.2"
5965+
resolved "https://registry.npmjs.org/giscus/-/giscus-1.2.2.tgz#b186ede8bba56869f2a1b902031c54d528662677"
5966+
integrity sha512-H71M3V5K/mmWr5/OwCMCH+s7Z2wVT6uQ1AXe5B0f0jjAGvJ9unAklfp4cNJU5zamjDdMZFVFi/9xhhbHIG3ZTQ==
5967+
dependencies:
5968+
lit "^2.4.1"
5969+
59465970
59475971
version "2.0.0"
59485972
resolved "https://registry.npmjs.org/git-raw-commits/-/git-raw-commits-2.0.0.tgz#d92addf74440c14bcc5c83ecce3fb7f8a79118b5"
@@ -7487,6 +7511,30 @@ lines-and-columns@^1.1.6:
74877511
resolved "https://registry.npmjs.org/lines-and-columns/-/lines-and-columns-1.1.6.tgz#1c00c743b433cd0a4e80758f7b64a57440d9ff00"
74887512
integrity sha1-HADHQ7QzzQpOgHWPe2SldEDZ/wA=
74897513

7514+
lit-element@^3.2.0:
7515+
version "3.2.2"
7516+
resolved "https://registry.npmjs.org/lit-element/-/lit-element-3.2.2.tgz#d148ab6bf4c53a33f707a5168e087725499e5f2b"
7517+
integrity sha512-6ZgxBR9KNroqKb6+htkyBwD90XGRiqKDHVrW/Eh0EZ+l+iC+u+v+w3/BA5NGi4nizAVHGYvQBHUDuSmLjPp7NQ==
7518+
dependencies:
7519+
"@lit/reactive-element" "^1.3.0"
7520+
lit-html "^2.2.0"
7521+
7522+
lit-html@^2.2.0, lit-html@^2.4.0:
7523+
version "2.4.0"
7524+
resolved "https://registry.npmjs.org/lit-html/-/lit-html-2.4.0.tgz#b510430f39a56ec959167ed1187241a4e3ab1574"
7525+
integrity sha512-G6qXu4JNUpY6aaF2VMfaszhO9hlWw0hOTRFDmuMheg/nDYGB+2RztUSOyrzALAbr8Nh0Y7qjhYkReh3rPnplVg==
7526+
dependencies:
7527+
"@types/trusted-types" "^2.0.2"
7528+
7529+
lit@^2.4.1:
7530+
version "2.4.1"
7531+
resolved "https://registry.npmjs.org/lit/-/lit-2.4.1.tgz#21251ac14eb1ec8ca7cd15c9ac3564359809f7b5"
7532+
integrity sha512-qohSgLiyN1cFnJG26dIiY03S4F49857A0AHQfnS0zYtnUVnD2MFvx+UT52rtXsIuNFQrnUupX+zyGSATlk1f/A==
7533+
dependencies:
7534+
"@lit/reactive-element" "^1.4.0"
7535+
lit-element "^3.2.0"
7536+
lit-html "^2.4.0"
7537+
74907538
load-json-file@^1.0.0:
74917539
version "1.1.0"
74927540
resolved "https://registry.npmjs.org/load-json-file/-/load-json-file-1.1.0.tgz#956905708d58b4bab4c2261b04f59f31c99374c0"

0 commit comments

Comments
 (0)