@ant-design/agentic-ui 2.20.2 → 2.22.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.
Files changed (52) hide show
  1. package/dist/MarkdownEditor/BaseMarkdownEditor.js +1 -1
  2. package/dist/MarkdownEditor/editor/elements/Code.js +1 -1
  3. package/dist/MarkdownEditor/editor/elements/Image/index.js +37 -9
  4. package/dist/MarkdownEditor/editor/elements/LinkCard/index.js +87 -2
  5. package/dist/MarkdownEditor/editor/elements/List/List.js +10 -2
  6. package/dist/MarkdownEditor/editor/elements/Media.js +75 -23
  7. package/dist/MarkdownEditor/editor/elements/Table/ReadonlyTableComponent.js +2 -13
  8. package/dist/MarkdownEditor/editor/elements/Table/SimpleTable.js +89 -15
  9. package/dist/MarkdownEditor/editor/elements/Table/Table.js +3 -75
  10. package/dist/MarkdownEditor/editor/elements/TagPopup/index.js +4 -4
  11. package/dist/MarkdownEditor/editor/elements/TagPopup/style.js +14 -12
  12. package/dist/MarkdownEditor/editor/elements/index.js +1 -32
  13. package/dist/MarkdownEditor/editor/parser/parse/applyContextPropsAndConfig.d.ts +8 -0
  14. package/dist/MarkdownEditor/editor/parser/parse/applyContextPropsAndConfig.js +58 -0
  15. package/dist/MarkdownEditor/editor/parser/parse/parseBlockElements.d.ts +67 -0
  16. package/dist/MarkdownEditor/editor/parser/parse/parseBlockElements.js +289 -0
  17. package/dist/MarkdownEditor/editor/parser/parse/parseCode.d.ts +32 -0
  18. package/dist/MarkdownEditor/editor/parser/parse/parseCode.js +186 -0
  19. package/dist/MarkdownEditor/editor/parser/parse/parseElements.d.ts +27 -0
  20. package/dist/MarkdownEditor/editor/parser/parse/parseElements.js +83 -0
  21. package/dist/MarkdownEditor/editor/parser/parse/parseEmptyLines.d.ts +9 -0
  22. package/dist/MarkdownEditor/editor/parser/parse/parseEmptyLines.js +60 -0
  23. package/dist/MarkdownEditor/editor/parser/parse/parseFootnote.d.ts +10 -0
  24. package/dist/MarkdownEditor/editor/parser/parse/parseFootnote.js +12 -0
  25. package/dist/MarkdownEditor/editor/parser/parse/parseHtml.d.ts +63 -0
  26. package/dist/MarkdownEditor/editor/parser/parse/parseHtml.js +759 -0
  27. package/dist/MarkdownEditor/editor/parser/parse/parseMath.d.ts +24 -0
  28. package/dist/MarkdownEditor/editor/parser/parse/parseMath.js +58 -0
  29. package/dist/MarkdownEditor/editor/parser/parse/parseMedia.d.ts +27 -0
  30. package/dist/MarkdownEditor/editor/parser/parse/parseMedia.js +127 -0
  31. package/dist/MarkdownEditor/editor/parser/parse/parseTable.d.ts +39 -0
  32. package/dist/MarkdownEditor/editor/parser/parse/parseTable.js +348 -0
  33. package/dist/MarkdownEditor/editor/parser/parse/parseText.d.ts +26 -0
  34. package/dist/MarkdownEditor/editor/parser/parse/parseText.js +304 -0
  35. package/dist/MarkdownEditor/editor/parser/parserMarkdownToSlateNode.d.ts +3 -44
  36. package/dist/MarkdownEditor/editor/parser/parserMarkdownToSlateNode.js +141 -2282
  37. package/dist/MarkdownEditor/editor/parser/parserSlateNodeToMarkdown.js +137 -12
  38. package/dist/MarkdownEditor/editor/parser/remarkParse.d.ts +11 -1
  39. package/dist/MarkdownEditor/editor/parser/remarkParse.js +225 -39
  40. package/dist/MarkdownEditor/editor/plugins/elements.js +1 -1
  41. package/dist/MarkdownEditor/editor/types/Table.d.ts +2 -1
  42. package/dist/MarkdownEditor/editor/utils/markdownToHtml.js +2 -1
  43. package/dist/MarkdownEditor/el.d.ts +3 -0
  44. package/dist/MarkdownEditor/style.js +2 -0
  45. package/dist/Plugins/chart/index.js +7 -7
  46. package/dist/Plugins/code/components/CodeRenderer.js +27 -10
  47. package/dist/Plugins/code/components/CodeToolbar.js +3 -16
  48. package/dist/Workspace/Task/index.d.ts +13 -8
  49. package/dist/Workspace/Task/index.js +19 -2
  50. package/dist/Workspace/index.js +3 -2
  51. package/dist/Workspace/types.d.ts +3 -1
  52. package/package.json +1 -1
@@ -1,14 +1,8 @@
1
- /**
2
- * 此文件包含大量相互依赖的函数,为了保持代码的可读性和逻辑分组,
3
- * 我们允许函数在定义前使用(函数提升)
4
- */ /* eslint-disable @typescript-eslint/no-use-before-define */ /* eslint-disable @typescript-eslint/no-unused-expressions */ function _array_like_to_array(arr, len) {
1
+ function _array_like_to_array(arr, len) {
5
2
  if (len == null || len > arr.length) len = arr.length;
6
3
  for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
7
4
  return arr2;
8
5
  }
9
- function _array_with_holes(arr) {
10
- if (Array.isArray(arr)) return arr;
11
- }
12
6
  function _array_without_holes(arr) {
13
7
  if (Array.isArray(arr)) return _array_like_to_array(arr);
14
8
  }
@@ -47,33 +41,6 @@ function _define_property(obj, key, value) {
47
41
  function _iterable_to_array(iter) {
48
42
  if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
49
43
  }
50
- function _iterable_to_array_limit(arr, i) {
51
- var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
52
- if (_i == null) return;
53
- var _arr = [];
54
- var _n = true;
55
- var _d = false;
56
- var _s, _e;
57
- try {
58
- for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
59
- _arr.push(_s.value);
60
- if (i && _arr.length === i) break;
61
- }
62
- } catch (err) {
63
- _d = true;
64
- _e = err;
65
- } finally{
66
- try {
67
- if (!_n && _i["return"] != null) _i["return"]();
68
- } finally{
69
- if (_d) throw _e;
70
- }
71
- }
72
- return _arr;
73
- }
74
- function _non_iterable_rest() {
75
- throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
76
- }
77
44
  function _non_iterable_spread() {
78
45
  throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
79
46
  }
@@ -92,33 +59,6 @@ function _object_spread(target) {
92
59
  }
93
60
  return target;
94
61
  }
95
- function ownKeys(object, enumerableOnly) {
96
- var keys = Object.keys(object);
97
- if (Object.getOwnPropertySymbols) {
98
- var symbols = Object.getOwnPropertySymbols(object);
99
- if (enumerableOnly) {
100
- symbols = symbols.filter(function(sym) {
101
- return Object.getOwnPropertyDescriptor(object, sym).enumerable;
102
- });
103
- }
104
- keys.push.apply(keys, symbols);
105
- }
106
- return keys;
107
- }
108
- function _object_spread_props(target, source) {
109
- source = source != null ? source : {};
110
- if (Object.getOwnPropertyDescriptors) {
111
- Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
112
- } else {
113
- ownKeys(Object(source)).forEach(function(key) {
114
- Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
115
- });
116
- }
117
- return target;
118
- }
119
- function _sliced_to_array(arr, i) {
120
- return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
121
- }
122
62
  function _to_consumable_array(arr) {
123
63
  return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
124
64
  }
@@ -130,1736 +70,17 @@ function _unsupported_iterable_to_array(o, minLen) {
130
70
  if (n === "Map" || n === "Set") return Array.from(n);
131
71
  if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
132
72
  }
133
- import json5 from "json5";
134
- //@ts-ignore
135
- import rehypeKatex from "rehype-katex";
136
- import rehypeRaw from "rehype-raw";
137
- import { remark } from "remark";
138
- import remarkFrontmatter from "remark-frontmatter";
139
- import remarkGfm from "remark-gfm";
140
- import remarkMath from "remark-math";
141
- import remarkParse from "remark-parse";
142
- import remarkRehype from "remark-rehype";
143
- import { fixStrongWithSpecialChars } from "./remarkParse";
144
- import { htmlToFragmentList } from "../plugins/insertParsedHtmlNodes";
145
- import { EditorUtils } from "../utils";
146
- import { isCodeBlockLikelyComplete } from "../utils/findMatchingClose";
147
- import partialJsonParse from "./json-parse";
73
+ import { applyContextPropsAndConfig } from "./parse/applyContextPropsAndConfig";
74
+ import { handleBlockquote, handleFootnoteDefinition, handleHeading, handleList, handleListItem, handleParagraph, handleTextAndInlineElements } from "./parse/parseBlockElements";
75
+ import { handleCode, handleYaml } from "./parse/parseCode";
76
+ import { handleDefinition, handleInlineCode, handleThematicBreak } from "./parse/parseElements";
77
+ import { addEmptyLinesIfNeeded } from "./parse/parseEmptyLines";
78
+ import { handleFootnoteReference } from "./parse/parseFootnote";
79
+ import { handleHtml, preprocessNonStandardHtmlTags, preprocessThinkTags } from "./parse/parseHtml";
80
+ import { handleInlineMath, handleMath } from "./parse/parseMath";
81
+ import { handleImage } from "./parse/parseMedia";
82
+ import { parseTableOrChart, preprocessMarkdownTableNewlines } from "./parse/parseTable";
148
83
  import mdastParser from "./remarkParse";
149
- // 常量定义
150
- var EMPTY_LINE_DISTANCE_THRESHOLD = 4; // 两个元素之间的行距阈值
151
- var EMPTY_LINE_CALCULATION_OFFSET = 2; // 计算空行数量时的偏移量
152
- var EMPTY_LINE_DIVISOR = 2; // 计算空行数量的除数
153
- var MIN_TABLE_CELL_LENGTH = 5; // 表格单元格最小长度
154
- var INLINE_MATH_SUFFIX_PATTERN = '(?:%|[kKmMbB]|千|万|亿|兆|万亿|百万|亿万)?';
155
- var INLINE_MATH_CURRENCY_PATTERN = new RegExp("^[+-]?\\d{1,3}(?:,\\d{3})*(?:\\.\\d+)?".concat(INLINE_MATH_SUFFIX_PATTERN, "$"));
156
- var INLINE_MATH_SIMPLE_NUMBER_PATTERN = new RegExp("^[+-]?\\d+(?:\\.\\d+)?".concat(INLINE_MATH_SUFFIX_PATTERN, "$"));
157
- // HTML 转义和代码块检测相关的常量
158
- var NOT_SPACE_START = /^\S*/;
159
- var ENDING_NEWLINE = /\n$/;
160
- var FENCED_CODE_REGEX = /^(`{3,}|~{3,})/;
161
- var shouldTreatInlineMathAsText = function(rawValue) {
162
- var trimmedValue = rawValue.trim();
163
- if (!trimmedValue) {
164
- return true;
165
- }
166
- if (/[=^_\\{}]/.test(trimmedValue)) {
167
- return false;
168
- }
169
- return INLINE_MATH_CURRENCY_PATTERN.test(trimmedValue) || INLINE_MATH_SIMPLE_NUMBER_PATTERN.test(trimmedValue);
170
- };
171
- // 处理schema类型语言的辅助函数
172
- var processSchemaLanguage = function(element, value) {
173
- var json = [];
174
- try {
175
- json = json5.parse(value || '[]');
176
- } catch (error) {
177
- try {
178
- json = partialJsonParse(value || '[]');
179
- } catch (error) {
180
- json = value;
181
- console.error('parse schema error', error);
182
- }
183
- }
184
- return _object_spread_props(_object_spread({}, element), {
185
- type: 'apaasify',
186
- value: json,
187
- children: [
188
- {
189
- text: value
190
- }
191
- ]
192
- });
193
- };
194
- // 语言类型处理策略配置
195
- var LANGUAGE_HANDLERS = {
196
- mermaid: function(element) {
197
- return _object_spread_props(_object_spread({}, element), {
198
- type: 'mermaid'
199
- });
200
- },
201
- schema: processSchemaLanguage,
202
- apaasify: processSchemaLanguage,
203
- apassify: processSchemaLanguage,
204
- katex: function(element) {
205
- return _object_spread_props(_object_spread({}, element), {
206
- type: 'katex'
207
- });
208
- },
209
- 'agentar-card': processSchemaLanguage
210
- };
211
- var advancedNumericCheck = function(value) {
212
- var numericPattern = /^[-+]?[0-9,]*\.?[0-9]+([eE][-+]?[0-9]+)?$/;
213
- return typeof value === 'number' || typeof value === 'string' && numericPattern.test(value);
214
- };
215
- var isNumericValue = function(value) {
216
- return typeof value === 'number' || !isNaN(parseFloat(value)) && isFinite(value) || advancedNumericCheck(value);
217
- };
218
- /**
219
- * 判断是否包含不完整输入
220
- * 如果一行中包含可能尚未完成的数字输入,返回 true
221
- */ var hasIncompleteNumericInput = function(values) {
222
- // 检查是否有可能是正在输入的不完整数字
223
- // 例如: '12.' 或 '0.' 或 '-' 或 仅有一个数字字符的情况
224
- return values.some(function(val) {
225
- if (typeof val !== 'string') return false;
226
- return val.endsWith('.') && /\d/.test(val) || // 以小数点结尾
227
- val === '-' || // 只有负号
228
- val === '+' || // 只有正号
229
- val.length === 1 && /\d/.test(val) // 只有一个数字
230
- ;
231
- });
232
- };
233
- /**
234
- * 规范化字段名,统一处理转义字符
235
- * 将 `index\_value` 转换为 `index_value`,确保字段名一致
236
- * @param fieldName - 原始字段名
237
- * @returns 规范化后的字段名
238
- */ var normalizeFieldName = function(fieldName) {
239
- if (!fieldName) return fieldName;
240
- // 移除转义字符:将 `\_` 转换为 `_`,`\\` 转换为 `\`
241
- return fieldName.replace(/\\_/g, '_').replace(/\\\\/g, '\\').replace(/\\(?=")/g, '') // 移除转义的双引号
242
- .trim();
243
- };
244
- var getColumnAlignment = function(data, columns) {
245
- if (!data.length) return [];
246
- // 缓存上一次的对齐结果,避免频繁切换
247
- var prevAlignments = [];
248
- return columns.map(function(col, index) {
249
- var values = data.map(function(row) {
250
- return row[col.dataIndex];
251
- }).filter(Boolean);
252
- values === null || values === void 0 ? void 0 : values.pop();
253
- // 如果检测到可能正在输入的数字,保持当前对齐状态
254
- if (hasIncompleteNumericInput(values)) {
255
- return prevAlignments[index] || null;
256
- }
257
- var alignment = values.every(isNumericValue) ? 'right' : null;
258
- prevAlignments[index] = alignment;
259
- return alignment;
260
- });
261
- };
262
- var stringifyObj = remark().use(remarkParse).use(fixStrongWithSpecialChars).use(remarkMath, {
263
- singleDollarTextMath: false
264
- }).use(remarkRehype, {
265
- allowDangerousHtml: true
266
- }).use(rehypeRaw).use(rehypeKatex).use(remarkGfm).use(remarkFrontmatter, [
267
- 'yaml'
268
- ]);
269
- var myRemark = {
270
- stringify: function(obj) {
271
- var mdStr = stringifyObj.stringify(obj);
272
- return mdStr;
273
- }
274
- };
275
- /**
276
- * 检测和解析 think 标签
277
- * @param str - 要检测的字符串
278
- * @returns think 标签的内容,如果不是 think 标签则返回 null
279
- */ var findThinkElement = function(str) {
280
- try {
281
- // 匹配 <think>内容</think> 格式
282
- var thinkMatch = str.match(/^\s*<think>([\s\S]*?)<\/think>\s*$/);
283
- if (thinkMatch) {
284
- return {
285
- content: thinkMatch[1].trim()
286
- };
287
- }
288
- return null;
289
- } catch (e) {
290
- return null;
291
- }
292
- };
293
- /**
294
- * 检测和解析 answer 标签
295
- * @param str - 要检测的字符串
296
- * @returns answer 标签的内容,如果不是 answer 标签则返回 null
297
- */ var findAnswerElement = function(str) {
298
- try {
299
- // 匹配 <answer>内容</answer> 格式
300
- var answerMatch = str.match(/^\s*<answer>([\s\S]*?)<\/answer>\s*$/);
301
- if (answerMatch) {
302
- return {
303
- content: answerMatch[1].trim()
304
- };
305
- }
306
- return null;
307
- } catch (e) {
308
- return null;
309
- }
310
- };
311
- /**
312
- * 从 HTML 字符串中提取媒体元素属性
313
- */ var extractMediaAttributes = function(str) {
314
- var _str_match, _str_match1, _str_match2, _str_match3, _str_match4;
315
- return {
316
- height: (_str_match = str.match(/height="(\d+)"/)) === null || _str_match === void 0 ? void 0 : _str_match[1],
317
- width: (_str_match1 = str.match(/width="(\d+)"/)) === null || _str_match1 === void 0 ? void 0 : _str_match1[1],
318
- align: (_str_match2 = str.match(/data-align="(\w+)"/)) === null || _str_match2 === void 0 ? void 0 : _str_match2[1],
319
- alt: (_str_match3 = str.match(/alt="([^"\n]+)"/)) === null || _str_match3 === void 0 ? void 0 : _str_match3[1],
320
- controls: str.match(/controls/),
321
- autoplay: str.match(/autoplay/),
322
- loop: str.match(/loop/),
323
- muted: str.match(/muted/),
324
- poster: (_str_match4 = str.match(/poster="([^"\n]+)"/)) === null || _str_match4 === void 0 ? void 0 : _str_match4[1]
325
- };
326
- };
327
- /**
328
- * 构建媒体元素对象
329
- */ var buildMediaElement = function(url, tagName, attrs) {
330
- return {
331
- url: url,
332
- height: attrs.height ? +attrs.height : undefined,
333
- width: attrs.width ? +attrs.width : undefined,
334
- align: attrs.align,
335
- alt: attrs.alt,
336
- tagName: tagName,
337
- controls: !!attrs.controls,
338
- autoplay: !!attrs.autoplay,
339
- loop: !!attrs.loop,
340
- muted: !!attrs.muted,
341
- poster: attrs.poster
342
- };
343
- };
344
- /**
345
- * 从字符串中提取视频源 URL
346
- */ var extractVideoSource = function(str, tagName) {
347
- // 首先尝试从标签本身获取 src 属性
348
- var url = str.match(/src="([^"\n]+)"/);
349
- // 如果是 video 标签且没有找到 src,尝试从 source 标签中获取
350
- if (tagName === 'video' && !url) {
351
- var sourceMatch = str.match(/<source[^>]*src="([^"\n]+)"[^>]*>/);
352
- if (sourceMatch) {
353
- url = sourceMatch;
354
- }
355
- }
356
- return url === null || url === void 0 ? void 0 : url[1];
357
- };
358
- /**
359
- * 查找并解析媒体元素(img/video/iframe)
360
- */ var findImageElement = function(str) {
361
- try {
362
- // 首先尝试匹配包含 source 标签的 video 格式
363
- var videoWithSourceMatch = str.match(/^\s*<video[^>\n]*>[\s\S]*?<source[^>]*src="([^"\n]+)"[^>]*>[\s\S]*?<\/video>\s*$/);
364
- if (videoWithSourceMatch) {
365
- var attrs = extractMediaAttributes(str);
366
- return buildMediaElement(videoWithSourceMatch[1], 'video', attrs);
367
- }
368
- // 尝试匹配各种媒体标签格式
369
- var patterns = [
370
- /^\s*<(img|video|iframe)[^>\n]*>.*?<\/(?:img|video|iframe)>\s*$/,
371
- /^\s*<(img|video|iframe)[^>\n]*\/?>(.*<\/(?:img|video|iframe)>)?\s*$/,
372
- /^\s*<(img|video|iframe)[^>\n]*\/>\s*$/,
373
- /^\s*<(img|video|iframe)[^>\n]*>\s*$/
374
- ];
375
- var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
376
- try {
377
- for(var _iterator = patterns[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
378
- var pattern = _step.value;
379
- var match = str.match(pattern);
380
- if (match) {
381
- var _match__match;
382
- var tagName = (_match__match = match[0].match(/<(img|video|iframe)/)) === null || _match__match === void 0 ? void 0 : _match__match[1];
383
- var url = extractVideoSource(match[0], tagName);
384
- var attrs1 = extractMediaAttributes(match[0]);
385
- return buildMediaElement(url, tagName, attrs1);
386
- }
387
- }
388
- } catch (err) {
389
- _didIteratorError = true;
390
- _iteratorError = err;
391
- } finally{
392
- try {
393
- if (!_iteratorNormalCompletion && _iterator.return != null) {
394
- _iterator.return();
395
- }
396
- } finally{
397
- if (_didIteratorError) {
398
- throw _iteratorError;
399
- }
400
- }
401
- }
402
- return null;
403
- } catch (e) {
404
- console.error('Failed to parse media element:', e);
405
- return null;
406
- }
407
- };
408
- /**
409
- * 根据媒体元素信息创建编辑器节点
410
- */ var createMediaNodeFromElement = function(mediaElement) {
411
- if (!mediaElement) return null;
412
- // 根据标签类型确定媒体类型
413
- var mediaTypeMap = {
414
- video: 'video',
415
- iframe: 'iframe',
416
- img: 'image'
417
- };
418
- var mediaType = mediaTypeMap[mediaElement.tagName] || 'image';
419
- return EditorUtils.createMediaNode(decodeURIComponentUrl(mediaElement.url || ''), mediaType, {
420
- align: mediaElement.align,
421
- alt: mediaElement.alt,
422
- height: mediaElement.height,
423
- width: mediaElement.width,
424
- controls: mediaElement.controls,
425
- autoplay: mediaElement.autoplay,
426
- loop: mediaElement.loop,
427
- muted: mediaElement.muted,
428
- poster: mediaElement.poster
429
- });
430
- };
431
- var findAttachment = function(str) {
432
- try {
433
- var match = str.match(/^\s*<a[^>\n]*download[^>\n]*\/?>(.*<\/a>:?)?\s*$/);
434
- if (match) {
435
- var url = match[0].match(/href="([^"\n]+)"/);
436
- var size = match[0].match(/data-size="(\d+)"/);
437
- if (url) {
438
- return {
439
- url: url[1],
440
- size: Number((size === null || size === void 0 ? void 0 : size[1]) || 0)
441
- };
442
- }
443
- }
444
- return null;
445
- } catch (e) {
446
- return null;
447
- }
448
- };
449
- var parseText = function(els) {
450
- var leaf = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {
451
- data: {}
452
- };
453
- var leafs = [];
454
- var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
455
- try {
456
- for(var _iterator = els[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
457
- var n = _step.value;
458
- if (n.type === 'strong') leafs = leafs.concat(parseText(n.children, _object_spread_props(_object_spread({}, leaf), {
459
- bold: true
460
- })));
461
- if (n.type === 'emphasis') leafs = leafs.concat(parseText(n.children, _object_spread_props(_object_spread({}, leaf), {
462
- italic: true
463
- })));
464
- if (n.type === 'delete') leafs = leafs.concat(parseText(n.children, _object_spread_props(_object_spread({}, leaf), {
465
- strikethrough: true
466
- })));
467
- if (n.type === 'link') {
468
- leafs = leafs.concat(parseText(n.children, _object_spread_props(_object_spread({}, leaf), {
469
- url: n === null || n === void 0 ? void 0 : n.url
470
- })));
471
- }
472
- if (n.type === 'inlineCode') leafs.push(_object_spread_props(_object_spread({}, leaf), {
473
- text: n.value,
474
- code: true
475
- }));
476
- if (n.type === 'inlineMath') {
477
- var inlineMathValue = typeof n.value === 'string' ? n.value : '';
478
- if (shouldTreatInlineMathAsText(inlineMathValue)) {
479
- leafs.push(_object_spread_props(_object_spread({}, leaf), {
480
- text: "$".concat(inlineMathValue, "$")
481
- }));
482
- continue;
483
- }
484
- // 处理内联数学公式,返回一个特殊的节点而不是叶子节点
485
- leafs.push(_object_spread_props(_object_spread({}, leaf), {
486
- type: 'inline-katex',
487
- children: [
488
- {
489
- text: inlineMathValue
490
- }
491
- ]
492
- }));
493
- continue; // 跳过后面的默认处理
494
- }
495
- // @ts-ignore
496
- leafs.push(_object_spread_props(_object_spread({}, leaf), {
497
- text: n.value || ''
498
- }));
499
- }
500
- } catch (err) {
501
- _didIteratorError = true;
502
- _iteratorError = err;
503
- } finally{
504
- try {
505
- if (!_iteratorNormalCompletion && _iterator.return != null) {
506
- _iterator.return();
507
- }
508
- } finally{
509
- if (_didIteratorError) {
510
- throw _iteratorError;
511
- }
512
- }
513
- }
514
- return leafs;
515
- };
516
- var parseTableOrChart = function(table, preNode, plugins, parserConfig) {
517
- var _table_children, _tableHeader_children, _table_children_slice, _table_children1, _table_align, _config_at, _config_at1, _table_otherProps;
518
- var keyMap = new Map();
519
- // @ts-ignore
520
- var config = // @ts-ignore
521
- (preNode === null || preNode === void 0 ? void 0 : preNode.type) === 'code' && // @ts-ignore
522
- (preNode === null || preNode === void 0 ? void 0 : preNode.language) === 'html' && (// @ts-ignore
523
- preNode === null || preNode === void 0 ? void 0 : preNode.otherProps) ? preNode === null || preNode === void 0 ? void 0 : preNode.otherProps : {};
524
- var tableHeader = table === null || table === void 0 ? void 0 : (_table_children = table.children) === null || _table_children === void 0 ? void 0 : _table_children.at(0);
525
- var columns = (tableHeader === null || tableHeader === void 0 ? void 0 : (_tableHeader_children = tableHeader.children) === null || _tableHeader_children === void 0 ? void 0 : _tableHeader_children.map(function(node) {
526
- var _myRemark_stringify;
527
- return (_myRemark_stringify = myRemark.stringify({
528
- type: 'root',
529
- children: [
530
- node
531
- ]
532
- })) === null || _myRemark_stringify === void 0 ? void 0 : _myRemark_stringify.replace(/\n/g, '').trim();
533
- }).map(function(title) {
534
- // 先规范化字段名,统一处理转义字符
535
- var normalizedTitle = normalizeFieldName(title || ' ');
536
- return normalizedTitle;
537
- }).map(function(title, index) {
538
- if (keyMap.has(title)) {
539
- keyMap.set(title, keyMap.get(title) + '_' + index);
540
- return {
541
- title: title,
542
- dataIndex: title + '_' + index,
543
- key: title + '_' + index
544
- };
545
- }
546
- keyMap.set(title, title);
547
- return {
548
- title: title,
549
- dataIndex: title,
550
- key: title
551
- };
552
- })) || [];
553
- var dataSource = (table === null || table === void 0 ? void 0 : (_table_children1 = table.children) === null || _table_children1 === void 0 ? void 0 : (_table_children_slice = _table_children1.slice(1)) === null || _table_children_slice === void 0 ? void 0 : _table_children_slice.map(function(row) {
554
- var _row_children;
555
- return (_row_children = row.children) === null || _row_children === void 0 ? void 0 : _row_children.reduce(function(acc, cell, index) {
556
- var _myRemark_stringify_replace_replace_replace, _myRemark_stringify_replace_replace, _myRemark_stringify_replace, _myRemark_stringify;
557
- // 如果数据列数超出表头列数,舍弃多余的数据
558
- if (index >= columns.length) {
559
- return acc;
560
- }
561
- acc[columns[index].dataIndex] = (_myRemark_stringify = myRemark.stringify({
562
- type: 'root',
563
- children: [
564
- cell
565
- ]
566
- })) === null || _myRemark_stringify === void 0 ? void 0 : (_myRemark_stringify_replace = _myRemark_stringify.replace(/\n/g, '')) === null || _myRemark_stringify_replace === void 0 ? void 0 : (_myRemark_stringify_replace_replace = _myRemark_stringify_replace.replace(/\\(?=")/g, '')) === null || _myRemark_stringify_replace_replace === void 0 ? void 0 : (_myRemark_stringify_replace_replace_replace = _myRemark_stringify_replace_replace.replace(/\\_/g, '')) === null || _myRemark_stringify_replace_replace_replace === void 0 ? void 0 : _myRemark_stringify_replace_replace_replace.trim();
567
- return acc;
568
- }, {});
569
- })) || [];
570
- if ((_table_align = table.align) === null || _table_align === void 0 ? void 0 : _table_align.every(function(item) {
571
- return !item;
572
- })) {
573
- var aligns = getColumnAlignment(dataSource, columns);
574
- table.align = aligns;
575
- }
576
- var aligns1 = table.align;
577
- var isChart = (config === null || config === void 0 ? void 0 : config.chartType) || (config === null || config === void 0 ? void 0 : (_config_at1 = config.at) === null || _config_at1 === void 0 ? void 0 : (_config_at = _config_at1.call(config, 0)) === null || _config_at === void 0 ? void 0 : _config_at.chartType);
578
- /**
579
- * 如果是分栏,将表格转换为分栏节点
580
- */ // 计算合并单元格信息
581
- var mergeCells = config.mergeCells || [];
582
- // 创建合并单元格映射,用于快速查找
583
- var mergeMap = new Map();
584
- mergeCells === null || mergeCells === void 0 ? void 0 : mergeCells.forEach(function(param) {
585
- var row = param.row, col = param.col, rowSpan = param.rowSpan, rowspan = param.rowspan, colSpan = param.colSpan, colspan = param.colspan;
586
- var rawRowSpan = rowSpan || rowspan;
587
- var rawColSpan = colSpan || colspan;
588
- // 主单元格
589
- mergeMap.set("".concat(row, "-").concat(col), {
590
- rowSpan: rawRowSpan,
591
- colSpan: rawColSpan
592
- });
593
- // 被合并的单元格标记为隐藏
594
- for(var r = row; r < row + rawRowSpan; r++){
595
- for(var c = col; c < col + rawColSpan; c++){
596
- if (r !== row || c !== col) {
597
- mergeMap.set("".concat(r, "-").concat(c), {
598
- rowSpan: 1,
599
- colSpan: 1,
600
- hidden: true
601
- });
602
- }
603
- }
604
- }
605
- });
606
- var children = table.children.map(function(r, l) {
607
- return {
608
- type: 'table-row',
609
- align: (aligns1 === null || aligns1 === void 0 ? void 0 : aligns1[l]) || undefined,
610
- children: r.children.map(function(c, i) {
611
- var _c_children;
612
- var mergeInfo = mergeMap.get("".concat(l, "-").concat(i));
613
- return _object_spread_props(_object_spread({
614
- type: 'table-cell',
615
- align: (aligns1 === null || aligns1 === void 0 ? void 0 : aligns1[i]) || undefined,
616
- title: l === 0,
617
- rows: l,
618
- cols: i
619
- }, (mergeInfo === null || mergeInfo === void 0 ? void 0 : mergeInfo.rowSpan) && mergeInfo.rowSpan > 1 ? {
620
- rowSpan: mergeInfo.rowSpan
621
- } : {}, (mergeInfo === null || mergeInfo === void 0 ? void 0 : mergeInfo.colSpan) && mergeInfo.colSpan > 1 ? {
622
- colSpan: mergeInfo.colSpan
623
- } : {}, (mergeInfo === null || mergeInfo === void 0 ? void 0 : mergeInfo.hidden) ? {
624
- hidden: true
625
- } : {}), {
626
- children: ((_c_children = c.children) === null || _c_children === void 0 ? void 0 : _c_children.length) ? [
627
- {
628
- type: 'paragraph',
629
- children: parseNodes(c.children, plugins, false, c, parserConfig)
630
- }
631
- ] : [
632
- {
633
- type: 'paragraph',
634
- children: [
635
- {
636
- text: ''
637
- }
638
- ]
639
- }
640
- ]
641
- });
642
- })
643
- };
644
- });
645
- // 检查表格是否完成(未闭合)
646
- // 如果 table 节点有 otherProps.finish,使用它;否则默认为 false(未完成)
647
- var isFinished = (table === null || table === void 0 ? void 0 : (_table_otherProps = table.otherProps) === null || _table_otherProps === void 0 ? void 0 : _table_otherProps.finish) !== undefined ? table.otherProps.finish : false;
648
- var otherProps = _object_spread_props(_object_spread({}, isChart ? {
649
- config: config
650
- } : config), {
651
- columns: columns,
652
- dataSource: dataSource.map(function(item) {
653
- item === null || item === void 0 ? true : delete item.chartType;
654
- return _object_spread({}, item);
655
- }),
656
- finish: isFinished
657
- });
658
- var node = {
659
- type: isChart ? 'chart' : 'table',
660
- children: children,
661
- otherProps: otherProps
662
- };
663
- return EditorUtils.wrapperCardNode(node);
664
- };
665
- /**
666
- * 处理标题节点
667
- * @param currentElement - 当前处理的标题元素,包含depth和children属性
668
- * @returns 返回格式化的标题节点对象
669
- */ var handleHeading = function(currentElement, plugins, parserConfig) {
670
- var _currentElement_children;
671
- return {
672
- type: 'head',
673
- level: currentElement.depth,
674
- children: ((_currentElement_children = currentElement.children) === null || _currentElement_children === void 0 ? void 0 : _currentElement_children.length) ? parseNodes(currentElement.children, plugins, false, currentElement, parserConfig) : [
675
- {
676
- text: ''
677
- }
678
- ]
679
- };
680
- };
681
- export var decodeURIComponentUrl = function(url) {
682
- try {
683
- return decodeURIComponent(url);
684
- } catch (e) {
685
- console.error('Failed to decode URI component:', e);
686
- return url;
687
- }
688
- };
689
- /**
690
- * 处理HTML节点
691
- * @param currentElement - 当前处理的HTML元素
692
- * @param parent - 父级元素,用于判断上下文
693
- * @param htmlTag - HTML标签栈,用于跟踪嵌套的HTML标签
694
- * @returns 返回包含解析后元素和上下文属性的对象
695
- */ var handleHtml = function(currentElement, parent, htmlTag) {
696
- var _currentElement_value, _currentElement_value_trim, _currentElement_value1, _currentElement_value_trim1;
697
- var value = (currentElement === null || currentElement === void 0 ? void 0 : (_currentElement_value = currentElement.value) === null || _currentElement_value === void 0 ? void 0 : _currentElement_value.replace('<!--', '').replace('-->', '').trim()) || '{}';
698
- var contextProps = {};
699
- if (value && (currentElement === null || currentElement === void 0 ? void 0 : (_currentElement_value1 = currentElement.value) === null || _currentElement_value1 === void 0 ? void 0 : (_currentElement_value_trim = _currentElement_value1.trim()) === null || _currentElement_value_trim === void 0 ? void 0 : _currentElement_value_trim.endsWith('-->')) && (currentElement === null || currentElement === void 0 ? void 0 : (_currentElement_value_trim1 = currentElement.value.trim()) === null || _currentElement_value_trim1 === void 0 ? void 0 : _currentElement_value_trim1.startsWith('<!--'))) {
700
- try {
701
- contextProps = json5.parse(value);
702
- } catch (e) {
703
- try {
704
- contextProps = partialJsonParse(value);
705
- } catch (parseError) {
706
- console.warn('Failed to parse HTML comment as JSON or partial JSON:', {
707
- value: value,
708
- error: parseError
709
- });
710
- }
711
- console.warn('HTML comment parse fallback attempted:', e);
712
- }
713
- }
714
- var el;
715
- if (!parent || [
716
- 'listItem',
717
- 'blockquote'
718
- ].includes(parent.type)) {
719
- // 检查是否为不完整的图片标记
720
- var incompleteImageMatch = currentElement.value.match(/<incomplete-image\s+data-raw="([^"]+)"\s*\/?>/);
721
- if (incompleteImageMatch) {
722
- var rawMarkdown = decodeURIComponent(incompleteImageMatch[1]);
723
- // 直接创建带有 loading 状态的图片节点(不通过 createMediaNode,因为 URL 为空)
724
- el = EditorUtils.wrapperCardNode({
725
- type: 'image',
726
- url: '',
727
- mediaType: 'image',
728
- alt: rawMarkdown,
729
- loading: true,
730
- rawMarkdown: rawMarkdown,
731
- children: [
732
- {
733
- text: ''
734
- }
735
- ]
736
- });
737
- } else {
738
- // 检查是否为 <think> 标签
739
- var thinkElement = findThinkElement(currentElement.value);
740
- if (thinkElement) {
741
- // 将 <think> 标签转换为 think 类型的代码块
742
- el = {
743
- type: 'code',
744
- language: 'think',
745
- value: thinkElement.content,
746
- children: [
747
- {
748
- text: thinkElement.content
749
- }
750
- ]
751
- };
752
- } else {
753
- // 检查是否为 <answer> 标签
754
- var answerElement = findAnswerElement(currentElement.value);
755
- if (answerElement) {
756
- // 将 <answer> 标签的内容作为普通文本
757
- el = {
758
- text: answerElement.content
759
- };
760
- } else {
761
- var mediaElement = findImageElement(currentElement.value);
762
- if (mediaElement) {
763
- el = createMediaNodeFromElement(mediaElement);
764
- } else if (currentElement.value === '<br/>') {
765
- el = {
766
- type: 'paragraph',
767
- children: [
768
- {
769
- text: ''
770
- }
771
- ]
772
- };
773
- } else if (currentElement.value.match(/^<\/(img|video|iframe)>/)) {
774
- // 如果是媒体标签的结束标签,跳过处理
775
- el = null;
776
- } else {
777
- // 检查是否为注释(注释需要特殊处理以提取配置)
778
- var isComment = currentElement.value.trim().startsWith('<!--') && currentElement.value.trim().endsWith('-->');
779
- // 检查是否为标准 HTML 元素或注释
780
- if (isComment || isStandardHtmlElement(currentElement.value)) {
781
- // 标准 HTML 元素或注释:按原逻辑处理
782
- el = currentElement.value.match(/<\/?(table|div|ul|li|ol|p|strong)[^\n>]*?>/) ? htmlToFragmentList(currentElement.value, '') : {
783
- type: 'code',
784
- language: 'html',
785
- render: true,
786
- value: currentElement.value,
787
- children: [
788
- {
789
- text: currentElement.value
790
- }
791
- ]
792
- };
793
- } else {
794
- // 非标准元素(如自定义标签):当作普通文本处理
795
- el = {
796
- text: currentElement.value
797
- };
798
- }
799
- }
800
- }
801
- }
802
- }
803
- } else {
804
- el = processInlineHtml(currentElement, htmlTag);
805
- }
806
- if (el && !Array.isArray(el)) {
807
- // 只有非文本节点才设置 isConfig 和 otherProps
808
- if (!('text' in el && Object.keys(el).length === 1)) {
809
- var _currentElement_value_trim2, _currentElement_value2;
810
- el.isConfig = currentElement === null || currentElement === void 0 ? void 0 : (_currentElement_value2 = currentElement.value) === null || _currentElement_value2 === void 0 ? void 0 : (_currentElement_value_trim2 = _currentElement_value2.trim()) === null || _currentElement_value_trim2 === void 0 ? void 0 : _currentElement_value_trim2.startsWith('<!--');
811
- el.otherProps = contextProps;
812
- }
813
- }
814
- return {
815
- el: el,
816
- contextProps: contextProps
817
- };
818
- };
819
- /**
820
- * 处理内联HTML元素
821
- * @param currentElement - 当前处理的HTML元素
822
- * @param htmlTag - HTML标签栈
823
- * @returns 返回处理后的元素对象,如果是标签则返回null
824
- */ var processInlineHtml = function(currentElement, htmlTag) {
825
- var breakMatch = currentElement.value.match(/<br\/?>/);
826
- if (breakMatch) {
827
- return {
828
- type: 'break',
829
- children: [
830
- {
831
- text: '\n'
832
- }
833
- ]
834
- };
835
- }
836
- // 检查是否为 <answer> 标签(内联场景)
837
- var answerElement = findAnswerElement(currentElement.value);
838
- if (answerElement) {
839
- // 将 <answer> 标签的内容作为普通文本
840
- return {
841
- text: answerElement.content
842
- };
843
- }
844
- // 检查是否为不完整的图片标记
845
- var incompleteImageMatch = currentElement.value.match(/<incomplete-image\s+data-raw="([^"]+)"\s*\/?>/);
846
- if (incompleteImageMatch) {
847
- var rawMarkdown = decodeURIComponent(incompleteImageMatch[1]);
848
- // 直接创建带有 loading 状态的图片节点(不通过 createMediaNode,因为 URL 为空)
849
- return EditorUtils.wrapperCardNode({
850
- type: 'image',
851
- url: '',
852
- mediaType: 'image',
853
- alt: rawMarkdown,
854
- loading: true,
855
- rawMarkdown: rawMarkdown,
856
- children: [
857
- {
858
- text: ''
859
- }
860
- ]
861
- });
862
- }
863
- // 检查是否为非标准 HTML 元素,如果是则直接当作文本
864
- if (!isStandardHtmlElement(currentElement.value)) {
865
- return {
866
- text: currentElement.value
867
- };
868
- }
869
- var htmlMatch = currentElement.value.match(/<\/?(b|i|del|font|code|span|sup|sub|strong|a)[^\n>]*?>/);
870
- if (htmlMatch) {
871
- var _htmlMatch = _sliced_to_array(htmlMatch, 2), str = _htmlMatch[0], tag = _htmlMatch[1];
872
- if (str.startsWith('</') && htmlTag.length && htmlTag[htmlTag.length - 1].tag === tag) {
873
- htmlTag.pop();
874
- }
875
- if (!str.startsWith('</')) {
876
- processHtmlTag(str, tag, htmlTag);
877
- }
878
- return null;
879
- } else {
880
- var mediaElement = findImageElement(currentElement.value);
881
- if (mediaElement) {
882
- return createMediaNodeFromElement(mediaElement);
883
- } else {
884
- return {
885
- text: currentElement.value
886
- };
887
- }
888
- }
889
- };
890
- /**
891
- * 处理HTML标签并添加到标签栈中
892
- * @param str - HTML标签字符串
893
- * @param tag - 标签名称
894
- * @param htmlTag - HTML标签栈
895
- */ var processHtmlTag = function(str, tag, htmlTag) {
896
- if (tag === 'span') {
897
- try {
898
- var styles = str.match(/style="([^"\n]+)"/);
899
- if (styles) {
900
- var stylesMap = new Map(styles[1].split(';').map(function(item) {
901
- return item.split(':').map(function(item) {
902
- return item.trim();
903
- });
904
- }));
905
- if (stylesMap.get('color')) {
906
- htmlTag.push({
907
- tag: tag,
908
- color: stylesMap.get('color')
909
- });
910
- }
911
- }
912
- } catch (e) {
913
- console.warn('Failed to parse span style attribute:', {
914
- str: str,
915
- error: e
916
- });
917
- }
918
- } else if (tag === 'a') {
919
- var url = str.match(/href="([\w:./_\-#\\]+)"/);
920
- if (url) {
921
- htmlTag.push({
922
- tag: tag,
923
- url: url[1]
924
- });
925
- }
926
- } else if (tag === 'font') {
927
- var color = str.match(/color="([^"\n]+)"/);
928
- if (!color) {
929
- color = str.match(/color=([^"\n]+)/);
930
- }
931
- if (color) {
932
- htmlTag.push({
933
- tag: tag,
934
- color: color[1].replaceAll('>', '')
935
- });
936
- }
937
- } else {
938
- htmlTag.push({
939
- tag: tag
940
- });
941
- }
942
- };
943
- /**
944
- * 处理图片节点
945
- * @param currentElement - 当前处理的图片元素,包含url和alt属性
946
- * @returns 返回格式化的图片节点对象
947
- */ var handleImage = function(currentElement) {
948
- return EditorUtils.createMediaNode(decodeURIComponent(currentElement === null || currentElement === void 0 ? void 0 : currentElement.url), 'image', {
949
- alt: currentElement.alt
950
- });
951
- };
952
- /**
953
- * 处理内联数学公式
954
- * @param currentElement - 当前处理的内联数学公式元素
955
- * @returns 返回格式化的内联KaTeX节点对象
956
- */ var handleInlineMath = function(currentElement) {
957
- var inlineMathValue = typeof (currentElement === null || currentElement === void 0 ? void 0 : currentElement.value) === 'string' ? currentElement.value : '';
958
- if (shouldTreatInlineMathAsText(inlineMathValue)) {
959
- return {
960
- type: 'paragraph',
961
- children: [
962
- {
963
- text: "$".concat(inlineMathValue, "$")
964
- }
965
- ]
966
- };
967
- }
968
- return {
969
- type: 'inline-katex',
970
- children: [
971
- {
972
- text: inlineMathValue
973
- }
974
- ]
975
- };
976
- };
977
- /**
978
- * 处理数学公式块
979
- * @param currentElement - 当前处理的数学公式块元素
980
- * @returns 返回格式化的KaTeX块节点对象
981
- */ var handleMath = function(currentElement) {
982
- return {
983
- type: 'katex',
984
- language: 'latex',
985
- katex: true,
986
- value: currentElement.value,
987
- children: [
988
- {
989
- text: ''
990
- }
991
- ]
992
- };
993
- };
994
- /**
995
- * 处理列表节点
996
- * @param currentElement - 当前处理的列表元素,包含ordered、start等属性
997
- * @returns 返回格式化的列表节点对象
998
- */ var handleList = function(currentElement, plugins, parserConfig) {
999
- var _el_children;
1000
- var el = {
1001
- type: 'list',
1002
- order: currentElement.ordered,
1003
- start: currentElement.start,
1004
- children: parseNodes(currentElement.children, plugins, false, currentElement, parserConfig)
1005
- };
1006
- el.task = (_el_children = el.children) === null || _el_children === void 0 ? void 0 : _el_children.some(function(s) {
1007
- return typeof s.checked === 'boolean';
1008
- });
1009
- return el;
1010
- };
1011
- /**
1012
- * 处理脚注引用
1013
- * @param currentElement - 当前处理的脚注引用元素
1014
- * @returns 返回格式化的脚注引用节点对象
1015
- */ var handleFootnoteReference = function(currentElement) {
1016
- var _currentElement_identifier;
1017
- return {
1018
- text: "".concat((_currentElement_identifier = currentElement.identifier) === null || _currentElement_identifier === void 0 ? void 0 : _currentElement_identifier.toUpperCase()),
1019
- identifier: currentElement.identifier,
1020
- type: 'footnoteReference'
1021
- };
1022
- };
1023
- /**
1024
- * 处理脚注定义
1025
- * @param currentElement - 当前处理的脚注定义元素
1026
- * @returns 返回格式化的脚注定义节点对象
1027
- */ var handleFootnoteDefinition = function(currentElement, plugins, parserConfig) {
1028
- var _parseNodes, _linkNode_children;
1029
- var linkNode = (_parseNodes = parseNodes(currentElement.children, plugins, false, currentElement, parserConfig)) === null || _parseNodes === void 0 ? void 0 : _parseNodes.at(0);
1030
- var cellNode = linkNode === null || linkNode === void 0 ? void 0 : (_linkNode_children = linkNode.children) === null || _linkNode_children === void 0 ? void 0 : _linkNode_children.at(0);
1031
- return {
1032
- value: cellNode === null || cellNode === void 0 ? void 0 : cellNode.text,
1033
- url: cellNode === null || cellNode === void 0 ? void 0 : cellNode.url,
1034
- type: 'footnoteDefinition',
1035
- identifier: currentElement.identifier,
1036
- children: [
1037
- cellNode
1038
- ]
1039
- };
1040
- };
1041
- /**
1042
- * 处理列表项节点
1043
- * @param currentElement - 当前处理的列表项元素
1044
- * @returns 返回格式化的列表项节点对象,包含复选框状态和提及信息
1045
- */ var handleListItem = function(currentElement, plugins, parserConfig) {
1046
- var _currentElement_children, _currentElement_children__children_, _currentElement_children__children, _currentElement_children_, _currentElement_children1, _currentElement_children__children1, _currentElement_children_1, _currentElement_children2, _children__children_;
1047
- var children = ((_currentElement_children = currentElement.children) === null || _currentElement_children === void 0 ? void 0 : _currentElement_children.length) ? parseNodes(currentElement.children, plugins, false, currentElement, parserConfig) : [
1048
- {
1049
- type: 'paragraph',
1050
- children: [
1051
- {
1052
- text: ''
1053
- }
1054
- ]
1055
- }
1056
- ];
1057
- var mentions = undefined;
1058
- if (((_currentElement_children1 = currentElement.children) === null || _currentElement_children1 === void 0 ? void 0 : (_currentElement_children_ = _currentElement_children1[0]) === null || _currentElement_children_ === void 0 ? void 0 : (_currentElement_children__children = _currentElement_children_.children) === null || _currentElement_children__children === void 0 ? void 0 : (_currentElement_children__children_ = _currentElement_children__children[0]) === null || _currentElement_children__children_ === void 0 ? void 0 : _currentElement_children__children_.type) === 'link' && ((_currentElement_children2 = currentElement.children) === null || _currentElement_children2 === void 0 ? void 0 : (_currentElement_children_1 = _currentElement_children2[0]) === null || _currentElement_children_1 === void 0 ? void 0 : (_currentElement_children__children1 = _currentElement_children_1.children) === null || _currentElement_children__children1 === void 0 ? void 0 : _currentElement_children__children1.length) > 1) {
1059
- var _children__children, _children_;
1060
- var item = children === null || children === void 0 ? void 0 : (_children_ = children[0]) === null || _children_ === void 0 ? void 0 : (_children__children = _children_.children) === null || _children__children === void 0 ? void 0 : _children__children[0];
1061
- var label = item === null || item === void 0 ? void 0 : item.text;
1062
- if (label) {
1063
- var _item_url, _children__children1, _children_1, _children_2;
1064
- mentions = [
1065
- {
1066
- avatar: item === null || item === void 0 ? void 0 : item.url,
1067
- name: label,
1068
- id: new URLSearchParams('?' + (item === null || item === void 0 ? void 0 : (_item_url = item.url) === null || _item_url === void 0 ? void 0 : _item_url.split('?')[1])).get('id') || undefined
1069
- }
1070
- ];
1071
- children === null || children === void 0 ? true : (_children_1 = children[0]) === null || _children_1 === void 0 ? true : (_children__children1 = _children_1.children) === null || _children__children1 === void 0 ? true : delete _children__children1[0];
1072
- if (children === null || children === void 0 ? void 0 : (_children_2 = children[0]) === null || _children_2 === void 0 ? void 0 : _children_2.children) {
1073
- var _children__children2, _children_3;
1074
- children[0].children = children === null || children === void 0 ? void 0 : (_children_3 = children[0]) === null || _children_3 === void 0 ? void 0 : (_children__children2 = _children_3.children) === null || _children__children2 === void 0 ? void 0 : _children__children2.filter(Boolean);
1075
- }
1076
- }
1077
- }
1078
- if (children[0].type === 'paragraph' && ((_children__children_ = children[0].children[0]) === null || _children__children_ === void 0 ? void 0 : _children__children_.text)) {
1079
- var _children__children_1;
1080
- var text = (_children__children_1 = children[0].children[0]) === null || _children__children_1 === void 0 ? void 0 : _children__children_1.text;
1081
- var m = text.match(/^\[([x\s])]/);
1082
- if (m) {
1083
- children[0].children[0].text = text.replace(/^\[([x\s])]/, '');
1084
- return {
1085
- type: 'list-item',
1086
- checked: m ? m[1] === 'x' : undefined,
1087
- children: children,
1088
- mentions: mentions
1089
- };
1090
- }
1091
- }
1092
- return {
1093
- type: 'list-item',
1094
- checked: currentElement.checked,
1095
- children: children,
1096
- mentions: mentions
1097
- };
1098
- };
1099
- /**
1100
- * 处理附件链接
1101
- */ var handleAttachmentLink = function(currentElement) {
1102
- var text = currentElement.children.map(function(n) {
1103
- return n.value || '';
1104
- }).join('');
1105
- var attach = findAttachment(text);
1106
- if (!attach) return null;
1107
- var name = text.match(/>(.*)<\/a>/);
1108
- return {
1109
- type: 'attach',
1110
- url: decodeURIComponentUrl(attach === null || attach === void 0 ? void 0 : attach.url),
1111
- size: attach.size,
1112
- children: [
1113
- {
1114
- type: 'card-before',
1115
- children: [
1116
- {
1117
- text: ''
1118
- }
1119
- ]
1120
- },
1121
- {
1122
- type: 'card-after',
1123
- children: [
1124
- {
1125
- text: ''
1126
- }
1127
- ]
1128
- }
1129
- ],
1130
- name: name ? name[1] : attach === null || attach === void 0 ? void 0 : attach.url
1131
- };
1132
- };
1133
- /**
1134
- * 处理链接卡片
1135
- */ var handleLinkCard = function(currentElement, config) {
1136
- var _currentElement_children;
1137
- var link = currentElement === null || currentElement === void 0 ? void 0 : (_currentElement_children = currentElement.children) === null || _currentElement_children === void 0 ? void 0 : _currentElement_children.at(0);
1138
- return _object_spread_props(_object_spread({}, config), {
1139
- type: 'link-card',
1140
- url: decodeURIComponentUrl(link === null || link === void 0 ? void 0 : link.url),
1141
- children: [
1142
- {
1143
- type: 'card-before',
1144
- children: [
1145
- {
1146
- text: ''
1147
- }
1148
- ]
1149
- },
1150
- {
1151
- type: 'card-after',
1152
- children: [
1153
- {
1154
- text: ''
1155
- }
1156
- ]
1157
- }
1158
- ],
1159
- name: link.title
1160
- });
1161
- };
1162
- /**
1163
- * 处理段落中的子元素
1164
- */ var processParagraphChildren = function(currentElement, plugins, parserConfig) {
1165
- var elements = [];
1166
- var textNodes = [];
1167
- var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
1168
- try {
1169
- for(var _iterator = (currentElement.children || [])[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
1170
- var currentChild = _step.value;
1171
- if (currentChild.type === 'image') {
1172
- // 将累积的文本节点生成段落
1173
- if (textNodes.length) {
1174
- elements.push({
1175
- type: 'paragraph',
1176
- children: parseNodes(textNodes, plugins, false, currentElement, parserConfig)
1177
- });
1178
- textNodes = [];
1179
- }
1180
- // 添加图片节点
1181
- elements.push(EditorUtils.createMediaNode(decodeURIComponentUrl(currentChild === null || currentChild === void 0 ? void 0 : currentChild.url), 'image', {
1182
- alt: currentChild.alt
1183
- }));
1184
- } else if (currentChild.type === 'html') {
1185
- // 跳过媒体标签的结束标签
1186
- if (currentChild.value.match(/^<\/(img|video|iframe)>/)) {
1187
- continue;
1188
- }
1189
- var mediaElement = findImageElement(currentChild.value);
1190
- if (mediaElement) {
1191
- var node = createMediaNodeFromElement(mediaElement);
1192
- if (node) {
1193
- elements.push(node);
1194
- }
1195
- } else {
1196
- textNodes.push({
1197
- type: 'html',
1198
- value: currentChild.value
1199
- });
1200
- }
1201
- } else {
1202
- textNodes.push(currentChild);
1203
- }
1204
- }
1205
- } catch (err) {
1206
- _didIteratorError = true;
1207
- _iteratorError = err;
1208
- } finally{
1209
- try {
1210
- if (!_iteratorNormalCompletion && _iterator.return != null) {
1211
- _iterator.return();
1212
- }
1213
- } finally{
1214
- if (_didIteratorError) {
1215
- throw _iteratorError;
1216
- }
1217
- }
1218
- }
1219
- // 处理剩余的文本节点
1220
- if (textNodes.length) {
1221
- elements.push({
1222
- type: 'paragraph',
1223
- children: parseNodes(textNodes, plugins, false, currentElement, undefined)
1224
- });
1225
- }
1226
- return elements;
1227
- };
1228
- /**
1229
- * 处理段落节点
1230
- * @param currentElement - 当前处理的段落元素
1231
- * @param config - 配置对象,包含样式和行为设置
1232
- * @param plugins - 插件数组
1233
- * @returns 返回格式化的段落节点对象或元素数组
1234
- */ var handleParagraph = function(currentElement, config, plugins, parserConfig) {
1235
- var _currentElement_children, _currentElement_children_at, _currentElement_children1;
1236
- // 检查是否是附件链接
1237
- if (((_currentElement_children = currentElement.children) === null || _currentElement_children === void 0 ? void 0 : _currentElement_children[0].type) === 'html' && currentElement.children[0].value.startsWith('<a')) {
1238
- var attachNode = handleAttachmentLink(currentElement);
1239
- if (attachNode) return attachNode;
1240
- }
1241
- // 检查是否是链接卡片
1242
- if ((currentElement === null || currentElement === void 0 ? void 0 : (_currentElement_children1 = currentElement.children) === null || _currentElement_children1 === void 0 ? void 0 : (_currentElement_children_at = _currentElement_children1.at(0)) === null || _currentElement_children_at === void 0 ? void 0 : _currentElement_children_at.type) === 'link' && config.type === 'card') {
1243
- return handleLinkCard(currentElement, config);
1244
- }
1245
- // 处理混合内容段落
1246
- return processParagraphChildren(currentElement, plugins, parserConfig);
1247
- };
1248
- /**
1249
- * 处理内联代码节点
1250
- * @param currentElement - 当前处理的内联代码元素
1251
- * @returns 返回格式化的内联代码节点对象,支持占位符和初始值
1252
- */ var handleInlineCode = function(currentElement) {
1253
- var _currentElement_value, _currentElement_value1;
1254
- var hasPlaceHolder = (_currentElement_value = currentElement.value) === null || _currentElement_value === void 0 ? void 0 : _currentElement_value.match(/\$\{(.*?)\}/);
1255
- var values = hasPlaceHolder ? hasPlaceHolder[1].split(';').map(function(item) {
1256
- var values = item === null || item === void 0 ? void 0 : item.split(':');
1257
- return _define_property({}, (values === null || values === void 0 ? void 0 : values.at(0)) || '', values === null || values === void 0 ? void 0 : values.at(1));
1258
- }).reduce(function(acc, item) {
1259
- return _object_spread({}, acc, item);
1260
- }, {}) : undefined;
1261
- return {
1262
- text: values ? (values === null || values === void 0 ? void 0 : values.initialValue) || ' ' : currentElement.value,
1263
- tag: (_currentElement_value1 = currentElement.value) === null || _currentElement_value1 === void 0 ? void 0 : _currentElement_value1.startsWith('${'),
1264
- placeholder: (values === null || values === void 0 ? void 0 : values.placeholder) || undefined,
1265
- initialValue: (values === null || values === void 0 ? void 0 : values.initialValue) || undefined,
1266
- code: true
1267
- };
1268
- };
1269
- /**
1270
- * 处理分割线节点
1271
- * @returns 返回格式化的分割线节点对象
1272
- */ var handleThematicBreak = function() {
1273
- return {
1274
- type: 'hr',
1275
- children: [
1276
- {
1277
- text: ''
1278
- }
1279
- ]
1280
- };
1281
- };
1282
- /**
1283
- * 处理代码块节点
1284
- * @param currentElement - 当前处理的代码块元素,包含语言和内容
1285
- * @returns 返回格式化的代码块节点对象,根据语言类型进行特殊处理
1286
- */ var handleCode = function(currentElement) {
1287
- var _match, _currentElement_otherProps, _currentElement_value_trim;
1288
- var rawValue = currentElement.value || '';
1289
- var langString = ((_match = (currentElement.lang || '').match(NOT_SPACE_START)) === null || _match === void 0 ? void 0 : _match[0]) || '';
1290
- var code = "".concat(rawValue.replace(ENDING_NEWLINE, ''), "\n");
1291
- // 检查代码块是否完整
1292
- // 如果是缩进代码块,认为是完整的(因为没有结束标记)
1293
- var isIndentedCode = currentElement.meta === 'indented';
1294
- // 使用更智能的方法判断代码块是否完整
1295
- var streamStatus = 'loading';
1296
- if (isIndentedCode) {
1297
- // 缩进代码块没有结束标记,认为是完整的
1298
- streamStatus = 'done';
1299
- } else {
1300
- // 对于围栏代码块,使用多种方法判断
1301
- var endsWithNewline = code.match(ENDING_NEWLINE);
1302
- // 如果代码以换行结尾,可能是完整的
1303
- if (endsWithNewline) {
1304
- // 进一步检查代码内容是否完整(特别是对于 Mermaid 等需要完整语法的情况)
1305
- var isLikelyComplete = isCodeBlockLikelyComplete(rawValue, currentElement.lang);
1306
- streamStatus = isLikelyComplete ? 'done' : 'loading';
1307
- } else {
1308
- // 没有换行结尾,肯定不完整
1309
- streamStatus = 'loading';
1310
- }
1311
- }
1312
- // 如果已经在 parseNodes 中设置了 finish(基于是否是最后一个节点),优先使用它
1313
- // 否则使用 streamStatus 判断
1314
- var finishValue = ((_currentElement_otherProps = currentElement.otherProps) === null || _currentElement_otherProps === void 0 ? void 0 : _currentElement_otherProps.finish) !== undefined ? currentElement.otherProps.finish : streamStatus === 'done';
1315
- var baseCodeElement = {
1316
- type: 'code',
1317
- language: currentElement.lang === 'apaasify' ? 'apaasify' : currentElement.lang,
1318
- render: currentElement.meta === 'render',
1319
- value: currentElement.value,
1320
- isConfig: currentElement === null || currentElement === void 0 ? void 0 : (_currentElement_value_trim = currentElement.value.trim()) === null || _currentElement_value_trim === void 0 ? void 0 : _currentElement_value_trim.startsWith('<!--'),
1321
- children: [
1322
- {
1323
- text: currentElement.value
1324
- }
1325
- ],
1326
- // 添加流式状态支持
1327
- otherProps: _object_spread(_object_spread_props(_object_spread({}, currentElement.otherProps || {}), {
1328
- 'data-block': 'true',
1329
- 'data-state': streamStatus,
1330
- // 优先使用 parseNodes 中设置的 finish,否则使用 streamStatus 判断
1331
- finish: finishValue
1332
- }), langString ? {
1333
- 'data-language': langString
1334
- } : {})
1335
- };
1336
- var handler = LANGUAGE_HANDLERS[currentElement.lang];
1337
- var result = handler ? handler(baseCodeElement, currentElement.value) : baseCodeElement;
1338
- // 确保 otherProps 被保留
1339
- var resultWithProps = result;
1340
- if (baseCodeElement.otherProps && !resultWithProps.otherProps) {
1341
- resultWithProps.otherProps = baseCodeElement.otherProps;
1342
- } else if (baseCodeElement.otherProps && resultWithProps.otherProps) {
1343
- resultWithProps.otherProps = _object_spread({}, resultWithProps.otherProps, baseCodeElement.otherProps);
1344
- }
1345
- return resultWithProps;
1346
- };
1347
- /**
1348
- * 处理YAML节点
1349
- * @param currentElement - 当前处理的YAML元素
1350
- * @returns 返回格式化的YAML代码块节点对象
1351
- */ var handleYaml = function(currentElement) {
1352
- return {
1353
- type: 'code',
1354
- language: 'yaml',
1355
- value: currentElement.value,
1356
- frontmatter: true,
1357
- children: [
1358
- {
1359
- text: currentElement.value
1360
- }
1361
- ]
1362
- };
1363
- };
1364
- /**
1365
- * 处理引用块节点
1366
- * @param currentElement - 当前处理的引用块元素
1367
- * @returns 返回格式化的引用块节点对象
1368
- */ var handleBlockquote = function(currentElement, plugins, parserConfig) {
1369
- var _currentElement_children;
1370
- return {
1371
- type: 'blockquote',
1372
- children: ((_currentElement_children = currentElement.children) === null || _currentElement_children === void 0 ? void 0 : _currentElement_children.length) ? parseNodes(currentElement.children, plugins, false, currentElement, parserConfig) : [
1373
- {
1374
- type: 'paragraph',
1375
- children: [
1376
- {
1377
- text: ''
1378
- }
1379
- ]
1380
- }
1381
- ]
1382
- };
1383
- };
1384
- /**
1385
- * 处理定义节点
1386
- * @param currentElement - 当前处理的定义元素,包含标签和URL
1387
- * @returns 返回格式化的定义段落节点对象
1388
- */ var handleDefinition = function(currentElement) {
1389
- return {
1390
- type: 'paragraph',
1391
- children: [
1392
- {
1393
- text: "[".concat(currentElement.label, "]: ") + (currentElement.url ? "".concat(currentElement.url) : '')
1394
- }
1395
- ]
1396
- };
1397
- };
1398
- /**
1399
- * 应用HTML标签样式到元素
1400
- * @param el - 目标元素对象
1401
- * @param htmlTag - HTML标签数组,包含样式信息
1402
- */ var applyHtmlTagsToElement = function(el, htmlTag) {
1403
- var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
1404
- try {
1405
- for(var _iterator = htmlTag[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
1406
- var t = _step.value;
1407
- if (t.tag === 'font') {
1408
- el.color = t.color;
1409
- }
1410
- if (t.tag === 'sup') el.identifier = el.text;
1411
- if (t.tag === 'sub') el.identifier = el.text;
1412
- if (t.tag === 'code') el.code = true;
1413
- if (t.tag === 'i') el.italic = true;
1414
- if (t.tag === 'b' || t.tag === 'strong') el.bold = true;
1415
- if (t.tag === 'del') el.strikethrough = true;
1416
- if ((t.tag === 'span' || t.tag === 'font') && t.color) el.highColor = t.color;
1417
- if (t.tag === 'a' && (t === null || t === void 0 ? void 0 : t.url)) {
1418
- el.url = t === null || t === void 0 ? void 0 : t.url;
1419
- }
1420
- }
1421
- } catch (err) {
1422
- _didIteratorError = true;
1423
- _iteratorError = err;
1424
- } finally{
1425
- try {
1426
- if (!_iteratorNormalCompletion && _iterator.return != null) {
1427
- _iterator.return();
1428
- }
1429
- } finally{
1430
- if (_didIteratorError) {
1431
- throw _iteratorError;
1432
- }
1433
- }
1434
- }
1435
- };
1436
- /**
1437
- * 应用上下文属性和配置到元素
1438
- * @param el - 目标元素或元素数组
1439
- * @param contextProps - 上下文属性对象
1440
- * @param config - 配置对象
1441
- * @returns 返回应用了属性和配置的元素
1442
- */ var applyContextPropsAndConfig = function(el, contextProps, config) {
1443
- if (Array.isArray(el)) {
1444
- return el.map(function(item) {
1445
- if (Object.keys(contextProps || {}).length) {
1446
- item.contextProps = contextProps;
1447
- }
1448
- if (Object.keys(config || {}).length && !item.otherProps) {
1449
- item.otherProps = config;
1450
- }
1451
- return item;
1452
- });
1453
- } else {
1454
- if (Object.keys(contextProps || {}).length) {
1455
- el.contextProps = contextProps;
1456
- }
1457
- if (Object.keys(config || {}).length && !el.otherProps) {
1458
- el.otherProps = config;
1459
- }
1460
- return el;
1461
- }
1462
- };
1463
- /**
1464
- * 根据行间距添加空行元素
1465
- * @param els - 目标元素数组
1466
- * @param preNode - 前一个节点
1467
- * @param currentElement - 当前处理的元素
1468
- * @param top - 是否为顶级解析
1469
- */ var addEmptyLinesIfNeeded = function(els, preNode, currentElement, top) {
1470
- if (preNode && top) {
1471
- var _currentElement_position, _preNode_position;
1472
- var distance = (((_currentElement_position = currentElement.position) === null || _currentElement_position === void 0 ? void 0 : _currentElement_position.start.line) || 0) - (((_preNode_position = preNode.position) === null || _preNode_position === void 0 ? void 0 : _preNode_position.end.line) || 0);
1473
- if (distance >= EMPTY_LINE_DISTANCE_THRESHOLD) {
1474
- var lines = Math.floor((distance - EMPTY_LINE_CALCULATION_OFFSET) / EMPTY_LINE_DIVISOR);
1475
- Array.from(new Array(lines)).forEach(function() {
1476
- els.push({
1477
- type: 'paragraph',
1478
- children: [
1479
- {
1480
- text: ''
1481
- }
1482
- ]
1483
- });
1484
- });
1485
- }
1486
- }
1487
- };
1488
- /**
1489
- * 元素处理器映射表
1490
- * 注意:这些处理器需要通过 handleSingleElement 传递 parserConfig
1491
- */ var elementHandlers = {
1492
- heading: {
1493
- handler: function(el, plugins) {
1494
- return handleHeading(el, plugins);
1495
- }
1496
- },
1497
- html: {
1498
- handler: function() {
1499
- return null;
1500
- },
1501
- needsHtmlResult: true
1502
- },
1503
- image: {
1504
- handler: function(el) {
1505
- return handleImage(el);
1506
- }
1507
- },
1508
- inlineMath: {
1509
- handler: function(el) {
1510
- return handleInlineMath(el);
1511
- }
1512
- },
1513
- math: {
1514
- handler: function(el) {
1515
- return handleMath(el);
1516
- }
1517
- },
1518
- list: {
1519
- handler: function(el, plugins) {
1520
- return handleList(el, plugins);
1521
- }
1522
- },
1523
- footnoteReference: {
1524
- handler: function(el) {
1525
- return handleFootnoteReference(el);
1526
- }
1527
- },
1528
- footnoteDefinition: {
1529
- handler: function(el, plugins) {
1530
- return handleFootnoteDefinition(el, plugins);
1531
- }
1532
- },
1533
- listItem: {
1534
- handler: function(el, plugins) {
1535
- return handleListItem(el, plugins);
1536
- }
1537
- },
1538
- paragraph: {
1539
- handler: function(el, plugins, config) {
1540
- return handleParagraph(el, config, plugins);
1541
- }
1542
- },
1543
- inlineCode: {
1544
- handler: function(el) {
1545
- return handleInlineCode(el);
1546
- }
1547
- },
1548
- thematicBreak: {
1549
- handler: function() {
1550
- return handleThematicBreak();
1551
- }
1552
- },
1553
- code: {
1554
- handler: function(el) {
1555
- return handleCode(el);
1556
- }
1557
- },
1558
- yaml: {
1559
- handler: function(el) {
1560
- return handleYaml(el);
1561
- }
1562
- },
1563
- blockquote: {
1564
- handler: function(el, plugins) {
1565
- return handleBlockquote(el, plugins);
1566
- }
1567
- },
1568
- table: {
1569
- handler: function(el, plugins, config, parent, htmlTag, preElement) {
1570
- return parseTableOrChart(el, preElement, plugins);
1571
- }
1572
- },
1573
- definition: {
1574
- handler: function(el) {
1575
- return handleDefinition(el);
1576
- }
1577
- }
1578
- };
1579
- /**
1580
- * 解析 Markdown AST 节点为 Slate 节点(独立函数版本,用于向后兼容)
1581
- * 内部使用 MarkdownToSlateParser 类来处理
1582
- *
1583
- * @deprecated 建议直接使用 MarkdownToSlateParser 类
1584
- */ var parseNodes = function(nodes, plugins) {
1585
- var top = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : false, parent = arguments.length > 3 ? arguments[3] : void 0, parserConfig = arguments.length > 4 ? arguments[4] : void 0;
1586
- // 创建一个临时 parser 实例来处理
1587
- // 注意:这不是最优的方式,但为了向后兼容保留
1588
- // 使用类型断言访问私有方法
1589
- var parser = new MarkdownToSlateParser(parserConfig || {}, plugins || []);
1590
- return parser.parseNodes(nodes, top, parent);
1591
- };
1592
- var tableRegex = /^\|.*\|\s*\n\|[-:| ]+\|/m;
1593
- /**
1594
- * 标准 HTML 元素列表
1595
- * 这些标签会被正常解析为 HTML,其他标签会被当作普通文本处理
1596
- */ var STANDARD_HTML_ELEMENTS = new Set([
1597
- // 文档结构
1598
- 'html',
1599
- 'head',
1600
- 'body',
1601
- 'title',
1602
- 'meta',
1603
- 'link',
1604
- 'style',
1605
- 'script',
1606
- // 内容分区
1607
- 'header',
1608
- 'nav',
1609
- 'main',
1610
- 'section',
1611
- 'article',
1612
- 'aside',
1613
- 'footer',
1614
- 'h1',
1615
- 'h2',
1616
- 'h3',
1617
- 'h4',
1618
- 'h5',
1619
- 'h6',
1620
- // 文本内容
1621
- 'div',
1622
- 'p',
1623
- 'hr',
1624
- 'pre',
1625
- 'blockquote',
1626
- // 列表
1627
- 'ul',
1628
- 'ol',
1629
- 'li',
1630
- 'dl',
1631
- 'dt',
1632
- 'dd',
1633
- // 表格
1634
- 'table',
1635
- 'thead',
1636
- 'tbody',
1637
- 'tfoot',
1638
- 'tr',
1639
- 'th',
1640
- 'td',
1641
- 'caption',
1642
- 'colgroup',
1643
- 'col',
1644
- // 表单
1645
- 'form',
1646
- 'input',
1647
- 'textarea',
1648
- 'button',
1649
- 'select',
1650
- 'option',
1651
- 'label',
1652
- 'fieldset',
1653
- 'legend',
1654
- // 内联文本语义
1655
- 'a',
1656
- 'em',
1657
- 'strong',
1658
- 'small',
1659
- 'mark',
1660
- 'del',
1661
- 'ins',
1662
- 'sub',
1663
- 'sup',
1664
- 'i',
1665
- 'b',
1666
- 'u',
1667
- 's',
1668
- 'code',
1669
- 'kbd',
1670
- 'samp',
1671
- 'var',
1672
- 'span',
1673
- 'br',
1674
- 'wbr',
1675
- // 图片和多媒体
1676
- 'img',
1677
- 'video',
1678
- 'audio',
1679
- 'source',
1680
- 'track',
1681
- 'iframe',
1682
- 'embed',
1683
- 'object',
1684
- 'param',
1685
- 'picture',
1686
- // 其他
1687
- 'canvas',
1688
- 'svg',
1689
- 'math',
1690
- 'details',
1691
- 'summary',
1692
- 'dialog',
1693
- 'menu',
1694
- 'menuitem',
1695
- // 字体
1696
- 'font',
1697
- // 自定义标签(用于流式渲染)
1698
- 'incomplete-image'
1699
- ]);
1700
- /**
1701
- * 检查 HTML 标签是否为标准元素
1702
- * @param htmlString - HTML 字符串
1703
- * @returns 是否为标准 HTML 元素
1704
- */ function isStandardHtmlElement(htmlString) {
1705
- // 提取标签名(支持开始标签和结束标签)
1706
- var tagMatch = htmlString.match(/<\/?(\w+)/);
1707
- if (!tagMatch) return false;
1708
- var tagName = tagMatch[1].toLowerCase();
1709
- return STANDARD_HTML_ELEMENTS.has(tagName);
1710
- }
1711
- /**
1712
- * 预处理特殊标签(think/answer),将其转换为代码块格式
1713
- * @param markdown - 原始 Markdown 字符串
1714
- * @param tagName - 标签名称(think 或 answer)
1715
- * @returns 处理后的 Markdown 字符串
1716
- */ function preprocessSpecialTags(markdown, tagName) {
1717
- var tagRegex = new RegExp("<".concat(tagName, ">([\\s\\S]*?)<\\/").concat(tagName, ">"), 'g');
1718
- return markdown === null || markdown === void 0 ? void 0 : markdown.replace(tagRegex, function(match, content) {
1719
- var trimmedContent = content.trim();
1720
- // 如果内容中包含代码块标记(三个反引号),需要进行转义
1721
- // 策略:使用特殊标记替换代码块,保持原始格式
1722
- var processedContent = trimmedContent === null || trimmedContent === void 0 ? void 0 : trimmedContent.replace(/```(\w*)\n?([\s\S]*?)```/g, function(_, lang, code) {
1723
- // 使用特殊标记包裹,保留语言和代码内容
1724
- // 格式:【CODE_BLOCK:lang】code【/CODE_BLOCK】
1725
- var marker = '\u200B'; // 零宽空格,用于标记
1726
- return "".concat(marker, "【CODE_BLOCK:").concat(lang || '', "】\n").concat(code, "\n【/CODE_BLOCK】").concat(marker);
1727
- });
1728
- // 构建对应类型的代码块
1729
- return "```".concat(tagName, "\n").concat(processedContent, "\n```");
1730
- });
1731
- }
1732
- /**
1733
- * 预处理 <think> 标签,将其转换为 ```think 代码块格式
1734
- * @param markdown - 原始 Markdown 字符串
1735
- * @returns 处理后的 Markdown 字符串
1736
- */ function preprocessThinkTags(markdown) {
1737
- return preprocessSpecialTags(markdown, 'think');
1738
- }
1739
- /**
1740
- * 检测不完整的图片标记
1741
- * 参考 useStreaming 中的逻辑,检测类似 ![ 或 ![text]( 的不完整图片语法
1742
- * @param markdown - 要检测的 Markdown 字符串
1743
- * @returns 是否为不完整的图片标记
1744
- */ function isIncompleteImage(markdown) {
1745
- // 匹配不完整的图片语法:
1746
- // 1. ![ 开始但还没有 ]
1747
- // 2. ![text] 开始但还没有 (
1748
- // 3. ![text]( 开始但还没有 )
1749
- var incompleteImagePatterns = [
1750
- /^!\[[^\]\r\n]{0,1000}$/,
1751
- /^!\[[^\r\n]{0,1000}\]\(*[^)\r\n]{0,1000}$/
1752
- ];
1753
- return incompleteImagePatterns.some(function(pattern) {
1754
- return pattern.test(markdown);
1755
- });
1756
- }
1757
- /**
1758
- * 预处理不完整的图片标记,将其转换为特殊的 HTML 标签
1759
- * @param markdown - 原始 Markdown 字符串
1760
- * @returns 处理后的 Markdown 字符串
1761
- */ function preprocessIncompleteImages(markdown) {
1762
- if (!markdown) return markdown;
1763
- // 按行处理,避免在代码块中处理
1764
- var lines = markdown.split('\n');
1765
- var inCodeBlock = false;
1766
- var codeBlockFence = '';
1767
- var codeBlockFenceLen = 0;
1768
- var processedLines = lines.map(function(line) {
1769
- // 检测代码块开始/结束
1770
- var fenceMatch = line.match(FENCED_CODE_REGEX);
1771
- if (fenceMatch) {
1772
- var currentFence = fenceMatch[1];
1773
- var char = currentFence[0];
1774
- var len = currentFence.length;
1775
- if (!inCodeBlock) {
1776
- inCodeBlock = true;
1777
- codeBlockFence = char;
1778
- codeBlockFenceLen = len;
1779
- } else if (char === codeBlockFence && len >= codeBlockFenceLen) {
1780
- inCodeBlock = false;
1781
- codeBlockFence = '';
1782
- codeBlockFenceLen = 0;
1783
- }
1784
- return line;
1785
- }
1786
- // 如果在代码块中,不处理
1787
- if (inCodeBlock) {
1788
- return line;
1789
- }
1790
- // 检测不完整的图片标记
1791
- // 匹配行尾的不完整图片语法(避免匹配完整的图片)
1792
- var trimmedLine = line.trim();
1793
- // 如果整行就是一个不完整的图片标记
1794
- if (isIncompleteImage(trimmedLine)) {
1795
- var encodedRaw = encodeURIComponent(trimmedLine);
1796
- return '<incomplete-image data-raw="'.concat(encodedRaw, '" />');
1797
- }
1798
- // 检测行内不完整的图片标记(在行尾)
1799
- // 使用负向前瞻确保不是完整图片的一部分
1800
- return line.replace(/(!\[[^\]]*\]?\(?[^)]*)$/, function(match) {
1801
- // 检查是否是不完整的图片
1802
- if (isIncompleteImage(match.trim())) {
1803
- var encodedRaw = encodeURIComponent(match.trim());
1804
- return '<incomplete-image data-raw="'.concat(encodedRaw, '" />');
1805
- }
1806
- return match;
1807
- });
1808
- });
1809
- return processedLines.join('\n');
1810
- }
1811
- /**
1812
- * 预处理所有非标准 HTML 标签,提取其内容(删除标签本身)
1813
- * @param markdown - 原始 Markdown 字符串
1814
- * @returns 处理后的 Markdown 字符串
1815
- */ function preprocessNonStandardHtmlTags(markdown) {
1816
- var result = markdown;
1817
- var hasNonStandardTags = true;
1818
- // 循环处理,直到没有非标准标签(处理嵌套情况)
1819
- while(hasNonStandardTags){
1820
- var before = result;
1821
- // 匹配所有 HTML 标签对:<tagname>content</tagname>
1822
- // 注意:跳过 incomplete-image 标签(它是自闭合标签,需要特殊处理)
1823
- result = result.replace(/<(\w+)>([\s\S]*?)<\/\1>/g, function(match, tagName, content) {
1824
- // 保护 incomplete-image 标签(虽然它是自闭合的,但为了安全起见)
1825
- if (tagName.toLowerCase() === 'incomplete-image') {
1826
- return match;
1827
- }
1828
- // 检查是否为标准 HTML 元素
1829
- if (STANDARD_HTML_ELEMENTS.has(tagName.toLowerCase())) {
1830
- // 标准元素保持不变
1831
- return match;
1832
- }
1833
- // 非标准元素只保留内容(不 trim,保持原始格式)
1834
- return content;
1835
- });
1836
- // 如果没有变化,说明处理完成
1837
- hasNonStandardTags = before !== result;
1838
- }
1839
- return result;
1840
- }
1841
- function preprocessMarkdownTableNewlines(markdown) {
1842
- // 检查是否包含表格
1843
- if (!tableRegex.test(markdown)) return markdown; // 如果没有表格,直接返回原始字符串
1844
- // 处理表格结尾的换行符:
1845
- // 1. 如果只有一个换行符,改成两个
1846
- // 2. 如果有两个以上换行符,改成两个
1847
- // 3. 如果已经是两个换行符,保持不变
1848
- var processedMarkdown = markdown.replace(/(\|[^|\n]*\|)\n(?!\n|\|)/g, '$1\n\n').replace(/(\|[^|\n]*\|)\n{3,}(?!\|)/g, '$1\n\n');
1849
- // 如果包含表格,处理换行符
1850
- return processedMarkdown === null || processedMarkdown === void 0 ? void 0 : processedMarkdown.split('\n\n').map(function(line) {
1851
- if (line.includes('```')) return line; // 如果包含代码块,直接返回原始字符串
1852
- // 检查是否包含表格
1853
- if (!tableRegex.test(line)) return line; // 如果没有表格,直接返回原始字符串
1854
- // 匹配所有表格的行(确保我们在表格行内匹配换行符)
1855
- return line.replace(/\|([^|]+)\|/g, function(match) {
1856
- var _match_replaceAll;
1857
- if (((_match_replaceAll = match.replaceAll('\n', '')) === null || _match_replaceAll === void 0 ? void 0 : _match_replaceAll.length) < MIN_TABLE_CELL_LENGTH) return match;
1858
- // 只替换每个表格单元格内的换行符
1859
- return match.split('\n').join('<br>');
1860
- });
1861
- }).join('\n\n');
1862
- }
1863
84
  /**
1864
85
  * Markdown 到 Slate 节点解析器类
1865
86
  *
@@ -1884,11 +105,12 @@ function preprocessMarkdownTableNewlines(markdown) {
1884
105
  * @returns 一个包含解析后的元素数组和链接信息的对象
1885
106
  */ key: "parse",
1886
107
  value: function parse(md) {
1887
- // 先预处理 <think> 标签,再预处理不完整的图片,然后预处理其他非标准 HTML 标签,最后处理表格换行
108
+ // 先预处理 <think> 标签,然后预处理其他非标准 HTML 标签,最后处理表格换行
1888
109
  var thinkProcessed = preprocessThinkTags(md || '');
1889
- var incompleteImageProcessed = preprocessIncompleteImages(thinkProcessed);
1890
- var nonStandardProcessed = preprocessNonStandardHtmlTags(incompleteImageProcessed);
1891
- var processedMarkdown = mdastParser.parse(preprocessMarkdownTableNewlines(nonStandardProcessed));
110
+ var nonStandardProcessed = preprocessNonStandardHtmlTags(thinkProcessed);
111
+ // parse() 只执行 parser,需要 runSync() 来执行 transformer 插件
112
+ var ast = mdastParser.parse(preprocessMarkdownTableNewlines(nonStandardProcessed));
113
+ var processedMarkdown = mdastParser.runSync(ast);
1892
114
  var markdownRoot = processedMarkdown.children;
1893
115
  // 使用类的配置和插件,通过 this 访问
1894
116
  var schema = this.parseNodes(markdownRoot, true, undefined);
@@ -1958,49 +180,28 @@ function preprocessMarkdownTableNewlines(markdown) {
1958
180
  }
1959
181
  // 如果插件没有处理,使用默认处理逻辑
1960
182
  if (!pluginHandled) {
1961
- var isLastNode = i === nodes.length - 1;
1962
- // 如果是 table 节点,检查是否是最后一个节点,设置 finish 属性
1963
- if (currentElement.type === 'table') {
1964
- // 如果 table 不是最后一个节点,finish 设置为 true
1965
- if (!isLastNode) {
1966
- if (!currentElement.otherProps) {
1967
- currentElement.otherProps = {};
1968
- }
1969
- currentElement.otherProps.finish = true;
1970
- } else {
1971
- // 如果是最后一个节点,且 typing=false,finish 设置为 true
1972
- if (_this.config.typing === false) {
1973
- if (!currentElement.otherProps) {
1974
- currentElement.otherProps = {};
1975
- }
1976
- currentElement.otherProps.finish = true;
1977
- }
1978
- // 否则保持原逻辑(在 parseTableOrChart 中处理,默认为 false)
1979
- }
1980
- }
1981
- // 如果是 code 节点,检查是否是最后一个节点,设置 finish 属性
1982
- if (currentElement.type === 'code') {
1983
- // 如果 code 不是最后一个节点,finish 设置为 true
1984
- if (!isLastNode) {
1985
- if (!currentElement.otherProps) {
1986
- currentElement.otherProps = {};
1987
- }
1988
- currentElement.otherProps.finish = true;
1989
- }
1990
- // 如果是最后一个节点,保持原逻辑(在 handleCode 中处理)
1991
- }
1992
183
  // 使用统一的处理函数,通过 this 访问配置和插件
1993
184
  var result = _this.handleSingleElement(currentElement, config, parent, htmlTag, preElement);
1994
185
  el = result.el;
1995
186
  if (result.contextProps) {
1996
187
  contextProps = _object_spread({}, contextProps, result.contextProps);
1997
188
  }
189
+ // 更新 htmlTag 数组,以便后续节点可以使用
190
+ if (result.htmlTag) {
191
+ htmlTag = result.htmlTag;
192
+ }
1998
193
  }
1999
- addEmptyLinesIfNeeded(els, preNode, currentElement, top);
194
+ els = addEmptyLinesIfNeeded(els, preNode, currentElement, top);
2000
195
  if (el) {
2001
- var _els;
2002
196
  el = applyContextPropsAndConfig(el, contextProps, config);
2003
- Array.isArray(el) ? (_els = els).push.apply(_els, _to_consumable_array(el)) : els.push(el);
197
+ // 使用 if-else 而不是条件表达式,避免类型错误
198
+ if (Array.isArray(el)) {
199
+ els = _to_consumable_array(els).concat(_to_consumable_array(el));
200
+ } else {
201
+ els = _to_consumable_array(els).concat([
202
+ el
203
+ ]);
204
+ }
2004
205
  }
2005
206
  preNode = currentElement;
2006
207
  preElement = el;
@@ -2026,482 +227,140 @@ function preprocessMarkdownTableNewlines(markdown) {
2026
227
  }
2027
228
  },
2028
229
  {
2029
- key: "handleSingleElement",
230
+ key: "getElementHandlers",
2030
231
  value: /**
2031
232
  * 处理单个元素(类方法版本)
2032
- */ function handleSingleElement(currentElement, config, parent, htmlTag, preElement) {
2033
- var elementType = currentElement.type;
2034
- var handlerInfo = elementHandlers[elementType];
2035
- // 特殊处理 html 类型
2036
- if (handlerInfo === null || handlerInfo === void 0 ? void 0 : handlerInfo.needsHtmlResult) {
2037
- var htmlResult = handleHtml(currentElement, parent, htmlTag);
2038
- return {
2039
- el: htmlResult.el,
2040
- contextProps: htmlResult.contextProps
2041
- };
2042
- }
2043
- // 使用处理器映射表
2044
- // 对于需要 parserConfig 的处理器,直接调用而不是通过 handler
2045
- if (handlerInfo) {
2046
- var handlerResult;
2047
- // 特殊处理需要 parserConfig 的处理器,使用 this.config 和 this.plugins
2048
- if (elementType === 'heading') {
2049
- handlerResult = this.handleHeading(currentElement);
2050
- } else if (elementType === 'list') {
2051
- handlerResult = this.handleList(currentElement);
2052
- } else if (elementType === 'listItem') {
2053
- handlerResult = this.handleListItem(currentElement);
2054
- } else if (elementType === 'blockquote') {
2055
- handlerResult = this.handleBlockquote(currentElement);
2056
- } else if (elementType === 'footnoteDefinition') {
2057
- handlerResult = this.handleFootnoteDefinition(currentElement);
2058
- } else if (elementType === 'paragraph') {
2059
- handlerResult = this.handleParagraph(currentElement, config);
2060
- } else if (elementType === 'table') {
2061
- handlerResult = this.parseTableOrChart(currentElement, preElement || parent);
2062
- } else {
2063
- // 使用默认的 handler 调用
2064
- handlerResult = handlerInfo.handler(currentElement, this.plugins, config, parent, htmlTag, preElement);
2065
- }
2066
- return {
2067
- el: handlerResult
2068
- };
2069
- }
2070
- // 默认处理
2071
- return {
2072
- el: this.handleTextAndInlineElements(currentElement, htmlTag)
2073
- };
2074
- }
2075
- },
2076
- {
2077
- key: "handleHeading",
2078
- value: /**
2079
- * 处理标题节点(类方法版本)
2080
- */ function handleHeading(currentElement) {
2081
- var _currentElement_children;
2082
- return {
2083
- type: 'head',
2084
- level: currentElement.depth,
2085
- children: ((_currentElement_children = currentElement.children) === null || _currentElement_children === void 0 ? void 0 : _currentElement_children.length) ? this.parseNodes(currentElement.children, false, currentElement) : [
2086
- {
2087
- text: ''
2088
- }
2089
- ]
2090
- };
2091
- }
2092
- },
2093
- {
2094
- key: "handleList",
2095
- value: /**
2096
- * 处理列表节点(类方法版本)
2097
- */ function handleList(currentElement) {
2098
- var _el_children;
2099
- var el = {
2100
- type: 'list',
2101
- order: currentElement.ordered,
2102
- start: currentElement.start,
2103
- children: this.parseNodes(currentElement.children, false, currentElement)
233
+ */ /**
234
+ * 获取统一的元素处理器映射表
235
+ */ function getElementHandlers() {
236
+ var _this = this;
237
+ var parseNodesFn = function(nodes, top, parent) {
238
+ return _this.parseNodes(nodes, top, parent);
2104
239
  };
2105
- el.task = (_el_children = el.children) === null || _el_children === void 0 ? void 0 : _el_children.some(function(s) {
2106
- return typeof s.checked === 'boolean';
2107
- });
2108
- return el;
2109
- }
2110
- },
2111
- {
2112
- key: "handleFootnoteDefinition",
2113
- value: /**
2114
- * 处理脚注定义节点(类方法版本)
2115
- */ function handleFootnoteDefinition(currentElement) {
2116
- var _this_parseNodes, _linkNode_children;
2117
- var linkNode = (_this_parseNodes = this.parseNodes(currentElement.children, false, currentElement)) === null || _this_parseNodes === void 0 ? void 0 : _this_parseNodes.at(0);
2118
- var cellNode = linkNode === null || linkNode === void 0 ? void 0 : (_linkNode_children = linkNode.children) === null || _linkNode_children === void 0 ? void 0 : _linkNode_children.at(0);
2119
- return {
2120
- value: cellNode === null || cellNode === void 0 ? void 0 : cellNode.text,
2121
- url: cellNode === null || cellNode === void 0 ? void 0 : cellNode.url,
2122
- type: 'footnoteDefinition',
2123
- identifier: currentElement.identifier,
2124
- children: [
2125
- cellNode
2126
- ]
240
+ var parseNodesForTable = function(nodes, _plugins, top, parent) {
241
+ return _this.parseNodes(nodes, top || false, parent);
2127
242
  };
2128
- }
2129
- },
2130
- {
2131
- key: "handleListItem",
2132
- value: /**
2133
- * 处理列表项节点(类方法版本)
2134
- */ function handleListItem(currentElement) {
2135
- var _currentElement_children, _currentElement_children__children_, _currentElement_children__children, _currentElement_children_, _currentElement_children1, _currentElement_children__children1, _currentElement_children_1, _currentElement_children2;
2136
- var children = ((_currentElement_children = currentElement.children) === null || _currentElement_children === void 0 ? void 0 : _currentElement_children.length) ? this.parseNodes(currentElement.children, false, currentElement) : [
2137
- {
2138
- type: 'paragraph',
2139
- children: [
2140
- {
2141
- text: ''
2142
- }
2143
- ]
2144
- }
2145
- ];
2146
- var mentions = undefined;
2147
- if (((_currentElement_children1 = currentElement.children) === null || _currentElement_children1 === void 0 ? void 0 : (_currentElement_children_ = _currentElement_children1[0]) === null || _currentElement_children_ === void 0 ? void 0 : (_currentElement_children__children = _currentElement_children_.children) === null || _currentElement_children__children === void 0 ? void 0 : (_currentElement_children__children_ = _currentElement_children__children[0]) === null || _currentElement_children__children_ === void 0 ? void 0 : _currentElement_children__children_.type) === 'link' && ((_currentElement_children2 = currentElement.children) === null || _currentElement_children2 === void 0 ? void 0 : (_currentElement_children_1 = _currentElement_children2[0]) === null || _currentElement_children_1 === void 0 ? void 0 : (_currentElement_children__children1 = _currentElement_children_1.children) === null || _currentElement_children__children1 === void 0 ? void 0 : _currentElement_children__children1.length) > 1) {
2148
- var _children__children, _children_;
2149
- var item = children === null || children === void 0 ? void 0 : (_children_ = children[0]) === null || _children_ === void 0 ? void 0 : (_children__children = _children_.children) === null || _children__children === void 0 ? void 0 : _children__children[0];
2150
- var label = item === null || item === void 0 ? void 0 : item.text;
2151
- if (label) {
2152
- var _item_url, _children__children1, _children_1;
2153
- mentions = [
2154
- {
2155
- avatar: item === null || item === void 0 ? void 0 : item.url,
2156
- name: label,
2157
- id: new URLSearchParams('?' + (item === null || item === void 0 ? void 0 : (_item_url = item.url) === null || _item_url === void 0 ? void 0 : _item_url.split('?')[1])).get('id') || undefined
2158
- }
2159
- ];
2160
- children === null || children === void 0 ? true : (_children_1 = children[0]) === null || _children_1 === void 0 ? true : (_children__children1 = _children_1.children) === null || _children__children1 === void 0 ? true : delete _children__children1[0];
2161
- }
2162
- }
2163
243
  return {
2164
- type: 'list-item',
2165
- checked: currentElement.checked,
2166
- children: children,
2167
- mentions: mentions
2168
- };
2169
- }
2170
- },
2171
- {
2172
- key: "processParagraphChildren",
2173
- value: /**
2174
- * 处理段落子节点(类方法版本)
2175
- */ function processParagraphChildren(currentElement) {
2176
- var elements = [];
2177
- var textNodes = [];
2178
- var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
2179
- try {
2180
- for(var _iterator = (currentElement.children || [])[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
2181
- var currentChild = _step.value;
2182
- if (currentChild.type === 'image') {
2183
- // 将累积的文本节点生成段落
2184
- if (textNodes.length) {
2185
- elements.push({
2186
- type: 'paragraph',
2187
- children: this.parseNodes(textNodes, false, currentElement)
2188
- });
2189
- textNodes = [];
2190
- }
2191
- // 添加图片节点
2192
- elements.push(EditorUtils.createMediaNode(decodeURIComponentUrl(currentChild === null || currentChild === void 0 ? void 0 : currentChild.url), 'image', {
2193
- alt: currentChild.alt
2194
- }));
2195
- } else if (currentChild.type === 'html') {
2196
- // 跳过媒体标签的结束标签
2197
- if (currentChild.value.match(/^<\/(img|video|iframe)>/)) {
2198
- continue;
2199
- }
2200
- var mediaElement = findImageElement(currentChild.value);
2201
- if (mediaElement) {
2202
- var node = createMediaNodeFromElement(mediaElement);
2203
- if (node) {
2204
- elements.push(node);
2205
- }
2206
- } else {
2207
- textNodes.push({
2208
- type: 'html',
2209
- value: currentChild.value
2210
- });
2211
- }
2212
- } else {
2213
- textNodes.push(currentChild);
244
+ html: {
245
+ handler: function() {
246
+ return null;
247
+ },
248
+ needsHtmlResult: true
249
+ },
250
+ image: {
251
+ handler: function(el) {
252
+ return handleImage(el);
2214
253
  }
2215
- }
2216
- } catch (err) {
2217
- _didIteratorError = true;
2218
- _iteratorError = err;
2219
- } finally{
2220
- try {
2221
- if (!_iteratorNormalCompletion && _iterator.return != null) {
2222
- _iterator.return();
254
+ },
255
+ inlineMath: {
256
+ handler: function(el) {
257
+ return handleInlineMath(el);
2223
258
  }
2224
- } finally{
2225
- if (_didIteratorError) {
2226
- throw _iteratorError;
259
+ },
260
+ math: {
261
+ handler: function(el) {
262
+ return handleMath(el);
2227
263
  }
2228
- }
2229
- }
2230
- // 处理剩余的文本节点
2231
- if (textNodes.length) {
2232
- elements.push({
2233
- type: 'paragraph',
2234
- children: this.parseNodes(textNodes, false, currentElement)
2235
- });
2236
- }
2237
- return elements;
2238
- }
2239
- },
2240
- {
2241
- key: "handleParagraph",
2242
- value: /**
2243
- * 处理段落节点(类方法版本)
2244
- */ function handleParagraph(currentElement, config) {
2245
- var _currentElement_children, _currentElement_children_at, _currentElement_children1;
2246
- // 检查是否是附件链接
2247
- if (((_currentElement_children = currentElement.children) === null || _currentElement_children === void 0 ? void 0 : _currentElement_children[0].type) === 'html' && currentElement.children[0].value.startsWith('<a')) {
2248
- var attachNode = handleAttachmentLink(currentElement);
2249
- if (attachNode) return attachNode;
2250
- }
2251
- // 检查是否是链接卡片
2252
- if ((currentElement === null || currentElement === void 0 ? void 0 : (_currentElement_children1 = currentElement.children) === null || _currentElement_children1 === void 0 ? void 0 : (_currentElement_children_at = _currentElement_children1.at(0)) === null || _currentElement_children_at === void 0 ? void 0 : _currentElement_children_at.type) === 'link' && config.type === 'card') {
2253
- return handleLinkCard(currentElement, config);
2254
- }
2255
- // 处理混合内容段落
2256
- return this.processParagraphChildren(currentElement);
2257
- }
2258
- },
2259
- {
2260
- key: "handleBlockquote",
2261
- value: /**
2262
- * 处理引用块节点(类方法版本)
2263
- */ function handleBlockquote(currentElement) {
2264
- var _currentElement_children;
2265
- return {
2266
- type: 'blockquote',
2267
- children: ((_currentElement_children = currentElement.children) === null || _currentElement_children === void 0 ? void 0 : _currentElement_children.length) ? this.parseNodes(currentElement.children, false, currentElement) : [
2268
- {
2269
- type: 'paragraph',
2270
- children: [
2271
- {
2272
- text: ''
2273
- }
2274
- ]
264
+ },
265
+ footnoteReference: {
266
+ handler: function(el) {
267
+ return handleFootnoteReference(el);
268
+ }
269
+ },
270
+ inlineCode: {
271
+ handler: function(el) {
272
+ return handleInlineCode(el);
273
+ }
274
+ },
275
+ thematicBreak: {
276
+ handler: function() {
277
+ return handleThematicBreak();
278
+ }
279
+ },
280
+ code: {
281
+ handler: function(el) {
282
+ return handleCode(el);
283
+ }
284
+ },
285
+ yaml: {
286
+ handler: function(el) {
287
+ return handleYaml(el);
288
+ }
289
+ },
290
+ definition: {
291
+ handler: function(el) {
292
+ return handleDefinition(el);
293
+ }
294
+ },
295
+ heading: {
296
+ handler: function(el) {
297
+ return handleHeading(el, parseNodesFn);
2275
298
  }
2276
- ]
299
+ },
300
+ list: {
301
+ handler: function(el) {
302
+ return handleList(el, parseNodesFn);
303
+ }
304
+ },
305
+ listItem: {
306
+ handler: function(el) {
307
+ return handleListItem(el, parseNodesFn);
308
+ }
309
+ },
310
+ blockquote: {
311
+ handler: function(el) {
312
+ return handleBlockquote(el, parseNodesFn);
313
+ }
314
+ },
315
+ footnoteDefinition: {
316
+ handler: function(el) {
317
+ return handleFootnoteDefinition(el, parseNodesFn);
318
+ }
319
+ },
320
+ paragraph: {
321
+ handler: function(el, _plugins, config) {
322
+ return handleParagraph(el, config, parseNodesFn);
323
+ }
324
+ },
325
+ table: {
326
+ handler: function(el, _plugins, _config, parent, _htmlTag, preElement) {
327
+ return parseTableOrChart(el, preElement || parent, _this.plugins, parseNodesForTable, _this.config);
328
+ }
329
+ }
2277
330
  };
2278
331
  }
2279
332
  },
2280
333
  {
2281
- key: "handleTextAndInlineElements",
2282
- value: /**
2283
- * 处理文本和内联元素节点(类方法版本)
2284
- */ function handleTextAndInlineElements(currentElement, htmlTag) {
2285
- if (currentElement.type === 'text' && htmlTag.length) {
2286
- var el = {
2287
- text: currentElement.value
334
+ key: "handleSingleElement",
335
+ value: function handleSingleElement(currentElement, config, parent, htmlTag, preElement) {
336
+ var _this = this;
337
+ var elementType = currentElement.type;
338
+ var elementHandlers = this.getElementHandlers();
339
+ var handlerInfo = elementHandlers[elementType];
340
+ if (handlerInfo === null || handlerInfo === void 0 ? void 0 : handlerInfo.needsHtmlResult) {
341
+ var htmlResult = handleHtml(currentElement, parent, htmlTag);
342
+ var result = {
343
+ el: htmlResult.el
2288
344
  };
2289
- if (currentElement.value) {
2290
- applyHtmlTagsToElement(el, htmlTag);
345
+ if (htmlResult.contextProps) {
346
+ result.contextProps = htmlResult.contextProps;
2291
347
  }
2292
- return el;
2293
- }
2294
- if ([
2295
- 'strong',
2296
- 'link',
2297
- 'text',
2298
- 'emphasis',
2299
- 'delete',
2300
- 'inlineCode'
2301
- ].includes(currentElement.type)) {
2302
- var _currentElement_children;
2303
- if (currentElement.type === 'text') {
2304
- return {
2305
- text: currentElement.value
2306
- };
2307
- }
2308
- var leaf = {};
2309
- this.applyInlineFormatting(leaf, currentElement);
2310
- applyHtmlTagsToElement(leaf, htmlTag);
2311
- if (currentElement === null || currentElement === void 0 ? void 0 : (_currentElement_children = currentElement.children) === null || _currentElement_children === void 0 ? void 0 : _currentElement_children.some(function(n) {
2312
- return n.type === 'html';
2313
- })) {
2314
- var _this_parseNodes;
2315
- return _object_spread_props(_object_spread({}, (_this_parseNodes = this.parseNodes(currentElement === null || currentElement === void 0 ? void 0 : currentElement.children, false, currentElement)) === null || _this_parseNodes === void 0 ? void 0 : _this_parseNodes.at(0)), {
2316
- url: leaf.url
2317
- });
2318
- } else {
2319
- var _currentElement_children1;
2320
- return parseText(((_currentElement_children1 = currentElement.children) === null || _currentElement_children1 === void 0 ? void 0 : _currentElement_children1.length) ? currentElement.children : [
2321
- {
2322
- value: (leaf === null || leaf === void 0 ? void 0 : leaf.url) || ''
2323
- }
2324
- ], leaf);
348
+ if (htmlResult.htmlTag) {
349
+ result.htmlTag = htmlResult.htmlTag;
2325
350
  }
351
+ return result;
2326
352
  }
2327
- if (currentElement.type === 'break') {
353
+ if (!handlerInfo) {
2328
354
  return {
2329
- text: '\n'
355
+ el: handleTextAndInlineElements(currentElement, htmlTag, function(nodes, top, parent) {
356
+ return _this.parseNodes(nodes, top, parent);
357
+ }, this.config)
2330
358
  };
2331
359
  }
360
+ var handlerResult = handlerInfo.handler(currentElement, this.plugins, config, parent, htmlTag, preElement, this);
2332
361
  return {
2333
- text: ''
2334
- };
2335
- }
2336
- },
2337
- {
2338
- key: "applyInlineFormatting",
2339
- value: /**
2340
- * 应用内联格式到叶子节点(类方法版本)
2341
- */ function applyInlineFormatting(leaf, currentElement) {
2342
- if (currentElement.type === 'strong') leaf.bold = true;
2343
- if (currentElement.type === 'emphasis') leaf.italic = true;
2344
- if (currentElement.type === 'delete') leaf.strikethrough = true;
2345
- if (currentElement.type === 'link') {
2346
- try {
2347
- var _this_config;
2348
- leaf.url = currentElement === null || currentElement === void 0 ? void 0 : currentElement.url;
2349
- // 如果配置了在新标签页打开链接,添加 target 和 rel 属性
2350
- if ((_this_config = this.config) === null || _this_config === void 0 ? void 0 : _this_config.openLinksInNewTab) {
2351
- // 使用 otherProps 存储额外的链接属性
2352
- if (!leaf.otherProps) {
2353
- leaf.otherProps = {};
2354
- }
2355
- leaf.otherProps.target = '_blank';
2356
- leaf.otherProps.rel = 'noopener noreferrer';
2357
- }
2358
- } catch (error) {
2359
- leaf.url = currentElement === null || currentElement === void 0 ? void 0 : currentElement.url;
2360
- }
2361
- }
2362
- }
2363
- },
2364
- {
2365
- key: "parseTableOrChart",
2366
- value: /**
2367
- * 解析表格或图表(类方法版本)
2368
- */ function parseTableOrChart(table, preNode) {
2369
- var _this = this;
2370
- var _table_children, _tableHeader_children, _table_children_slice, _table_children1, _table_align, _config_at, _config_at1, _table_otherProps;
2371
- var keyMap = new Map();
2372
- // @ts-ignore
2373
- var config = // @ts-ignore
2374
- (preNode === null || preNode === void 0 ? void 0 : preNode.type) === 'code' && // @ts-ignore
2375
- (preNode === null || preNode === void 0 ? void 0 : preNode.language) === 'html' && (// @ts-ignore
2376
- preNode === null || preNode === void 0 ? void 0 : preNode.otherProps) ? preNode === null || preNode === void 0 ? void 0 : preNode.otherProps : {};
2377
- var tableHeader = table === null || table === void 0 ? void 0 : (_table_children = table.children) === null || _table_children === void 0 ? void 0 : _table_children.at(0);
2378
- var columns = (tableHeader === null || tableHeader === void 0 ? void 0 : (_tableHeader_children = tableHeader.children) === null || _tableHeader_children === void 0 ? void 0 : _tableHeader_children.map(function(node) {
2379
- return myRemark.stringify({
2380
- type: 'root',
2381
- children: [
2382
- node
2383
- ]
2384
- }).trim();
2385
- }).map(function(key) {
2386
- // 规范化字段名,统一处理转义字符
2387
- var normalizedKey = normalizeFieldName(key);
2388
- var value = keyMap.get(normalizedKey) || normalizedKey;
2389
- return value;
2390
- }).map(function(key) {
2391
- return {
2392
- dataIndex: key
2393
- };
2394
- })) || [];
2395
- var dataSource = (table === null || table === void 0 ? void 0 : (_table_children1 = table.children) === null || _table_children1 === void 0 ? void 0 : (_table_children_slice = _table_children1.slice(1)) === null || _table_children_slice === void 0 ? void 0 : _table_children_slice.map(function(row) {
2396
- var _row_children;
2397
- return (_row_children = row.children) === null || _row_children === void 0 ? void 0 : _row_children.reduce(function(acc, cell, index) {
2398
- var column = columns[index];
2399
- var key = (column === null || column === void 0 ? void 0 : column.dataIndex) || (typeof column === 'string' ? column : undefined);
2400
- if (key) {
2401
- acc[key] = myRemark.stringify({
2402
- type: 'root',
2403
- children: [
2404
- cell
2405
- ]
2406
- }).trim();
2407
- }
2408
- return acc;
2409
- }, {});
2410
- })) || [];
2411
- if ((_table_align = table.align) === null || _table_align === void 0 ? void 0 : _table_align.every(function(item) {
2412
- return !item;
2413
- })) {
2414
- var aligns = getColumnAlignment(dataSource, columns);
2415
- table.align = aligns;
2416
- }
2417
- var aligns1 = table.align;
2418
- var isChart = (config === null || config === void 0 ? void 0 : config.chartType) || (config === null || config === void 0 ? void 0 : (_config_at1 = config.at) === null || _config_at1 === void 0 ? void 0 : (_config_at = _config_at1.call(config, 0)) === null || _config_at === void 0 ? void 0 : _config_at.chartType);
2419
- // 计算合并单元格信息
2420
- var mergeCells = config.mergeCells || [];
2421
- // 创建合并单元格映射,用于快速查找
2422
- var mergeMap = new Map();
2423
- mergeCells === null || mergeCells === void 0 ? void 0 : mergeCells.forEach(function(param) {
2424
- var row = param.row, col = param.col, rowSpan = param.rowSpan, rowspan = param.rowspan, colSpan = param.colSpan, colspan = param.colspan;
2425
- var rawRowSpan = rowSpan || rowspan;
2426
- var rawColSpan = colSpan || colspan;
2427
- // 主单元格
2428
- mergeMap.set("".concat(row, "-").concat(col), {
2429
- rowSpan: rawRowSpan,
2430
- colSpan: rawColSpan
2431
- });
2432
- // 被合并的单元格标记为隐藏
2433
- for(var r = row; r < row + rawRowSpan; r++){
2434
- for(var c = col; c < col + rawColSpan; c++){
2435
- if (r !== row || c !== col) {
2436
- mergeMap.set("".concat(r, "-").concat(c), {
2437
- rowSpan: 1,
2438
- colSpan: 1,
2439
- hidden: true
2440
- });
2441
- }
2442
- }
2443
- }
2444
- });
2445
- var children = table.children.map(function(r, l) {
2446
- return {
2447
- type: 'table-row',
2448
- align: (aligns1 === null || aligns1 === void 0 ? void 0 : aligns1[l]) || undefined,
2449
- children: r.children.map(function(c, i) {
2450
- var _c_children;
2451
- var mergeInfo = mergeMap.get("".concat(l, "-").concat(i));
2452
- return _object_spread_props(_object_spread({
2453
- type: 'table-cell',
2454
- align: (aligns1 === null || aligns1 === void 0 ? void 0 : aligns1[i]) || undefined,
2455
- title: l === 0,
2456
- rows: l,
2457
- cols: i
2458
- }, (mergeInfo === null || mergeInfo === void 0 ? void 0 : mergeInfo.rowSpan) && mergeInfo.rowSpan > 1 ? {
2459
- rowSpan: mergeInfo.rowSpan
2460
- } : {}, (mergeInfo === null || mergeInfo === void 0 ? void 0 : mergeInfo.colSpan) && mergeInfo.colSpan > 1 ? {
2461
- colSpan: mergeInfo.colSpan
2462
- } : {}, (mergeInfo === null || mergeInfo === void 0 ? void 0 : mergeInfo.hidden) ? {
2463
- hidden: true
2464
- } : {}), {
2465
- children: ((_c_children = c.children) === null || _c_children === void 0 ? void 0 : _c_children.length) ? [
2466
- {
2467
- type: 'paragraph',
2468
- children: _this.parseNodes(c.children, false, c)
2469
- }
2470
- ] : [
2471
- {
2472
- type: 'paragraph',
2473
- children: [
2474
- {
2475
- text: ''
2476
- }
2477
- ]
2478
- }
2479
- ]
2480
- });
2481
- })
2482
- };
2483
- });
2484
- // 检查表格是否完成(未闭合)
2485
- // 如果 table 节点有 otherProps.finish,使用它;否则默认为 false(未完成)
2486
- var isFinished = (table === null || table === void 0 ? void 0 : (_table_otherProps = table.otherProps) === null || _table_otherProps === void 0 ? void 0 : _table_otherProps.finish) !== undefined ? table.otherProps.finish : false;
2487
- var otherProps = _object_spread_props(_object_spread({}, isChart ? {
2488
- config: config
2489
- } : config), {
2490
- columns: columns.map(function(col) {
2491
- return col.dataIndex;
2492
- }),
2493
- dataSource: dataSource.map(function(item) {
2494
- item === null || item === void 0 ? true : delete item.chartType;
2495
- return _object_spread({}, item);
2496
- }),
2497
- finish: isFinished
2498
- });
2499
- var node = {
2500
- type: isChart ? 'chart' : 'table',
2501
- children: children,
2502
- otherProps: otherProps
362
+ el: handlerResult
2503
363
  };
2504
- return EditorUtils.wrapperCardNode(node);
2505
364
  }
2506
365
  }
2507
366
  ]);