Skip to content

Commit

Permalink
✨ feat: remove highlight.js and use shiki only
Browse files Browse the repository at this point in the history
  • Loading branch information
rdmclin2 committed Mar 21, 2024
1 parent d3bf590 commit cb0a31d
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 272 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@
"copy-to-clipboard": "^3.3.3",
"diff": "^5.1.0",
"fast-deep-equal": "^3.1.3",
"highlight.js": "~10.5.0",
"immer": "^9.0.21",
"lodash.flatten": "^4.4.0",
"lodash.get": "^4.4.2",
Expand Down
65 changes: 0 additions & 65 deletions src/Highlight/components/HighLightJS/index.tsx

This file was deleted.

82 changes: 0 additions & 82 deletions src/Highlight/components/HighLightJS/style.ts

This file was deleted.

38 changes: 11 additions & 27 deletions src/Highlight/components/HighLighter/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,30 @@
import { THEME_LIGHT } from '@/Highlight/theme';
import { Loading3QuartersOutlined as Loading } from '@ant-design/icons';
import classNames from 'classnames';
import { memo, useMemo } from 'react';
import { memo } from 'react';
import { Center } from 'react-layout-kit';
import { useShiki } from '../../hooks/useShiki';
import { HighlightProps } from '../../index';
import HighLightJS from '../HighLightJS';
import { useStyles } from './style';

export type ShikiProps = Pick<
HighlightProps,
'language' | 'children' | 'theme' | 'lineNumber' | 'shiki' | 'className' | 'style'
'language' | 'children' | 'theme' | 'lineNumber' | 'className' | 'style'
>;

const HighLighter: React.FC<ShikiProps> = memo((props) => {
const { children, lineNumber = false, theme = THEME_LIGHT, language, shiki = true } = props;
const { children, lineNumber = false, theme = THEME_LIGHT, language } = props;
const { styles } = useStyles({ lineNumber, theme });
const { renderShiki, loading } = useShiki(language, theme, shiki);
const { renderShiki, loading } = useShiki(language, theme);

const HighlightJSBlock = useMemo(
() => (
<HighLightJS lineNumber={lineNumber} theme={theme} language={language}>
{children}
</HighLightJS>
),
[lineNumber, theme, language, children],
);

return shiki === false ? (
HighlightJSBlock
) : (
return (
<>
{loading ? (
HighlightJSBlock
) : (
<div
className={classNames(styles.shiki)}
dangerouslySetInnerHTML={{
__html: renderShiki(children) || '',
}}
/>
)}
<div
className={classNames(styles.shiki)}
dangerouslySetInnerHTML={{
__html: renderShiki(children) || '',
}}
/>
{loading ? (
<Center className={styles.center} gap={8} horizontal>
<Loading spin className={styles.loading} />
Expand Down
4 changes: 0 additions & 4 deletions src/Highlight/demos/lineNumber.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
*/

import { Highlight } from '@ant-design/pro-editor';

export default () => (
Expand Down
15 changes: 0 additions & 15 deletions src/Highlight/demos/shiki.tsx

This file was deleted.

4 changes: 0 additions & 4 deletions src/Highlight/demos/theme.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
*/

import { Highlight } from '@ant-design/pro-editor';
import { Select, Space } from 'antd';
import { useState } from 'react';
Expand Down
56 changes: 0 additions & 56 deletions src/Highlight/hooks/useHighlight.tsx

This file was deleted.

25 changes: 20 additions & 5 deletions src/Highlight/hooks/useShiki.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
import { useEffect, useState } from 'react';
import { getHighlighter, type Highlighter } from 'shiki/bundle/web';
import { themeConfig } from '../theme';
import { languageMap } from './useHighlight';

// 目前支持的语言列表
export const HIGHLIGHT_LANGUAGES = Object.keys(languageMap);
export const HIGHLIGHT_LANGUAGES = [
'javascript',
'typescript',
'css',
'json',
'markdown',
'xml',
'html',
'yaml',
'tsx',
'jsx',
'java',
'python',
'sql',
'bash',
'sh',
];

export const useShiki = (language, theme, isShiki) => {
export const useShiki = (language, theme) => {
const [shiki, setShiki] = useState<Highlighter>(null);

const initShiki = async () => {
Expand All @@ -18,8 +33,8 @@ export const useShiki = (language, theme, isShiki) => {
};

useEffect(() => {
if (isShiki) initShiki();
}, [isShiki]);
initShiki();
}, []);

const renderShiki = (content) => {
if (shiki && shiki.getLoadedLanguages().includes(language)) {
Expand Down
4 changes: 1 addition & 3 deletions src/Highlight/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ group: Basic

# Highlight

Used to display code, Highlight defaults to using [Shiki](https://github.com/shikijs/shiki) for rendering. Shiki uses a TextMate grammar for more precision, but it requires loading additional wasm files, which may result in a poor experience in case of poor network conditions. If initialization fails, the component will set the renderer to [highlight.js](https://highlightjs.org/) as a replacement.
Used to display code, Highlight defaults to using [Shiki](https://github.com/shikijs/shiki) for rendering. Shiki uses a TextMate grammar for more precision.

## Code Demo

<code src="./demos/auto.tsx" title="Automatic Theme" description="By default, `auto` is enabled, which will automatically switch themes based on the current environment"></code>

<code src="./demos/shiki.tsx" title="Disable Shiki" description="By default, `shiki` rendering is enabled, but it can be disabled by setting `shiki=false` in weak or no network situations"></code>

<code src="./demos/basic.tsx" title="Specify Language and Theme" description="You can specify the language for highlighting using `language` and the highlighting theme using `theme`"></code>

<code src="./demos/lineNumber.tsx" title="Display Code Block Line Numbers"
Expand Down
7 changes: 1 addition & 6 deletions src/Highlight/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ export interface HighlightProps {
* 是否需默认展示语言种类
*/
showLanguage?: boolean;
/**
* Shiki 高亮组件
*/
shiki?: boolean;
}

const BaseHighlight: React.FC<HighlightProps> = memo((props) => {
Expand All @@ -78,7 +74,6 @@ const BaseHighlight: React.FC<HighlightProps> = memo((props) => {
copyable = true,
theme: outTheme = THEME_AUTO,
language = 'tsx',
shiki = true,
showLanguage = true,
type = 'block',
onCopy,
Expand All @@ -103,7 +98,7 @@ const BaseHighlight: React.FC<HighlightProps> = memo((props) => {
{showLanguage && language && (
<LanguageTag theme={theme}>{language.toLowerCase()}</LanguageTag>
)}
<HighLighter lineNumber={lineNumber} language={language} theme={theme} shiki={shiki}>
<HighLighter lineNumber={lineNumber} language={language} theme={theme}>
{children}
</HighLighter>
</div>
Expand Down
Loading

0 comments on commit cb0a31d

Please sign in to comment.