1
1
import React , { useImperativeHandle } from 'react' ;
2
2
import ReactMarkdown from 'react-markdown' ;
3
3
import gfm from 'remark-gfm' ;
4
+ // @ts -ignore
5
+ import slug from 'remark-slug' ;
6
+ // @ts -ignore
7
+ import headings from 'remark-autolink-headings'
4
8
import rehypeRaw from 'rehype-raw' ;
5
9
import rehypePrism from '@mapbox/rehype-prism' ;
10
+ import rehypeRewrite from 'rehype-rewrite' ;
6
11
import './styles/markdown.less' ;
7
12
import './styles/markdowncolor.less' ;
8
13
14
+ const rehypeRewriteHandle = ( node : any , index : number , parent : any ) => {
15
+ if ( node . type === 'element' && parent . type === 'root' && / h ( 1 | 2 | 3 | 4 | 5 | 6 ) / . test ( node . tagName ) && index !== 0 ) {
16
+ const child = node . children && node . children [ 0 ] ? node . children [ 0 ] : null ;
17
+ if ( child && child . properties && child . properties . ariaHidden === 'true' ) {
18
+ child . properties = { class : 'anchor' , ...child . properties } ;
19
+ child . children = [
20
+ {
21
+ type : 'element' ,
22
+ tagName : 'svg' ,
23
+ properties : {
24
+ class : 'octicon octicon-link' ,
25
+ viewBox : '0 0 16 16' ,
26
+ version : '1.1' ,
27
+ width : '16' ,
28
+ height : '16' ,
29
+ ariaHidden : 'true' ,
30
+ } ,
31
+ children : [
32
+ {
33
+ type : 'element' ,
34
+ tagName : 'path' ,
35
+ properties : {
36
+ fillRule : 'evenodd' ,
37
+ 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' ,
38
+ }
39
+ }
40
+ ]
41
+ }
42
+ ] ;
43
+ }
44
+ }
45
+ }
46
+
9
47
export type MarkdownPreviewProps = {
10
48
className ?: string ;
11
49
source ?: string ;
@@ -29,8 +67,9 @@ export default React.forwardRef<MarkdownPreviewRef, MarkdownPreviewProps>((props
29
67
< div ref = { mdp } onScroll = { onScroll } onMouseOver = { onMouseOver } { ...warpperElement } className = { cls } style = { style } >
30
68
< ReactMarkdown
31
69
{ ...other }
32
- plugins = { [ gfm , ...( other . plugins || [ ] ) ] }
33
- rehypePlugins = { [ [ rehypePrism , { ignoreMissing : true } ] , rehypeRaw , ...( other . rehypePlugins || [ ] ) ] }
70
+ plugins = { [ gfm , ...( other . plugins || [ ] ) ] }
71
+ rehypePlugins = { [ [ rehypePrism , { ignoreMissing : true } ] , [ rehypeRewrite , rehypeRewriteHandle ] , rehypeRaw , ...( other . rehypePlugins || [ ] ) ] }
72
+ remarkPlugins = { [ slug , headings , ...( other . remarkPlugins || [ ] ) ] }
34
73
children = { source || '' }
35
74
/>
36
75
</ div >
0 commit comments