1
1
import React , { useImperativeHandle } from 'react' ;
2
2
import ReactMarkdown , { Options } from 'react-markdown' ;
3
3
import { Root , Element , ElementContent } from 'hast' ;
4
+ import { PluggableList } from 'unified' ;
4
5
import gfm from 'remark-gfm' ;
5
6
import slug from 'rehype-slug' ;
6
7
import headings from 'rehype-autolink-headings' ;
@@ -44,6 +45,7 @@ export interface MarkdownPreviewProps extends Omit<Options, 'children'> {
44
45
className ?: string ;
45
46
source ?: string ;
46
47
style ?: React . CSSProperties ;
48
+ pluginsFilter ?: ( type : 'rehype' | 'remark' , plugin : PluggableList ) => PluggableList ;
47
49
warpperElement ?: React . DetailedHTMLProps < React . HTMLAttributes < HTMLDivElement > , HTMLDivElement > ;
48
50
onScroll ?: ( e : React . UIEvent < HTMLDivElement > ) => void ;
49
51
onMouseOver ?: ( e : React . MouseEvent < HTMLDivElement > ) => void ;
@@ -61,26 +63,29 @@ export default React.forwardRef<MarkdownPreviewRef, MarkdownPreviewProps>((props
61
63
style,
62
64
onScroll,
63
65
onMouseOver,
66
+ pluginsFilter,
64
67
warpperElement = { } ,
65
68
...other
66
69
} = props ;
67
70
const mdp = React . createRef < HTMLDivElement > ( ) ;
68
71
useImperativeHandle ( ref , ( ) => ( { ...props , mdp } ) , [ mdp , props ] ) ;
69
72
const cls = `${ prefixCls || '' } ${ className || '' } ` ;
73
+ const rehypePlugins : PluggableList = [
74
+ [ rehypePrism , { ignoreMissing : true } ] ,
75
+ rehypeRaw ,
76
+ slug ,
77
+ headings ,
78
+ [ rehypeRewrite , { rewrite : rehypeRewriteHandle } ] ,
79
+ [ rehypeAttrs , { properties : 'attr' } ] ,
80
+ ...( other . rehypePlugins || [ ] ) ,
81
+ ] ;
82
+ const remarkPlugins = [ ...( other . remarkPlugins || [ ] ) , gfm ] ;
70
83
return (
71
84
< div ref = { mdp } onScroll = { onScroll } onMouseOver = { onMouseOver } { ...warpperElement } className = { cls } style = { style } >
72
85
< ReactMarkdown
73
86
{ ...other }
74
- rehypePlugins = { [
75
- [ rehypePrism , { ignoreMissing : true } ] ,
76
- rehypeRaw ,
77
- slug ,
78
- headings ,
79
- [ rehypeRewrite , { rewrite : rehypeRewriteHandle } ] ,
80
- [ rehypeAttrs , { properties : 'attr' } ] ,
81
- ...( other . rehypePlugins || [ ] ) ,
82
- ] }
83
- remarkPlugins = { [ ...( other . remarkPlugins || [ ] ) , gfm ] }
87
+ rehypePlugins = { pluginsFilter ? pluginsFilter ( 'rehype' , rehypePlugins ) : rehypePlugins }
88
+ remarkPlugins = { pluginsFilter ? pluginsFilter ( 'remark' , remarkPlugins ) : remarkPlugins }
84
89
children = { source || '' }
85
90
/>
86
91
</ div >
0 commit comments