@@ -9,33 +9,11 @@ import rehypeAttrs from 'rehype-attr';
9
9
// @ts -ignore
10
10
import rehypePrism from '@mapbox/rehype-prism' ;
11
11
import rehypeRewrite from 'rehype-rewrite' ;
12
+ import { octiconLink } from './nodes/octiconLink' ;
13
+ import { copyElement } from './nodes/copy' ;
12
14
import './styles/markdown.less' ;
13
15
import './styles/markdowncolor.less' ;
14
16
15
- const octiconLink : Element = {
16
- type : 'element' ,
17
- tagName : 'svg' ,
18
- properties : {
19
- class : 'octicon octicon-link' ,
20
- viewBox : '0 0 16 16' ,
21
- version : '1.1' ,
22
- width : '16' ,
23
- height : '16' ,
24
- ariaHidden : 'true' ,
25
- } ,
26
- children : [
27
- {
28
- type : 'element' ,
29
- tagName : 'path' ,
30
- children : [ ] ,
31
- properties : {
32
- fillRule : 'evenodd' ,
33
- 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' ,
34
- } ,
35
- } ,
36
- ] ,
37
- } ;
38
-
39
17
const rehypeRewriteHandle = ( node : ElementContent , index : number | null , parent : Root | Element | null ) => {
40
18
if ( node . type === 'element' && parent && parent . type === 'root' && / h ( 1 | 2 | 3 | 4 | 5 | 6 ) / . test ( node . tagName ) ) {
41
19
const child = node . children && ( node . children [ 0 ] as Element ) ;
@@ -44,6 +22,21 @@ const rehypeRewriteHandle = (node: ElementContent, index: number | null, parent:
44
22
child . children = [ octiconLink ] ;
45
23
}
46
24
}
25
+ if ( node . type === 'element' && node . tagName === 'pre' ) {
26
+ const code = getCodeStr ( node . children ) ;
27
+ node . children . unshift ( copyElement ( code ) ) ;
28
+ }
29
+ } ;
30
+
31
+ const getCodeStr = ( data : ElementContent [ ] = [ ] , code : string = '' ) => {
32
+ data . forEach ( ( node ) => {
33
+ if ( node . type === 'text' ) {
34
+ code += node . value ;
35
+ } else if ( node . type === 'element' && node . children && Array . isArray ( node . children ) ) {
36
+ code += getCodeStr ( node . children ) ;
37
+ }
38
+ } ) ;
39
+ return code ;
47
40
} ;
48
41
49
42
export interface MarkdownPreviewProps extends Omit < Options , 'children' > {
0 commit comments