Skip to content

Commit

Permalink
✨ feat: use shikiji to replace shikiji-es
Browse files Browse the repository at this point in the history
feat: use shikiji to replace shikiji-es
  • Loading branch information
rdmclin2 committed Nov 6, 2023
1 parent fc0b503 commit 9621cc2
Show file tree
Hide file tree
Showing 4 changed files with 353 additions and 50 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
"react-rnd": "^10.4.1",
"reactflow": "^11.8.3",
"rxjs": "^7.8.1",
"shiki-es": "~0.2.0",
"shikiji": "^0.6.12",
"type-fest": "^3.13.1",
"umi-request": "^1.4.0",
"use-merge-value": "^1.2.0",
Expand Down
9 changes: 8 additions & 1 deletion src/Highlight/components/HighLighter/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* 如果没有在 https://github.com/highlightjs/highlight.js/tree/master/src/languages 中查找是否支持,然后添加
* 优先支持主流语言,没有import在代码中使用的不会打包
*/
import { THEME_LIGHT } from '@/Highlight/theme';
import { STUDIO_UI_PREFIX } from '@/theme';
import { Loading3QuartersOutlined as Loading } from '@ant-design/icons';
import classNames from 'classnames';
Expand All @@ -19,7 +20,13 @@ export type ShikiProps = Pick<
>;

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

Expand Down
8 changes: 2 additions & 6 deletions src/Highlight/hooks/useShiki.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { useEffect, useState } from 'react';
import { getHighlighter, setCDN, type Highlighter } from 'shiki-es';
import { getHighlighter, type Highlighter } from 'shikiji';
import { themeConfig } from '../theme';

// 国内使用 CDN 加速, 测试环境为 node,会加载失败
if (process.env.NODE_ENV !== 'test') {
setCDN('https://npm.elemecdn.com/shiki-es/dist/assets');
}

// 目前支持的语言列表
export const languageMap = [
'javascript',
Expand All @@ -21,6 +16,7 @@ export const languageMap = [
'java',
'python',
'sql',
'sh',
];

export const useShiki = (language, theme) => {
Expand Down
Loading

0 comments on commit 9621cc2

Please sign in to comment.