@lobehub/ui 2.0.16 → 2.1.0
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/Highlighter.js +4 -2
- package/es/Highlighter/SyntaxHighlighter/Line.d.ts +5 -0
- package/es/Highlighter/SyntaxHighlighter/Line.js +78 -0
- package/es/Highlighter/SyntaxHighlighter/Span.d.ts +5 -0
- package/es/Highlighter/SyntaxHighlighter/Span.js +14 -0
- package/es/Highlighter/SyntaxHighlighter/index.js +49 -63
- package/es/Highlighter/SyntaxHighlighter/style.js +1 -1
- package/es/Highlighter/type.d.ts +2 -0
- package/es/Markdown/SyntaxMarkdown/index.js +70 -42
- package/es/Markdown/components/CodeBlock.d.ts +2 -2
- package/es/Markdown/components/CodeBlock.js +4 -10
- package/es/hooks/useHighlight.js +16 -16
- package/es/hooks/useMarkdown/index.d.ts +3 -1
- package/es/hooks/useMarkdown/index.js +147 -90
- package/es/hooks/useMarkdown/latex.d.ts +44 -0
- package/es/hooks/useMarkdown/latex.js +138 -0
- package/es/hooks/useMarkdown/utils.d.ts +56 -5
- package/es/hooks/useMarkdown/utils.js +157 -66
- package/es/mdx/Mdx/index.js +4 -3
- package/es/mdx/mdxComponents/Pre.js +6 -2
- package/package.json +3 -4
- package/es/Markdown/utils.d.ts +0 -21
- package/es/Markdown/utils.js +0 -166
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
3
|
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
4
|
-
var _excluded = ["fullFeatured", "actionIconSize", "children", "language", "className", "copyable", "showLanguage", "variant", "shadow", "wrap", "bodyRender", "actionsRender", "enableTransformer", "theme"];
|
|
4
|
+
var _excluded = ["animated", "fullFeatured", "actionIconSize", "children", "language", "className", "copyable", "showLanguage", "variant", "shadow", "wrap", "bodyRender", "actionsRender", "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
|
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
@@ -20,7 +20,8 @@ import { useStyles } from "./style";
|
|
|
20
20
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
21
21
|
import { jsxs as _jsxs } from "react/jsx-runtime";
|
|
22
22
|
export var Highlighter = /*#__PURE__*/memo(function (_ref) {
|
|
23
|
-
var
|
|
23
|
+
var animated = _ref.animated,
|
|
24
|
+
fullFeatured = _ref.fullFeatured,
|
|
24
25
|
actionIconSize = _ref.actionIconSize,
|
|
25
26
|
children = _ref.children,
|
|
26
27
|
_ref$language = _ref.language,
|
|
@@ -81,6 +82,7 @@ export var Highlighter = /*#__PURE__*/memo(function (_ref) {
|
|
|
81
82
|
originalNode: originalActions
|
|
82
83
|
}) : originalActions;
|
|
83
84
|
var originalBody = /*#__PURE__*/_jsx(SyntaxHighlighter, {
|
|
85
|
+
animated: animated,
|
|
84
86
|
enableTransformer: enableTransformer,
|
|
85
87
|
language: language === null || language === void 0 ? void 0 : language.toLowerCase(),
|
|
86
88
|
theme: theme,
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
|
|
4
|
+
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
5
|
+
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
6
|
+
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
7
|
+
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
8
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
9
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
10
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
11
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
12
|
+
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
13
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
14
|
+
import { memo, useEffect, useState } from 'react';
|
|
15
|
+
import Span from "./Span";
|
|
16
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
17
|
+
var Line = /*#__PURE__*/memo(function (_ref) {
|
|
18
|
+
var children = _ref.children;
|
|
19
|
+
var _useState = useState([]),
|
|
20
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
21
|
+
contents = _useState2[0],
|
|
22
|
+
setContents = _useState2[1];
|
|
23
|
+
useEffect(function () {
|
|
24
|
+
if (children && typeof children === 'string') {
|
|
25
|
+
// Extract all lines from the HTML content
|
|
26
|
+
// We need to handle the structure from shiki which gives us HTML with a <pre><code> structure
|
|
27
|
+
var parser = new DOMParser();
|
|
28
|
+
var doc = parser.parseFromString(children, 'text/html');
|
|
29
|
+
var codeElement = doc.querySelector('span.line');
|
|
30
|
+
if (codeElement) {
|
|
31
|
+
var spanLines = codeElement.querySelectorAll('span');
|
|
32
|
+
var newLines = _toConsumableArray(spanLines).map(function (line) {
|
|
33
|
+
return line.outerHTML;
|
|
34
|
+
});
|
|
35
|
+
setContents(function (prevLines) {
|
|
36
|
+
if (prevLines.length !== newLines.length) return newLines;
|
|
37
|
+
var hasChanged = false;
|
|
38
|
+
var _iterator = _createForOfIteratorHelper(newLines.entries()),
|
|
39
|
+
_step;
|
|
40
|
+
try {
|
|
41
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
42
|
+
var _step$value = _slicedToArray(_step.value, 2),
|
|
43
|
+
i = _step$value[0],
|
|
44
|
+
newLine = _step$value[1];
|
|
45
|
+
if (prevLines[i] !== newLine) {
|
|
46
|
+
hasChanged = true;
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
} catch (err) {
|
|
51
|
+
_iterator.e(err);
|
|
52
|
+
} finally {
|
|
53
|
+
_iterator.f();
|
|
54
|
+
}
|
|
55
|
+
return hasChanged ? newLines : prevLines;
|
|
56
|
+
});
|
|
57
|
+
} else {
|
|
58
|
+
// Fallback if the structure is different
|
|
59
|
+
var htmlLines = children.split('\n').map(function (line) {
|
|
60
|
+
return "<span>".concat(line, "</span>");
|
|
61
|
+
});
|
|
62
|
+
setContents(htmlLines);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}, [children]);
|
|
66
|
+
return /*#__PURE__*/_jsx("span", {
|
|
67
|
+
className: 'line',
|
|
68
|
+
children: contents && contents.length > 0 ? contents.map(function (span, index) {
|
|
69
|
+
return /*#__PURE__*/_jsx(Span, {
|
|
70
|
+
children: span
|
|
71
|
+
}, index);
|
|
72
|
+
}) : /*#__PURE__*/_jsx("span", {
|
|
73
|
+
children: " "
|
|
74
|
+
})
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
Line.displayName = 'HighlighterLine';
|
|
78
|
+
export default Line;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { memo } from 'react';
|
|
4
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
5
|
+
var Span = /*#__PURE__*/memo(function (_ref) {
|
|
6
|
+
var children = _ref.children;
|
|
7
|
+
return /*#__PURE__*/_jsx("span", {
|
|
8
|
+
dangerouslySetInnerHTML: {
|
|
9
|
+
__html: children || ''
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
});
|
|
13
|
+
Span.displayName = 'HighlighterSpan';
|
|
14
|
+
export default Span;
|
|
@@ -14,66 +14,19 @@ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
|
14
14
|
import { cva } from 'class-variance-authority';
|
|
15
15
|
import { memo, useEffect, useMemo, useState } from 'react';
|
|
16
16
|
import { useHighlight } from "../../hooks/useHighlight";
|
|
17
|
+
import Line from "./Line";
|
|
17
18
|
import { useStyles } from "./style";
|
|
18
19
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
19
|
-
var
|
|
20
|
-
var
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
var _useState = useState([]),
|
|
30
|
-
_useState2 = _slicedToArray(_useState, 2),
|
|
31
|
-
contents = _useState2[0],
|
|
32
|
-
setContents = _useState2[1];
|
|
33
|
-
useEffect(function () {
|
|
34
|
-
if (data && typeof data === 'string') {
|
|
35
|
-
// Extract all lines from the HTML content
|
|
36
|
-
// We need to handle the structure from shiki which gives us HTML with a <pre><code> structure
|
|
37
|
-
var parser = new DOMParser();
|
|
38
|
-
var doc = parser.parseFromString(data, 'text/html');
|
|
39
|
-
var codeElement = doc.querySelector('span.line');
|
|
40
|
-
if (codeElement) {
|
|
41
|
-
var spanLines = codeElement.querySelectorAll('span');
|
|
42
|
-
var newLines = _toConsumableArray(spanLines).map(function (line) {
|
|
43
|
-
return line.outerHTML;
|
|
44
|
-
});
|
|
45
|
-
setContents(newLines);
|
|
46
|
-
} else {
|
|
47
|
-
// Fallback if the structure is different
|
|
48
|
-
var htmlLines = data.split('\n').map(function (line) {
|
|
49
|
-
return "<span>".concat(line, "</span>");
|
|
50
|
-
});
|
|
51
|
-
setContents(htmlLines);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}, [data]);
|
|
55
|
-
return /*#__PURE__*/_jsx("span", {
|
|
56
|
-
className: 'line',
|
|
57
|
-
children: contents && contents.length > 0 ? contents.map(function (span, index) {
|
|
58
|
-
return /*#__PURE__*/_jsx(Span, {
|
|
59
|
-
data: span || ' '
|
|
60
|
-
}, index);
|
|
61
|
-
}) : /*#__PURE__*/_jsx("span", {
|
|
62
|
-
children: " "
|
|
63
|
-
})
|
|
64
|
-
});
|
|
65
|
-
});
|
|
66
|
-
Line.displayName = 'HighlighterLine';
|
|
67
|
-
Span.displayName = 'HighlighterSpan';
|
|
68
|
-
var SyntaxHighlighter = /*#__PURE__*/memo(function (_ref3) {
|
|
69
|
-
var ref = _ref3.ref,
|
|
70
|
-
children = _ref3.children,
|
|
71
|
-
language = _ref3.language,
|
|
72
|
-
className = _ref3.className,
|
|
73
|
-
style = _ref3.style,
|
|
74
|
-
enableTransformer = _ref3.enableTransformer,
|
|
75
|
-
variant = _ref3.variant,
|
|
76
|
-
theme = _ref3.theme;
|
|
20
|
+
var SyntaxHighlighter = /*#__PURE__*/memo(function (_ref) {
|
|
21
|
+
var ref = _ref.ref,
|
|
22
|
+
children = _ref.children,
|
|
23
|
+
language = _ref.language,
|
|
24
|
+
className = _ref.className,
|
|
25
|
+
style = _ref.style,
|
|
26
|
+
enableTransformer = _ref.enableTransformer,
|
|
27
|
+
variant = _ref.variant,
|
|
28
|
+
theme = _ref.theme,
|
|
29
|
+
animated = _ref.animated;
|
|
77
30
|
var _useStyles = useStyles(),
|
|
78
31
|
styles = _useStyles.styles,
|
|
79
32
|
cx = _useStyles.cx;
|
|
@@ -85,16 +38,28 @@ var SyntaxHighlighter = /*#__PURE__*/memo(function (_ref3) {
|
|
|
85
38
|
theme: isDefaultTheme ? undefined : theme
|
|
86
39
|
}),
|
|
87
40
|
data = _useHighlight.data;
|
|
88
|
-
var
|
|
41
|
+
var _useState = useState([]),
|
|
42
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
43
|
+
contentLines = _useState2[0],
|
|
44
|
+
setContentLines = _useState2[1];
|
|
45
|
+
var _useState3 = useState({}),
|
|
89
46
|
_useState4 = _slicedToArray(_useState3, 2),
|
|
90
|
-
|
|
91
|
-
|
|
47
|
+
preStyle = _useState4[0],
|
|
48
|
+
setPreStyle = _useState4[1];
|
|
92
49
|
useEffect(function () {
|
|
93
50
|
if (data && typeof data === 'string') {
|
|
94
51
|
// Extract all lines from the HTML content
|
|
95
52
|
// We need to handle the structure from shiki which gives us HTML with a <pre><code> structure
|
|
96
53
|
var parser = new DOMParser();
|
|
97
54
|
var doc = parser.parseFromString(data, 'text/html');
|
|
55
|
+
var preElement = doc.querySelector('pre');
|
|
56
|
+
var _preStyle = preElement === null || preElement === void 0 ? void 0 : preElement.style;
|
|
57
|
+
if (_preStyle) {
|
|
58
|
+
setPreStyle({
|
|
59
|
+
backgroundColor: _preStyle === null || _preStyle === void 0 ? void 0 : _preStyle.backgroundColor,
|
|
60
|
+
color: _preStyle === null || _preStyle === void 0 ? void 0 : _preStyle.color
|
|
61
|
+
});
|
|
62
|
+
}
|
|
98
63
|
var codeElement = doc.querySelector('pre code');
|
|
99
64
|
if (codeElement) {
|
|
100
65
|
var spanLines = codeElement.querySelectorAll('.line');
|
|
@@ -175,6 +140,21 @@ var SyntaxHighlighter = /*#__PURE__*/memo(function (_ref3) {
|
|
|
175
140
|
})
|
|
176
141
|
})
|
|
177
142
|
});
|
|
143
|
+
if (!animated) {
|
|
144
|
+
return /*#__PURE__*/_jsx("div", {
|
|
145
|
+
className: cx(variants({
|
|
146
|
+
shiki: true,
|
|
147
|
+
showBackground: showBackground,
|
|
148
|
+
variant: variant
|
|
149
|
+
}), className),
|
|
150
|
+
dangerouslySetInnerHTML: {
|
|
151
|
+
__html: data || ''
|
|
152
|
+
},
|
|
153
|
+
dir: "ltr",
|
|
154
|
+
ref: ref,
|
|
155
|
+
style: style
|
|
156
|
+
});
|
|
157
|
+
}
|
|
178
158
|
return /*#__PURE__*/_jsx("div", {
|
|
179
159
|
className: cx(variants({
|
|
180
160
|
shiki: true,
|
|
@@ -185,11 +165,17 @@ var SyntaxHighlighter = /*#__PURE__*/memo(function (_ref3) {
|
|
|
185
165
|
ref: ref,
|
|
186
166
|
style: style,
|
|
187
167
|
children: /*#__PURE__*/_jsx("pre", {
|
|
188
|
-
className:
|
|
168
|
+
className: cx('shiki', isDefaultTheme ? undefined : theme),
|
|
169
|
+
style: preStyle,
|
|
170
|
+
tabIndex: 0,
|
|
189
171
|
children: /*#__PURE__*/_jsx("code", {
|
|
172
|
+
style: {
|
|
173
|
+
display: 'flex',
|
|
174
|
+
flexDirection: 'column'
|
|
175
|
+
},
|
|
190
176
|
children: contentLines.map(function (line, index) {
|
|
191
177
|
return /*#__PURE__*/_jsx(Line, {
|
|
192
|
-
|
|
178
|
+
children: line
|
|
193
179
|
}, index);
|
|
194
180
|
})
|
|
195
181
|
})
|
|
@@ -11,7 +11,7 @@ export var useStyles = createStyles(function (_ref) {
|
|
|
11
11
|
noPadding: css(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["\n pre {\n padding: 0;\n }\n "]))),
|
|
12
12
|
padding: css(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["\n pre {\n padding: 16px;\n }\n "]))),
|
|
13
13
|
root: css(_templateObject4 || (_templateObject4 = _taggedTemplateLiteral(["\n direction: ltr;\n margin: 0;\n padding: 0;\n text-align: start;\n\n pre {\n overflow-x: auto;\n margin: 0;\n padding: 0;\n }\n "]))),
|
|
14
|
-
shiki: cx("".concat(prefixCls, "-highlighter-shiki"), css(_templateObject5 || (_templateObject5 = _taggedTemplateLiteral(["\n pre {\n code {\n display: block;\n\n .line {\n display: 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
|
|
14
|
+
shiki: cx("".concat(prefixCls, "-highlighter-shiki"), css(_templateObject5 || (_templateObject5 = _taggedTemplateLiteral(["\n pre {\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 border: 1px solid ", ";\n border-radius: ", "px;\n\n background: ", ";\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 "])), token.colorFillTertiary, token.colorWarningBg, token.colorErrorBg, token.colorBorderSecondary, token.borderRadius, token.colorFillTertiary, token.colorErrorBg, token.colorErrorText, token.colorSuccessBg, token.colorSuccessText)),
|
|
15
15
|
unshiki: css(_templateObject6 || (_templateObject6 = _taggedTemplateLiteral(["\n color: ", ";\n "])), token.colorTextDescription)
|
|
16
16
|
};
|
|
17
17
|
});
|
package/es/Highlighter/type.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ import type { BuiltinTheme } from 'shiki';
|
|
|
4
4
|
import type { ActionIconProps } from "../ActionIcon";
|
|
5
5
|
import { DivProps } from "../types";
|
|
6
6
|
export interface SyntaxHighlighterProps extends DivProps {
|
|
7
|
+
animated?: boolean;
|
|
7
8
|
children: string;
|
|
8
9
|
enableTransformer?: HighlighterProps['enableTransformer'];
|
|
9
10
|
language: HighlighterProps['language'];
|
|
@@ -20,6 +21,7 @@ export interface HighlighterProps extends Omit<FlexboxProps, 'children' | 'wrap'
|
|
|
20
21
|
originalNode: ReactNode;
|
|
21
22
|
}) => ReactNode;
|
|
22
23
|
allowChangeLanguage?: boolean;
|
|
24
|
+
animated?: boolean;
|
|
23
25
|
bodyRender?: (props: {
|
|
24
26
|
content: string;
|
|
25
27
|
language: string;
|
|
@@ -7,39 +7,64 @@ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key i
|
|
|
7
7
|
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
8
8
|
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
9
9
|
import { memo, useMemo } from 'react';
|
|
10
|
-
import
|
|
10
|
+
import { MarkdownHooks } from 'react-markdown';
|
|
11
11
|
import { PreviewGroup } from "../../Image";
|
|
12
|
-
import { useMarkdown } from "../../hooks/useMarkdown";
|
|
12
|
+
import { useMarkdown, useMarkdownContent } from "../../hooks/useMarkdown";
|
|
13
13
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
enableMermaid = _ref$enableMermaid === void 0 ? true : _ref$enableMermaid,
|
|
22
|
-
_ref$enableImageGalle = _ref.enableImageGallery,
|
|
23
|
-
enableImageGallery = _ref$enableImageGalle === void 0 ? true : _ref$enableImageGalle,
|
|
24
|
-
enableCustomFootnotes = _ref.enableCustomFootnotes,
|
|
25
|
-
componentProps = _ref.componentProps,
|
|
26
|
-
allowHtml = _ref.allowHtml,
|
|
27
|
-
showFootnotes = _ref.showFootnotes,
|
|
28
|
-
_ref$variant = _ref.variant,
|
|
29
|
-
variant = _ref$variant === void 0 ? 'default' : _ref$variant,
|
|
14
|
+
// Create a pure component that only renders ReactMarkdown
|
|
15
|
+
// This helps prevent unnecessary rerenders of ReactMarkdown
|
|
16
|
+
var MarkdownRenderer = /*#__PURE__*/memo(function (_ref) {
|
|
17
|
+
var escapedContent = _ref.escapedContent,
|
|
18
|
+
memoComponents = _ref.memoComponents,
|
|
19
|
+
rehypePluginsList = _ref.rehypePluginsList,
|
|
20
|
+
remarkPluginsList = _ref.remarkPluginsList,
|
|
30
21
|
reactMarkdownProps = _ref.reactMarkdownProps,
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
22
|
+
enableImageGallery = _ref.enableImageGallery;
|
|
23
|
+
return /*#__PURE__*/_jsx(PreviewGroup, {
|
|
24
|
+
enable: enableImageGallery,
|
|
25
|
+
children: /*#__PURE__*/_jsx(MarkdownHooks, _objectSpread(_objectSpread({}, reactMarkdownProps), {}, {
|
|
26
|
+
components: memoComponents,
|
|
27
|
+
rehypePlugins: rehypePluginsList,
|
|
28
|
+
remarkPlugins: remarkPluginsList,
|
|
29
|
+
children: escapedContent || ''
|
|
30
|
+
}))
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
MarkdownRenderer.displayName = 'MarkdownRenderer';
|
|
34
|
+
var SyntaxMarkdown = /*#__PURE__*/memo(function (_ref2) {
|
|
35
|
+
var children = _ref2.children,
|
|
36
|
+
fullFeaturedCodeBlock = _ref2.fullFeaturedCodeBlock,
|
|
37
|
+
animated = _ref2.animated,
|
|
38
|
+
_ref2$enableLatex = _ref2.enableLatex,
|
|
39
|
+
enableLatex = _ref2$enableLatex === void 0 ? true : _ref2$enableLatex,
|
|
40
|
+
_ref2$enableMermaid = _ref2.enableMermaid,
|
|
41
|
+
enableMermaid = _ref2$enableMermaid === void 0 ? true : _ref2$enableMermaid,
|
|
42
|
+
_ref2$enableImageGall = _ref2.enableImageGallery,
|
|
43
|
+
enableImageGallery = _ref2$enableImageGall === void 0 ? true : _ref2$enableImageGall,
|
|
44
|
+
enableCustomFootnotes = _ref2.enableCustomFootnotes,
|
|
45
|
+
componentProps = _ref2.componentProps,
|
|
46
|
+
allowHtml = _ref2.allowHtml,
|
|
47
|
+
showFootnotes = _ref2.showFootnotes,
|
|
48
|
+
_ref2$variant = _ref2.variant,
|
|
49
|
+
variant = _ref2$variant === void 0 ? 'default' : _ref2$variant,
|
|
50
|
+
reactMarkdownProps = _ref2.reactMarkdownProps,
|
|
51
|
+
rehypePlugins = _ref2.rehypePlugins,
|
|
52
|
+
remarkPlugins = _ref2.remarkPlugins,
|
|
53
|
+
remarkPluginsAhead = _ref2.remarkPluginsAhead,
|
|
54
|
+
_ref2$components = _ref2.components,
|
|
55
|
+
components = _ref2$components === void 0 ? {} : _ref2$components,
|
|
56
|
+
customRender = _ref2.customRender,
|
|
57
|
+
citations = _ref2.citations;
|
|
58
|
+
var escapedContent = useMarkdownContent({
|
|
59
|
+
animated: animated,
|
|
60
|
+
children: children,
|
|
61
|
+
citations: citations,
|
|
62
|
+
enableCustomFootnotes: enableCustomFootnotes,
|
|
63
|
+
enableLatex: enableLatex
|
|
64
|
+
});
|
|
39
65
|
var _useMarkdown = useMarkdown({
|
|
40
66
|
allowHtml: allowHtml,
|
|
41
67
|
animated: animated,
|
|
42
|
-
children: children,
|
|
43
68
|
citations: citations,
|
|
44
69
|
componentProps: componentProps,
|
|
45
70
|
components: components,
|
|
@@ -54,29 +79,32 @@ var SyntaxMarkdown = /*#__PURE__*/memo(function (_ref) {
|
|
|
54
79
|
showFootnotes: showFootnotes,
|
|
55
80
|
variant: variant
|
|
56
81
|
}),
|
|
57
|
-
escapedContent = _useMarkdown.escapedContent,
|
|
58
82
|
memoComponents = _useMarkdown.memoComponents,
|
|
59
83
|
rehypePluginsList = _useMarkdown.rehypePluginsList,
|
|
60
84
|
remarkPluginsList = _useMarkdown.remarkPluginsList;
|
|
61
85
|
|
|
62
|
-
//
|
|
86
|
+
// Memoize the renderer configuration to prevent unnecessary re-renders
|
|
87
|
+
var rendererProps = useMemo(function () {
|
|
88
|
+
return {
|
|
89
|
+
enableImageGallery: enableImageGallery,
|
|
90
|
+
memoComponents: memoComponents,
|
|
91
|
+
reactMarkdownProps: reactMarkdownProps,
|
|
92
|
+
rehypePluginsList: rehypePluginsList,
|
|
93
|
+
remarkPluginsList: remarkPluginsList
|
|
94
|
+
};
|
|
95
|
+
}, [memoComponents, rehypePluginsList, remarkPluginsList, enableImageGallery, reactMarkdownProps]);
|
|
96
|
+
|
|
97
|
+
// Render default content using memoized MarkdownRenderer
|
|
63
98
|
var defaultDOM = useMemo(function () {
|
|
64
|
-
return /*#__PURE__*/_jsx(
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
rehypePlugins: rehypePluginsList,
|
|
69
|
-
remarkPlugins: remarkPluginsList,
|
|
70
|
-
children: escapedContent
|
|
71
|
-
}))
|
|
72
|
-
});
|
|
73
|
-
}, [escapedContent, memoComponents, rehypePluginsList, remarkPluginsList, enableImageGallery, reactMarkdownProps]);
|
|
99
|
+
return /*#__PURE__*/_jsx(MarkdownRenderer, _objectSpread({
|
|
100
|
+
escapedContent: escapedContent
|
|
101
|
+
}, rendererProps));
|
|
102
|
+
}, [rendererProps, escapedContent]);
|
|
74
103
|
|
|
75
|
-
//
|
|
76
|
-
|
|
104
|
+
// Apply custom rendering if needed
|
|
105
|
+
return customRender ? customRender(defaultDOM, {
|
|
77
106
|
text: escapedContent || ''
|
|
78
107
|
}) : defaultDOM;
|
|
79
|
-
return markdownContent;
|
|
80
108
|
});
|
|
81
109
|
SyntaxMarkdown.displayName = 'SyntaxMarkdown';
|
|
82
110
|
export default SyntaxMarkdown;
|
|
@@ -7,12 +7,12 @@ export declare const useCode: (raw: any) => {
|
|
|
7
7
|
lang: any;
|
|
8
8
|
} | undefined;
|
|
9
9
|
interface CodeBlockProps {
|
|
10
|
+
animated?: boolean;
|
|
10
11
|
children: any;
|
|
11
12
|
enableMermaid?: boolean;
|
|
12
13
|
fullFeatured?: boolean;
|
|
13
14
|
highlight?: HighlighterProps;
|
|
14
15
|
mermaid?: MermaidProps;
|
|
15
16
|
}
|
|
16
|
-
export declare const
|
|
17
|
-
export declare const CodeFullFeatured: FC<CodeBlockProps>;
|
|
17
|
+
export declare const CodeBlock: FC<CodeBlockProps>;
|
|
18
18
|
export {};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
2
|
-
var _excluded = ["fullFeatured", "enableMermaid", "highlight", "mermaid", "children"];
|
|
2
|
+
var _excluded = ["fullFeatured", "enableMermaid", "highlight", "mermaid", "children", "animated"];
|
|
3
3
|
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; }
|
|
4
4
|
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; }
|
|
5
5
|
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
@@ -30,12 +30,13 @@ export var useCode = function useCode(raw) {
|
|
|
30
30
|
lang: lang
|
|
31
31
|
};
|
|
32
32
|
};
|
|
33
|
-
var CodeBlock = function CodeBlock(_ref) {
|
|
33
|
+
export var CodeBlock = function CodeBlock(_ref) {
|
|
34
34
|
var fullFeatured = _ref.fullFeatured,
|
|
35
35
|
enableMermaid = _ref.enableMermaid,
|
|
36
36
|
highlight = _ref.highlight,
|
|
37
37
|
mermaid = _ref.mermaid,
|
|
38
38
|
children = _ref.children,
|
|
39
|
+
animated = _ref.animated,
|
|
39
40
|
rest = _objectWithoutProperties(_ref, _excluded);
|
|
40
41
|
var code = useCode(children);
|
|
41
42
|
if (!code) return;
|
|
@@ -49,17 +50,10 @@ var CodeBlock = function CodeBlock(_ref) {
|
|
|
49
50
|
children: code.content
|
|
50
51
|
});
|
|
51
52
|
return /*#__PURE__*/_jsx(Pre, _objectSpread(_objectSpread(_objectSpread({
|
|
53
|
+
animated: animated,
|
|
52
54
|
fullFeatured: fullFeatured,
|
|
53
55
|
language: code.lang
|
|
54
56
|
}, highlight), rest), {}, {
|
|
55
57
|
children: code.content
|
|
56
58
|
}));
|
|
57
|
-
};
|
|
58
|
-
export var CodeLite = function CodeLite(props) {
|
|
59
|
-
return /*#__PURE__*/_jsx(CodeBlock, _objectSpread({}, props));
|
|
60
|
-
};
|
|
61
|
-
export var CodeFullFeatured = function CodeFullFeatured(props) {
|
|
62
|
-
return /*#__PURE__*/_jsx(CodeBlock, _objectSpread({
|
|
63
|
-
fullFeatured: true
|
|
64
|
-
}, props));
|
|
65
59
|
};
|
package/es/hooks/useHighlight.js
CHANGED
|
@@ -12,12 +12,12 @@ import { Md5 } from 'ts-md5';
|
|
|
12
12
|
import { languages } from "../Highlighter/const";
|
|
13
13
|
export var FALLBACK_LANG = 'txt';
|
|
14
14
|
|
|
15
|
-
//
|
|
16
|
-
var MD5_LENGTH_THRESHOLD = 10000; //
|
|
15
|
+
// Application-level cache to avoid repeated calculations
|
|
16
|
+
var MD5_LENGTH_THRESHOLD = 10000; // Use async MD5 for text exceeding this length
|
|
17
17
|
|
|
18
|
-
//
|
|
18
|
+
// Color replacement mapping type
|
|
19
19
|
|
|
20
|
-
//
|
|
20
|
+
// Lazy load shiki
|
|
21
21
|
var loadShiki = function loadShiki() {
|
|
22
22
|
if (typeof window === 'undefined') return Promise.resolve(null);
|
|
23
23
|
return import('shiki').then(function (mod) {
|
|
@@ -26,12 +26,12 @@ var loadShiki = function loadShiki() {
|
|
|
26
26
|
};
|
|
27
27
|
var shikiPromise = loadShiki();
|
|
28
28
|
|
|
29
|
-
//
|
|
29
|
+
// Helper function: Safe HTML escaping
|
|
30
30
|
var escapeHtml = function escapeHtml(str) {
|
|
31
31
|
return str.replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>').replaceAll('"', '"').replaceAll("'", ''');
|
|
32
32
|
};
|
|
33
33
|
|
|
34
|
-
//
|
|
34
|
+
// Main highlight component
|
|
35
35
|
export var useHighlight = function useHighlight(text, _ref) {
|
|
36
36
|
var language = _ref.language,
|
|
37
37
|
enableTransformer = _ref.enableTransformer,
|
|
@@ -41,18 +41,18 @@ export var useHighlight = function useHighlight(text, _ref) {
|
|
|
41
41
|
var theme = useTheme();
|
|
42
42
|
var lang = language.toLowerCase();
|
|
43
43
|
|
|
44
|
-
//
|
|
44
|
+
// Match supported languages
|
|
45
45
|
var matchedLanguage = useMemo(function () {
|
|
46
46
|
return languages.includes(lang) ? lang : FALLBACK_LANG;
|
|
47
47
|
}, [lang]);
|
|
48
48
|
|
|
49
|
-
//
|
|
49
|
+
// Optimize transformer creation
|
|
50
50
|
var transformers = useMemo(function () {
|
|
51
51
|
if (!enableTransformer) return;
|
|
52
52
|
return [transformerNotationDiff(), transformerNotationHighlight(), transformerNotationWordHighlight(), transformerNotationFocus(), transformerNotationErrorLevel()];
|
|
53
53
|
}, [enableTransformer]);
|
|
54
54
|
|
|
55
|
-
//
|
|
55
|
+
// Optimize color replacement configuration
|
|
56
56
|
var colorReplacements = useMemo(function () {
|
|
57
57
|
return {
|
|
58
58
|
'slack-dark': {
|
|
@@ -81,14 +81,14 @@ export var useHighlight = function useHighlight(text, _ref) {
|
|
|
81
81
|
};
|
|
82
82
|
}, [theme]);
|
|
83
83
|
|
|
84
|
-
//
|
|
84
|
+
// Build cache key
|
|
85
85
|
var cacheKey = useMemo(function () {
|
|
86
|
-
//
|
|
86
|
+
// Use hash for long text
|
|
87
87
|
var hash = text.length < MD5_LENGTH_THRESHOLD ? text : Md5.hashStr(text);
|
|
88
88
|
return [matchedLanguage, builtinTheme || (isDarkMode ? 'd' : 'l'), hash].filter(Boolean).join('-');
|
|
89
89
|
}, [text, matchedLanguage, isDarkMode, builtinTheme]);
|
|
90
90
|
|
|
91
|
-
//
|
|
91
|
+
// Use SWR to get highlighted HTML
|
|
92
92
|
return useSWR(cacheKey, /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
93
93
|
var codeToHtml, html, _codeToHtml, _html, fallbackHtml;
|
|
94
94
|
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
@@ -118,7 +118,7 @@ export var useHighlight = function useHighlight(text, _ref) {
|
|
|
118
118
|
case 12:
|
|
119
119
|
_context.prev = 12;
|
|
120
120
|
_context.t0 = _context["catch"](0);
|
|
121
|
-
console.error('
|
|
121
|
+
console.error('Advanced rendering failed:', _context.t0);
|
|
122
122
|
_context.prev = 15;
|
|
123
123
|
_context.next = 18;
|
|
124
124
|
return shikiPromise;
|
|
@@ -141,7 +141,7 @@ export var useHighlight = function useHighlight(text, _ref) {
|
|
|
141
141
|
case 27:
|
|
142
142
|
_context.prev = 27;
|
|
143
143
|
_context.t1 = _context["catch"](15);
|
|
144
|
-
//
|
|
144
|
+
// Fallback to plain text
|
|
145
145
|
fallbackHtml = "<pre class=\"fallback\"><code>".concat(escapeHtml(text), "</code></pre>");
|
|
146
146
|
return _context.abrupt("return", fallbackHtml);
|
|
147
147
|
case 31:
|
|
@@ -151,9 +151,9 @@ export var useHighlight = function useHighlight(text, _ref) {
|
|
|
151
151
|
}, _callee, null, [[0, 12], [15, 27]]);
|
|
152
152
|
})), {
|
|
153
153
|
dedupingInterval: 3000,
|
|
154
|
-
// 3
|
|
154
|
+
// Only execute once for the same request within 3 seconds
|
|
155
155
|
errorRetryCount: 2,
|
|
156
|
-
//
|
|
156
|
+
// Retry at most 2 times
|
|
157
157
|
revalidateOnFocus: false,
|
|
158
158
|
revalidateOnReconnect: false
|
|
159
159
|
});
|
|
@@ -3,10 +3,12 @@ import type { Pluggable } from 'unified';
|
|
|
3
3
|
import type { MarkdownProps } from "../../Markdown/type";
|
|
4
4
|
/**
|
|
5
5
|
* Processes Markdown content and prepares rendering components and configurations
|
|
6
|
+
* Optimized version with better memoization and performance
|
|
6
7
|
*/
|
|
7
|
-
export declare const useMarkdown: ({
|
|
8
|
+
export declare const useMarkdown: ({ fullFeaturedCodeBlock, animated, enableLatex, enableMermaid, enableImageGallery, enableCustomFootnotes, componentProps, allowHtml, showFootnotes, variant, rehypePlugins, remarkPlugins, remarkPluginsAhead, components, citations, }: Pick<MarkdownProps, 'fullFeaturedCodeBlock' | 'animated' | 'enableLatex' | 'enableMermaid' | 'enableImageGallery' | 'enableCustomFootnotes' | 'componentProps' | 'allowHtml' | 'showFootnotes' | 'variant' | 'rehypePlugins' | 'remarkPlugins' | 'remarkPluginsAhead' | 'components' | 'citations'>) => {
|
|
8
9
|
escapedContent?: string | undefined;
|
|
9
10
|
memoComponents: Components;
|
|
10
11
|
rehypePluginsList: Pluggable[];
|
|
11
12
|
remarkPluginsList: Pluggable[];
|
|
12
13
|
};
|
|
14
|
+
export declare const useMarkdownContent: ({ children, animated, enableLatex, enableCustomFootnotes, citations, }: Pick<MarkdownProps, 'children' | 'animated' | 'enableLatex' | 'enableCustomFootnotes' | 'citations'>) => string | undefined;
|