Skip to content

Commit 8405c79

Browse files
committed
feat: New syntax for adding attributes to Markdown.
1 parent b70c8e8 commit 8405c79

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"@mapbox/rehype-prism": "0.8.0",
5050
"rehype-autolink-headings": "6.1.0",
5151
"rehype-raw": "6.1.0",
52+
"rehype-attr": "2.0.6",
5253
"rehype-slug": "5.0.0",
5354
"rehype-rewrite": "3.0.4",
5455
"remark-gfm": "3.0.0",
@@ -67,7 +68,7 @@
6768
"@uiw/reset.css": "1.0.5",
6869
"compile-less-cli": "1.8.9",
6970
"kkt": "6.11.0",
70-
"jest": "27.2.4",
71+
"jest": "27.2.5",
7172
"react": "17.0.2",
7273
"react-dom": "17.0.2",
7374
"react-test-renderer": "17.0.2",

src/index.tsx

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import ReactMarkdown, { Options } from 'react-markdown';
33
import { Root, Element, ElementContent } from 'hast';
44
import gfm from 'remark-gfm';
55
import slug from 'rehype-slug';
6-
import headings from 'rehype-autolink-headings'
6+
import headings from 'rehype-autolink-headings';
77
import rehypeRaw from 'rehype-raw';
8+
import rehypeAttrs from 'rehype-attr';
89
// @ts-ignore
910
import rehypePrism from '@mapbox/rehype-prism';
1011
import rehypeRewrite from 'rehype-rewrite';
@@ -30,20 +31,20 @@ const octiconLink: Element = {
3031
properties: {
3132
fillRule: 'evenodd',
3233
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+
};
3738

3839
const rehypeRewriteHandle = (node: ElementContent, index: number | null, parent: Root | Element | null) => {
3940
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);
4142
if (child && child.properties && child.properties.ariaHidden === 'true') {
4243
child.properties = { class: 'anchor', ...child.properties };
43-
child.children = [ octiconLink ];
44+
child.children = [octiconLink];
4445
}
4546
}
46-
}
47+
};
4748

4849
export type MarkdownPreviewProps = {
4950
prefixCls?: string;
@@ -60,16 +61,33 @@ export type MarkdownPreviewRef = {
6061
} & MarkdownPreviewProps;
6162

6263
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;
6474
const mdp = React.createRef<HTMLDivElement>();
6575
useImperativeHandle(ref, () => ({ ...props, mdp }), [mdp, props]);
6676
const cls = `${prefixCls || ''} ${className || ''}`;
6777
return (
6878
<div ref={mdp} onScroll={onScroll} onMouseOver={onMouseOver} {...warpperElement} className={cls} style={style}>
6979
<ReactMarkdown
7080
{...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]}
7391
children={source || ''}
7492
/>
7593
</div>

0 commit comments

Comments
 (0)