@lobehub/ui 2.10.0 → 2.10.1

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.
@@ -14,7 +14,6 @@ import { marked } from 'marked';
14
14
  import { memo, useId, useMemo } from 'react';
15
15
  import { MarkdownHooks } from 'react-markdown';
16
16
  import { useMarkdownComponents, useMarkdownContent, useMarkdownRehypePlugins, useMarkdownRemarkPlugins } from "../../hooks/useMarkdown";
17
- import { parseIncompleteMarkdown } from "./parseIncompleteMarkdown";
18
17
  import { jsx as _jsx } from "react/jsx-runtime";
19
18
  var parseMarkdownIntoBlocks = function parseMarkdownIntoBlocks(markdown) {
20
19
  var tokens = marked.lexer(markdown);
@@ -25,11 +24,8 @@ var parseMarkdownIntoBlocks = function parseMarkdownIntoBlocks(markdown) {
25
24
  var StreamdownBlock = /*#__PURE__*/memo(function (_ref) {
26
25
  var children = _ref.children,
27
26
  rest = _objectWithoutProperties(_ref, _excluded);
28
- var parsedContent = useMemo(function () {
29
- return typeof children === 'string' ? parseIncompleteMarkdown(children.trim()) : children;
30
- }, [children]);
31
27
  return /*#__PURE__*/_jsx(MarkdownHooks, _objectSpread(_objectSpread({}, rest), {}, {
32
- children: parsedContent
28
+ children: children
33
29
  }));
34
30
  }, function (prevProps, nextProps) {
35
31
  return prevProps.children === nextProps.children;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lobehub/ui",
3
- "version": "2.10.0",
3
+ "version": "2.10.1",
4
4
  "description": "Lobe UI is an open-source UI component library for building AIGC web apps",
5
5
  "keywords": [
6
6
  "lobehub",
@@ -1 +0,0 @@
1
- export declare const parseIncompleteMarkdown: (text: string) => string;
@@ -1,293 +0,0 @@
1
- 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; } } }; }
2
- 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); }
3
- 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; }
4
- var linkImagePattern = /(!?\[)([^\]]*?)$/;
5
- var boldPattern = /(\*\*)([^*]*?)$/;
6
- var italicPattern = /(__)([^_]*?)$/;
7
- var boldItalicPattern = /(\*{3})([^*]*?)$/;
8
- var singleAsteriskPattern = /(\*)([^*]*?)$/;
9
- var singleUnderscorePattern = /(_)([^_]*?)$/;
10
- var inlineCodePattern = /(`)([^`]*?)$/;
11
- var strikethroughPattern = /(~~)([^~]*?)$/;
12
- var inlineKatexPattern = /(\$)([^$]*?)$/;
13
- var blockKatexPattern = /(\$\$)([^$]*?)$/;
14
-
15
- // Handles incomplete links and images by removing them if not closed
16
- var handleIncompleteLinksAndImages = function handleIncompleteLinksAndImages(text) {
17
- var linkMatch = text.match(linkImagePattern);
18
- if (linkMatch) {
19
- var startIndex = text.lastIndexOf(linkMatch[1]);
20
- return text.slice(0, Math.max(0, startIndex));
21
- }
22
- return text;
23
- };
24
-
25
- // Completes incomplete bold formatting (**)
26
- var handleIncompleteBold = function handleIncompleteBold(text) {
27
- var boldMatch = text.match(boldPattern);
28
- if (boldMatch) {
29
- var asteriskPairs = (text.match(/\*\*/g) || []).length;
30
- if (asteriskPairs % 2 === 1) {
31
- return "".concat(text, "**");
32
- }
33
- }
34
- return text;
35
- };
36
-
37
- // Completes incomplete italic formatting with double underscores (__)
38
- var handleIncompleteDoubleUnderscoreItalic = function handleIncompleteDoubleUnderscoreItalic(text) {
39
- var italicMatch = text.match(italicPattern);
40
- if (italicMatch) {
41
- var underscorePairs = (text.match(/__/g) || []).length;
42
- if (underscorePairs % 2 === 1) {
43
- return "".concat(text, "__");
44
- }
45
- }
46
- return text;
47
- };
48
-
49
- // Counts single asterisks that are not part of double asterisks and not escaped
50
- var countSingleAsterisks = function countSingleAsterisks(text) {
51
- return text.split('').reduce(function (acc, char, index) {
52
- if (char === '*') {
53
- var prevChar = text[index - 1];
54
- var nextChar = text[index + 1];
55
- // Skip if escaped with backslash
56
- if (prevChar === '\\') {
57
- return acc;
58
- }
59
- if (prevChar !== '*' && nextChar !== '*') {
60
- return acc + 1;
61
- }
62
- }
63
- return acc;
64
- }, 0);
65
- };
66
-
67
- // Completes incomplete italic formatting with single asterisks (*)
68
- var handleIncompleteSingleAsteriskItalic = function handleIncompleteSingleAsteriskItalic(text) {
69
- var singleAsteriskMatch = text.match(singleAsteriskPattern);
70
- if (singleAsteriskMatch) {
71
- var singleAsterisks = countSingleAsterisks(text);
72
- if (singleAsterisks % 2 === 1) {
73
- return "".concat(text, "*");
74
- }
75
- }
76
- return text;
77
- };
78
-
79
- // Counts single underscores that are not part of double underscores and not escaped
80
- var countSingleUnderscores = function countSingleUnderscores(text) {
81
- return text.split('').reduce(function (acc, char, index) {
82
- if (char === '_') {
83
- var prevChar = text[index - 1];
84
- var nextChar = text[index + 1];
85
- // Skip if escaped with backslash
86
- if (prevChar === '\\') {
87
- return acc;
88
- }
89
- if (prevChar !== '_' && nextChar !== '_') {
90
- return acc + 1;
91
- }
92
- }
93
- return acc;
94
- }, 0);
95
- };
96
-
97
- // Completes incomplete italic formatting with single underscores (_)
98
- var handleIncompleteSingleUnderscoreItalic = function handleIncompleteSingleUnderscoreItalic(text) {
99
- var singleUnderscoreMatch = text.match(singleUnderscorePattern);
100
- if (singleUnderscoreMatch) {
101
- var singleUnderscores = countSingleUnderscores(text);
102
- if (singleUnderscores % 2 === 1) {
103
- return "".concat(text, "_");
104
- }
105
- }
106
- return text;
107
- };
108
-
109
- // Checks if a backtick at position i is part of a triple backtick sequence
110
- var isPartOfTripleBacktick = function isPartOfTripleBacktick(text, i) {
111
- // eslint-disable-next-line unicorn/prefer-string-slice
112
- var isTripleStart = text.substring(i, i + 3) === '```';
113
- // eslint-disable-next-line unicorn/prefer-string-slice
114
- var isTripleMiddle = i > 0 && text.substring(i - 1, i + 2) === '```';
115
- // eslint-disable-next-line unicorn/prefer-string-slice
116
- var isTripleEnd = i > 1 && text.substring(i - 2, i + 1) === '```';
117
- return isTripleStart || isTripleMiddle || isTripleEnd;
118
- };
119
-
120
- // Counts single backticks that are not part of triple backticks
121
- var countSingleBackticks = function countSingleBackticks(text) {
122
- var count = 0;
123
- for (var i = 0; i < text.length; i++) {
124
- if (text[i] === '`' && !isPartOfTripleBacktick(text, i)) {
125
- count++;
126
- }
127
- }
128
- return count;
129
- };
130
-
131
- // Completes incomplete inline code formatting (`)
132
- // Avoids completing if inside an incomplete code block
133
- var handleIncompleteInlineCode = function handleIncompleteInlineCode(text) {
134
- // Check if we have inline triple backticks (starts with ``` and should end with ```)
135
- // This pattern should ONLY match truly inline code (no newlines)
136
- // Examples: ```code``` or ```python code```
137
- var inlineTripleBacktickMatch = text.match(/^```[^\n`]*```?$/);
138
- if (inlineTripleBacktickMatch && !text.includes('\n')) {
139
- // Check if it ends with exactly 2 backticks (incomplete)
140
- if (text.endsWith('``') && !text.endsWith('```')) {
141
- return "".concat(text) + '`';
142
- }
143
- // Already complete inline triple backticks
144
- return text;
145
- }
146
-
147
- // Check if we're inside a multi-line code block (complete or incomplete)
148
- var allTripleBackticks = (text.match(/```/g) || []).length;
149
- var insideIncompleteCodeBlock = allTripleBackticks % 2 === 1;
150
-
151
- // Special case: if text ends with ```\n (triple backticks followed by newline)
152
- // This is actually a complete code block, not incomplete
153
- if ((text.endsWith('```\n') || text.endsWith('```')) &&
154
- // Count all triple backticks - if even, it's complete
155
- allTripleBackticks % 2 === 0) {
156
- return text;
157
- }
158
-
159
- // Don't modify text if we have complete multi-line code blocks (even pairs of ```)
160
- if (allTripleBackticks > 0 && allTripleBackticks % 2 === 0 && text.includes('\n')) {
161
- // We have complete multi-line code blocks, don't add any backticks
162
- return text;
163
- }
164
- var inlineCodeMatch = text.match(inlineCodePattern);
165
- if (inlineCodeMatch && !insideIncompleteCodeBlock) {
166
- var singleBacktickCount = countSingleBackticks(text);
167
- if (singleBacktickCount % 2 === 1) {
168
- return "".concat(text) + '`';
169
- }
170
- }
171
- return text;
172
- };
173
-
174
- // Completes incomplete strikethrough formatting (~~)
175
- var handleIncompleteStrikethrough = function handleIncompleteStrikethrough(text) {
176
- var strikethroughMatch = text.match(strikethroughPattern);
177
- if (strikethroughMatch) {
178
- var tildePairs = (text.match(/~~/g) || []).length;
179
- if (tildePairs % 2 === 1) {
180
- return "".concat(text, "~~");
181
- }
182
- }
183
- return text;
184
- };
185
-
186
- // Counts single dollar signs that are not part of double dollar signs and not escaped
187
- var countSingleDollarSigns = function countSingleDollarSigns(text) {
188
- return text.split('').reduce(function (acc, char, index) {
189
- if (char === '$') {
190
- var prevChar = text[index - 1];
191
- var nextChar = text[index + 1];
192
- // Skip if escaped with backslash
193
- if (prevChar === '\\') {
194
- return acc;
195
- }
196
- if (prevChar !== '$' && nextChar !== '$') {
197
- return acc + 1;
198
- }
199
- }
200
- return acc;
201
- }, 0);
202
- };
203
-
204
- // Completes incomplete block KaTeX formatting ($$)
205
- var handleIncompleteBlockKatex = function handleIncompleteBlockKatex(text) {
206
- var blockKatexMatch = text.match(blockKatexPattern);
207
- if (blockKatexMatch) {
208
- var dollarPairs = (text.match(/\$\$/g) || []).length;
209
- if (dollarPairs % 2 === 1) {
210
- return "".concat(text, "$$");
211
- }
212
- }
213
- return text;
214
- };
215
-
216
- // Completes incomplete inline KaTeX formatting ($)
217
- var handleIncompleteInlineKatex = function handleIncompleteInlineKatex(text) {
218
- var inlineKatexMatch = text.match(inlineKatexPattern);
219
- if (inlineKatexMatch) {
220
- var singleDollars = countSingleDollarSigns(text);
221
- if (singleDollars % 2 === 1) {
222
- return "".concat(text, "$");
223
- }
224
- }
225
- return text;
226
- };
227
-
228
- // Counts triple asterisks that are not part of quadruple or more asterisks
229
- var countTripleAsterisks = function countTripleAsterisks(text) {
230
- var count = 0;
231
- var matches = text.match(/\*+/g) || [];
232
- var _iterator = _createForOfIteratorHelper(matches),
233
- _step;
234
- try {
235
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
236
- var match = _step.value;
237
- // Count how many complete triple asterisks are in this sequence
238
- var asteriskCount = match.length;
239
- if (asteriskCount >= 3) {
240
- // Each group of exactly 3 asterisks counts as one triple asterisk marker
241
- count += Math.floor(asteriskCount / 3);
242
- }
243
- }
244
- } catch (err) {
245
- _iterator.e(err);
246
- } finally {
247
- _iterator.f();
248
- }
249
- return count;
250
- };
251
-
252
- // Completes incomplete bold-italic formatting (***)
253
- var handleIncompleteBoldItalic = function handleIncompleteBoldItalic(text) {
254
- // Don't process if text is only asterisks and has 4 or more consecutive asterisks
255
- // This prevents cases like **** from being treated as incomplete ***
256
- if (/^\*{4,}$/.test(text)) {
257
- return text;
258
- }
259
- var boldItalicMatch = text.match(boldItalicPattern);
260
- if (boldItalicMatch) {
261
- var tripleAsteriskCount = countTripleAsterisks(text);
262
- if (tripleAsteriskCount % 2 === 1) {
263
- return "".concat(text, "***");
264
- }
265
- }
266
- return text;
267
- };
268
-
269
- // Parses markdown text and removes incomplete tokens to prevent partial rendering
270
- export var parseIncompleteMarkdown = function parseIncompleteMarkdown(text) {
271
- if (!text || typeof text !== 'string') {
272
- return text;
273
- }
274
- var result = text;
275
-
276
- // Handle incomplete links and images first (removes content)
277
- result = handleIncompleteLinksAndImages(result);
278
-
279
- // Handle various formatting completions
280
- // Handle triple asterisks first (most specific)
281
- result = handleIncompleteBoldItalic(result);
282
- result = handleIncompleteBold(result);
283
- result = handleIncompleteDoubleUnderscoreItalic(result);
284
- result = handleIncompleteSingleAsteriskItalic(result);
285
- result = handleIncompleteSingleUnderscoreItalic(result);
286
- result = handleIncompleteInlineCode(result);
287
- result = handleIncompleteStrikethrough(result);
288
-
289
- // Handle KaTeX formatting (block first, then inline)
290
- result = handleIncompleteBlockKatex(result);
291
- result = handleIncompleteInlineKatex(result);
292
- return result;
293
- };