File tree 1 file changed +42
-1
lines changed
1 file changed +42
-1
lines changed Original file line number Diff line number Diff line change @@ -5,7 +5,48 @@ import MdEditor from 'react-markdown-editor-lite';
5
5
6
6
import * as styles from './index.less' ;
7
7
8
- const mdParser = new MarkdownIt ( ) ;
8
+ const mdParser = new MarkdownIt ( 'commonmark' , {
9
+ html : true ,
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
+ } ;
9
50
10
51
const Markdown : React . FC < Props > = ( props ) => {
11
52
const { value = '' , type, onChange, customClassName = '' } = props ;
You can’t perform that action at this time.
0 commit comments