@@ -3,8 +3,9 @@ import ReactMarkdown, { Options } from 'react-markdown';
3
3
import { Root , Element , ElementContent } from 'hast' ;
4
4
import gfm from 'remark-gfm' ;
5
5
import slug from 'rehype-slug' ;
6
- import headings from 'rehype-autolink-headings'
6
+ import headings from 'rehype-autolink-headings' ;
7
7
import rehypeRaw from 'rehype-raw' ;
8
+ import rehypeAttrs from 'rehype-attr' ;
8
9
// @ts -ignore
9
10
import rehypePrism from '@mapbox/rehype-prism' ;
10
11
import rehypeRewrite from 'rehype-rewrite' ;
@@ -30,20 +31,20 @@ const octiconLink: Element = {
30
31
properties : {
31
32
fillRule : 'evenodd' ,
32
33
d : 'M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z' ,
33
- }
34
- }
35
- ]
36
- }
34
+ } ,
35
+ } ,
36
+ ] ,
37
+ } ;
37
38
38
39
const rehypeRewriteHandle = ( node : ElementContent , index : number | null , parent : Root | Element | null ) => {
39
40
if ( node . type === 'element' && parent && parent . type === 'root' && / h ( 1 | 2 | 3 | 4 | 5 | 6 ) / . test ( node . tagName ) ) {
40
- const child = node . children && node . children [ 0 ] as Element ;
41
+ const child = node . children && ( node . children [ 0 ] as Element ) ;
41
42
if ( child && child . properties && child . properties . ariaHidden === 'true' ) {
42
43
child . properties = { class : 'anchor' , ...child . properties } ;
43
- child . children = [ octiconLink ] ;
44
+ child . children = [ octiconLink ] ;
44
45
}
45
46
}
46
- }
47
+ } ;
47
48
48
49
export type MarkdownPreviewProps = {
49
50
prefixCls ?: string ;
@@ -60,16 +61,33 @@ export type MarkdownPreviewRef = {
60
61
} & MarkdownPreviewProps ;
61
62
62
63
export default React . forwardRef < MarkdownPreviewRef , MarkdownPreviewProps > ( ( props , ref ) => {
63
- const { prefixCls = 'wmde-markdown wmde-markdown-color' , className, source, style, onScroll, onMouseOver, warpperElement = { } , ...other } = props ;
64
+ const {
65
+ prefixCls = 'wmde-markdown wmde-markdown-color' ,
66
+ className,
67
+ source,
68
+ style,
69
+ onScroll,
70
+ onMouseOver,
71
+ warpperElement = { } ,
72
+ ...other
73
+ } = props ;
64
74
const mdp = React . createRef < HTMLDivElement > ( ) ;
65
75
useImperativeHandle ( ref , ( ) => ( { ...props , mdp } ) , [ mdp , props ] ) ;
66
76
const cls = `${ prefixCls || '' } ${ className || '' } ` ;
67
77
return (
68
78
< div ref = { mdp } onScroll = { onScroll } onMouseOver = { onMouseOver } { ...warpperElement } className = { cls } style = { style } >
69
79
< ReactMarkdown
70
80
{ ...other }
71
- rehypePlugins = { [ [ rehypePrism , { ignoreMissing : true } ] , rehypeRaw , slug , headings , [ rehypeRewrite , { rewrite : rehypeRewriteHandle } ] , ...( other . rehypePlugins || [ ] ) ] }
72
- remarkPlugins = { [ ...( other . remarkPlugins || [ ] ) , gfm ] }
81
+ rehypePlugins = { [
82
+ [ rehypePrism , { ignoreMissing : true } ] ,
83
+ rehypeRaw ,
84
+ slug ,
85
+ headings ,
86
+ [ rehypeRewrite , { rewrite : rehypeRewriteHandle } ] ,
87
+ [ rehypeAttrs , { properties : 'attr' } ] ,
88
+ ...( other . rehypePlugins || [ ] ) ,
89
+ ] }
90
+ remarkPlugins = { [ ...( other . remarkPlugins || [ ] ) , gfm ] }
73
91
children = { source || '' }
74
92
/>
75
93
</ div >
0 commit comments