Skip to content

Commit 859338b

Browse files
authored
feat: replace markdown-it with remark (#31)
1 parent 532ab2f commit 859338b

File tree

4 files changed

+35
-50
lines changed

4 files changed

+35
-50
lines changed

config/config.ts

-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ export default defineConfig({
9696
'//unpkg.com/[email protected]/dist/antd.js',
9797
'//unpkg.com/@ant-design/[email protected]/dist/index.umd.js',
9898
'//unpkg.com/[email protected]/dayjs.min.js',
99-
'//unpkg.com/[email protected]/dist/markdown-it.js',
10099
'//unpkg.com/[email protected]/lib/index.js',
101100
]
102101
: [
@@ -105,7 +104,6 @@ export default defineConfig({
105104
'//unpkg.com/[email protected]/dist/antd.min.js',
106105
'//unpkg.com/@ant-design/[email protected]/dist/index.umd.min.js',
107106
'//unpkg.com/[email protected]/dayjs.min.js',
108-
'//unpkg.com/[email protected]/dist/markdown-it.min.js',
109107
'//unpkg.com/[email protected]/lib/index.js',
110108
],
111109

package.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,14 @@
6060
"dependencies": {
6161
"dotenv": "^10.0.0",
6262
"react": "17.x",
63-
"react-dom": "17.x"
63+
"react-dom": "17.x",
64+
"rehype-attr": "^2.0.7",
65+
"rehype-raw": "^6.1.1",
66+
"rehype-sanitize": "^5.0.1",
67+
"rehype-stringify": "^9.0.2",
68+
"remark-parse": "^10.0.1",
69+
"remark-rehype": "^10.1.0",
70+
"unified": "^10.1.1"
6471
},
6572
"devDependencies": {
6673
"@ant-design/icons": "^4.7.0",
@@ -81,7 +88,6 @@
8188
"dayjs": "^1.10.7",
8289
"egg-ci": "^1.19.0",
8390
"lint-staged": "^10.0.7",
84-
"markdown-it": "^12.3.0",
8591
"prettier": "^2.2.0",
8692
"react-markdown-editor-lite": "^1.3.2",
8793
"semantic-release": "^18.0.1",

src/component/Markdown/index.tsx

+25-44
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,30 @@
11
import React from 'react';
2-
import MarkdownIt from 'markdown-it';
2+
import { unified } from 'unified';
3+
import remarkParse from 'remark-parse';
4+
import remarkRehype from 'remark-rehype';
5+
import rehypeRaw from 'rehype-raw';
6+
import rehypeAttr from 'rehype-attr';
7+
import rehypeSanitize, { defaultSchema } from 'rehype-sanitize';
8+
import rehypeStringify from 'rehype-stringify';
9+
310
import MdEditor from 'react-markdown-editor-lite';
411
// import 'react-markdown-editor-lite/esm/index.less';
512

613
import * as styles from './index.less';
714

8-
const mdParser = new MarkdownIt('commonmark', {
9-
html: false,
10-
});
11-
12-
const getAttributes = (content: string = 'image') => {
13-
const attrs = content.split(' ');
14-
const alt = attrs.shift();
15-
16-
const attributes = attrs.reduce((prev: string[], curr) => {
17-
const [key, value] = curr.split('=');
18-
19-
if (!key) {
20-
return prev;
21-
}
22-
23-
if (!value) {
24-
return prev.concat(`${key}`);
25-
}
26-
27-
return prev.concat(`${key}="${value}"`);
28-
}, []);
29-
30-
return {
31-
alt,
32-
attributes,
33-
};
34-
};
35-
36-
mdParser.renderer.rules.image = function (tokens, index) {
37-
const token = tokens[index];
38-
const srcIndex = token.attrIndex('src');
39-
40-
if (!token.attrs) {
41-
return '';
42-
}
43-
44-
const src = token.attrs[srcIndex][1];
45-
const content = mdParser.utils.escapeHtml(token.content);
46-
const { alt, attributes } = getAttributes(content);
47-
48-
return `<img src="${src}" alt="${alt}" ${attributes.join(' ')}/>`;
49-
};
15+
const processor = unified()
16+
.use(remarkParse)
17+
.use(remarkRehype, { allowDangerousHtml: true })
18+
.use(rehypeRaw)
19+
.use(rehypeAttr, { properties: 'attr' })
20+
.use(rehypeSanitize, {
21+
...defaultSchema,
22+
attributes: {
23+
...defaultSchema.attributes,
24+
img: [...(defaultSchema?.attributes?.img || []), ['style']],
25+
},
26+
})
27+
.use(rehypeStringify);
5028

5129
const Markdown: React.FC<Props> = (props) => {
5230
const { value = '', type, onChange, customClassName = '' } = props;
@@ -84,7 +62,10 @@ const Markdown: React.FC<Props> = (props) => {
8462
readOnly={type === 'render'}
8563
view={view}
8664
value={value}
87-
renderHTML={(text) => mdParser.render(text)}
65+
renderHTML={async (text) => {
66+
const content: any = await processor.process(text);
67+
return content.value;
68+
}}
8869
onChange={(data) => {
8970
onChange && onChange(data.text);
9071
}}

src/page/about/index.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ CNode 的 SLA 保证是,一个9,即 90.000000%。
2929
## 贡献者
3030
3131
> egg-cnode
32-
33-
[![contributors](https://ergatejs.implements.io/badges/contributors/cnodejs/egg-cnode.svg?owner=cnodejs&repo=egg-cnode&type=svg&width=1232&size=64&padding=8)](https://github.com/cnodejs/egg-cnode/graphs/contributors)
32+
33+
[![contributors](https://ergatejs.implements.io/badges/contributors/cnodejs/egg-cnode.svg?owner=cnodejs&repo=egg-cnode&type=svg&width=1232&size=64&padding=8)<!--rehype:style=width:50%;-->](https://github.com/cnodejs/egg-cnode/graphs/contributors)
3434
`;
3535

3636
const AboutPage: React.FC<Props> = (props) => {

0 commit comments

Comments
 (0)