@lobehub/ui 2.0.12 → 2.0.13
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/SyntaxHighlighter/index.js +91 -15
- package/es/Markdown/Markdown.js +32 -111
- package/es/Markdown/SyntaxMarkdown/index.d.ts +4 -0
- package/es/Markdown/SyntaxMarkdown/index.js +82 -0
- package/es/Markdown/{CodeBlock.d.ts → components/CodeBlock.d.ts} +2 -2
- package/es/Markdown/{CodeBlock.js → components/CodeBlock.js} +2 -2
- package/es/Markdown/plugins/animated.d.ts +2 -0
- package/es/Markdown/plugins/animated.js +56 -0
- package/es/Markdown/style.d.ts +1 -0
- package/es/Markdown/style.js +7 -5
- package/es/Markdown/type.d.ts +8 -5
- package/es/Markdown/utils.d.ts +2 -0
- package/es/Markdown/utils.js +40 -2
- package/es/Mermaid/SyntaxMermaid/index.js +5 -6
- package/es/hooks/useMarkdown/index.d.ts +12 -0
- package/es/hooks/useMarkdown/index.js +146 -0
- package/es/hooks/useMarkdown/utils.d.ts +21 -0
- package/es/hooks/useMarkdown/utils.js +166 -0
- package/es/hooks/useMermaid.js +1 -1
- package/package.json +3 -1
|
@@ -1,19 +1,39 @@
|
|
|
1
1
|
'use client';
|
|
2
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; }
|
|
3
14
|
import { cva } from 'class-variance-authority';
|
|
4
|
-
import { memo, useMemo } from 'react';
|
|
15
|
+
import { memo, useEffect, useMemo, useState } from 'react';
|
|
5
16
|
import { useHighlight } from "../../hooks/useHighlight";
|
|
6
17
|
import { useStyles } from "./style";
|
|
7
18
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
8
|
-
var
|
|
9
|
-
var
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
19
|
+
var Line = /*#__PURE__*/memo(function (_ref) {
|
|
20
|
+
var content = _ref.content;
|
|
21
|
+
return /*#__PURE__*/_jsx("div", {
|
|
22
|
+
dangerouslySetInnerHTML: {
|
|
23
|
+
__html: content
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
Line.displayName = 'HighlighterLine';
|
|
28
|
+
var SyntaxHighlighter = /*#__PURE__*/memo(function (_ref2) {
|
|
29
|
+
var ref = _ref2.ref,
|
|
30
|
+
children = _ref2.children,
|
|
31
|
+
language = _ref2.language,
|
|
32
|
+
className = _ref2.className,
|
|
33
|
+
style = _ref2.style,
|
|
34
|
+
enableTransformer = _ref2.enableTransformer,
|
|
35
|
+
variant = _ref2.variant,
|
|
36
|
+
theme = _ref2.theme;
|
|
17
37
|
var _useStyles = useStyles(),
|
|
18
38
|
styles = _useStyles.styles,
|
|
19
39
|
cx = _useStyles.cx;
|
|
@@ -25,6 +45,55 @@ var SyntaxHighlighter = /*#__PURE__*/memo(function (_ref) {
|
|
|
25
45
|
theme: isDefaultTheme ? undefined : theme
|
|
26
46
|
}),
|
|
27
47
|
data = _useHighlight.data;
|
|
48
|
+
var _useState = useState([]),
|
|
49
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
50
|
+
contentLines = _useState2[0],
|
|
51
|
+
setContentLines = _useState2[1];
|
|
52
|
+
useEffect(function () {
|
|
53
|
+
if (data && typeof data === 'string') {
|
|
54
|
+
// Extract all lines from the HTML content
|
|
55
|
+
// We need to handle the structure from shiki which gives us HTML with a <pre><code> structure
|
|
56
|
+
var parser = new DOMParser();
|
|
57
|
+
var doc = parser.parseFromString(data, 'text/html');
|
|
58
|
+
var codeElement = doc.querySelector('pre code');
|
|
59
|
+
if (codeElement) {
|
|
60
|
+
var spanLines = codeElement.querySelectorAll('.line');
|
|
61
|
+
var newLines = _toConsumableArray(spanLines).map(function (line) {
|
|
62
|
+
return line.outerHTML;
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// Only update if the lines have changed
|
|
66
|
+
setContentLines(function (prevLines) {
|
|
67
|
+
if (prevLines.length !== newLines.length) return newLines;
|
|
68
|
+
var hasChanged = false;
|
|
69
|
+
var _iterator = _createForOfIteratorHelper(newLines.entries()),
|
|
70
|
+
_step;
|
|
71
|
+
try {
|
|
72
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
73
|
+
var _step$value = _slicedToArray(_step.value, 2),
|
|
74
|
+
i = _step$value[0],
|
|
75
|
+
newLine = _step$value[1];
|
|
76
|
+
if (prevLines[i] !== newLine) {
|
|
77
|
+
hasChanged = true;
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
} catch (err) {
|
|
82
|
+
_iterator.e(err);
|
|
83
|
+
} finally {
|
|
84
|
+
_iterator.f();
|
|
85
|
+
}
|
|
86
|
+
return hasChanged ? newLines : prevLines;
|
|
87
|
+
});
|
|
88
|
+
} else {
|
|
89
|
+
// Fallback if the structure is different
|
|
90
|
+
var htmlLines = data.split('\n').map(function (line) {
|
|
91
|
+
return "<span class=\"line\">".concat(line, "</span>");
|
|
92
|
+
});
|
|
93
|
+
setContentLines(htmlLines);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}, [data]);
|
|
28
97
|
var variants = useMemo(function () {
|
|
29
98
|
return cva(styles.root, {
|
|
30
99
|
defaultVariants: {
|
|
@@ -51,7 +120,7 @@ var SyntaxHighlighter = /*#__PURE__*/memo(function (_ref) {
|
|
|
51
120
|
/* eslint-enable sort-keys-fix/sort-keys-fix */
|
|
52
121
|
});
|
|
53
122
|
}, [styles]);
|
|
54
|
-
if (
|
|
123
|
+
if (contentLines.length === 0) return /*#__PURE__*/_jsx("div", {
|
|
55
124
|
className: cx(variants({
|
|
56
125
|
shiki: false,
|
|
57
126
|
showBackground: showBackground,
|
|
@@ -72,12 +141,19 @@ var SyntaxHighlighter = /*#__PURE__*/memo(function (_ref) {
|
|
|
72
141
|
showBackground: showBackground,
|
|
73
142
|
variant: variant
|
|
74
143
|
}), className),
|
|
75
|
-
dangerouslySetInnerHTML: {
|
|
76
|
-
__html: data
|
|
77
|
-
},
|
|
78
144
|
dir: "ltr",
|
|
79
145
|
ref: ref,
|
|
80
|
-
style: style
|
|
146
|
+
style: style,
|
|
147
|
+
children: /*#__PURE__*/_jsx("pre", {
|
|
148
|
+
className: "shiki",
|
|
149
|
+
children: /*#__PURE__*/_jsx("code", {
|
|
150
|
+
children: contentLines.map(function (line, index) {
|
|
151
|
+
return /*#__PURE__*/_jsx(Line, {
|
|
152
|
+
content: line
|
|
153
|
+
}, index);
|
|
154
|
+
})
|
|
155
|
+
})
|
|
156
|
+
})
|
|
81
157
|
});
|
|
82
158
|
});
|
|
83
159
|
SyntaxHighlighter.displayName = 'SyntaxHighlighter';
|
package/es/Markdown/Markdown.js
CHANGED
|
@@ -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 = ["ref", "children", "className", "style", "fullFeaturedCodeBlock", "onDoubleClick", "enableLatex", "enableMermaid", "enableImageGallery", "enableCustomFootnotes", "componentProps", "allowHtml", "fontSize", "headerMultiple", "marginMultiple", "showFootnotes", "variant", "reactMarkdownProps", "lineHeight", "rehypePlugins", "remarkPlugins", "remarkPluginsAhead", "components", "customRender", "citations"];
|
|
4
|
+
var _excluded = ["ref", "children", "className", "style", "fullFeaturedCodeBlock", "onDoubleClick", "animated", "enableLatex", "enableMermaid", "enableImageGallery", "enableCustomFootnotes", "componentProps", "allowHtml", "fontSize", "headerMultiple", "marginMultiple", "showFootnotes", "variant", "reactMarkdownProps", "lineHeight", "rehypePlugins", "remarkPlugins", "remarkPluginsAhead", "components", "customRender", "citations"];
|
|
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; }
|
|
@@ -10,17 +10,10 @@ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e
|
|
|
10
10
|
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
|
11
11
|
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
|
12
12
|
import { cva } from 'class-variance-authority';
|
|
13
|
-
import { memo,
|
|
14
|
-
import
|
|
15
|
-
import { PreviewGroup } from "../Image";
|
|
16
|
-
import Image from "../mdx/mdxComponents/Image";
|
|
17
|
-
import Link from "../mdx/mdxComponents/Link";
|
|
18
|
-
import Section from "../mdx/mdxComponents/Section";
|
|
19
|
-
import Video from "../mdx/mdxComponents/Video";
|
|
20
|
-
import { CodeFullFeatured, CodeLite } from "./CodeBlock";
|
|
13
|
+
import { memo, useMemo } from 'react';
|
|
14
|
+
import SyntaxMarkdown from "./SyntaxMarkdown";
|
|
21
15
|
import Typography from "./Typography";
|
|
22
16
|
import { useStyles } from "./style";
|
|
23
|
-
import { addToCache, contentCache, createPlugins, escapeBrackets, escapeMhchem, fixMarkdownBold, transformCitations } from "./utils";
|
|
24
17
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
25
18
|
var Markdown = /*#__PURE__*/memo(function (_ref) {
|
|
26
19
|
var ref = _ref.ref,
|
|
@@ -29,6 +22,7 @@ var Markdown = /*#__PURE__*/memo(function (_ref) {
|
|
|
29
22
|
style = _ref.style,
|
|
30
23
|
fullFeaturedCodeBlock = _ref.fullFeaturedCodeBlock,
|
|
31
24
|
onDoubleClick = _ref.onDoubleClick,
|
|
25
|
+
animated = _ref.animated,
|
|
32
26
|
_ref$enableLatex = _ref.enableLatex,
|
|
33
27
|
enableLatex = _ref$enableLatex === void 0 ? true : _ref$enableLatex,
|
|
34
28
|
_ref$enableMermaid = _ref.enableMermaid,
|
|
@@ -61,10 +55,12 @@ var Markdown = /*#__PURE__*/memo(function (_ref) {
|
|
|
61
55
|
var _useStyles = useStyles(),
|
|
62
56
|
cx = _useStyles.cx,
|
|
63
57
|
styles = _useStyles.styles;
|
|
64
|
-
|
|
58
|
+
|
|
59
|
+
// Style variant handling
|
|
65
60
|
var variants = useMemo(function () {
|
|
66
61
|
return cva(styles.root, {
|
|
67
62
|
defaultVariants: {
|
|
63
|
+
animated: false,
|
|
68
64
|
enableLatex: true,
|
|
69
65
|
variant: 'default'
|
|
70
66
|
},
|
|
@@ -77,112 +73,18 @@ var Markdown = /*#__PURE__*/memo(function (_ref) {
|
|
|
77
73
|
enableLatex: {
|
|
78
74
|
true: styles.latex,
|
|
79
75
|
false: null
|
|
76
|
+
},
|
|
77
|
+
animated: {
|
|
78
|
+
true: styles.animated,
|
|
79
|
+
false: null
|
|
80
80
|
}
|
|
81
81
|
}
|
|
82
82
|
/* eslint-enable sort-keys-fix/sort-keys-fix */
|
|
83
83
|
});
|
|
84
84
|
}, [styles]);
|
|
85
|
-
|
|
86
|
-
// 计算缓存键
|
|
87
|
-
var cacheKey = useMemo(function () {
|
|
88
|
-
return "".concat(children, "-").concat(enableLatex, "-").concat(enableCustomFootnotes, "-").concat((citations === null || citations === void 0 ? void 0 : citations.length) || 0);
|
|
89
|
-
}, [children, enableLatex, enableCustomFootnotes, citations === null || citations === void 0 ? void 0 : citations.length]);
|
|
90
|
-
|
|
91
|
-
// 处理内容并利用缓存避免重复计算
|
|
92
|
-
var escapedContent = useMemo(function () {
|
|
93
|
-
// 尝试从缓存获取
|
|
94
|
-
if (contentCache.has(cacheKey)) {
|
|
95
|
-
return contentCache.get(cacheKey);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// 处理新内容
|
|
99
|
-
var processedContent;
|
|
100
|
-
if (enableLatex) {
|
|
101
|
-
var baseContent = fixMarkdownBold(escapeMhchem(escapeBrackets(children)));
|
|
102
|
-
processedContent = enableCustomFootnotes ? transformCitations(baseContent, citations === null || citations === void 0 ? void 0 : citations.length) : baseContent;
|
|
103
|
-
} else {
|
|
104
|
-
processedContent = fixMarkdownBold(children);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// 缓存处理结果
|
|
108
|
-
addToCache(cacheKey, processedContent);
|
|
109
|
-
return processedContent;
|
|
110
|
-
}, [cacheKey, children, enableLatex, enableCustomFootnotes, citations === null || citations === void 0 ? void 0 : citations.length]);
|
|
111
|
-
|
|
112
|
-
// 创建插件
|
|
113
|
-
var _useMemo = useMemo(function () {
|
|
114
|
-
return createPlugins({
|
|
115
|
-
allowHtml: allowHtml,
|
|
116
|
-
enableCustomFootnotes: enableCustomFootnotes,
|
|
117
|
-
enableLatex: enableLatex,
|
|
118
|
-
isChatMode: isChatMode,
|
|
119
|
-
rehypePlugins: rehypePlugins,
|
|
120
|
-
remarkPlugins: remarkPlugins,
|
|
121
|
-
remarkPluginsAhead: remarkPluginsAhead
|
|
122
|
-
});
|
|
123
|
-
}, [allowHtml, enableLatex, enableCustomFootnotes, isChatMode, rehypePlugins, remarkPlugins, remarkPluginsAhead]),
|
|
124
|
-
rehypePluginsList = _useMemo.rehypePluginsList,
|
|
125
|
-
remarkPluginsList = _useMemo.remarkPluginsList;
|
|
126
|
-
|
|
127
|
-
// 使用 useCallback 优化渲染子组件
|
|
128
|
-
var renderLink = useCallback(function (props) {
|
|
129
|
-
return /*#__PURE__*/_jsx(Link, _objectSpread(_objectSpread({
|
|
130
|
-
citations: citations
|
|
131
|
-
}, props), componentProps === null || componentProps === void 0 ? void 0 : componentProps.a));
|
|
132
|
-
}, [citations, componentProps === null || componentProps === void 0 ? void 0 : componentProps.a]);
|
|
133
|
-
var renderImage = useCallback(function (props) {
|
|
134
|
-
return /*#__PURE__*/_jsx(Image, _objectSpread(_objectSpread({}, props), componentProps === null || componentProps === void 0 ? void 0 : componentProps.img));
|
|
135
|
-
}, [isChatMode, componentProps === null || componentProps === void 0 ? void 0 : componentProps.img]);
|
|
136
|
-
var renderCodeBlock = useCallback(function (props) {
|
|
137
|
-
return fullFeaturedCodeBlock ? /*#__PURE__*/_jsx(CodeFullFeatured, _objectSpread(_objectSpread({
|
|
138
|
-
enableMermaid: enableMermaid,
|
|
139
|
-
highlight: componentProps === null || componentProps === void 0 ? void 0 : componentProps.highlight,
|
|
140
|
-
mermaid: componentProps === null || componentProps === void 0 ? void 0 : componentProps.mermaid
|
|
141
|
-
}, props), componentProps === null || componentProps === void 0 ? void 0 : componentProps.pre)) : /*#__PURE__*/_jsx(CodeLite, _objectSpread(_objectSpread({
|
|
142
|
-
enableMermaid: enableMermaid,
|
|
143
|
-
highlight: componentProps === null || componentProps === void 0 ? void 0 : componentProps.highlight,
|
|
144
|
-
mermaid: componentProps === null || componentProps === void 0 ? void 0 : componentProps.mermaid
|
|
145
|
-
}, props), componentProps === null || componentProps === void 0 ? void 0 : componentProps.pre));
|
|
146
|
-
}, [enableMermaid, fullFeaturedCodeBlock, componentProps === null || componentProps === void 0 ? void 0 : componentProps.highlight, componentProps === null || componentProps === void 0 ? void 0 : componentProps.mermaid, componentProps === null || componentProps === void 0 ? void 0 : componentProps.pre]);
|
|
147
|
-
var renderSection = useCallback(function (props) {
|
|
148
|
-
return /*#__PURE__*/_jsx(Section, _objectSpread({
|
|
149
|
-
showCitations: showFootnotes
|
|
150
|
-
}, props));
|
|
151
|
-
}, [showFootnotes]);
|
|
152
|
-
var renderVideo = useCallback(function (props) {
|
|
153
|
-
return /*#__PURE__*/_jsx(Video, _objectSpread(_objectSpread({}, props), componentProps === null || componentProps === void 0 ? void 0 : componentProps.video));
|
|
154
|
-
}, [componentProps === null || componentProps === void 0 ? void 0 : componentProps.video]);
|
|
155
|
-
|
|
156
|
-
// 创建组件映射
|
|
157
|
-
var memoComponents = useMemo(function () {
|
|
158
|
-
return _objectSpread({
|
|
159
|
-
a: renderLink,
|
|
160
|
-
img: enableImageGallery ? renderImage : undefined,
|
|
161
|
-
pre: renderCodeBlock,
|
|
162
|
-
section: renderSection,
|
|
163
|
-
video: renderVideo
|
|
164
|
-
}, components);
|
|
165
|
-
}, [renderLink, renderImage, renderCodeBlock, renderSection, renderVideo, enableImageGallery, components]);
|
|
166
|
-
|
|
167
|
-
// 渲染默认内容
|
|
168
|
-
var defaultDOM = useMemo(function () {
|
|
169
|
-
return /*#__PURE__*/_jsx(PreviewGroup, {
|
|
170
|
-
enable: enableImageGallery,
|
|
171
|
-
children: /*#__PURE__*/_jsx(ReactMarkdown, _objectSpread(_objectSpread({}, reactMarkdownProps), {}, {
|
|
172
|
-
components: memoComponents,
|
|
173
|
-
rehypePlugins: rehypePluginsList,
|
|
174
|
-
remarkPlugins: remarkPluginsList,
|
|
175
|
-
children: escapedContent
|
|
176
|
-
}))
|
|
177
|
-
});
|
|
178
|
-
}, [escapedContent, memoComponents, rehypePluginsList, remarkPluginsList, enableImageGallery]);
|
|
179
|
-
|
|
180
|
-
// 应用自定义渲染
|
|
181
|
-
var markdownContent = customRender ? customRender(defaultDOM, {
|
|
182
|
-
text: escapedContent || ''
|
|
183
|
-
}) : defaultDOM;
|
|
184
85
|
return /*#__PURE__*/_jsx(Typography, _objectSpread(_objectSpread({
|
|
185
86
|
className: cx(variants({
|
|
87
|
+
animated: animated,
|
|
186
88
|
enableLatex: enableLatex,
|
|
187
89
|
variant: variant
|
|
188
90
|
}), className),
|
|
@@ -195,7 +97,26 @@ var Markdown = /*#__PURE__*/memo(function (_ref) {
|
|
|
195
97
|
ref: ref,
|
|
196
98
|
style: style
|
|
197
99
|
}, rest), {}, {
|
|
198
|
-
children:
|
|
100
|
+
children: /*#__PURE__*/_jsx(SyntaxMarkdown, {
|
|
101
|
+
allowHtml: allowHtml,
|
|
102
|
+
animated: animated,
|
|
103
|
+
citations: citations,
|
|
104
|
+
componentProps: componentProps,
|
|
105
|
+
components: components,
|
|
106
|
+
customRender: customRender,
|
|
107
|
+
enableCustomFootnotes: enableCustomFootnotes,
|
|
108
|
+
enableImageGallery: enableImageGallery,
|
|
109
|
+
enableLatex: enableLatex,
|
|
110
|
+
enableMermaid: enableMermaid,
|
|
111
|
+
fullFeaturedCodeBlock: fullFeaturedCodeBlock,
|
|
112
|
+
reactMarkdownProps: reactMarkdownProps,
|
|
113
|
+
rehypePlugins: rehypePlugins,
|
|
114
|
+
remarkPlugins: remarkPlugins,
|
|
115
|
+
remarkPluginsAhead: remarkPluginsAhead,
|
|
116
|
+
showFootnotes: showFootnotes,
|
|
117
|
+
variant: variant,
|
|
118
|
+
children: children
|
|
119
|
+
})
|
|
199
120
|
}));
|
|
200
121
|
});
|
|
201
122
|
Markdown.displayName = 'Markdown';
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
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
|
+
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; }
|
|
5
|
+
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; }
|
|
6
|
+
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; }
|
|
7
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
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
|
+
import { memo, useMemo } from 'react';
|
|
10
|
+
import ReactMarkdown from 'react-markdown';
|
|
11
|
+
import { PreviewGroup } from "../../Image";
|
|
12
|
+
import { useMarkdown } from "../../hooks/useMarkdown";
|
|
13
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
14
|
+
var SyntaxMarkdown = /*#__PURE__*/memo(function (_ref) {
|
|
15
|
+
var children = _ref.children,
|
|
16
|
+
fullFeaturedCodeBlock = _ref.fullFeaturedCodeBlock,
|
|
17
|
+
animated = _ref.animated,
|
|
18
|
+
_ref$enableLatex = _ref.enableLatex,
|
|
19
|
+
enableLatex = _ref$enableLatex === void 0 ? true : _ref$enableLatex,
|
|
20
|
+
_ref$enableMermaid = _ref.enableMermaid,
|
|
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,
|
|
30
|
+
reactMarkdownProps = _ref.reactMarkdownProps,
|
|
31
|
+
rehypePlugins = _ref.rehypePlugins,
|
|
32
|
+
remarkPlugins = _ref.remarkPlugins,
|
|
33
|
+
remarkPluginsAhead = _ref.remarkPluginsAhead,
|
|
34
|
+
_ref$components = _ref.components,
|
|
35
|
+
components = _ref$components === void 0 ? {} : _ref$components,
|
|
36
|
+
customRender = _ref.customRender,
|
|
37
|
+
citations = _ref.citations;
|
|
38
|
+
// Use our new hook to handle Markdown processing
|
|
39
|
+
var _useMarkdown = useMarkdown({
|
|
40
|
+
allowHtml: allowHtml,
|
|
41
|
+
animated: animated,
|
|
42
|
+
children: children,
|
|
43
|
+
citations: citations,
|
|
44
|
+
componentProps: componentProps,
|
|
45
|
+
components: components,
|
|
46
|
+
enableCustomFootnotes: enableCustomFootnotes,
|
|
47
|
+
enableImageGallery: enableImageGallery,
|
|
48
|
+
enableLatex: enableLatex,
|
|
49
|
+
enableMermaid: enableMermaid,
|
|
50
|
+
fullFeaturedCodeBlock: fullFeaturedCodeBlock,
|
|
51
|
+
rehypePlugins: rehypePlugins,
|
|
52
|
+
remarkPlugins: remarkPlugins,
|
|
53
|
+
remarkPluginsAhead: remarkPluginsAhead,
|
|
54
|
+
showFootnotes: showFootnotes,
|
|
55
|
+
variant: variant
|
|
56
|
+
}),
|
|
57
|
+
escapedContent = _useMarkdown.escapedContent,
|
|
58
|
+
memoComponents = _useMarkdown.memoComponents,
|
|
59
|
+
rehypePluginsList = _useMarkdown.rehypePluginsList,
|
|
60
|
+
remarkPluginsList = _useMarkdown.remarkPluginsList;
|
|
61
|
+
|
|
62
|
+
// 渲染默认内容
|
|
63
|
+
var defaultDOM = useMemo(function () {
|
|
64
|
+
return /*#__PURE__*/_jsx(PreviewGroup, {
|
|
65
|
+
enable: enableImageGallery,
|
|
66
|
+
children: /*#__PURE__*/_jsx(ReactMarkdown, _objectSpread(_objectSpread({}, reactMarkdownProps), {}, {
|
|
67
|
+
components: memoComponents,
|
|
68
|
+
rehypePlugins: rehypePluginsList,
|
|
69
|
+
remarkPlugins: remarkPluginsList,
|
|
70
|
+
children: escapedContent
|
|
71
|
+
}))
|
|
72
|
+
});
|
|
73
|
+
}, [escapedContent, memoComponents, rehypePluginsList, remarkPluginsList, enableImageGallery, reactMarkdownProps]);
|
|
74
|
+
|
|
75
|
+
// 应用自定义渲染
|
|
76
|
+
var markdownContent = customRender ? customRender(defaultDOM, {
|
|
77
|
+
text: escapedContent || ''
|
|
78
|
+
}) : defaultDOM;
|
|
79
|
+
return markdownContent;
|
|
80
|
+
});
|
|
81
|
+
SyntaxMarkdown.displayName = 'SyntaxMarkdown';
|
|
82
|
+
export default SyntaxMarkdown;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { FC } from 'react';
|
|
2
|
-
import { type HighlighterProps } from "
|
|
3
|
-
import { type MermaidProps } from "
|
|
2
|
+
import { type HighlighterProps } from "../../Highlighter";
|
|
3
|
+
import { type MermaidProps } from "../../Mermaid";
|
|
4
4
|
export declare const useCode: (raw: any) => {
|
|
5
5
|
content: any;
|
|
6
6
|
isSingleLine: boolean;
|
|
@@ -7,8 +7,8 @@ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol"
|
|
|
7
7
|
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); }
|
|
8
8
|
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
|
9
9
|
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
|
10
|
-
import
|
|
11
|
-
import {
|
|
10
|
+
import { FALLBACK_LANG } from "../../hooks/useHighlight";
|
|
11
|
+
import Pre, { PreMermaid, PreSingleLine } from "../../mdx/mdxComponents/Pre";
|
|
12
12
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
13
13
|
var countLines = function countLines(str) {
|
|
14
14
|
var regex = /\n/g;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
2
|
+
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."); }
|
|
3
|
+
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
4
|
+
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
5
|
+
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; } } }; }
|
|
6
|
+
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); }
|
|
7
|
+
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; }
|
|
8
|
+
// Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
|
9
|
+
// SPDX-License-Identifier: MIT
|
|
10
|
+
|
|
11
|
+
import { visit } from 'unist-util-visit';
|
|
12
|
+
export var animatedPlugin = function animatedPlugin() {
|
|
13
|
+
return function (tree) {
|
|
14
|
+
visit(tree, 'element', function (node) {
|
|
15
|
+
if (['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'li', 'strong'].includes(node.tagName) && node.children) {
|
|
16
|
+
var newChildren = [];
|
|
17
|
+
var _iterator = _createForOfIteratorHelper(node.children),
|
|
18
|
+
_step;
|
|
19
|
+
try {
|
|
20
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
21
|
+
var child = _step.value;
|
|
22
|
+
if (child.type === 'text') {
|
|
23
|
+
var segmenter = new Intl.Segmenter('zh', {
|
|
24
|
+
granularity: 'word'
|
|
25
|
+
});
|
|
26
|
+
var segments = segmenter.segment(child.value);
|
|
27
|
+
var words = _toConsumableArray(segments).map(function (segment) {
|
|
28
|
+
return segment.segment;
|
|
29
|
+
}).filter(Boolean);
|
|
30
|
+
words.forEach(function (word) {
|
|
31
|
+
newChildren.push({
|
|
32
|
+
children: [{
|
|
33
|
+
type: 'text',
|
|
34
|
+
value: word
|
|
35
|
+
}],
|
|
36
|
+
properties: {
|
|
37
|
+
className: 'animate-fade-in'
|
|
38
|
+
},
|
|
39
|
+
tagName: 'span',
|
|
40
|
+
type: 'element'
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
} else {
|
|
44
|
+
newChildren.push(child);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
} catch (err) {
|
|
48
|
+
_iterator.e(err);
|
|
49
|
+
} finally {
|
|
50
|
+
_iterator.f();
|
|
51
|
+
}
|
|
52
|
+
node.children = newChildren;
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
};
|
package/es/Markdown/style.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export declare const useStyles: (props?: unknown) => import("antd-style").ReturnStyles<{
|
|
2
|
+
animated: import("antd-style").SerializedStyles;
|
|
2
3
|
chat: import("antd-style").SerializedStyles;
|
|
3
4
|
latex: import("antd-style").SerializedStyles;
|
|
4
5
|
root: import("antd-style").SerializedStyles;
|
package/es/Markdown/style.js
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
|
-
var _templateObject, _templateObject2, _templateObject3;
|
|
1
|
+
var _templateObject, _templateObject2, _templateObject3, _templateObject4, _templateObject5;
|
|
2
2
|
function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
|
|
3
|
-
import { createStyles } from 'antd-style';
|
|
3
|
+
import { createStyles, keyframes } from 'antd-style';
|
|
4
4
|
export var useStyles = createStyles(function (_ref) {
|
|
5
5
|
var css = _ref.css,
|
|
6
6
|
token = _ref.token,
|
|
7
7
|
isDarkMode = _ref.isDarkMode;
|
|
8
8
|
var cyanColor = isDarkMode ? token.cyan9A : token.cyan11A;
|
|
9
|
+
var fadeIn = keyframes(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n 0% {\n opacity: 0;\n }\n 100% {\n opacity: 1;\n }\n "])));
|
|
9
10
|
return {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
animated: css(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["\n .animate-fade-in,\n .katex-html span,\n span.line,\n > * {\n animation: ", " 1s ease-in-out;\n }\n "])), fadeIn),
|
|
12
|
+
chat: css(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["\n --lobe-markdown-border-radius: ", ";\n\n ol,\n ul {\n > li {\n &::marker {\n color: ", " !important;\n }\n\n > li {\n &::marker {\n color: ", " !important;\n }\n }\n }\n }\n\n ul {\n list-style: unset;\n\n > li {\n &::before {\n content: unset;\n display: unset;\n }\n }\n }\n "])), token.borderRadius, cyanColor, token.colorTextSecondary),
|
|
13
|
+
latex: css(_templateObject4 || (_templateObject4 = _taggedTemplateLiteral(["\n .katex-error {\n color: ", " !important;\n }\n\n .katex-html {\n overflow: auto hidden;\n padding: 3px;\n\n .base {\n margin-block: 0;\n margin-inline: auto;\n }\n\n .tag {\n position: relative !important;\n display: inline-block;\n padding-inline-start: 0.5rem;\n }\n }\n "])), token.colorTextDescription),
|
|
14
|
+
root: css(_templateObject5 || (_templateObject5 = _taggedTemplateLiteral(["\n position: relative;\n overflow: hidden;\n max-width: 100%;\n\n #footnote-label {\n display: none;\n }\n\n sup:has(a[aria-describedby='footnote-label']) {\n vertical-align: super !important;\n }\n "])))
|
|
13
15
|
};
|
|
14
16
|
});
|
package/es/Markdown/type.d.ts
CHANGED
|
@@ -15,11 +15,11 @@ export interface TypographyProps extends DivProps {
|
|
|
15
15
|
marginMultiple?: number;
|
|
16
16
|
ref?: Ref<HTMLDivElement>;
|
|
17
17
|
}
|
|
18
|
-
export interface
|
|
18
|
+
export interface SyntaxMarkdownProps {
|
|
19
19
|
allowHtml?: boolean;
|
|
20
|
+
animated?: boolean;
|
|
20
21
|
children: string;
|
|
21
22
|
citations?: CitationItem[];
|
|
22
|
-
className?: string;
|
|
23
23
|
componentProps?: {
|
|
24
24
|
a?: Partial<AProps & AnchorProps>;
|
|
25
25
|
highlight?: Partial<HighlighterProps>;
|
|
@@ -37,13 +37,16 @@ export interface MarkdownProps extends TypographyProps {
|
|
|
37
37
|
enableLatex?: boolean;
|
|
38
38
|
enableMermaid?: boolean;
|
|
39
39
|
fullFeaturedCodeBlock?: boolean;
|
|
40
|
-
onDoubleClick?: () => void;
|
|
41
40
|
reactMarkdownProps?: Omit<Readonly<ReactMarkdownOptions>, 'components' | 'rehypePlugins' | 'remarkPlugins'>;
|
|
42
|
-
ref?: Ref<HTMLDivElement>;
|
|
43
41
|
rehypePlugins?: Pluggable[];
|
|
44
42
|
remarkPlugins?: Pluggable[];
|
|
45
43
|
remarkPluginsAhead?: Pluggable[];
|
|
46
44
|
showFootnotes?: boolean;
|
|
47
|
-
style?: CSSProperties;
|
|
48
45
|
variant?: 'default' | 'chat';
|
|
49
46
|
}
|
|
47
|
+
export interface MarkdownProps extends SyntaxMarkdownProps, Omit<TypographyProps, 'children'> {
|
|
48
|
+
className?: string;
|
|
49
|
+
onDoubleClick?: () => void;
|
|
50
|
+
ref?: Ref<HTMLDivElement>;
|
|
51
|
+
style?: CSSProperties;
|
|
52
|
+
}
|
package/es/Markdown/utils.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ export declare const contentCache: Map<string, string>;
|
|
|
3
3
|
export declare const addToCache: (key: string, value: string) => void;
|
|
4
4
|
export declare const createPlugins: (props: {
|
|
5
5
|
allowHtml?: boolean;
|
|
6
|
+
animated?: boolean;
|
|
6
7
|
enableCustomFootnotes?: boolean;
|
|
7
8
|
enableLatex?: boolean;
|
|
8
9
|
isChatMode: boolean;
|
|
@@ -17,3 +18,4 @@ export declare function escapeBrackets(text: string): string;
|
|
|
17
18
|
export declare function escapeMhchem(text: string): string;
|
|
18
19
|
export declare function fixMarkdownBold(text: string): string;
|
|
19
20
|
export declare const transformCitations: (rawContent: string, length?: number) => string;
|
|
21
|
+
export declare const areFormulasRenderable: (text: string) => boolean;
|
package/es/Markdown/utils.js
CHANGED
|
@@ -4,11 +4,13 @@ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o =
|
|
|
4
4
|
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
5
5
|
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
6
6
|
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; }
|
|
7
|
+
import katex from 'katex';
|
|
7
8
|
import rehypeKatex from 'rehype-katex';
|
|
8
9
|
import rehypeRaw from 'rehype-raw';
|
|
9
10
|
import remarkBreaks from 'remark-breaks';
|
|
10
11
|
import remarkGfm from 'remark-gfm';
|
|
11
12
|
import remarkMath from 'remark-math';
|
|
13
|
+
import { animatedPlugin } from "./plugins/animated";
|
|
12
14
|
import { rehypeFootnoteLinks, remarkCustomFootnotes } from "./plugins/footnote";
|
|
13
15
|
import { rehypeKatexDir } from "./plugins/katexDir";
|
|
14
16
|
|
|
@@ -34,7 +36,8 @@ export var createPlugins = function createPlugins(props) {
|
|
|
34
36
|
isChatMode = props.isChatMode,
|
|
35
37
|
rehypePlugins = props.rehypePlugins,
|
|
36
38
|
remarkPlugins = props.remarkPlugins,
|
|
37
|
-
remarkPluginsAhead = props.remarkPluginsAhead
|
|
39
|
+
remarkPluginsAhead = props.remarkPluginsAhead,
|
|
40
|
+
animated = props.animated;
|
|
38
41
|
|
|
39
42
|
// 预处理插件数组
|
|
40
43
|
var normalizedRehypePlugins = Array.isArray(rehypePlugins) ? rehypePlugins : rehypePlugins ? [rehypePlugins] : [];
|
|
@@ -42,7 +45,7 @@ export var createPlugins = function createPlugins(props) {
|
|
|
42
45
|
var normalizedRemarkPluginsAhead = Array.isArray(remarkPluginsAhead) ? remarkPluginsAhead : remarkPluginsAhead ? [remarkPluginsAhead] : [];
|
|
43
46
|
|
|
44
47
|
// 创建 rehype 插件列表
|
|
45
|
-
var rehypePluginsList = [allowHtml && rehypeRaw, enableLatex && rehypeKatex, enableLatex && rehypeKatexDir, enableCustomFootnotes && rehypeFootnoteLinks].concat(_toConsumableArray(normalizedRehypePlugins)).filter(Boolean);
|
|
48
|
+
var rehypePluginsList = [allowHtml && rehypeRaw, enableLatex && rehypeKatex, enableLatex && rehypeKatexDir, enableCustomFootnotes && rehypeFootnoteLinks, animated && animatedPlugin].concat(_toConsumableArray(normalizedRehypePlugins)).filter(Boolean);
|
|
46
49
|
|
|
47
50
|
// 创建 remark 插件列表
|
|
48
51
|
var remarkPluginsList = [].concat(_toConsumableArray(normalizedRemarkPluginsAhead), [[remarkGfm, {
|
|
@@ -125,4 +128,39 @@ export var transformCitations = function transformCitations(rawContent) {
|
|
|
125
128
|
return rawContent.replaceAll(pattern, function (match, id) {
|
|
126
129
|
return "[#citation-".concat(id, "](citation-").concat(id, ")");
|
|
127
130
|
}).replaceAll('][', '] [');
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
// 新增: 检测LaTeX公式是否可渲染
|
|
134
|
+
var extractFormulas = function extractFormulas(text) {
|
|
135
|
+
// 计算$$的数量
|
|
136
|
+
var dollarsCount = (text.match(/\$\$/g) || []).length;
|
|
137
|
+
|
|
138
|
+
// 奇数个$$时,获取最后一个$$后的内容
|
|
139
|
+
if (dollarsCount % 2 === 1) {
|
|
140
|
+
var match = text.match(/\$\$([^]*)$/);
|
|
141
|
+
return match ? match[1] : '';
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// 偶数个$$时,返回空字符串
|
|
145
|
+
return '';
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
// 只检查最后一个公式
|
|
149
|
+
export var areFormulasRenderable = function areFormulasRenderable(text) {
|
|
150
|
+
var formulas = extractFormulas(text);
|
|
151
|
+
|
|
152
|
+
// 如果没有公式,返回true
|
|
153
|
+
if (!formulas) return true;
|
|
154
|
+
|
|
155
|
+
// 仅检查最后一个公式是否可渲染
|
|
156
|
+
try {
|
|
157
|
+
katex.renderToString(formulas, {
|
|
158
|
+
displayMode: true,
|
|
159
|
+
throwOnError: true
|
|
160
|
+
});
|
|
161
|
+
return true;
|
|
162
|
+
} catch (error) {
|
|
163
|
+
console.log("LaTeX\u516C\u5F0F\u6E32\u67D3\u9519\u8BEF: ".concat(error));
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
128
166
|
};
|
|
@@ -34,7 +34,8 @@ var SyntaxMermaid = /*#__PURE__*/memo(function (_ref) {
|
|
|
34
34
|
id: mermaidId,
|
|
35
35
|
theme: isDefaultTheme ? undefined : customTheme
|
|
36
36
|
}),
|
|
37
|
-
data = _useMermaid.data
|
|
37
|
+
data = _useMermaid.data,
|
|
38
|
+
isLoading = _useMermaid.isLoading;
|
|
38
39
|
var _useState = useState(),
|
|
39
40
|
_useState2 = _slicedToArray(_useState, 2),
|
|
40
41
|
blobUrl = _useState2[0],
|
|
@@ -47,18 +48,16 @@ var SyntaxMermaid = /*#__PURE__*/memo(function (_ref) {
|
|
|
47
48
|
};
|
|
48
49
|
}, [blobUrl]);
|
|
49
50
|
useEffect(function () {
|
|
50
|
-
if (!data) return;
|
|
51
|
+
if (isLoading || !data) return;
|
|
51
52
|
// 创建Blob对象
|
|
52
53
|
var svgBlob = new Blob([data], {
|
|
53
54
|
type: 'image/svg+xml'
|
|
54
55
|
});
|
|
55
|
-
// 如果已有旧的URL,先释放它
|
|
56
|
-
if (blobUrl) URL.revokeObjectURL(blobUrl);
|
|
57
56
|
// 创建并保存Blob URL
|
|
58
57
|
var url = URL.createObjectURL(svgBlob);
|
|
59
58
|
setBlobUrl(url);
|
|
60
|
-
}, [data]);
|
|
61
|
-
if (!
|
|
59
|
+
}, [isLoading, data]);
|
|
60
|
+
if (!blobUrl) return null;
|
|
62
61
|
return /*#__PURE__*/_jsx(Image, {
|
|
63
62
|
alt: 'mermaid',
|
|
64
63
|
maxHeight: 480,
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Components } from 'react-markdown/lib';
|
|
2
|
+
import type { Pluggable } from 'unified';
|
|
3
|
+
import type { MarkdownProps } from "../../Markdown/type";
|
|
4
|
+
/**
|
|
5
|
+
* Processes Markdown content and prepares rendering components and configurations
|
|
6
|
+
*/
|
|
7
|
+
export declare const useMarkdown: ({ children, fullFeaturedCodeBlock, animated, enableLatex, enableMermaid, enableImageGallery, enableCustomFootnotes, componentProps, allowHtml, showFootnotes, variant, rehypePlugins, remarkPlugins, remarkPluginsAhead, components, citations, }: Pick<MarkdownProps, 'children' | 'fullFeaturedCodeBlock' | 'animated' | 'enableLatex' | 'enableMermaid' | 'enableImageGallery' | 'enableCustomFootnotes' | 'componentProps' | 'allowHtml' | 'showFootnotes' | 'variant' | 'rehypePlugins' | 'remarkPlugins' | 'remarkPluginsAhead' | 'components' | 'citations'>) => {
|
|
8
|
+
escapedContent?: string | undefined;
|
|
9
|
+
memoComponents: Components;
|
|
10
|
+
rehypePluginsList: Pluggable[];
|
|
11
|
+
remarkPluginsList: Pluggable[];
|
|
12
|
+
};
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
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
|
+
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; }
|
|
5
|
+
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; }
|
|
6
|
+
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; }
|
|
7
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
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
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
10
|
+
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."); }
|
|
11
|
+
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); }
|
|
12
|
+
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; }
|
|
13
|
+
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; } }
|
|
14
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
15
|
+
import { useCallback, useMemo, useState } from 'react';
|
|
16
|
+
import { CodeFullFeatured, CodeLite } from "../../Markdown/components/CodeBlock";
|
|
17
|
+
import Image from "../../mdx/mdxComponents/Image";
|
|
18
|
+
import Link from "../../mdx/mdxComponents/Link";
|
|
19
|
+
import Section from "../../mdx/mdxComponents/Section";
|
|
20
|
+
import Video from "../../mdx/mdxComponents/Video";
|
|
21
|
+
import { addToCache, areFormulasRenderable, contentCache, createPlugins, escapeBrackets, escapeMhchem, fixMarkdownBold, transformCitations } from "./utils";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Processes Markdown content and prepares rendering components and configurations
|
|
25
|
+
*/
|
|
26
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
27
|
+
export var useMarkdown = function useMarkdown(_ref) {
|
|
28
|
+
var children = _ref.children,
|
|
29
|
+
fullFeaturedCodeBlock = _ref.fullFeaturedCodeBlock,
|
|
30
|
+
animated = _ref.animated,
|
|
31
|
+
_ref$enableLatex = _ref.enableLatex,
|
|
32
|
+
enableLatex = _ref$enableLatex === void 0 ? true : _ref$enableLatex,
|
|
33
|
+
_ref$enableMermaid = _ref.enableMermaid,
|
|
34
|
+
enableMermaid = _ref$enableMermaid === void 0 ? true : _ref$enableMermaid,
|
|
35
|
+
_ref$enableImageGalle = _ref.enableImageGallery,
|
|
36
|
+
enableImageGallery = _ref$enableImageGalle === void 0 ? true : _ref$enableImageGalle,
|
|
37
|
+
enableCustomFootnotes = _ref.enableCustomFootnotes,
|
|
38
|
+
componentProps = _ref.componentProps,
|
|
39
|
+
allowHtml = _ref.allowHtml,
|
|
40
|
+
showFootnotes = _ref.showFootnotes,
|
|
41
|
+
_ref$variant = _ref.variant,
|
|
42
|
+
variant = _ref$variant === void 0 ? 'default' : _ref$variant,
|
|
43
|
+
rehypePlugins = _ref.rehypePlugins,
|
|
44
|
+
remarkPlugins = _ref.remarkPlugins,
|
|
45
|
+
remarkPluginsAhead = _ref.remarkPluginsAhead,
|
|
46
|
+
_ref$components = _ref.components,
|
|
47
|
+
components = _ref$components === void 0 ? {} : _ref$components,
|
|
48
|
+
citations = _ref.citations;
|
|
49
|
+
var _useState = useState(''),
|
|
50
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
51
|
+
vaildContent = _useState2[0],
|
|
52
|
+
setVaildContent = _useState2[1];
|
|
53
|
+
var isChatMode = variant === 'chat';
|
|
54
|
+
|
|
55
|
+
// 计算缓存键
|
|
56
|
+
var cacheKey = useMemo(function () {
|
|
57
|
+
return "".concat(children, "-").concat(enableLatex, "-").concat(enableCustomFootnotes, "-").concat((citations === null || citations === void 0 ? void 0 : citations.length) || 0);
|
|
58
|
+
}, [children, enableLatex, enableCustomFootnotes, citations === null || citations === void 0 ? void 0 : citations.length]);
|
|
59
|
+
|
|
60
|
+
// 处理内容并利用缓存避免重复计算
|
|
61
|
+
var escapedContent = useMemo(function () {
|
|
62
|
+
// 尝试从缓存获取
|
|
63
|
+
if (contentCache.has(cacheKey)) {
|
|
64
|
+
return contentCache.get(cacheKey);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// 处理新内容
|
|
68
|
+
var processedContent;
|
|
69
|
+
if (enableLatex) {
|
|
70
|
+
var baseContent = fixMarkdownBold(escapeMhchem(escapeBrackets(children)));
|
|
71
|
+
var tempContent = enableCustomFootnotes ? transformCitations(baseContent, citations === null || citations === void 0 ? void 0 : citations.length) : baseContent;
|
|
72
|
+
processedContent = areFormulasRenderable(tempContent) ? tempContent : vaildContent;
|
|
73
|
+
} else {
|
|
74
|
+
processedContent = fixMarkdownBold(children);
|
|
75
|
+
}
|
|
76
|
+
setVaildContent(processedContent);
|
|
77
|
+
|
|
78
|
+
// 缓存处理结果
|
|
79
|
+
addToCache(cacheKey, processedContent);
|
|
80
|
+
return processedContent;
|
|
81
|
+
}, [vaildContent, cacheKey, children, enableLatex, enableCustomFootnotes, citations === null || citations === void 0 ? void 0 : citations.length]);
|
|
82
|
+
|
|
83
|
+
// 创建插件
|
|
84
|
+
var _useMemo = useMemo(function () {
|
|
85
|
+
return createPlugins({
|
|
86
|
+
allowHtml: allowHtml,
|
|
87
|
+
animated: animated,
|
|
88
|
+
enableCustomFootnotes: enableCustomFootnotes,
|
|
89
|
+
enableLatex: enableLatex,
|
|
90
|
+
isChatMode: isChatMode,
|
|
91
|
+
rehypePlugins: rehypePlugins,
|
|
92
|
+
remarkPlugins: remarkPlugins,
|
|
93
|
+
remarkPluginsAhead: remarkPluginsAhead
|
|
94
|
+
});
|
|
95
|
+
}, [allowHtml, enableLatex, enableCustomFootnotes, isChatMode, rehypePlugins, remarkPlugins, remarkPluginsAhead, animated]),
|
|
96
|
+
rehypePluginsList = _useMemo.rehypePluginsList,
|
|
97
|
+
remarkPluginsList = _useMemo.remarkPluginsList;
|
|
98
|
+
|
|
99
|
+
// 使用 useCallback 优化渲染子组件
|
|
100
|
+
var renderLink = useCallback(function (props) {
|
|
101
|
+
return /*#__PURE__*/_jsx(Link, _objectSpread(_objectSpread({
|
|
102
|
+
citations: citations
|
|
103
|
+
}, props), componentProps === null || componentProps === void 0 ? void 0 : componentProps.a));
|
|
104
|
+
}, [citations, componentProps === null || componentProps === void 0 ? void 0 : componentProps.a]);
|
|
105
|
+
var renderImage = useCallback(function (props) {
|
|
106
|
+
return /*#__PURE__*/_jsx(Image, _objectSpread(_objectSpread({}, props), componentProps === null || componentProps === void 0 ? void 0 : componentProps.img));
|
|
107
|
+
}, [componentProps === null || componentProps === void 0 ? void 0 : componentProps.img]);
|
|
108
|
+
var renderCodeBlock = useCallback(function (props) {
|
|
109
|
+
return fullFeaturedCodeBlock ? /*#__PURE__*/_jsx(CodeFullFeatured, _objectSpread(_objectSpread({
|
|
110
|
+
enableMermaid: enableMermaid,
|
|
111
|
+
highlight: componentProps === null || componentProps === void 0 ? void 0 : componentProps.highlight,
|
|
112
|
+
mermaid: componentProps === null || componentProps === void 0 ? void 0 : componentProps.mermaid
|
|
113
|
+
}, props), componentProps === null || componentProps === void 0 ? void 0 : componentProps.pre)) : /*#__PURE__*/_jsx(CodeLite, _objectSpread(_objectSpread({
|
|
114
|
+
enableMermaid: enableMermaid,
|
|
115
|
+
highlight: componentProps === null || componentProps === void 0 ? void 0 : componentProps.highlight,
|
|
116
|
+
mermaid: componentProps === null || componentProps === void 0 ? void 0 : componentProps.mermaid
|
|
117
|
+
}, props), componentProps === null || componentProps === void 0 ? void 0 : componentProps.pre));
|
|
118
|
+
}, [enableMermaid, fullFeaturedCodeBlock, componentProps === null || componentProps === void 0 ? void 0 : componentProps.highlight, componentProps === null || componentProps === void 0 ? void 0 : componentProps.mermaid, componentProps === null || componentProps === void 0 ? void 0 : componentProps.pre]);
|
|
119
|
+
var renderSection = useCallback(function (props) {
|
|
120
|
+
return /*#__PURE__*/_jsx(Section, _objectSpread({
|
|
121
|
+
showCitations: showFootnotes
|
|
122
|
+
}, props));
|
|
123
|
+
}, [showFootnotes]);
|
|
124
|
+
var renderVideo = useCallback(function (props) {
|
|
125
|
+
return /*#__PURE__*/_jsx(Video, _objectSpread(_objectSpread({}, props), componentProps === null || componentProps === void 0 ? void 0 : componentProps.video));
|
|
126
|
+
}, [componentProps === null || componentProps === void 0 ? void 0 : componentProps.video]);
|
|
127
|
+
|
|
128
|
+
// 创建组件映射
|
|
129
|
+
var memoComponents = useMemo(function () {
|
|
130
|
+
return _objectSpread({
|
|
131
|
+
a: renderLink,
|
|
132
|
+
img: enableImageGallery ? renderImage : undefined,
|
|
133
|
+
pre: renderCodeBlock,
|
|
134
|
+
section: renderSection,
|
|
135
|
+
video: renderVideo
|
|
136
|
+
}, components);
|
|
137
|
+
}, [renderLink, renderImage, renderCodeBlock, renderSection, renderVideo, enableImageGallery, components]);
|
|
138
|
+
return useMemo(function () {
|
|
139
|
+
return {
|
|
140
|
+
escapedContent: escapedContent,
|
|
141
|
+
memoComponents: memoComponents,
|
|
142
|
+
rehypePluginsList: rehypePluginsList,
|
|
143
|
+
remarkPluginsList: remarkPluginsList
|
|
144
|
+
};
|
|
145
|
+
}, [escapedContent, memoComponents, rehypePluginsList, remarkPluginsList]);
|
|
146
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Pluggable } from 'unified';
|
|
2
|
+
export declare const contentCache: Map<string, string>;
|
|
3
|
+
export declare const addToCache: (key: string, value: string) => void;
|
|
4
|
+
export declare const createPlugins: (props: {
|
|
5
|
+
allowHtml?: boolean;
|
|
6
|
+
animated?: boolean;
|
|
7
|
+
enableCustomFootnotes?: boolean;
|
|
8
|
+
enableLatex?: boolean;
|
|
9
|
+
isChatMode: boolean;
|
|
10
|
+
rehypePlugins?: Pluggable | Pluggable[];
|
|
11
|
+
remarkPlugins?: Pluggable | Pluggable[];
|
|
12
|
+
remarkPluginsAhead?: Pluggable | Pluggable[];
|
|
13
|
+
}) => {
|
|
14
|
+
rehypePluginsList: Pluggable[];
|
|
15
|
+
remarkPluginsList: Pluggable[];
|
|
16
|
+
};
|
|
17
|
+
export declare function escapeBrackets(text: string): string;
|
|
18
|
+
export declare function escapeMhchem(text: string): string;
|
|
19
|
+
export declare function fixMarkdownBold(text: string): string;
|
|
20
|
+
export declare const transformCitations: (rawContent: string, length?: number) => string;
|
|
21
|
+
export declare const areFormulasRenderable: (text: string) => boolean;
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
2
|
+
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."); }
|
|
3
|
+
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); }
|
|
4
|
+
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
5
|
+
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
6
|
+
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; }
|
|
7
|
+
import katex from 'katex';
|
|
8
|
+
import rehypeKatex from 'rehype-katex';
|
|
9
|
+
import rehypeRaw from 'rehype-raw';
|
|
10
|
+
import remarkBreaks from 'remark-breaks';
|
|
11
|
+
import remarkGfm from 'remark-gfm';
|
|
12
|
+
import remarkMath from 'remark-math';
|
|
13
|
+
import { animatedPlugin } from "../../Markdown/plugins/animated";
|
|
14
|
+
import { rehypeFootnoteLinks, remarkCustomFootnotes } from "../../Markdown/plugins/footnote";
|
|
15
|
+
import { rehypeKatexDir } from "../../Markdown/plugins/katexDir";
|
|
16
|
+
|
|
17
|
+
// 使用普通 Map 代替 WeakMap,并限制缓存大小
|
|
18
|
+
var CACHE_SIZE = 50;
|
|
19
|
+
export var contentCache = new Map();
|
|
20
|
+
|
|
21
|
+
// 添加内容到缓存时,保持缓存大小不超过限制
|
|
22
|
+
export var addToCache = function addToCache(key, value) {
|
|
23
|
+
if (contentCache.size >= CACHE_SIZE) {
|
|
24
|
+
// 移除最早加入的缓存项
|
|
25
|
+
var firstKey = contentCache.keys().next().value;
|
|
26
|
+
if (firstKey) contentCache.delete(firstKey);
|
|
27
|
+
}
|
|
28
|
+
contentCache.set(key, value);
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
// 使用工厂函数处理插件,减少组件中的逻辑负担
|
|
32
|
+
export var createPlugins = function createPlugins(props) {
|
|
33
|
+
var allowHtml = props.allowHtml,
|
|
34
|
+
enableLatex = props.enableLatex,
|
|
35
|
+
enableCustomFootnotes = props.enableCustomFootnotes,
|
|
36
|
+
isChatMode = props.isChatMode,
|
|
37
|
+
rehypePlugins = props.rehypePlugins,
|
|
38
|
+
remarkPlugins = props.remarkPlugins,
|
|
39
|
+
remarkPluginsAhead = props.remarkPluginsAhead,
|
|
40
|
+
animated = props.animated;
|
|
41
|
+
|
|
42
|
+
// 预处理插件数组
|
|
43
|
+
var normalizedRehypePlugins = Array.isArray(rehypePlugins) ? rehypePlugins : rehypePlugins ? [rehypePlugins] : [];
|
|
44
|
+
var normalizedRemarkPlugins = Array.isArray(remarkPlugins) ? remarkPlugins : remarkPlugins ? [remarkPlugins] : [];
|
|
45
|
+
var normalizedRemarkPluginsAhead = Array.isArray(remarkPluginsAhead) ? remarkPluginsAhead : remarkPluginsAhead ? [remarkPluginsAhead] : [];
|
|
46
|
+
|
|
47
|
+
// 创建 rehype 插件列表
|
|
48
|
+
var rehypePluginsList = [allowHtml && rehypeRaw, enableLatex && rehypeKatex, enableLatex && rehypeKatexDir, enableCustomFootnotes && rehypeFootnoteLinks, animated && animatedPlugin].concat(_toConsumableArray(normalizedRehypePlugins)).filter(Boolean);
|
|
49
|
+
|
|
50
|
+
// 创建 remark 插件列表
|
|
51
|
+
var remarkPluginsList = [].concat(_toConsumableArray(normalizedRemarkPluginsAhead), [[remarkGfm, {
|
|
52
|
+
singleTilde: false
|
|
53
|
+
}], enableCustomFootnotes && remarkCustomFootnotes, enableLatex && remarkMath, isChatMode && remarkBreaks], _toConsumableArray(normalizedRemarkPlugins)).filter(Boolean);
|
|
54
|
+
return {
|
|
55
|
+
rehypePluginsList: rehypePluginsList,
|
|
56
|
+
remarkPluginsList: remarkPluginsList
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
export function escapeBrackets(text) {
|
|
60
|
+
var pattern = /(```[\S\s]*?```|`.*?`)|\\\[([\S\s]*?[^\\])\\]|\\\((.*?)\\\)/g;
|
|
61
|
+
return text.replaceAll(pattern, function (match, codeBlock, squareBracket, roundBracket) {
|
|
62
|
+
if (codeBlock) {
|
|
63
|
+
return codeBlock;
|
|
64
|
+
} else if (squareBracket) {
|
|
65
|
+
return "$$".concat(squareBracket, "$$");
|
|
66
|
+
} else if (roundBracket) {
|
|
67
|
+
return "$".concat(roundBracket, "$");
|
|
68
|
+
}
|
|
69
|
+
return match;
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
export function escapeMhchem(text) {
|
|
73
|
+
return text.replaceAll('$\\ce{', '$\\\\ce{').replaceAll('$\\pu{', '$\\\\pu{');
|
|
74
|
+
}
|
|
75
|
+
export function fixMarkdownBold(text) {
|
|
76
|
+
var count = 0;
|
|
77
|
+
var count2 = 0;
|
|
78
|
+
var result = '';
|
|
79
|
+
var inCodeBlock = false;
|
|
80
|
+
var inInlineCode = false;
|
|
81
|
+
for (var i = 0; i < text.length; i++) {
|
|
82
|
+
var char = text[i];
|
|
83
|
+
if (text.slice(i, i + 3) === '```') {
|
|
84
|
+
inCodeBlock = !inCodeBlock;
|
|
85
|
+
result += '```';
|
|
86
|
+
i += 2;
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (char === '`') {
|
|
90
|
+
inInlineCode = !inInlineCode;
|
|
91
|
+
result += '`';
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
if (char === '*' && !inInlineCode && !inCodeBlock) {
|
|
95
|
+
count++;
|
|
96
|
+
if (count === 2) {
|
|
97
|
+
count2++;
|
|
98
|
+
}
|
|
99
|
+
if (count > 2) {
|
|
100
|
+
result += char;
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if (count === 2 && count2 % 2 === 0) {
|
|
104
|
+
var prevChar = i > 0 ? text[i - 2] : '';
|
|
105
|
+
var isPrevCharSymbol = /(?:[!-\/:-@\[-`\{-~\xA1-\xA9\xAB\xAC\xAE-\xB1\xB4\xB6-\xB8\xBB\xBF\xD7\xF7\u02C2-\u02C5\u02D2-\u02DF\u02E5-\u02EB\u02ED\u02EF-\u02FF\u0375\u037E\u0384\u0385\u0387\u03F6\u0482\u055A-\u055F\u0589\u058A\u058D-\u058F\u05BE\u05C0\u05C3\u05C6\u05F3\u05F4\u0606-\u060F\u061B\u061D-\u061F\u066A-\u066D\u06D4\u06DE\u06E9\u06FD\u06FE\u0700-\u070D\u07F6-\u07F9\u07FE\u07FF\u0830-\u083E\u085E\u0888\u0964\u0965\u0970\u09F2\u09F3\u09FA\u09FB\u09FD\u0A76\u0AF0\u0AF1\u0B70\u0BF3-\u0BFA\u0C77\u0C7F\u0C84\u0D4F\u0D79\u0DF4\u0E3F\u0E4F\u0E5A\u0E5B\u0F01-\u0F17\u0F1A-\u0F1F\u0F34\u0F36\u0F38\u0F3A-\u0F3D\u0F85\u0FBE-\u0FC5\u0FC7-\u0FCC\u0FCE-\u0FDA\u104A-\u104F\u109E\u109F\u10FB\u1360-\u1368\u1390-\u1399\u1400\u166D\u166E\u169B\u169C\u16EB-\u16ED\u1735\u1736\u17D4-\u17D6\u17D8-\u17DB\u1800-\u180A\u1940\u1944\u1945\u19DE-\u19FF\u1A1E\u1A1F\u1AA0-\u1AA6\u1AA8-\u1AAD\u1B5A-\u1B6A\u1B74-\u1B7E\u1BFC-\u1BFF\u1C3B-\u1C3F\u1C7E\u1C7F\u1CC0-\u1CC7\u1CD3\u1FBD\u1FBF-\u1FC1\u1FCD-\u1FCF\u1FDD-\u1FDF\u1FED-\u1FEF\u1FFD\u1FFE\u2010-\u2027\u2030-\u205E\u207A-\u207E\u208A-\u208E\u20A0-\u20C0\u2100\u2101\u2103-\u2106\u2108\u2109\u2114\u2116-\u2118\u211E-\u2123\u2125\u2127\u2129\u212E\u213A\u213B\u2140-\u2144\u214A-\u214D\u214F\u218A\u218B\u2190-\u2426\u2440-\u244A\u249C-\u24E9\u2500-\u2775\u2794-\u2B73\u2B76-\u2B95\u2B97-\u2BFF\u2CE5-\u2CEA\u2CF9-\u2CFC\u2CFE\u2CFF\u2D70\u2E00-\u2E2E\u2E30-\u2E5D\u2E80-\u2E99\u2E9B-\u2EF3\u2F00-\u2FD5\u2FF0-\u2FFF\u3001-\u3004\u3008-\u3020\u3030\u3036\u3037\u303D-\u303F\u309B\u309C\u30A0\u30FB\u3190\u3191\u3196-\u319F\u31C0-\u31E3\u31EF\u3200-\u321E\u322A-\u3247\u3250\u3260-\u327F\u328A-\u32B0\u32C0-\u33FF\u4DC0-\u4DFF\uA490-\uA4C6\uA4FE\uA4FF\uA60D-\uA60F\uA673\uA67E\uA6F2-\uA6F7\uA700-\uA716\uA720\uA721\uA789\uA78A\uA828-\uA82B\uA836-\uA839\uA874-\uA877\uA8CE\uA8CF\uA8F8-\uA8FA\uA8FC\uA92E\uA92F\uA95F\uA9C1-\uA9CD\uA9DE\uA9DF\uAA5C-\uAA5F\uAA77-\uAA79\uAADE\uAADF\uAAF0\uAAF1\uAB5B\uAB6A\uAB6B\uABEB\uFB29\uFBB2-\uFBC2\uFD3E-\uFD4F\uFDCF\uFDFC-\uFDFF\uFE10-\uFE19\uFE30-\uFE52\uFE54-\uFE66\uFE68-\uFE6B\uFF01-\uFF0F\uFF1A-\uFF20\uFF3B-\uFF40\uFF5B-\uFF65\uFFE0-\uFFE6\uFFE8-\uFFEE\uFFFC\uFFFD]|\uD800[\uDD00-\uDD02\uDD37-\uDD3F\uDD79-\uDD89\uDD8C-\uDD8E\uDD90-\uDD9C\uDDA0\uDDD0-\uDDFC\uDF9F\uDFD0]|\uD801\uDD6F|\uD802[\uDC57\uDC77\uDC78\uDD1F\uDD3F\uDE50-\uDE58\uDE7F\uDEC8\uDEF0-\uDEF6\uDF39-\uDF3F\uDF99-\uDF9C]|\uD803[\uDEAD\uDF55-\uDF59\uDF86-\uDF89]|\uD804[\uDC47-\uDC4D\uDCBB\uDCBC\uDCBE-\uDCC1\uDD40-\uDD43\uDD74\uDD75\uDDC5-\uDDC8\uDDCD\uDDDB\uDDDD-\uDDDF\uDE38-\uDE3D\uDEA9]|\uD805[\uDC4B-\uDC4F\uDC5A\uDC5B\uDC5D\uDCC6\uDDC1-\uDDD7\uDE41-\uDE43\uDE60-\uDE6C\uDEB9\uDF3C-\uDF3F]|\uD806[\uDC3B\uDD44-\uDD46\uDDE2\uDE3F-\uDE46\uDE9A-\uDE9C\uDE9E-\uDEA2\uDF00-\uDF09]|\uD807[\uDC41-\uDC45\uDC70\uDC71\uDEF7\uDEF8\uDF43-\uDF4F\uDFD5-\uDFF1\uDFFF]|\uD809[\uDC70-\uDC74]|\uD80B[\uDFF1\uDFF2]|\uD81A[\uDE6E\uDE6F\uDEF5\uDF37-\uDF3F\uDF44\uDF45]|\uD81B[\uDE97-\uDE9A\uDFE2]|\uD82F[\uDC9C\uDC9F]|\uD833[\uDF50-\uDFC3]|\uD834[\uDC00-\uDCF5\uDD00-\uDD26\uDD29-\uDD64\uDD6A-\uDD6C\uDD83\uDD84\uDD8C-\uDDA9\uDDAE-\uDDEA\uDE00-\uDE41\uDE45\uDF00-\uDF56]|\uD835[\uDEC1\uDEDB\uDEFB\uDF15\uDF35\uDF4F\uDF6F\uDF89\uDFA9\uDFC3]|\uD836[\uDC00-\uDDFF\uDE37-\uDE3A\uDE6D-\uDE74\uDE76-\uDE83\uDE85-\uDE8B]|\uD838[\uDD4F\uDEFF]|\uD83A[\uDD5E\uDD5F]|\uD83B[\uDCAC\uDCB0\uDD2E\uDEF0\uDEF1]|\uD83C[\uDC00-\uDC2B\uDC30-\uDC93\uDCA0-\uDCAE\uDCB1-\uDCBF\uDCC1-\uDCCF\uDCD1-\uDCF5\uDD0D-\uDDAD\uDDE6-\uDE02\uDE10-\uDE3B\uDE40-\uDE48\uDE50\uDE51\uDE60-\uDE65\uDF00-\uDFFF]|\uD83D[\uDC00-\uDED7\uDEDC-\uDEEC\uDEF0-\uDEFC\uDF00-\uDF76\uDF7B-\uDFD9\uDFE0-\uDFEB\uDFF0]|\uD83E[\uDC00-\uDC0B\uDC10-\uDC47\uDC50-\uDC59\uDC60-\uDC87\uDC90-\uDCAD\uDCB0\uDCB1\uDD00-\uDE53\uDE60-\uDE6D\uDE70-\uDE7C\uDE80-\uDE88\uDE90-\uDEBD\uDEBF-\uDEC5\uDECE-\uDEDB\uDEE0-\uDEE8\uDEF0-\uDEF8\uDF00-\uDF92\uDF94-\uDFCA])/.test(prevChar);
|
|
106
|
+
result += i + 1 < text.length && text[i + 1] !== ' ' && isPrevCharSymbol ? '* ' : '*';
|
|
107
|
+
} else {
|
|
108
|
+
result += '*';
|
|
109
|
+
}
|
|
110
|
+
} else {
|
|
111
|
+
result += char;
|
|
112
|
+
count = 0;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return result;
|
|
116
|
+
}
|
|
117
|
+
export var transformCitations = function transformCitations(rawContent) {
|
|
118
|
+
var length = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
|
119
|
+
if (length === 0) return rawContent;
|
|
120
|
+
|
|
121
|
+
// 生成动态正则表达式模式
|
|
122
|
+
var idx = Array.from({
|
|
123
|
+
length: length
|
|
124
|
+
}).fill('').map(function (_, index) {
|
|
125
|
+
return index + 1;
|
|
126
|
+
});
|
|
127
|
+
var pattern = new RegExp("\\[(".concat(idx.join('|'), ")\\]"), 'g');
|
|
128
|
+
return rawContent.replaceAll(pattern, function (match, id) {
|
|
129
|
+
return "[#citation-".concat(id, "](citation-").concat(id, ")");
|
|
130
|
+
}).replaceAll('][', '] [');
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
// 新增: 检测LaTeX公式是否可渲染
|
|
134
|
+
var extractFormulas = function extractFormulas(text) {
|
|
135
|
+
// 计算$$的数量
|
|
136
|
+
var dollarsCount = (text.match(/\$\$/g) || []).length;
|
|
137
|
+
|
|
138
|
+
// 奇数个$$时,获取最后一个$$后的内容
|
|
139
|
+
if (dollarsCount % 2 === 1) {
|
|
140
|
+
var match = text.match(/\$\$([^]*)$/);
|
|
141
|
+
return match ? match[1] : '';
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// 偶数个$$时,返回空字符串
|
|
145
|
+
return '';
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
// 只检查最后一个公式
|
|
149
|
+
export var areFormulasRenderable = function areFormulasRenderable(text) {
|
|
150
|
+
var formulas = extractFormulas(text);
|
|
151
|
+
|
|
152
|
+
// 如果没有公式,返回true
|
|
153
|
+
if (!formulas) return true;
|
|
154
|
+
|
|
155
|
+
// 仅检查最后一个公式是否可渲染
|
|
156
|
+
try {
|
|
157
|
+
katex.renderToString(formulas, {
|
|
158
|
+
displayMode: true,
|
|
159
|
+
throwOnError: true
|
|
160
|
+
});
|
|
161
|
+
return true;
|
|
162
|
+
} catch (error) {
|
|
163
|
+
console.log("LaTeX\u516C\u5F0F\u6E32\u67D3\u9519\u8BEF: ".concat(error));
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
};
|
package/es/hooks/useMermaid.js
CHANGED
|
@@ -123,7 +123,7 @@ export var useMermaid = function useMermaid(content, _ref) {
|
|
|
123
123
|
case 22:
|
|
124
124
|
_context.prev = 22;
|
|
125
125
|
_context.t0 = _context["catch"](0);
|
|
126
|
-
console.error('Mermaid 解析错误:', _context.t0);
|
|
126
|
+
if (!validContent) console.error('Mermaid 解析错误:', _context.t0);
|
|
127
127
|
// 返回最近一次有效的内容,或空字符串
|
|
128
128
|
return _context.abrupt("return", validContent || '');
|
|
129
129
|
case 26:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lobehub/ui",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.13",
|
|
4
4
|
"description": "Lobe UI is an open-source UI component library for building AIGC web apps",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"lobehub",
|
|
@@ -69,7 +69,9 @@
|
|
|
69
69
|
"emoji-mart": "^5.6.0",
|
|
70
70
|
"fast-deep-equal": "^3.1.3",
|
|
71
71
|
"framer-motion": "^12.6.3",
|
|
72
|
+
"hast": "^1.0.0",
|
|
72
73
|
"immer": "^10.1.1",
|
|
74
|
+
"katex": "^0.16.9",
|
|
73
75
|
"leva": "^0.10.0",
|
|
74
76
|
"lodash-es": "^4.17.21",
|
|
75
77
|
"lucide-react": "^0.484.0",
|