1
- import React , { useEffect , useImperativeHandle } from 'react' ;
1
+ import React , { useImperativeHandle } from 'react' ;
2
2
import ReactMarkdown from 'react-markdown' ;
3
3
import gfm from 'remark-gfm' ;
4
4
import rehypeRaw from 'rehype-raw' ;
5
- import Prism from 'prismjs' ;
6
- import 'prismjs/components/prism-markup' ;
7
- import { loadLang } from './langs' ;
5
+ import rehypePrism from '@mapbox/rehype-prism' ;
8
6
import './styles/markdown.less' ;
9
7
import './styles/markdowncolor.less' ;
10
8
@@ -19,48 +17,22 @@ export type MarkdownPreviewProps = {
19
17
20
18
export type MarkdownPreviewRef = {
21
19
mdp : React . RefObject < HTMLDivElement > ;
22
- lang : string [ ] ,
23
20
} & MarkdownPreviewProps ;
24
21
25
22
export default React . forwardRef < MarkdownPreviewRef , MarkdownPreviewProps > ( ( props , ref ) => {
26
23
const { className, source, style, onScroll, onMouseOver, warpperElement = { } , ...other } = props || { } ;
27
24
const mdp = React . createRef < HTMLDivElement > ( ) ;
28
- const loadedLang = React . useRef < string [ ] > ( [ 'markup' ] ) ;
29
- useEffect ( ( ) => {
30
- highlight ( ) ;
31
- // eslint-disable-next-line react-hooks/exhaustive-deps
32
- } , [ source ] ) ;
33
-
34
- async function highlight ( ) {
35
- if ( ! mdp . current ) return ;
36
- const codes = mdp . current . getElementsByTagName ( 'code' ) as unknown as HTMLElement [ ] ;
37
- for ( const val of codes ) {
38
- const tag = val . parentNode as HTMLElement ;
39
- if ( tag && tag . tagName === 'PRE' && / ^ l a n g u a g e - / . test ( val . className . trim ( ) ) ) {
40
- const lang = val . className . trim ( ) . replace ( / ^ l a n g u a g e - / , '' ) ;
41
- try {
42
- if ( ! loadedLang . current . includes ( lang as never ) ) {
43
- loadedLang . current . push ( lang ) ;
44
- await loadLang ( lang ) ;
45
- }
46
- await Prism . highlightElement ( val ) ;
47
- } catch ( error ) { }
48
- }
49
- }
50
- }
51
-
52
- useImperativeHandle ( ref , ( ) => ( { ...props , lang : loadedLang . current , mdp } ) , [ mdp , props ] ) ;
25
+ useImperativeHandle ( ref , ( ) => ( { ...props , mdp } ) , [ mdp , props ] ) ;
53
26
54
27
const cls = `wmde-markdown wmde-markdown-color ${ className || '' } ` ;
55
28
return (
56
29
< div ref = { mdp } onScroll = { onScroll } onMouseOver = { onMouseOver } { ...warpperElement } className = { cls } style = { style } >
57
30
< ReactMarkdown
58
31
{ ...other }
59
32
plugins = { [ gfm , ...( other . plugins || [ ] ) ] }
60
- rehypePlugins = { [ rehypeRaw , ...( other . rehypePlugins || [ ] ) ] }
61
- >
62
- { source || '' }
63
- </ ReactMarkdown >
33
+ rehypePlugins = { [ rehypePrism , rehypeRaw , ...( other . rehypePlugins || [ ] ) ] }
34
+ children = { source || '' }
35
+ />
64
36
</ div >
65
37
) ;
66
38
} ) ;
0 commit comments