1- import React , { useEffect , useImperativeHandle } from 'react' ;
1+ import React , { useImperativeHandle } from 'react' ;
22import ReactMarkdown from 'react-markdown' ;
33import gfm from 'remark-gfm' ;
44import 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' ;
86import './styles/markdown.less' ;
97import './styles/markdowncolor.less' ;
108
@@ -19,48 +17,22 @@ export type MarkdownPreviewProps = {
1917
2018export type MarkdownPreviewRef = {
2119 mdp : React . RefObject < HTMLDivElement > ;
22- lang : string [ ] ,
2320} & MarkdownPreviewProps ;
2421
2522export default React . forwardRef < MarkdownPreviewRef , MarkdownPreviewProps > ( ( props , ref ) => {
2623 const { className, source, style, onScroll, onMouseOver, warpperElement = { } , ...other } = props || { } ;
2724 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 ] ) ;
5326
5427 const cls = `wmde-markdown wmde-markdown-color ${ className || '' } ` ;
5528 return (
5629 < div ref = { mdp } onScroll = { onScroll } onMouseOver = { onMouseOver } { ...warpperElement } className = { cls } style = { style } >
5730 < ReactMarkdown
5831 { ...other }
5932 plugins = { [ gfm , ...( other . plugins || [ ] ) ] }
60- rehypePlugins = { [ rehypeRaw , ...( other . rehypePlugins || [ ] ) ] }
61- >
62- { source || '' }
63- </ ReactMarkdown >
33+ rehypePlugins = { [ rehypePrism , rehypeRaw , ...( other . rehypePlugins || [ ] ) ] }
34+ children = { source || '' }
35+ />
6436 </ div >
6537 ) ;
6638} ) ;
0 commit comments