|
1 |
| -import React, { useImperativeHandle } from 'react'; |
2 |
| -import ReactMarkdown, { Options } from 'react-markdown'; |
3 |
| -import { Element } from 'hast'; |
| 1 | +import React from 'react'; |
| 2 | +import MarkdownPreview, { type MarkdownPreviewProps, type MarkdownPreviewRef } from './preview'; |
| 3 | +import rehypePrism from 'rehype-prism-plus'; |
4 | 4 | import { PluggableList } from 'unified';
|
5 |
| -import gfm from 'remark-gfm'; |
6 |
| -import raw from 'rehype-raw'; |
7 |
| -import slug from 'rehype-slug'; |
8 |
| -import headings from 'rehype-autolink-headings'; |
| 5 | +import rehypeRewrite from 'rehype-rewrite'; |
9 | 6 | import rehypeAttrs from 'rehype-attr';
|
10 |
| -import rehypeIgnore from 'rehype-ignore'; |
11 |
| -import rehypePrism from 'rehype-prism-plus'; |
12 |
| -import rehypeRewrite, { getCodeString, RehypeRewriteOptions } from 'rehype-rewrite'; |
13 |
| -import { octiconLink } from './nodes/octiconLink'; |
14 |
| -import { copyElement } from './nodes/copy'; |
15 |
| -import { useCopied } from './plugins/useCopied'; |
16 |
| -import './styles/markdown.less'; |
17 |
| - |
18 | 7 | import { reservedMeta } from './plugins/reservedMeta';
|
19 |
| - |
20 |
| -export interface MarkdownPreviewProps extends Omit<Options, 'children'> { |
21 |
| - prefixCls?: string; |
22 |
| - className?: string; |
23 |
| - source?: string; |
24 |
| - disableCopy?: boolean; |
25 |
| - style?: React.CSSProperties; |
26 |
| - pluginsFilter?: (type: 'rehype' | 'remark', plugin: PluggableList) => PluggableList; |
27 |
| - wrapperElement?: React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement> & { |
28 |
| - 'data-color-mode'?: 'light' | 'dark'; |
29 |
| - }; |
30 |
| - /** |
31 |
| - * Please use wrapperElement, Will be removed in v5 release. |
32 |
| - * @deprecated |
33 |
| - */ |
34 |
| - warpperElement?: React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement> & { |
35 |
| - 'data-color-mode'?: 'light' | 'dark'; |
36 |
| - }; |
37 |
| - onScroll?: (e: React.UIEvent<HTMLDivElement>) => void; |
38 |
| - onMouseOver?: (e: React.MouseEvent<HTMLDivElement>) => void; |
39 |
| - rehypeRewrite?: RehypeRewriteOptions['rewrite']; |
40 |
| -} |
41 |
| - |
42 |
| -export interface MarkdownPreviewRef extends MarkdownPreviewProps { |
43 |
| - mdp: React.RefObject<HTMLDivElement>; |
44 |
| -} |
| 8 | +import { rehypeRewriteHandle, defaultRehypePlugins } from './rehypePlugins'; |
45 | 9 |
|
46 | 10 | export default React.forwardRef<MarkdownPreviewRef, MarkdownPreviewProps>((props, ref) => {
|
47 |
| - const { |
48 |
| - prefixCls = 'wmde-markdown wmde-markdown-color', |
49 |
| - className, |
50 |
| - source, |
51 |
| - style, |
52 |
| - disableCopy = false, |
53 |
| - skipHtml = true, |
54 |
| - onScroll, |
55 |
| - onMouseOver, |
56 |
| - pluginsFilter, |
57 |
| - rehypeRewrite: rewrite, |
58 |
| - wrapperElement = {}, |
59 |
| - warpperElement = {}, |
60 |
| - ...other |
61 |
| - } = props; |
62 |
| - const mdp = React.useRef<HTMLDivElement>(null); |
63 |
| - useImperativeHandle(ref, () => ({ ...props, mdp }), [mdp, props]); |
64 |
| - const cls = `${prefixCls || ''} ${className || ''}`; |
65 |
| - useCopied(mdp); |
66 |
| - |
67 |
| - const rehypeRewriteHandle: RehypeRewriteOptions['rewrite'] = (node, index, parent) => { |
68 |
| - if (node.type === 'element' && parent && parent.type === 'root' && /h(1|2|3|4|5|6)/.test(node.tagName)) { |
69 |
| - const child = node.children && (node.children[0] as Element); |
70 |
| - if (child && child.properties && child.properties.ariaHidden === 'true') { |
71 |
| - child.properties = { class: 'anchor', ...child.properties }; |
72 |
| - child.children = [octiconLink]; |
73 |
| - } |
74 |
| - } |
75 |
| - if (node.type === 'element' && node.tagName === 'pre' && !disableCopy) { |
76 |
| - const code = getCodeString(node.children); |
77 |
| - node.children.push(copyElement(code)); |
78 |
| - } |
79 |
| - rewrite && rewrite(node, index, parent); |
80 |
| - }; |
81 |
| - |
82 | 11 | const rehypePlugins: PluggableList = [
|
83 | 12 | reservedMeta,
|
84 | 13 | [rehypePrism, { ignoreMissing: true }],
|
85 |
| - slug, |
86 |
| - headings, |
87 |
| - rehypeIgnore, |
88 |
| - [rehypeRewrite, { rewrite: rehypeRewriteHandle }], |
| 14 | + ...defaultRehypePlugins, |
| 15 | + [rehypeRewrite, { rewrite: rehypeRewriteHandle(props.disableCopy ?? false, props.rehypeRewrite) }], |
89 | 16 | [rehypeAttrs, { properties: 'attr' }],
|
90 |
| - ...(other.rehypePlugins || []), |
| 17 | + ...(props.rehypePlugins || []), |
91 | 18 | ];
|
92 |
| - const customProps: MarkdownPreviewProps = { |
93 |
| - allowElement: (element, index, parent) => { |
94 |
| - if (other.allowElement) { |
95 |
| - return other.allowElement(element, index, parent); |
96 |
| - } |
97 |
| - return /^[A-Za-z0-9]+$/.test(element.tagName); |
98 |
| - }, |
99 |
| - }; |
100 |
| - if (skipHtml) { |
101 |
| - rehypePlugins.push(raw); |
102 |
| - } |
103 |
| - const remarkPlugins = [...(other.remarkPlugins || []), gfm]; |
104 |
| - const wrapperProps = { ...warpperElement, ...wrapperElement }; |
105 |
| - return ( |
106 |
| - <div ref={mdp} onScroll={onScroll} onMouseOver={onMouseOver} {...wrapperProps} className={cls} style={style}> |
107 |
| - <ReactMarkdown |
108 |
| - {...customProps} |
109 |
| - {...other} |
110 |
| - skipHtml={skipHtml} |
111 |
| - rehypePlugins={pluginsFilter ? pluginsFilter('rehype', rehypePlugins) : rehypePlugins} |
112 |
| - remarkPlugins={pluginsFilter ? pluginsFilter('remark', remarkPlugins) : remarkPlugins} |
113 |
| - children={source || ''} |
114 |
| - /> |
115 |
| - </div> |
116 |
| - ); |
| 19 | + return <MarkdownPreview {...props} rehypePlugins={rehypePlugins} ref={ref} />; |
117 | 20 | });
|
0 commit comments