Skip to content

Commit cac44b2

Browse files
committed
fix: Fix the code highlight issue.
1 parent e0f3e89 commit cac44b2

File tree

4 files changed

+12
-294
lines changed

4 files changed

+12
-294
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@
4545
},
4646
"dependencies": {
4747
"@babel/runtime": "7.13.10",
48-
"@types/prismjs": "1.16.4",
49-
"prismjs": "1.23.0",
48+
"@mapbox/rehype-prism": "0.6.0",
5049
"rehype-raw": "5.1.0",
5150
"remark-gfm": "1.0.0",
5251
"react-markdown": "6.0.1"

src/index.tsx

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import React, { useEffect, useImperativeHandle } from 'react';
1+
import React, { useImperativeHandle } from 'react';
22
import ReactMarkdown from 'react-markdown';
33
import gfm from 'remark-gfm';
44
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';
86
import './styles/markdown.less';
97
import './styles/markdowncolor.less';
108

@@ -19,48 +17,22 @@ export type MarkdownPreviewProps = {
1917

2018
export type MarkdownPreviewRef = {
2119
mdp: React.RefObject<HTMLDivElement>;
22-
lang: string[],
2320
} & MarkdownPreviewProps;
2421

2522
export 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' && /^language-/.test(val.className.trim())) {
40-
const lang = val.className.trim().replace(/^language-/, '');
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
});

src/langs.ts

Lines changed: 0 additions & 258 deletions
This file was deleted.

src/react-app-env.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
/// <reference types="react-scripts" />
2+
3+
declare module '@mapbox/rehype-prism' {
4+
const src: any;
5+
export default src;
6+
}

0 commit comments

Comments
 (0)