Skip to content

Commit 3ab9622

Browse files
authored
feat: support remark-gfm (#32)
1 parent 859338b commit 3ab9622

File tree

3 files changed

+42
-36
lines changed

3 files changed

+42
-36
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"rehype-raw": "^6.1.1",
6666
"rehype-sanitize": "^5.0.1",
6767
"rehype-stringify": "^9.0.2",
68+
"remark-gfm": "^3.0.1",
6869
"remark-parse": "^10.0.1",
6970
"remark-rehype": "^10.1.0",
7071
"unified": "^10.1.1"

src/component/Markdown/index.tsx

+37-32
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,58 @@ import { unified } from 'unified';
33
import remarkParse from 'remark-parse';
44
import remarkRehype from 'remark-rehype';
55
import rehypeRaw from 'rehype-raw';
6+
import remarkGfm from 'remark-gfm';
67
import rehypeAttr from 'rehype-attr';
78
import rehypeSanitize, { defaultSchema } from 'rehype-sanitize';
89
import rehypeStringify from 'rehype-stringify';
10+
import type { Schema } from 'hast-util-sanitize';
911

1012
import MdEditor from 'react-markdown-editor-lite';
1113
// import 'react-markdown-editor-lite/esm/index.less';
1214

1315
import * as styles from './index.less';
1416

17+
const CONFIG_MAP = {
18+
render: {
19+
view: {
20+
menu: false,
21+
md: false,
22+
html: true,
23+
},
24+
classname: styles.markdown_render,
25+
},
26+
editor: {
27+
view: {
28+
menu: true,
29+
md: true,
30+
html: false,
31+
},
32+
classname: styles.markdown_editor,
33+
},
34+
};
35+
36+
const CONFIG_SCHEMA: Schema = {
37+
...defaultSchema,
38+
attributes: {
39+
...defaultSchema.attributes,
40+
img: [...(defaultSchema?.attributes?.img || []), ['style']],
41+
},
42+
};
43+
1544
const processor = unified()
1645
.use(remarkParse)
46+
.use(remarkGfm)
1747
.use(remarkRehype, { allowDangerousHtml: true })
1848
.use(rehypeRaw)
1949
.use(rehypeAttr, { properties: 'attr' })
20-
.use(rehypeSanitize, {
21-
...defaultSchema,
22-
attributes: {
23-
...defaultSchema.attributes,
24-
img: [...(defaultSchema?.attributes?.img || []), ['style']],
25-
},
26-
})
50+
.use(rehypeSanitize, CONFIG_SCHEMA)
2751
.use(rehypeStringify);
2852

2953
const Markdown: React.FC<Props> = (props) => {
3054
const { value = '', type, onChange, customClassName = '' } = props;
55+
const { view, classname: defaultClassName } = CONFIG_MAP[type];
3156

32-
let view;
33-
let classname = styles.markdown;
34-
35-
if (type === 'render') {
36-
view = {
37-
menu: false,
38-
md: false,
39-
html: true,
40-
};
41-
42-
classname += ` ${styles.markdown_render}`;
43-
}
44-
45-
if (type === 'editor') {
46-
view = {
47-
menu: true,
48-
md: true,
49-
html: false,
50-
};
51-
52-
classname += ` ${styles.markdown_editor}`;
53-
}
57+
let classname = `${styles.markdown} ${defaultClassName}`;
5458

5559
if (customClassName) {
5660
classname += ` ${customClassName}`;
@@ -63,8 +67,8 @@ const Markdown: React.FC<Props> = (props) => {
6367
view={view}
6468
value={value}
6569
renderHTML={async (text) => {
66-
const content: any = await processor.process(text);
67-
return content.value;
70+
const content = await processor.process(text);
71+
return content.toString();
6872
}}
6973
onChange={(data) => {
7074
onChange && onChange(data.text);
@@ -77,7 +81,8 @@ export default Markdown;
7781

7882
interface Props {
7983
type: 'editor' | 'render';
84+
customClassName?: string;
85+
8086
value?: string;
81-
customClassName?: '';
8287
onChange?: (text: string) => void;
8388
}

src/page/about/index.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@ CNode 社区由一批热爱 Node.js 技术的工程师发起,目前已经吸
1010
1111
CNode 的 SLA 保证是,一个9,即 90.000000%。
1212
13-
社区目前由 [@alsotang](http://cnodejs.org/user/alsotang) 在维护,有问题请联系:[https://github.com/alsotang](https://github.com/alsotang)
13+
社区目前由 [@alsotang](http://cnodejs.org/user/alsotang) 在维护,有问题请联系:https://github.com/alsotang
1414
15-
请关注我们的官方微博:[http://weibo.com/cnodejs](http://weibo.com/cnodejs)
15+
请关注我们的官方微博:http://weibo.com/cnodejs
1616
1717
1818
## 客户端
1919
2020
客户端由 [@soliury](https://cnodejs.org/user/soliury) 开发维护。
2121
22-
源码地址: [https://github.com/soliury/noder-react-native](https://github.com/soliury/noder-react-native)
22+
源码地址: https://github.com/soliury/noder-react-native 。
2323
2424
立即体验 CNode 客户端,直接扫描页面右侧二维码。
2525
26-
另,安卓用户同时可选择:[https://github.com/TakWolf/CNode-Material-Design](https://github.com/TakWolf/CNode-Material-Design) ,这是 Java 原生开发的安卓客户端。
26+
另,安卓用户同时可选择:https://github.com/TakWolf/CNode-Material-Design ,这是 Java 原生开发的安卓客户端。
2727
2828
2929
## 贡献者

0 commit comments

Comments
 (0)