Skip to content

Commit ba0598c

Browse files
committed
feat: add pluginsFilter props.
1 parent f3e92b9 commit ba0598c

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

src/index.tsx

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { useImperativeHandle } from 'react';
22
import ReactMarkdown, { Options } from 'react-markdown';
33
import { Root, Element, ElementContent } from 'hast';
4+
import { PluggableList } from 'unified';
45
import gfm from 'remark-gfm';
56
import slug from 'rehype-slug';
67
import headings from 'rehype-autolink-headings';
@@ -44,6 +45,7 @@ export interface MarkdownPreviewProps extends Omit<Options, 'children'> {
4445
className?: string;
4546
source?: string;
4647
style?: React.CSSProperties;
48+
pluginsFilter?: (type: 'rehype' | 'remark', plugin: PluggableList) => PluggableList;
4749
warpperElement?: React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>;
4850
onScroll?: (e: React.UIEvent<HTMLDivElement>) => void;
4951
onMouseOver?: (e: React.MouseEvent<HTMLDivElement>) => void;
@@ -61,26 +63,29 @@ export default React.forwardRef<MarkdownPreviewRef, MarkdownPreviewProps>((props
6163
style,
6264
onScroll,
6365
onMouseOver,
66+
pluginsFilter,
6467
warpperElement = {},
6568
...other
6669
} = props;
6770
const mdp = React.createRef<HTMLDivElement>();
6871
useImperativeHandle(ref, () => ({ ...props, mdp }), [mdp, props]);
6972
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];
7083
return (
7184
<div ref={mdp} onScroll={onScroll} onMouseOver={onMouseOver} {...warpperElement} className={cls} style={style}>
7285
<ReactMarkdown
7386
{...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}
8489
children={source || ''}
8590
/>
8691
</div>

0 commit comments

Comments
 (0)