@lobehub/ui 1.94.3 → 1.94.5

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.
@@ -1,11 +1,9 @@
1
- import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2
1
  import { useThemeMode } from 'antd-style';
3
2
  import { Loader2 } from 'lucide-react';
4
- import { memo, useEffect, useState } from 'react';
3
+ import { memo } from 'react';
5
4
  import { Center } from 'react-layout-kit';
6
- import { codeToHtml } from 'shikiji';
7
- import { themeConfig } from "../theme";
8
5
  import Icon from "../../Icon";
6
+ import { useHighlight } from "../../hooks/useHighlight";
9
7
  import { useStyles } from "./style";
10
8
  import { jsx as _jsx } from "react/jsx-runtime";
11
9
  import { jsxs as _jsxs } from "react/jsx-runtime";
@@ -20,27 +18,12 @@ var SyntaxHighlighter = /*#__PURE__*/memo(function (_ref) {
20
18
  cx = _useStyles.cx;
21
19
  var _useThemeMode = useThemeMode(),
22
20
  isDarkMode = _useThemeMode.isDarkMode;
23
- var _useState = useState(true),
24
- _useState2 = _slicedToArray(_useState, 2),
25
- isLoading = _useState2[0],
26
- setIsLoading = _useState2[1];
27
- var _useState3 = useState(''),
28
- _useState4 = _slicedToArray(_useState3, 2),
29
- data = _useState4[0],
30
- setData = _useState4[1];
31
- useEffect(function () {
32
- setIsLoading(true);
33
- codeToHtml(children.trim(), {
34
- lang: language,
35
- theme: themeConfig(isDarkMode)
36
- }).then(function (code) {
37
- setData(code);
38
- setIsLoading(false);
39
- });
40
- }, [children, language, isDarkMode]);
21
+ var _useHighlight = useHighlight(children.trim(), language, isDarkMode),
22
+ data = _useHighlight.data,
23
+ isLoading = _useHighlight.isLoading;
41
24
  return /*#__PURE__*/_jsxs(_Fragment, {
42
25
  children: [isLoading ? /*#__PURE__*/_jsx("code", {
43
- className: className,
26
+ className: cx(styles.unshiki, className),
44
27
  style: style,
45
28
  children: children
46
29
  }) : /*#__PURE__*/_jsx("div", {
@@ -2,4 +2,5 @@ export declare const useStyles: (props?: unknown) => import("antd-style").Return
2
2
  loading: string;
3
3
  prism: import("antd-style").SerializedStyles;
4
4
  shiki: string;
5
+ unshiki: import("antd-style").SerializedStyles;
5
6
  }>;
@@ -1,5 +1,5 @@
1
1
  import _taggedTemplateLiteral from "@babel/runtime/helpers/esm/taggedTemplateLiteral";
2
- var _templateObject, _templateObject2, _templateObject3;
2
+ var _templateObject, _templateObject2, _templateObject3, _templateObject4;
3
3
  import { createStyles } from 'antd-style';
4
4
  export var useStyles = createStyles(function (_ref) {
5
5
  var css = _ref.css,
@@ -11,6 +11,7 @@ export var useStyles = createStyles(function (_ref) {
11
11
  return {
12
12
  loading: cx(stylish.blur, css(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n position: absolute;\n z-index: 10;\n top: 0;\n right: 0;\n\n display: flex;\n align-items: center;\n justify-content: center;\n\n height: 34px;\n padding: 0 8px;\n\n font-family: ", ";\n color: ", ";\n\n border-radius: ", ";\n "])), token.fontFamilyCode, token.colorTextTertiary, token.borderRadius)),
13
13
  prism: css(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["\n pre {\n overflow: auto;\n font-family: ", " !important;\n }\n "])), token.fontFamilyCode),
14
- shiki: cx("".concat(prefix, "-shiki"), css(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["\n margin: 0;\n padding: 0;\n\n .shiki {\n overflow-x: auto;\n margin: 0;\n padding: 0;\n background: none !important;\n }\n "]))))
14
+ shiki: cx("".concat(prefix, "-shiki"), css(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["\n margin: 0;\n padding: 0;\n\n .shiki {\n overflow-x: auto;\n margin: 0;\n padding: 0;\n background: none !important;\n }\n "])))),
15
+ unshiki: css(_templateObject4 || (_templateObject4 = _taggedTemplateLiteral(["\n padding: 8px;\n "])))
15
16
  };
16
17
  });
@@ -9,6 +9,7 @@ import { createStyles } from 'antd-style';
9
9
  import { memo } from 'react';
10
10
  import Highlighter from "../Highlighter";
11
11
  import Snippet from "../Snippet";
12
+ import { FALLBACK_LANG } from "../hooks/useHighlight";
12
13
  import { jsx as _jsx } from "react/jsx-runtime";
13
14
  var useStyles = createStyles(function (_ref) {
14
15
  var css = _ref.css;
@@ -34,11 +35,12 @@ var Code = /*#__PURE__*/memo(function (_ref2) {
34
35
  className = _properties$children$.className;
35
36
  if (!children) return;
36
37
  var content = Array.isArray(children) ? children[0] : children;
38
+ var lang = (className === null || className === void 0 ? void 0 : className.replace('language-', '')) || FALLBACK_LANG;
37
39
  if (countLines(content) === 1 && content.length <= 60) {
38
40
  return /*#__PURE__*/_jsx(Snippet, {
39
41
  className: styles.container,
40
42
  "data-code-type": "highlighter",
41
- language: (className === null || className === void 0 ? void 0 : className.replace('language-', '')) || 'markdown',
43
+ language: lang,
42
44
  type: 'block',
43
45
  children: content
44
46
  });
@@ -50,7 +52,7 @@ var Code = /*#__PURE__*/memo(function (_ref2) {
50
52
  fontSize: 16
51
53
  },
52
54
  fullFeatured: fullFeatured,
53
- language: (className === null || className === void 0 ? void 0 : className.replace('language-', '')) || 'markdown',
55
+ language: lang,
54
56
  type: "block",
55
57
  children: content
56
58
  });
@@ -1,2 +1,3 @@
1
+ export declare const FALLBACK_LANG = "markdown";
1
2
  export declare const useHighlight: (text: string, language: string, isDarkMode: boolean) => import("swr/_internal").SWRResponse<string, any, any>;
2
3
  export { default as languageMap } from './languageMap';
@@ -1,12 +1,91 @@
1
- import { codeToHtml } from 'shikiji';
1
+ import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
2
+ import _regeneratorRuntime from "@babel/runtime/regenerator";
3
+ import { getHighlighter } from 'shikiji';
2
4
  import useSWR from 'swr';
3
5
  import { themeConfig } from "../Highlighter/theme";
6
+ import languageMap from "./languageMap";
7
+ export var FALLBACK_LANG = 'markdown';
8
+ var FALLBACK_LANGS = ['ts', 'typescript', 'js', 'javascript', 'markdown', 'md'];
9
+ var THEME = [themeConfig(true), themeConfig(false)];
10
+ var cacheHighlighter;
11
+ var initHighlighter = /*#__PURE__*/function () {
12
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
13
+ var highlighter;
14
+ return _regeneratorRuntime.wrap(function _callee$(_context) {
15
+ while (1) switch (_context.prev = _context.next) {
16
+ case 0:
17
+ highlighter = cacheHighlighter;
18
+ if (!highlighter) {
19
+ _context.next = 5;
20
+ break;
21
+ }
22
+ return _context.abrupt("return", highlighter);
23
+ case 5:
24
+ _context.next = 7;
25
+ return getHighlighter({
26
+ langs: FALLBACK_LANGS,
27
+ themes: THEME
28
+ });
29
+ case 7:
30
+ highlighter = _context.sent;
31
+ cacheHighlighter = highlighter;
32
+ return _context.abrupt("return", highlighter);
33
+ case 10:
34
+ case "end":
35
+ return _context.stop();
36
+ }
37
+ }, _callee);
38
+ }));
39
+ return function initHighlighter() {
40
+ return _ref.apply(this, arguments);
41
+ };
42
+ }();
4
43
  export var useHighlight = function useHighlight(text, language, isDarkMode) {
5
- return useSWR([text, language, String(isDarkMode)].join('-'), function () {
6
- return codeToHtml(text, {
7
- lang: language,
8
- theme: themeConfig(isDarkMode)
9
- });
10
- });
44
+ return useSWR([text, language, Number(isDarkMode)].join('-'), /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
45
+ var theme, lang, highlighter, html;
46
+ return _regeneratorRuntime.wrap(function _callee2$(_context2) {
47
+ while (1) switch (_context2.prev = _context2.next) {
48
+ case 0:
49
+ theme = isDarkMode ? 'dark' : 'light';
50
+ lang = language;
51
+ _context2.prev = 2;
52
+ _context2.next = 5;
53
+ return initHighlighter();
54
+ case 5:
55
+ highlighter = _context2.sent;
56
+ if (FALLBACK_LANGS.includes(lang)) {
57
+ _context2.next = 13;
58
+ break;
59
+ }
60
+ if (!languageMap.includes(lang)) {
61
+ _context2.next = 12;
62
+ break;
63
+ }
64
+ _context2.next = 10;
65
+ return highlighter === null || highlighter === void 0 ? void 0 : highlighter.loadLanguage(lang);
66
+ case 10:
67
+ _context2.next = 13;
68
+ break;
69
+ case 12:
70
+ lang = FALLBACK_LANG;
71
+ case 13:
72
+ _context2.next = 15;
73
+ return highlighter === null || highlighter === void 0 ? void 0 : highlighter.codeToHtml(text, {
74
+ lang: lang,
75
+ theme: theme
76
+ });
77
+ case 15:
78
+ html = _context2.sent;
79
+ return _context2.abrupt("return", html);
80
+ case 19:
81
+ _context2.prev = 19;
82
+ _context2.t0 = _context2["catch"](2);
83
+ return _context2.abrupt("return", "<code>".concat(text, "</code>"));
84
+ case 22:
85
+ case "end":
86
+ return _context2.stop();
87
+ }
88
+ }, _callee2, null, [[2, 19]]);
89
+ })));
11
90
  };
12
91
  export { default as languageMap } from "./languageMap";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lobehub/ui",
3
- "version": "1.94.3",
3
+ "version": "1.94.5",
4
4
  "description": "Lobe UI is an open-source UI component library for building AIGC web apps",
5
5
  "keywords": [
6
6
  "lobehub",