@yuntijs/ui 1.2.0-beta.5 → 1.2.0-beta.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/es/Highlighter/FullFeatured.d.ts +2 -0
- package/es/Highlighter/FullFeatured.js +17 -5
- package/es/Highlighter/SyntaxHighlighter/index.d.ts +2 -0
- package/es/Highlighter/SyntaxHighlighter/index.js +7 -6
- package/es/Highlighter/SyntaxHighlighter/style.d.ts +3 -2
- package/es/Highlighter/SyntaxHighlighter/style.js +4 -4
- package/es/Highlighter/index.d.ts +2 -0
- package/es/Highlighter/index.js +11 -4
- package/es/Highlighter/style.d.ts +5 -2
- package/es/Highlighter/style.js +5 -3
- package/es/hooks/languageMap.js +1 -1
- package/es/hooks/themeMap.d.ts +1 -0
- package/es/hooks/themeMap.js +1 -0
- package/es/hooks/useHighlight.d.ts +9 -1
- package/es/hooks/useHighlight.js +124 -43
- package/es/index.d.ts +1 -0
- package/es/index.js +1 -0
- package/package.json +1 -1
- package/umd/index.min.js +1 -1
- package/umd/index.min.js.map +1 -1
|
@@ -13,6 +13,8 @@ export interface FullFeaturedHighlighterProps extends DivProps {
|
|
|
13
13
|
*/
|
|
14
14
|
language: string;
|
|
15
15
|
contentStyle?: React.CSSProperties;
|
|
16
|
+
enableTransformer?: boolean;
|
|
17
|
+
theme?: string;
|
|
16
18
|
}
|
|
17
19
|
export declare const FullFeaturedHighlighter: import("react").NamedExoticComponent<FullFeaturedHighlighterProps>;
|
|
18
20
|
export * from './SyntaxHighlighter';
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
2
2
|
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
|
3
3
|
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
|
|
4
|
-
var _excluded = ["children", "language", "className", "style", "allowChangeLanguage", "fileName", "icon", "contentStyle"];
|
|
4
|
+
var _excluded = ["children", "language", "className", "style", "allowChangeLanguage", "fileName", "icon", "contentStyle", "enableTransformer", "theme"];
|
|
5
5
|
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
6
6
|
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
7
7
|
import { ActionIcon, CopyButton } from '@lobehub/ui';
|
|
8
8
|
import { Select } from 'antd';
|
|
9
9
|
import { ChevronDown, ChevronRight } from 'lucide-react';
|
|
10
|
-
import { memo, useState } from 'react';
|
|
10
|
+
import { memo, useEffect, useState } from 'react';
|
|
11
11
|
import { Flexbox } from 'react-layout-kit';
|
|
12
12
|
import { languageMap } from "../hooks/useHighlight";
|
|
13
13
|
import { SyntaxHighlighter } from "./SyntaxHighlighter";
|
|
@@ -31,6 +31,8 @@ export var FullFeaturedHighlighter = /*#__PURE__*/memo(function (_ref) {
|
|
|
31
31
|
fileName = _ref.fileName,
|
|
32
32
|
icon = _ref.icon,
|
|
33
33
|
contentStyle = _ref.contentStyle,
|
|
34
|
+
enableTransformer = _ref.enableTransformer,
|
|
35
|
+
theme = _ref.theme,
|
|
34
36
|
rest = _objectWithoutProperties(_ref, _excluded);
|
|
35
37
|
var _useState = useState(true),
|
|
36
38
|
_useState2 = _slicedToArray(_useState, 2),
|
|
@@ -40,11 +42,17 @@ export var FullFeaturedHighlighter = /*#__PURE__*/memo(function (_ref) {
|
|
|
40
42
|
_useState4 = _slicedToArray(_useState3, 2),
|
|
41
43
|
lang = _useState4[0],
|
|
42
44
|
setLang = _useState4[1];
|
|
43
|
-
var _useStyles = useStyles(
|
|
45
|
+
var _useStyles = useStyles({
|
|
46
|
+
type: 'block',
|
|
47
|
+
expand: expand
|
|
48
|
+
}),
|
|
44
49
|
styles = _useStyles.styles,
|
|
45
50
|
cx = _useStyles.cx;
|
|
46
51
|
var container = cx(styles.container, className);
|
|
47
|
-
|
|
52
|
+
useEffect(function () {
|
|
53
|
+
setLang(language);
|
|
54
|
+
}, [language]);
|
|
55
|
+
return /*#__PURE__*/_jsxs(Flexbox, _objectSpread(_objectSpread({
|
|
48
56
|
className: container,
|
|
49
57
|
"data-code-type": "highlighter",
|
|
50
58
|
style: style
|
|
@@ -90,11 +98,15 @@ export var FullFeaturedHighlighter = /*#__PURE__*/memo(function (_ref) {
|
|
|
90
98
|
}
|
|
91
99
|
})]
|
|
92
100
|
}), /*#__PURE__*/_jsx(SyntaxHighlighter, {
|
|
101
|
+
enableTransformer: enableTransformer,
|
|
93
102
|
language: lang === null || lang === void 0 ? void 0 : lang.toLowerCase(),
|
|
94
|
-
style: expand ?
|
|
103
|
+
style: expand ? _objectSpread(_objectSpread({}, contentStyle), {}, {
|
|
104
|
+
flex: 1
|
|
105
|
+
}) : _objectSpread(_objectSpread({}, contentStyle), {}, {
|
|
95
106
|
height: 0,
|
|
96
107
|
overflow: 'hidden'
|
|
97
108
|
}),
|
|
109
|
+
theme: theme,
|
|
98
110
|
children: children
|
|
99
111
|
})]
|
|
100
112
|
}));
|
|
@@ -3,5 +3,7 @@ import { DivProps } from "../../types";
|
|
|
3
3
|
export interface SyntaxHighlighterProps extends DivProps {
|
|
4
4
|
children: string;
|
|
5
5
|
language: string;
|
|
6
|
+
enableTransformer?: boolean;
|
|
7
|
+
theme?: string;
|
|
6
8
|
}
|
|
7
9
|
export declare const SyntaxHighlighter: import("react").NamedExoticComponent<SyntaxHighlighterProps>;
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { Icon } from '@lobehub/ui';
|
|
2
|
-
import { useThemeMode } from 'antd-style';
|
|
3
2
|
import { Loader2 } from 'lucide-react';
|
|
4
3
|
import { memo } from 'react';
|
|
5
4
|
import { Flexbox } from 'react-layout-kit';
|
|
@@ -11,19 +10,21 @@ import { jsxs as _jsxs } from "react/jsx-runtime";
|
|
|
11
10
|
export var SyntaxHighlighter = /*#__PURE__*/memo(function (_ref) {
|
|
12
11
|
var children = _ref.children,
|
|
13
12
|
language = _ref.language,
|
|
13
|
+
enableTransformer = _ref.enableTransformer,
|
|
14
|
+
theme = _ref.theme,
|
|
14
15
|
className = _ref.className,
|
|
15
16
|
style = _ref.style;
|
|
16
|
-
var _useStyles = useStyles(
|
|
17
|
+
var _useStyles = useStyles({
|
|
18
|
+
theme: theme
|
|
19
|
+
}),
|
|
17
20
|
styles = _useStyles.styles,
|
|
18
21
|
cx = _useStyles.cx;
|
|
19
|
-
var
|
|
20
|
-
isDarkMode = _useThemeMode.isDarkMode;
|
|
21
|
-
var _useHighlight = useHighlight(children.trim(), language, isDarkMode),
|
|
22
|
+
var _useHighlight = useHighlight(children.trim(), language, enableTransformer, theme),
|
|
22
23
|
data = _useHighlight.data,
|
|
23
24
|
isLoading = _useHighlight.isLoading;
|
|
24
25
|
return /*#__PURE__*/_jsxs(_Fragment, {
|
|
25
26
|
children: [isLoading || !data ? /*#__PURE__*/_jsx("div", {
|
|
26
|
-
className: cx(styles.
|
|
27
|
+
className: cx(styles.shiki, className),
|
|
27
28
|
style: style,
|
|
28
29
|
children: /*#__PURE__*/_jsx("pre", {
|
|
29
30
|
children: /*#__PURE__*/_jsx("div", {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
export declare const useStyles: (props?:
|
|
1
|
+
export declare const useStyles: (props?: {
|
|
2
|
+
theme?: string | undefined;
|
|
3
|
+
} | undefined) => import("antd-style").ReturnStyles<{
|
|
2
4
|
loading: string;
|
|
3
5
|
shiki: string;
|
|
4
|
-
unshiki: import("antd-style").SerializedStyles;
|
|
5
6
|
}>;
|
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import _taggedTemplateLiteral from "@babel/runtime/helpers/esm/taggedTemplateLiteral";
|
|
2
2
|
var _templateObject, _templateObject2, _templateObject3;
|
|
3
3
|
import { createStyles } from 'antd-style';
|
|
4
|
-
export var useStyles = createStyles(function (_ref) {
|
|
4
|
+
export var useStyles = createStyles(function (_ref, _ref2) {
|
|
5
5
|
var css = _ref.css,
|
|
6
6
|
token = _ref.token,
|
|
7
7
|
cx = _ref.cx,
|
|
8
8
|
prefixCls = _ref.prefixCls,
|
|
9
9
|
stylish = _ref.stylish;
|
|
10
|
+
var theme = _ref2.theme;
|
|
10
11
|
var prefix = "".concat(prefixCls, "-highlighter");
|
|
11
12
|
return {
|
|
12
|
-
loading: cx(stylish.blur, css(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n
|
|
13
|
-
shiki: cx("".concat(prefix, "-shiki"), css(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["\n
|
|
14
|
-
unshiki: css(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["\n overflow: auto;\n margin: 0;\n padding: 0;\n color: ", ";\n "])), token.colorTextDescription)
|
|
13
|
+
loading: cx(stylish.blur, css(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n position: absolute;\n z-index: 10;\n inset-block-start: 0;\n inset-inline-end: 0;\n\n height: 34px;\n padding-block: 0;\n padding-inline: 8px;\n\n font-family: ", ";\n color: ", ";\n\n border-radius: ", ";\n "])), token.fontFamilyCode, token.colorTextTertiary, token.borderRadius)),
|
|
14
|
+
shiki: cx("".concat(prefix, "-shiki"), css(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["\n overflow: hidden;\n height: 100%;\n margin: 0;\n padding: 0;\n\n & > pre {\n height: 100%;\n overflow: auto;\n margin: 0;\n padding: 0;\n ", "\n\n code {\n display: block;\n\n .line {\n display: inline-block;\n\n width: calc(100% + 32px);\n margin-block: 0;\n margin-inline: -16px;\n padding-block: 0;\n padding-inline: 16px;\n }\n }\n\n &.has-focused {\n .line:not(.focused) {\n opacity: 0.5;\n }\n }\n\n .highlighted {\n background: ", ";\n\n &.warning {\n background: ", ";\n }\n\n &.error {\n background: ", ";\n }\n }\n\n .highlighted-word {\n padding-block: 0.1em;\n padding-inline: 0.2em;\n\n background: ", ";\n border: 1px solid ", ";\n border-radius: ", "px;\n }\n\n .diff {\n &.remove {\n background: ", ";\n\n &::before {\n content: '-';\n\n position: absolute;\n inset-inline-start: 4px;\n\n display: inline-block;\n\n color: ", ";\n }\n }\n\n &.add {\n background: ", ";\n\n &::before {\n content: '+';\n\n position: absolute;\n inset-inline-start: 4px;\n\n display: inline-block;\n\n color: ", ";\n }\n }\n }\n }\n "])), !theme && css(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["\n background: none !important;\n "]))), token.colorFillTertiary, token.colorWarningBg, token.colorErrorBg, token.colorFillTertiary, token.colorBorderSecondary, token.borderRadius, token.colorErrorBg, token.colorErrorText, token.colorSuccessBg, token.colorSuccessText))
|
|
15
15
|
};
|
|
16
16
|
});
|
|
@@ -44,6 +44,8 @@ export interface HighlighterProps extends DivProps {
|
|
|
44
44
|
* @default false
|
|
45
45
|
*/
|
|
46
46
|
wrap?: boolean;
|
|
47
|
+
enableTransformer?: boolean;
|
|
48
|
+
theme?: string;
|
|
47
49
|
}
|
|
48
50
|
export declare const Highlighter: React.NamedExoticComponent<HighlighterProps>;
|
|
49
51
|
export * from './FullFeatured';
|
package/es/Highlighter/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
4
4
|
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
|
|
5
|
-
var _excluded = ["fullFeatured", "copyButtonSize", "children", "language", "className", "style", "copyable", "showLanguage", "type", "spotlight", "allowChangeLanguage", "fileName", "icon", "contentStyle", "wrap"];
|
|
5
|
+
var _excluded = ["fullFeatured", "copyButtonSize", "children", "language", "className", "style", "copyable", "showLanguage", "type", "spotlight", "allowChangeLanguage", "fileName", "icon", "contentStyle", "wrap", "enableTransformer", "theme"];
|
|
6
6
|
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
7
7
|
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
8
8
|
import { CopyButton, Spotlight, Tag } from '@lobehub/ui';
|
|
@@ -32,11 +32,15 @@ export var Highlighter = /*#__PURE__*/memo(function (_ref) {
|
|
|
32
32
|
icon = _ref.icon,
|
|
33
33
|
contentStyle = _ref.contentStyle,
|
|
34
34
|
wrap = _ref.wrap,
|
|
35
|
+
enableTransformer = _ref.enableTransformer,
|
|
36
|
+
theme = _ref.theme,
|
|
35
37
|
rest = _objectWithoutProperties(_ref, _excluded);
|
|
36
|
-
var _useStyles = useStyles(
|
|
38
|
+
var _useStyles = useStyles({
|
|
39
|
+
type: type
|
|
40
|
+
}),
|
|
37
41
|
styles = _useStyles.styles,
|
|
38
42
|
cx = _useStyles.cx;
|
|
39
|
-
var container = cx(styles.container,
|
|
43
|
+
var container = cx(styles.container, wrap && styles.wrap, className);
|
|
40
44
|
if (fullFeatured) return /*#__PURE__*/_jsx(FullFeaturedHighlighter, _objectSpread(_objectSpread({
|
|
41
45
|
allowChangeLanguage: allowChangeLanguage,
|
|
42
46
|
className: className,
|
|
@@ -44,7 +48,8 @@ export var Highlighter = /*#__PURE__*/memo(function (_ref) {
|
|
|
44
48
|
fileName: fileName,
|
|
45
49
|
icon: icon,
|
|
46
50
|
language: language,
|
|
47
|
-
style: style
|
|
51
|
+
style: style,
|
|
52
|
+
theme: theme
|
|
48
53
|
}, rest), {}, {
|
|
49
54
|
children: children
|
|
50
55
|
}));
|
|
@@ -66,8 +71,10 @@ export var Highlighter = /*#__PURE__*/memo(function (_ref) {
|
|
|
66
71
|
}), /*#__PURE__*/_jsx("div", {
|
|
67
72
|
className: styles.scroller,
|
|
68
73
|
children: /*#__PURE__*/_jsx(SyntaxHighlighter, {
|
|
74
|
+
enableTransformer: enableTransformer,
|
|
69
75
|
language: language === null || language === void 0 ? void 0 : language.toLowerCase(),
|
|
70
76
|
style: contentStyle,
|
|
77
|
+
theme: theme,
|
|
71
78
|
children: children
|
|
72
79
|
})
|
|
73
80
|
})]
|
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
export declare const useStyles: (props?:
|
|
1
|
+
export declare const useStyles: (props?: {
|
|
2
|
+
type: 'ghost' | 'block' | 'pure';
|
|
3
|
+
expand?: boolean | undefined;
|
|
4
|
+
} | undefined) => import("antd-style").ReturnStyles<{
|
|
2
5
|
button: string;
|
|
3
6
|
container: string;
|
|
4
7
|
header: import("antd-style").SerializedStyles;
|
|
5
8
|
lang: string;
|
|
6
|
-
|
|
9
|
+
wrap: import("antd-style").SerializedStyles;
|
|
7
10
|
scroller: import("antd-style").SerializedStyles;
|
|
8
11
|
select: import("antd-style").SerializedStyles;
|
|
9
12
|
}>;
|
package/es/Highlighter/style.js
CHANGED
|
@@ -1,22 +1,24 @@
|
|
|
1
1
|
import _taggedTemplateLiteral from "@babel/runtime/helpers/esm/taggedTemplateLiteral";
|
|
2
2
|
var _templateObject, _templateObject2, _templateObject3, _templateObject4, _templateObject5, _templateObject6, _templateObject7, _templateObject8;
|
|
3
3
|
import { createStyles } from 'antd-style';
|
|
4
|
-
export var useStyles = createStyles(function (_ref,
|
|
4
|
+
export var useStyles = createStyles(function (_ref, _ref2) {
|
|
5
5
|
var token = _ref.token,
|
|
6
6
|
css = _ref.css,
|
|
7
7
|
cx = _ref.cx,
|
|
8
8
|
prefixCls = _ref.prefixCls,
|
|
9
9
|
stylish = _ref.stylish;
|
|
10
|
+
var type = _ref2.type,
|
|
11
|
+
expand = _ref2.expand;
|
|
10
12
|
var prefix = "".concat(prefixCls, "-highlighter");
|
|
11
13
|
var buttonHoverCls = "".concat(prefix, "-hover-btn");
|
|
12
14
|
var langHoverCls = "".concat(prefix, "-hover-lang");
|
|
13
15
|
var typeStylish = css(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n background-color: ", ";\n border: 1px solid ", ";\n\n &:hover {\n background-color: ", ";\n }\n "])), type === 'block' ? token.colorFillTertiary : 'transparent', type === 'block' ? 'transparent' : token.colorBorder, type === 'block' ? token.colorFillTertiary : token.colorFillQuaternary);
|
|
14
16
|
return {
|
|
15
17
|
button: cx(buttonHoverCls, css(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["\n position: absolute;\n z-index: 2;\n inset-block-start: ", ";\n inset-inline-end: ", ";\n\n opacity: 0;\n "])), type === 'pure' ? 0 : '12px', type === 'pure' ? 0 : '12px')),
|
|
16
|
-
container: cx(prefix, type !== 'pure' && typeStylish, css(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["\n position: relative;\n overflow: hidden;\n border-radius: ", "px;\n transition: background-color 100ms ", ";\n\n &:hover {\n .", " {\n opacity: 1;\n }\n\n .", " {\n opacity: 1;\n }\n }\n\n .prism-code {\n background: none !important;\n }\n\n pre {\n
|
|
18
|
+
container: cx(prefix, type !== 'pure' && typeStylish, css(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["\n position: relative;\n\n overflow: hidden;\n\n height: ", ";\n\n border-radius: ", "px;\n\n transition: background-color 100ms ", ";\n\n &:hover {\n .", " {\n opacity: 1;\n }\n\n .", " {\n opacity: 1;\n }\n }\n\n .prism-code {\n background: none !important;\n }\n\n pre {\n height: 100%;\n margin: 0 !important;\n padding: ", " !important;\n }\n "])), expand ? '100%' : 'auto', token.borderRadius, token.motionEaseOut, buttonHoverCls, langHoverCls, type === 'pure' ? 0 : "16px")),
|
|
17
19
|
header: css(_templateObject4 || (_templateObject4 = _taggedTemplateLiteral(["\n padding-block: 4px;\n padding-inline: 8px;\n background: ", ";\n "])), token.colorFillQuaternary),
|
|
18
20
|
lang: cx(langHoverCls, stylish.blur, css(_templateObject5 || (_templateObject5 = _taggedTemplateLiteral(["\n position: absolute;\n z-index: 2;\n inset-block-end: 12px;\n inset-inline-end: 4px;\n\n font-family: ", ";\n color: ", ";\n\n opacity: 0;\n\n transition: opacity 0.1s;\n "])), token.fontFamilyCode, token.colorTextSecondary)),
|
|
19
|
-
|
|
21
|
+
wrap: css(_templateObject6 || (_templateObject6 = _taggedTemplateLiteral(["\n pre {\n white-space: break-spaces;\n }\n "]))),
|
|
20
22
|
scroller: css(_templateObject7 || (_templateObject7 = _taggedTemplateLiteral(["\n overflow: auto;\n width: 100%;\n height: 100%;\n "]))),
|
|
21
23
|
select: css(_templateObject8 || (_templateObject8 = _taggedTemplateLiteral(["\n user-select: none;\n font-size: 14px;\n color: ", ";\n .", "-select-selection-item {\n min-width: 100px;\n padding-inline-end: 0 !important;\n color: ", ";\n text-align: center;\n }\n "])), token.colorTextDescription, prefixCls, token.colorTextDescription)
|
|
22
24
|
};
|
package/es/hooks/languageMap.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export var languageMap = ['abap', 'actionscript-3', 'ada', 'apache', 'apex', 'apl', 'applescript', 'ara', 'asm', 'astro', 'awk', 'ballerina', 'bat', 'beancount', 'berry', 'bibtex', 'bicep', 'blade', 'c', 'cadence', 'clarity', 'clojure', 'cmake', 'cobol', 'codeql', 'coffee', 'cpp', 'crystal', 'csharp', 'css', 'cue', 'cypher', 'd', 'dart', 'dax', 'diff', 'docker', 'dream-maker', 'elixir', 'elm', 'erb', 'erlang', 'fish', 'fsharp', 'gdresource', 'gdscript', 'gdshader', 'gherkin', 'git-commit', 'git-rebase', 'glimmer-js', 'glimmer-ts', 'glsl', 'gnuplot', 'go', 'graphql', 'groovy', 'hack', 'haml', 'handlebars', 'haskell', 'hcl', 'hjson', 'hlsl', 'html', 'http', 'imba', 'ini', 'java', 'javascript', 'jinja-html', 'jison', 'json', 'json5', 'jsonc', 'jsonl', 'jsonnet', 'jssm', 'jsx', 'julia', 'kotlin', 'kusto', 'latex', 'less', 'liquid', 'lisp', 'logo', 'lua', 'make', 'markdown', 'marko', 'matlab', 'mdc', 'mdx', 'mermaid', 'mojo', 'narrat', 'nextflow', 'nginx', 'nim', 'nix', 'objective-c', 'objective-cpp', 'ocaml', 'pascal', 'perl', 'php', 'plsql', 'postcss', 'powerquery', 'powershell', 'prisma', 'prolog', 'proto', 'pug', 'puppet', 'purescript', 'python', 'r', 'raku', 'razor', 'reg', 'rel', 'riscv', 'rst', 'ruby', 'rust', 'sas', 'sass', 'scala', 'scheme', 'scss', 'shaderlab', 'shellscript', 'shellsession', 'smalltalk', 'solidity', 'sparql', 'splunk', 'sql', 'ssh-config', 'stata', 'stylus', 'svelte', 'swift', 'system-verilog', 'tasl', 'tcl', 'tex', 'toml', 'tsx', 'turtle', 'twig', 'typescript', 'v', 'vb', 'verilog', 'vhdl', 'viml', 'vue', 'vue-html', 'vyper', 'wasm', 'wenyan', 'wgsl', 'wolfram', 'xml', 'xsl', 'yaml', 'zenscript', 'zig', 'bash', 'batch', 'be', 'c#', 'cdc', 'clj', 'cmd', 'console', 'cql', 'cs', 'dockerfile', 'erl', 'f#', 'fs', 'fsl', 'gjs', 'gts', 'hbs', 'hs', 'jade', 'js', 'kql', 'makefile', 'md', 'nar', 'nf', 'objc', 'perl6', 'properties', 'ps', 'ps1', 'py', 'ql', 'rb', 'rs', 'sh', 'shader', 'shell', 'spl', 'styl', 'ts', 'vim', 'vimscript', 'vy', 'yml', 'zsh', '文言'];
|
|
1
|
+
export var languageMap = ['abap', 'actionscript-3', 'ada', 'apache', 'apex', 'apl', 'applescript', 'ara', 'asm', 'astro', 'awk', 'ballerina', 'bat', 'beancount', 'berry', 'bibtex', 'bicep', 'blade', 'c', 'cadence', 'clarity', 'clojure', 'cmake', 'cobol', 'codeql', 'coffee', 'cpp', 'crystal', 'csharp', 'css', 'cue', 'cypher', 'd', 'dart', 'dax', 'diff', 'docker', 'dream-maker', 'elixir', 'elm', 'erb', 'erlang', 'fish', 'fsharp', 'gdresource', 'gdscript', 'gdshader', 'gherkin', 'git-commit', 'git-rebase', 'glimmer-js', 'glimmer-ts', 'glsl', 'gnuplot', 'go', 'graphql', 'groovy', 'hack', 'haml', 'handlebars', 'haskell', 'hcl', 'hjson', 'hlsl', 'html', 'http', 'imba', 'ini', 'java', 'javascript', 'jinja-html', 'jison', 'json', 'json5', 'jsonc', 'jsonl', 'jsonnet', 'jssm', 'jsx', 'julia', 'kotlin', 'kusto', 'latex', 'less', 'liquid', 'lisp', 'logo', 'lua', 'make', 'markdown', 'marko', 'matlab', 'mdc', 'mdx', 'mermaid', 'mojo', 'narrat', 'nextflow', 'nginx', 'nim', 'nix', 'objective-c', 'objective-cpp', 'ocaml', 'pascal', 'perl', 'php', 'plsql', 'postcss', 'powerquery', 'powershell', 'prisma', 'prolog', 'proto', 'pug', 'puppet', 'purescript', 'python', 'r', 'raku', 'razor', 'reg', 'rel', 'riscv', 'rst', 'ruby', 'rust', 'sas', 'sass', 'scala', 'scheme', 'scss', 'shaderlab', 'shellscript', 'shellsession', 'smalltalk', 'solidity', 'sparql', 'splunk', 'sql', 'ssh-config', 'stata', 'stylus', 'svelte', 'swift', 'system-verilog', 'tasl', 'tcl', 'tex', 'toml', 'tsx', 'turtle', 'twig', 'typescript', 'v', 'vb', 'verilog', 'vhdl', 'viml', 'vue', 'vue-html', 'vyper', 'wasm', 'wenyan', 'wgsl', 'wolfram', 'xml', 'xsl', 'yaml', 'zenscript', 'zig', 'bash', 'batch', 'be', 'c#', 'cdc', 'clj', 'cmd', 'console', 'cql', 'cs', 'dockerfile', 'erl', 'f#', 'fs', 'fsl', 'gjs', 'gts', 'hbs', 'hs', 'jade', 'js', 'kql', 'makefile', 'md', 'nar', 'nf', 'objc', 'perl6', 'properties', 'ps', 'ps1', 'py', 'ql', 'rb', 'rs', 'sh', 'shader', 'shell', 'shellsession', 'spl', 'styl', 'ts', 'vim', 'vimscript', 'vy', 'yml', 'zsh', '文言'];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const themeMap: string[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export var themeMap = ['catppuccin-latte', 'everforest-light', 'github-light', 'github-light-default', 'github-light-high-contrast', 'gruvbox-light-hard', 'gruvbox-light-medium', 'gruvbox-light-soft', 'kanagawa-lotus', 'light-plus', 'material-theme-lighter', 'min-light', 'one-light', 'rose-pine-dawn', 'slack-ochin', 'snazzy-light', 'solarized-light', 'vitesse-light', 'andromeeda', 'aurora-x', 'ayu-dark', 'catppuccin-frappe', 'catppuccin-macchiato', 'catppuccin-mocha', 'dark-plus', 'dracula', 'dracula-soft', 'everforest-dark', 'github-dark', 'github-dark-default', 'github-dark-dimmed', 'github-dark-high-contrast', 'gruvbox-dark-hard', 'gruvbox-dark-medium', 'gruvbox-dark-soft', 'houston', 'kanagawa-dragon', 'kanagawa-wave', 'laserwave', 'material-theme', 'material-theme-darker', 'material-theme-ocean', 'material-theme-palenight', 'min-dark', 'monokai', 'night-owl', 'nord', 'one-dark-pro', 'plastic', 'poimandres', 'red', 'rose-pine', 'rose-pine-moon', 'slack-dark', 'solarized-dark', 'synthwave-84', 'tokyo-night', 'vesper', 'vitesse-black', 'vitesse-dark'];
|
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
import type { codeToHtml } from 'shiki';
|
|
2
|
+
import { SWRResponse } from 'swr';
|
|
1
3
|
export declare const FALLBACK_LANG = "txt";
|
|
2
|
-
|
|
4
|
+
declare const MD5_LENGTH_THRESHOLD = 10000;
|
|
5
|
+
declare const loadShiki: () => Promise<typeof codeToHtml>;
|
|
6
|
+
declare const shikiPromise: Promise<typeof codeToHtml>;
|
|
7
|
+
declare const escapeHtml: (str: string) => string;
|
|
8
|
+
export declare const useHighlight: (text: string, lang: string, enableTransformer?: boolean, theme?: string) => SWRResponse<string, Error>;
|
|
3
9
|
export { languageMap } from './languageMap';
|
|
10
|
+
export { themeMap } from './themeMap';
|
|
11
|
+
export { escapeHtml, loadShiki, MD5_LENGTH_THRESHOLD, shikiPromise };
|
package/es/hooks/useHighlight.js
CHANGED
|
@@ -3,78 +3,159 @@ import _regeneratorRuntime from "@babel/runtime/regenerator";
|
|
|
3
3
|
import { transformerNotationDiff, transformerNotationErrorLevel, transformerNotationFocus, transformerNotationHighlight, transformerNotationWordHighlight } from '@shikijs/transformers';
|
|
4
4
|
import { useTheme, useThemeMode } from 'antd-style';
|
|
5
5
|
import { useMemo } from 'react';
|
|
6
|
-
import { codeToHtml } from 'shiki';
|
|
7
6
|
import useSWR from 'swr';
|
|
8
7
|
import { Md5 } from 'ts-md5';
|
|
9
8
|
import { languageMap } from "./languageMap";
|
|
9
|
+
import { themeMap } from "./themeMap";
|
|
10
10
|
export var FALLBACK_LANG = 'txt';
|
|
11
11
|
|
|
12
|
-
//
|
|
13
|
-
|
|
12
|
+
// 应用级缓存,避免重复计算
|
|
13
|
+
var MD5_LENGTH_THRESHOLD = 10000; // 超过该长度使用异步MD5
|
|
14
|
+
|
|
15
|
+
// 颜色替换映射类型
|
|
16
|
+
|
|
17
|
+
// 懒加载 shiki
|
|
18
|
+
var loadShiki = function loadShiki() {
|
|
19
|
+
return import('shiki').then(function (mod) {
|
|
20
|
+
return mod.codeToHtml;
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
var shikiPromise = loadShiki();
|
|
24
|
+
|
|
25
|
+
// 辅助函数:安全的HTML转义
|
|
26
|
+
var escapeHtml = function escapeHtml(str) {
|
|
27
|
+
return str.replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>').replaceAll('"', '"').replaceAll("'", ''');
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// 主高亮组件
|
|
31
|
+
export var useHighlight = function useHighlight(text, lang, enableTransformer, theme) {
|
|
14
32
|
var _useThemeMode = useThemeMode(),
|
|
15
33
|
isDarkMode = _useThemeMode.isDarkMode;
|
|
34
|
+
var antTheme = useTheme();
|
|
16
35
|
var language = lang.toLowerCase();
|
|
36
|
+
|
|
37
|
+
// 匹配支持的语言
|
|
17
38
|
var matchedLanguage = useMemo(function () {
|
|
18
39
|
return languageMap.includes(language) ? language : FALLBACK_LANG;
|
|
19
40
|
}, [language]);
|
|
20
|
-
|
|
41
|
+
// 匹配支持的主题
|
|
42
|
+
var matchedTheme = useMemo(function () {
|
|
43
|
+
return themeMap.includes(theme) ? theme : undefined;
|
|
44
|
+
}, [theme]);
|
|
45
|
+
|
|
46
|
+
// 优化 transformer 创建
|
|
21
47
|
var transformers = useMemo(function () {
|
|
22
48
|
if (!enableTransformer) return;
|
|
23
49
|
return [transformerNotationDiff(), transformerNotationHighlight(), transformerNotationWordHighlight(), transformerNotationFocus(), transformerNotationErrorLevel()];
|
|
24
50
|
}, [enableTransformer]);
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
51
|
+
|
|
52
|
+
// 优化颜色替换配置
|
|
53
|
+
var colorReplacements = useMemo(function () {
|
|
54
|
+
return {
|
|
55
|
+
'slack-dark': {
|
|
56
|
+
'#4ec9b0': antTheme.yellow,
|
|
57
|
+
'#569cd6': antTheme.colorError,
|
|
58
|
+
'#6a9955': antTheme.gray,
|
|
59
|
+
'#9cdcfe': antTheme.colorText,
|
|
60
|
+
'#b5cea8': antTheme.purple10,
|
|
61
|
+
'#c586c0': antTheme.colorInfo,
|
|
62
|
+
'#ce9178': antTheme.colorSuccess,
|
|
63
|
+
'#dcdcaa': antTheme.colorWarning,
|
|
64
|
+
'#e6e6e6': antTheme.colorText
|
|
65
|
+
},
|
|
66
|
+
'slack-ochin': {
|
|
67
|
+
'#002339': antTheme.colorText,
|
|
68
|
+
'#0991b6': antTheme.colorError,
|
|
69
|
+
'#174781': antTheme.purple10,
|
|
70
|
+
'#2f86d2': antTheme.colorText,
|
|
71
|
+
'#357b42': antTheme.gray,
|
|
72
|
+
'#7b30d0': antTheme.colorInfo,
|
|
73
|
+
'#7eb233': antTheme.colorWarningTextActive,
|
|
74
|
+
'#a44185': antTheme.colorSuccess,
|
|
75
|
+
'#dc3eb7': antTheme.yellow11
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}, [antTheme]);
|
|
79
|
+
|
|
80
|
+
// 构建 shiki 主题
|
|
28
81
|
var shikiTheme = useMemo(function () {
|
|
82
|
+
if (matchedTheme) {
|
|
83
|
+
return matchedTheme;
|
|
84
|
+
}
|
|
29
85
|
if (language === 'md') {
|
|
30
86
|
return isDarkMode ? 'catppuccin-mocha' : 'catppuccin-latte';
|
|
31
87
|
}
|
|
88
|
+
if (language === 'shellsession') {
|
|
89
|
+
return isDarkMode ? 'material-theme-darker' : 'material-theme-lighter';
|
|
90
|
+
}
|
|
32
91
|
return isDarkMode ? 'slack-dark' : 'slack-ochin';
|
|
33
|
-
}, [isDarkMode, language]);
|
|
34
|
-
|
|
92
|
+
}, [isDarkMode, language, matchedTheme]);
|
|
93
|
+
|
|
94
|
+
// 构建缓存键
|
|
95
|
+
var cacheKey = useMemo(function () {
|
|
96
|
+
// 长文本使用 hash
|
|
97
|
+
var hash = text.length < MD5_LENGTH_THRESHOLD ? text : Md5.hashStr(text);
|
|
98
|
+
return [matchedLanguage, shikiTheme, hash].join('-');
|
|
99
|
+
}, [text, matchedLanguage, shikiTheme]);
|
|
100
|
+
|
|
101
|
+
// 使用SWR获取高亮HTML
|
|
102
|
+
return useSWR(cacheKey, /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
|
|
103
|
+
var codeToHtml, html, _codeToHtml, _html, fallbackHtml;
|
|
35
104
|
return _regeneratorRuntime.wrap(function _callee$(_context) {
|
|
36
105
|
while (1) switch (_context.prev = _context.next) {
|
|
37
106
|
case 0:
|
|
38
107
|
_context.prev = 0;
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
'#b5cea8': theme.purple10,
|
|
47
|
-
'#c586c0': theme.colorInfo,
|
|
48
|
-
'#ce9178': theme.colorSuccess,
|
|
49
|
-
'#dcdcaa': theme.colorWarning,
|
|
50
|
-
'#e6e6e6': theme.colorText
|
|
51
|
-
},
|
|
52
|
-
'slack-ochin': {
|
|
53
|
-
'#002339': theme.colorText,
|
|
54
|
-
'#0991b6': theme.colorError,
|
|
55
|
-
'#174781': theme.purple10,
|
|
56
|
-
'#2f86d2': theme.colorText,
|
|
57
|
-
'#357b42': theme.gray,
|
|
58
|
-
'#7b30d0': theme.colorInfo,
|
|
59
|
-
'#7eb233': theme.colorWarningTextActive,
|
|
60
|
-
'#a44185': theme.colorSuccess,
|
|
61
|
-
'#dc3eb7': theme.yellow11
|
|
62
|
-
}
|
|
63
|
-
},
|
|
108
|
+
_context.next = 3;
|
|
109
|
+
return shikiPromise;
|
|
110
|
+
case 3:
|
|
111
|
+
codeToHtml = _context.sent;
|
|
112
|
+
_context.next = 6;
|
|
113
|
+
return codeToHtml(text, {
|
|
114
|
+
colorReplacements: colorReplacements,
|
|
64
115
|
lang: matchedLanguage,
|
|
65
116
|
theme: shikiTheme,
|
|
66
117
|
transformers: transformers
|
|
67
|
-
})
|
|
68
|
-
case
|
|
69
|
-
|
|
118
|
+
});
|
|
119
|
+
case 6:
|
|
120
|
+
html = _context.sent;
|
|
121
|
+
return _context.abrupt("return", html);
|
|
122
|
+
case 10:
|
|
123
|
+
_context.prev = 10;
|
|
70
124
|
_context.t0 = _context["catch"](0);
|
|
71
|
-
console.warn('
|
|
72
|
-
|
|
73
|
-
|
|
125
|
+
console.warn('高级渲染失败:', _context.t0);
|
|
126
|
+
_context.prev = 13;
|
|
127
|
+
_context.next = 16;
|
|
128
|
+
return shikiPromise;
|
|
129
|
+
case 16:
|
|
130
|
+
_codeToHtml = _context.sent;
|
|
131
|
+
_context.next = 19;
|
|
132
|
+
return _codeToHtml(text, {
|
|
133
|
+
lang: matchedLanguage,
|
|
134
|
+
theme: shikiTheme
|
|
135
|
+
});
|
|
136
|
+
case 19:
|
|
137
|
+
_html = _context.sent;
|
|
138
|
+
return _context.abrupt("return", _html);
|
|
139
|
+
case 23:
|
|
140
|
+
_context.prev = 23;
|
|
141
|
+
_context.t1 = _context["catch"](13);
|
|
142
|
+
// 最终降级到纯文本
|
|
143
|
+
fallbackHtml = "<pre class=\"fallback\"><code>".concat(escapeHtml(text), "</code></pre>");
|
|
144
|
+
return _context.abrupt("return", fallbackHtml);
|
|
145
|
+
case 27:
|
|
74
146
|
case "end":
|
|
75
147
|
return _context.stop();
|
|
76
148
|
}
|
|
77
|
-
}, _callee, null, [[0,
|
|
78
|
-
}))
|
|
149
|
+
}, _callee, null, [[0, 10], [13, 23]]);
|
|
150
|
+
})), {
|
|
151
|
+
dedupingInterval: 3000,
|
|
152
|
+
// 3秒内相同请求只执行一次
|
|
153
|
+
errorRetryCount: 2,
|
|
154
|
+
// 最多重试2次
|
|
155
|
+
revalidateOnFocus: false,
|
|
156
|
+
revalidateOnReconnect: false
|
|
157
|
+
});
|
|
79
158
|
};
|
|
80
|
-
export { languageMap } from "./languageMap";
|
|
159
|
+
export { languageMap } from "./languageMap";
|
|
160
|
+
export { themeMap } from "./themeMap";
|
|
161
|
+
export { escapeHtml, loadShiki, MD5_LENGTH_THRESHOLD, shikiPromise };
|
package/es/index.d.ts
CHANGED
|
@@ -34,6 +34,7 @@ export * from './ChatMarkdown';
|
|
|
34
34
|
export * from './CodeEditor';
|
|
35
35
|
export * from './EditableMessage';
|
|
36
36
|
export * from './Highlighter';
|
|
37
|
+
export * from './hooks/useHighlight';
|
|
37
38
|
export * from './styles';
|
|
38
39
|
export * from './useSpeechSynthes';
|
|
39
40
|
export * from './WaveformIcon';
|
package/es/index.js
CHANGED