@@ -3,54 +3,58 @@ import { unified } from 'unified';
3
3
import remarkParse from 'remark-parse' ;
4
4
import remarkRehype from 'remark-rehype' ;
5
5
import rehypeRaw from 'rehype-raw' ;
6
+ import remarkGfm from 'remark-gfm' ;
6
7
import rehypeAttr from 'rehype-attr' ;
7
8
import rehypeSanitize , { defaultSchema } from 'rehype-sanitize' ;
8
9
import rehypeStringify from 'rehype-stringify' ;
10
+ import type { Schema } from 'hast-util-sanitize' ;
9
11
10
12
import MdEditor from 'react-markdown-editor-lite' ;
11
13
// import 'react-markdown-editor-lite/esm/index.less';
12
14
13
15
import * as styles from './index.less' ;
14
16
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
+
15
44
const processor = unified ( )
16
45
. use ( remarkParse )
46
+ . use ( remarkGfm )
17
47
. use ( remarkRehype , { allowDangerousHtml : true } )
18
48
. use ( rehypeRaw )
19
49
. 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 )
27
51
. use ( rehypeStringify ) ;
28
52
29
53
const Markdown : React . FC < Props > = ( props ) => {
30
54
const { value = '' , type, onChange, customClassName = '' } = props ;
55
+ const { view, classname : defaultClassName } = CONFIG_MAP [ type ] ;
31
56
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 } ` ;
54
58
55
59
if ( customClassName ) {
56
60
classname += ` ${ customClassName } ` ;
@@ -63,8 +67,8 @@ const Markdown: React.FC<Props> = (props) => {
63
67
view = { view }
64
68
value = { value }
65
69
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 ( ) ;
68
72
} }
69
73
onChange = { ( data ) => {
70
74
onChange && onChange ( data . text ) ;
@@ -77,7 +81,8 @@ export default Markdown;
77
81
78
82
interface Props {
79
83
type : 'editor' | 'render' ;
84
+ customClassName ?: string ;
85
+
80
86
value ?: string ;
81
- customClassName ?: '' ;
82
87
onChange ?: ( text : string ) => void ;
83
88
}
0 commit comments