@lobehub/ui 1.94.2 → 1.94.4

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,15 @@ 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
- });
39
- setIsLoading(false);
40
- }, [children, language, isDarkMode]);
21
+ var useCodeToHtml = useHighlight(function (s) {
22
+ return s.useCodeToHtml;
23
+ });
24
+ var _useCodeToHtml = useCodeToHtml(children.trim(), language, isDarkMode),
25
+ data = _useCodeToHtml.data,
26
+ isLoading = _useCodeToHtml.isLoading;
41
27
  return /*#__PURE__*/_jsxs(_Fragment, {
42
28
  children: [isLoading ? /*#__PURE__*/_jsx("code", {
43
- className: className,
29
+ className: cx(styles.unshiki, className),
44
30
  style: style,
45
31
  children: children
46
32
  }) : /*#__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,10 @@
1
- export declare const useHighlight: (text: string, language: string, isDarkMode: boolean) => import("swr/_internal").SWRResponse<string, any, any>;
1
+ import { type Highlighter } from 'shikiji';
2
+ import { type SWRResponse } from 'swr';
3
+ export declare const FALLBACK_LANG = "markdown";
4
+ interface Store {
5
+ highlighter?: Highlighter;
6
+ initHighlighter: () => Promise<Highlighter>;
7
+ useCodeToHtml: (text: string, language: string, isDarkMode: boolean) => SWRResponse<string>;
8
+ }
9
+ export declare const useHighlight: import("zustand/traditional").UseBoundStoreWithEqualityFn<import("zustand/vanilla").StoreApi<Store>>;
2
10
  export { default as languageMap } from './languageMap';
@@ -1,12 +1,100 @@
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';
5
+ import { shallow } from 'zustand/shallow';
6
+ import { createWithEqualityFn } from 'zustand/traditional';
3
7
  import { themeConfig } from "../Highlighter/theme";
4
- 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
- });
11
- };
8
+ import languageMap from "./languageMap";
9
+ export var FALLBACK_LANG = 'markdown';
10
+ var FALLBACK_LANGS = ['ts', 'typescript', 'js', 'javascript', 'markdown', 'md'];
11
+ var THEME = [themeConfig(true), themeConfig(false)];
12
+ export var useHighlight = createWithEqualityFn(function (set, get) {
13
+ return {
14
+ highlighter: undefined,
15
+ initHighlighter: function () {
16
+ var _initHighlighter = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
17
+ var highlighter;
18
+ return _regeneratorRuntime.wrap(function _callee$(_context) {
19
+ while (1) switch (_context.prev = _context.next) {
20
+ case 0:
21
+ highlighter = get().highlighter;
22
+ if (!highlighter) {
23
+ _context.next = 5;
24
+ break;
25
+ }
26
+ return _context.abrupt("return", highlighter);
27
+ case 5:
28
+ _context.next = 7;
29
+ return getHighlighter({
30
+ langs: FALLBACK_LANGS,
31
+ themes: THEME
32
+ });
33
+ case 7:
34
+ highlighter = _context.sent;
35
+ set({
36
+ highlighter: highlighter
37
+ });
38
+ return _context.abrupt("return", highlighter);
39
+ case 10:
40
+ case "end":
41
+ return _context.stop();
42
+ }
43
+ }, _callee);
44
+ }));
45
+ function initHighlighter() {
46
+ return _initHighlighter.apply(this, arguments);
47
+ }
48
+ return initHighlighter;
49
+ }(),
50
+ useCodeToHtml: function useCodeToHtml(text, language, isDarkMode) {
51
+ return useSWR([text, language, Number(isDarkMode)].join('-'), /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
52
+ var theme, lang, highlighter, html;
53
+ return _regeneratorRuntime.wrap(function _callee2$(_context2) {
54
+ while (1) switch (_context2.prev = _context2.next) {
55
+ case 0:
56
+ theme = isDarkMode ? 'dark' : 'light';
57
+ lang = language;
58
+ _context2.prev = 2;
59
+ _context2.next = 5;
60
+ return get().initHighlighter();
61
+ case 5:
62
+ highlighter = _context2.sent;
63
+ if (FALLBACK_LANGS.includes(lang)) {
64
+ _context2.next = 13;
65
+ break;
66
+ }
67
+ if (!languageMap.includes(lang)) {
68
+ _context2.next = 12;
69
+ break;
70
+ }
71
+ _context2.next = 10;
72
+ return highlighter === null || highlighter === void 0 ? void 0 : highlighter.loadLanguage(lang);
73
+ case 10:
74
+ _context2.next = 13;
75
+ break;
76
+ case 12:
77
+ lang = FALLBACK_LANG;
78
+ case 13:
79
+ _context2.next = 15;
80
+ return highlighter === null || highlighter === void 0 ? void 0 : highlighter.codeToHtml(text, {
81
+ lang: lang,
82
+ theme: theme
83
+ });
84
+ case 15:
85
+ html = _context2.sent;
86
+ return _context2.abrupt("return", html);
87
+ case 19:
88
+ _context2.prev = 19;
89
+ _context2.t0 = _context2["catch"](2);
90
+ return _context2.abrupt("return", "<code>".concat(text, "</code>"));
91
+ case 22:
92
+ case "end":
93
+ return _context2.stop();
94
+ }
95
+ }, _callee2, null, [[2, 19]]);
96
+ })));
97
+ }
98
+ };
99
+ }, shallow);
12
100
  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.2",
3
+ "version": "1.94.4",
4
4
  "description": "Lobe UI is an open-source UI component library for building AIGC web apps",
5
5
  "keywords": [
6
6
  "lobehub",