Skip to content

Commit

Permalink
feat: replace markdown-it with remark (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
thonatos authored Jan 19, 2022
1 parent 532ab2f commit 859338b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 50 deletions.
2 changes: 0 additions & 2 deletions config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ export default defineConfig({
'//unpkg.com/[email protected]/dist/antd.js',
'//unpkg.com/@ant-design/[email protected]/dist/index.umd.js',
'//unpkg.com/[email protected]/dayjs.min.js',
'//unpkg.com/[email protected]/dist/markdown-it.js',
'//unpkg.com/[email protected]/lib/index.js',
]
: [
Expand All @@ -105,7 +104,6 @@ export default defineConfig({
'//unpkg.com/[email protected]/dist/antd.min.js',
'//unpkg.com/@ant-design/[email protected]/dist/index.umd.min.js',
'//unpkg.com/[email protected]/dayjs.min.js',
'//unpkg.com/[email protected]/dist/markdown-it.min.js',
'//unpkg.com/[email protected]/lib/index.js',
],

Expand Down
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,14 @@
"dependencies": {
"dotenv": "^10.0.0",
"react": "17.x",
"react-dom": "17.x"
"react-dom": "17.x",
"rehype-attr": "^2.0.7",
"rehype-raw": "^6.1.1",
"rehype-sanitize": "^5.0.1",
"rehype-stringify": "^9.0.2",
"remark-parse": "^10.0.1",
"remark-rehype": "^10.1.0",
"unified": "^10.1.1"
},
"devDependencies": {
"@ant-design/icons": "^4.7.0",
Expand All @@ -81,7 +88,6 @@
"dayjs": "^1.10.7",
"egg-ci": "^1.19.0",
"lint-staged": "^10.0.7",
"markdown-it": "^12.3.0",
"prettier": "^2.2.0",
"react-markdown-editor-lite": "^1.3.2",
"semantic-release": "^18.0.1",
Expand Down
69 changes: 25 additions & 44 deletions src/component/Markdown/index.tsx
Original file line number Diff line number Diff line change
@@ -1,52 +1,30 @@
import React from 'react';
import MarkdownIt from 'markdown-it';
import { unified } from 'unified';
import remarkParse from 'remark-parse';
import remarkRehype from 'remark-rehype';
import rehypeRaw from 'rehype-raw';
import rehypeAttr from 'rehype-attr';
import rehypeSanitize, { defaultSchema } from 'rehype-sanitize';
import rehypeStringify from 'rehype-stringify';

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

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

const mdParser = new MarkdownIt('commonmark', {
html: false,
});

const getAttributes = (content: string = 'image') => {
const attrs = content.split(' ');
const alt = attrs.shift();

const attributes = attrs.reduce((prev: string[], curr) => {
const [key, value] = curr.split('=');

if (!key) {
return prev;
}

if (!value) {
return prev.concat(`${key}`);
}

return prev.concat(`${key}="${value}"`);
}, []);

return {
alt,
attributes,
};
};

mdParser.renderer.rules.image = function (tokens, index) {
const token = tokens[index];
const srcIndex = token.attrIndex('src');

if (!token.attrs) {
return '';
}

const src = token.attrs[srcIndex][1];
const content = mdParser.utils.escapeHtml(token.content);
const { alt, attributes } = getAttributes(content);

return `<img src="${src}" alt="${alt}" ${attributes.join(' ')}/>`;
};
const processor = unified()
.use(remarkParse)
.use(remarkRehype, { allowDangerousHtml: true })
.use(rehypeRaw)
.use(rehypeAttr, { properties: 'attr' })
.use(rehypeSanitize, {
...defaultSchema,
attributes: {
...defaultSchema.attributes,
img: [...(defaultSchema?.attributes?.img || []), ['style']],
},
})
.use(rehypeStringify);

const Markdown: React.FC<Props> = (props) => {
const { value = '', type, onChange, customClassName = '' } = props;
Expand Down Expand Up @@ -84,7 +62,10 @@ const Markdown: React.FC<Props> = (props) => {
readOnly={type === 'render'}
view={view}
value={value}
renderHTML={(text) => mdParser.render(text)}
renderHTML={async (text) => {
const content: any = await processor.process(text);
return content.value;
}}
onChange={(data) => {
onChange && onChange(data.text);
}}
Expand Down
4 changes: 2 additions & 2 deletions src/page/about/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ CNode 的 SLA 保证是,一个9,即 90.000000%。
## 贡献者
> egg-cnode
[![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)
[![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)
`;

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

0 comments on commit 859338b

Please sign in to comment.