react-ai-renderer 0.1.19 → 0.1.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -31,58 +31,6 @@ function _interopNamespaceDefault(e) {
31
31
  var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
32
32
  var runtime__namespace = /*#__PURE__*/_interopNamespaceDefault(runtime);
33
33
 
34
- // SSR 兼容性 polyfill
35
- // 这个文件确保在服务端渲染时不会因为访问 document 而报错
36
- // 在模块加载时就设置全局保护
37
- if (typeof globalThis !== 'undefined' && globalThis.document === undefined) {
38
- // 为 SSR 环境创建一个模拟的 document 对象
39
- var mockElement_1 = {
40
- innerHTML: '',
41
- textContent: '',
42
- appendChild: function () {
43
- return mockElement_1;
44
- },
45
- removeChild: function () {
46
- return mockElement_1;
47
- },
48
- remove: function () {},
49
- id: '',
50
- className: '',
51
- setAttribute: function () {},
52
- getAttribute: function () {
53
- return null;
54
- },
55
- removeAttribute: function () {},
56
- querySelector: function () {
57
- return null;
58
- },
59
- querySelectorAll: function () {
60
- return [];
61
- }
62
- };
63
- // 创建模拟的 document 对象
64
- var mockDocument = {
65
- createElement: function () {
66
- return mockElement_1;
67
- },
68
- getElementById: function () {
69
- return null;
70
- },
71
- querySelector: function () {
72
- return null;
73
- },
74
- querySelectorAll: function () {
75
- return [];
76
- },
77
- body: mockElement_1,
78
- head: mockElement_1,
79
- addEventListener: function () {},
80
- removeEventListener: function () {}
81
- };
82
- // 在 SSR 环境中提供全局的 document
83
- globalThis.document = mockDocument;
84
- }
85
-
86
34
  /******************************************************************************
87
35
  Copyright (c) Microsoft Corporation.
88
36
 
@@ -219,6 +167,165 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
219
167
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
220
168
  };
221
169
 
170
+ if (typeof globalThis !== 'undefined' && typeof globalThis.window === 'undefined') {
171
+ var mockElement_1 = {
172
+ innerHTML: '',
173
+ textContent: '',
174
+ nodeValue: '',
175
+ appendChild: function () {
176
+ return mockElement_1;
177
+ },
178
+ removeChild: function () {
179
+ return mockElement_1;
180
+ },
181
+ insertBefore: function () {
182
+ return mockElement_1;
183
+ },
184
+ replaceChild: function () {
185
+ return mockElement_1;
186
+ },
187
+ cloneNode: function () {
188
+ return mockElement_1;
189
+ },
190
+ remove: function () {},
191
+ id: '',
192
+ className: '',
193
+ classList: {
194
+ add: function () {},
195
+ remove: function () {},
196
+ contains: function () {
197
+ return false;
198
+ },
199
+ toggle: function () {
200
+ return false;
201
+ }
202
+ },
203
+ style: {},
204
+ setAttribute: function () {},
205
+ getAttribute: function () {
206
+ return null;
207
+ },
208
+ removeAttribute: function () {},
209
+ hasAttribute: function () {
210
+ return false;
211
+ },
212
+ querySelector: function () {
213
+ return null;
214
+ },
215
+ querySelectorAll: function () {
216
+ return [];
217
+ },
218
+ getElementsByTagName: function () {
219
+ return [];
220
+ },
221
+ getElementsByClassName: function () {
222
+ return [];
223
+ },
224
+ addEventListener: function () {},
225
+ removeEventListener: function () {},
226
+ dispatchEvent: function () {
227
+ return true;
228
+ },
229
+ parentNode: null,
230
+ parentElement: null,
231
+ childNodes: [],
232
+ children: [],
233
+ firstChild: null,
234
+ lastChild: null,
235
+ nextSibling: null,
236
+ previousSibling: null
237
+ };
238
+ var mockDocument = {
239
+ createElement: function () {
240
+ return mockElement_1;
241
+ },
242
+ createElementNS: function () {
243
+ return mockElement_1;
244
+ },
245
+ createTextNode: function (text) {
246
+ return __assign(__assign({}, mockElement_1), {
247
+ nodeValue: text,
248
+ textContent: text
249
+ });
250
+ },
251
+ createDocumentFragment: function () {
252
+ return mockElement_1;
253
+ },
254
+ getElementById: function () {
255
+ return null;
256
+ },
257
+ querySelector: function () {
258
+ return null;
259
+ },
260
+ querySelectorAll: function () {
261
+ return [];
262
+ },
263
+ getElementsByTagName: function () {
264
+ return [];
265
+ },
266
+ getElementsByClassName: function () {
267
+ return [];
268
+ },
269
+ body: mockElement_1,
270
+ head: mockElement_1,
271
+ documentElement: mockElement_1,
272
+ addEventListener: function () {},
273
+ removeEventListener: function () {},
274
+ createEvent: function () {
275
+ return {};
276
+ }
277
+ };
278
+ var mockWindow = {
279
+ document: mockDocument,
280
+ location: {
281
+ href: '',
282
+ origin: '',
283
+ protocol: 'https:',
284
+ host: '',
285
+ hostname: '',
286
+ port: '',
287
+ pathname: '/',
288
+ search: '',
289
+ hash: ''
290
+ },
291
+ navigator: {
292
+ userAgent: '',
293
+ language: 'en-US',
294
+ platform: ''
295
+ },
296
+ addEventListener: function () {},
297
+ removeEventListener: function () {},
298
+ getComputedStyle: function () {
299
+ return {};
300
+ },
301
+ matchMedia: function () {
302
+ return {
303
+ matches: false,
304
+ media: '',
305
+ addListener: function () {},
306
+ removeListener: function () {},
307
+ addEventListener: function () {},
308
+ removeEventListener: function () {},
309
+ dispatchEvent: function () {
310
+ return true;
311
+ }
312
+ };
313
+ },
314
+ requestAnimationFrame: function (cb) {
315
+ return setTimeout(cb, 16);
316
+ },
317
+ cancelAnimationFrame: function (id) {
318
+ return clearTimeout(id);
319
+ },
320
+ setTimeout: setTimeout,
321
+ clearTimeout: clearTimeout,
322
+ setInterval: setInterval,
323
+ clearInterval: clearInterval
324
+ };
325
+ globalThis.window = mockWindow;
326
+ globalThis.document = mockDocument;
327
+ }
328
+
222
329
  var MDXStreamingParser = /** @class */function () {
223
330
  function MDXStreamingParser(components) {
224
331
  // 将组件名称存储在Set中,而不是整个组件对象
@@ -43158,8 +43265,6 @@ function decodeNamedCharacterReference(value) {
43158
43265
  // reference to decode was not a semicolon (`;`), we can assume that the
43159
43266
  // matching was not complete.
43160
43267
  if (
43161
- // @ts-expect-error: TypeScript is wrong that `textContent` on elements can
43162
- // yield `null`.
43163
43268
  character.charCodeAt(character.length - 1) === 59 /* `;` */ &&
43164
43269
  value !== 'semi'
43165
43270
  ) {
@@ -43168,8 +43273,6 @@ function decodeNamedCharacterReference(value) {
43168
43273
 
43169
43274
  // If the decoded string is equal to the input, the character reference was
43170
43275
  // not valid.
43171
- // @ts-expect-error: TypeScript is wrong that `textContent` on elements can
43172
- // yield `null`.
43173
43276
  return character === characterReference ? false : character
43174
43277
  }
43175
43278
 
@@ -54898,9 +55001,8 @@ function toResult(value) {
54898
55001
  }
54899
55002
 
54900
55003
  /**
54901
- * @typedef {import('unist').Node} UnistNode
54902
- * @typedef {import('unist').Parent} UnistParent
54903
- * @typedef {import('unist-util-visit-parents').VisitorResult} VisitorResult
55004
+ * @import {Node as UnistNode, Parent as UnistParent} from 'unist'
55005
+ * @import {VisitorResult} from 'unist-util-visit-parents'
54904
55006
  */
54905
55007
 
54906
55008
 
@@ -58500,11 +58602,18 @@ function parseComponentRecursively(allComponentHandlers, item, scope) {
58500
58602
  }))];
58501
58603
  case 1:
58502
58604
  parsedChildren_1 = _a.sent();
58503
- item.children.map(function (item, index) {
58504
- if (item.type === 'text') {
58505
- childrenContent_1 += escapeContentForStream(parsedChildren_1[index].value); // encodeURIComponent(parsedChildren[index]);
58605
+ item.children.forEach(function (childItem, index) {
58606
+ var parsedChild = parsedChildren_1[index];
58607
+ if (!parsedChild) {
58608
+ return;
58609
+ }
58610
+ if (!parsedChild.value) {
58611
+ return;
58612
+ }
58613
+ if (childItem.type === 'text') {
58614
+ childrenContent_1 += escapeContentForStream(parsedChild.value);
58506
58615
  } else {
58507
- childrenContent_1 += parsedChildren_1[index].value;
58616
+ childrenContent_1 += parsedChild.value;
58508
58617
  }
58509
58618
  });
58510
58619
  _a.label = 2;
@@ -59209,7 +59318,6 @@ var ComponentPlaceholder = function (_a) {
59209
59318
  }
59210
59319
  } catch (e) {
59211
59320
  // 如果骨架图生成失败,继续使用默认占位内容
59212
- console.warn('Failed to generate skeleton:', e);
59213
59321
  }
59214
59322
  }
59215
59323
  // 默认占位内容
@@ -59314,37 +59422,55 @@ function extractComponent(value) {
59314
59422
  * 将 components 转换为 ComponentHandler 数组
59315
59423
  */
59316
59424
  function convertComponentsToHandlers(components, componentHandlers) {
59317
- return Object.entries(components).filter(function (_a) {
59318
- var _b = __read(_a, 1),
59319
- name = _b[0];
59320
- return !componentHandlers.some(function (handler) {
59321
- return handler.name === name;
59322
- });
59323
- }).map(function (_a) {
59324
- var _b = __read(_a, 2),
59325
- name = _b[0],
59326
- componentValue = _b[1];
59425
+ var e_1, _a;
59426
+ var handlers = [];
59427
+ var _loop_1 = function (name_1, componentValue) {
59428
+ if (!componentValue) return "continue";
59429
+ if (componentHandlers.some(function (handler) {
59430
+ return handler.name === name_1;
59431
+ })) return "continue";
59327
59432
  if (isEnhancedComponentConfig(componentValue)) {
59328
59433
  var config = componentValue;
59329
- return {
59434
+ if (!config.value) {
59435
+ return "continue";
59436
+ }
59437
+ handlers.push({
59330
59438
  component: config.value,
59331
- name: name,
59439
+ name: name_1,
59332
59440
  selfClosing: config.selfClosing !== undefined ? config.selfClosing : isSelfClosingComponent(config.value),
59333
59441
  onRenderStart: config.onRenderStart,
59334
59442
  onRenderProcess: config.onRenderProcess,
59335
59443
  onRenderFinished: config.onRenderFinished,
59336
59444
  loader: config.loader,
59337
59445
  label: config.label
59338
- };
59446
+ });
59339
59447
  } else {
59340
- // 普通模式:直接使用组件
59341
- return {
59448
+ handlers.push({
59342
59449
  component: componentValue,
59343
- name: name,
59450
+ name: name_1,
59344
59451
  selfClosing: isSelfClosingComponent(componentValue)
59345
- };
59452
+ });
59346
59453
  }
59347
- });
59454
+ };
59455
+ try {
59456
+ for (var _b = __values(Object.entries(components)), _c = _b.next(); !_c.done; _c = _b.next()) {
59457
+ var _d = __read(_c.value, 2),
59458
+ name_1 = _d[0],
59459
+ componentValue = _d[1];
59460
+ _loop_1(name_1, componentValue);
59461
+ }
59462
+ } catch (e_1_1) {
59463
+ e_1 = {
59464
+ error: e_1_1
59465
+ };
59466
+ } finally {
59467
+ try {
59468
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
59469
+ } finally {
59470
+ if (e_1) throw e_1.error;
59471
+ }
59472
+ }
59473
+ return handlers;
59348
59474
  }
59349
59475
  function ReactAIRenderer(_a) {
59350
59476
  var _this = this;
@@ -59359,34 +59485,28 @@ function ReactAIRenderer(_a) {
59359
59485
  useGithubStyles = _a.useGithubStyles,
59360
59486
  mdxLayoutClassName = _a.mdxLayoutClassName,
59361
59487
  mdxLayoutStyle = _a.mdxLayoutStyle;
59362
- // 检查是否在浏览器环境中
59488
+ var _e = __read(React.useState(null), 2),
59489
+ component = _e[0],
59490
+ setComponent = _e[1];
59491
+ var fallbackErrorRef = React.useRef({
59492
+ hasError: false
59493
+ });
59494
+ var completedComponentsCacheRef = React.useRef(new Map());
59363
59495
  var isBrowser = typeof window !== 'undefined';
59364
- // 在服务端直接返回空内容,避免使用 hooks
59365
- if (!isBrowser) {
59366
- return /*#__PURE__*/runtime.jsx(React.Fragment, {
59367
- children: null
59368
- });
59369
- }
59370
59496
  content = content || children || '';
59371
- // 构建 allComponents:从增强配置中提取实际的组件
59372
- var normalizedComponents = components ? Object.fromEntries(Object.entries(components).map(function (_a) {
59497
+ var normalizedComponents = components ? Object.fromEntries(Object.entries(components).filter(function (_a) {
59498
+ var _b = __read(_a, 2);
59499
+ _b[0];
59500
+ var value = _b[1];
59501
+ return value != null;
59502
+ }).map(function (_a) {
59373
59503
  var _b = __read(_a, 2),
59374
59504
  name = _b[0],
59375
59505
  value = _b[1];
59376
59506
  return [name, extractComponent(value)];
59377
59507
  })) : {};
59378
59508
  var allComponents = __assign(__assign({}, BuiltInComponents), normalizedComponents);
59379
- // 构建 allComponentHandlers:支持增强模式
59380
59509
  var allComponentHandlers = __spreadArray(__spreadArray([], __read(components ? convertComponentsToHandlers(components, componentHandlers) : []), false), __read(componentHandlers), false);
59381
- var _e = __read(React.useState(null), 2),
59382
- component = _e[0],
59383
- setComponent = _e[1];
59384
- // 用于跟踪 FallbackView 的错误状态
59385
- var fallbackErrorRef = React.useRef({
59386
- hasError: false
59387
- });
59388
- // 用于缓存已完成的组件,避免重复渲染
59389
- var completedComponentsCacheRef = React.useRef(new Map());
59390
59510
  var parser = new MDXStreamingParser(allComponentHandlers);
59391
59511
  var mdxContent = content || '';
59392
59512
  // FallbackView 错误状态变化时的回调
@@ -59397,10 +59517,11 @@ function ReactAIRenderer(_a) {
59397
59517
  };
59398
59518
  };
59399
59519
  React.useEffect(function () {
59520
+ if (!isBrowser) return;
59400
59521
  var parseMDX = function () {
59401
59522
  return __awaiter(_this, void 0, void 0, function () {
59402
- var ThinkComponent, ResultComponent, protectedContent, _a, thinkContent, resultContent, parsedData, parsedComponents, currentComponentCount, cacheKeys, cacheKeys_1, cacheKeys_1_1, key, indexMatch, index, finalComponent, fallbackComponent, _loop_1, i, MDXComponent, parsedDataLength, lastItem;
59403
- var e_1, _b;
59523
+ var ThinkComponent, ResultComponent, protectedContent, _a, thinkContent, resultContent, parsedData, parsedComponents, currentComponentCount, cacheKeys, cacheKeys_1, cacheKeys_1_1, key, indexMatch, index, finalComponent, fallbackComponent, _loop_2, i, MDXComponent, parsedDataLength, lastItem;
59524
+ var e_2, _b;
59404
59525
  return __generator(this, function (_c) {
59405
59526
  switch (_c.label) {
59406
59527
  case 0:
@@ -59418,7 +59539,6 @@ function ReactAIRenderer(_a) {
59418
59539
  if (!resultContent) return [3 /*break*/, 8];
59419
59540
  resultContent = fixMDXContent(resultContent);
59420
59541
  parsedData = parser.parse('magic', resultContent);
59421
- console.log('parsedData', parsedData);
59422
59542
  return [4 /*yield*/, Promise.all(parsedData.map(function (item) {
59423
59543
  return parseComponentRecursively(allComponentHandlers, item, scope);
59424
59544
  }))];
@@ -59438,20 +59558,20 @@ function ReactAIRenderer(_a) {
59438
59558
  }
59439
59559
  }
59440
59560
  }
59441
- } catch (e_1_1) {
59442
- e_1 = {
59443
- error: e_1_1
59561
+ } catch (e_2_1) {
59562
+ e_2 = {
59563
+ error: e_2_1
59444
59564
  };
59445
59565
  } finally {
59446
59566
  try {
59447
59567
  if (cacheKeys_1_1 && !cacheKeys_1_1.done && (_b = cacheKeys_1.return)) _b.call(cacheKeys_1);
59448
59568
  } finally {
59449
- if (e_1) throw e_1.error;
59569
+ if (e_2) throw e_2.error;
59450
59570
  }
59451
59571
  }
59452
59572
  finalComponent = [];
59453
59573
  fallbackComponent = [];
59454
- _loop_1 = function (i) {
59574
+ _loop_2 = function (i) {
59455
59575
  var item, currentParsedItem, _result, placeholderForFallback, componentNameMatch, componentName_1, componentHandler, displayName, cacheKey;
59456
59576
  return __generator(this, function (_d) {
59457
59577
  switch (_d.label) {
@@ -59540,7 +59660,7 @@ function ReactAIRenderer(_a) {
59540
59660
  _c.label = 4;
59541
59661
  case 4:
59542
59662
  if (!(i < parsedComponents.length)) return [3 /*break*/, 7];
59543
- return [5 /*yield**/, _loop_1(i)];
59663
+ return [5 /*yield**/, _loop_2(i)];
59544
59664
  case 5:
59545
59665
  _c.sent();
59546
59666
  _c.label = 6;
@@ -59584,6 +59704,9 @@ function ReactAIRenderer(_a) {
59584
59704
  };
59585
59705
  parseMDX();
59586
59706
  }, [content]);
59707
+ if (!isBrowser) {
59708
+ return null;
59709
+ }
59587
59710
  return /*#__PURE__*/runtime.jsx(ErrorBoundary, {
59588
59711
  content: content,
59589
59712
  children: /*#__PURE__*/runtime.jsx(React.Fragment, {
@@ -59770,7 +59893,6 @@ var getMermaid = function () {
59770
59893
  mermaidPromise = import('mermaid').then(function (module) {
59771
59894
  return module.default || module;
59772
59895
  }).catch(function (error) {
59773
- console.warn('Mermaid is not available. Install it to enable chart rendering.');
59774
59896
  return null;
59775
59897
  });
59776
59898
  return mermaidPromise;
@@ -59821,7 +59943,6 @@ function MermaidRenderer(_a) {
59821
59943
  if (!!mermaid.initialized) return [3 /*break*/, 4];
59822
59944
  // 设置全局错误处理函数
59823
59945
  mermaid.parseError = function (err) {
59824
- console.warn('Mermaid parse error:', err.message);
59825
59946
  };
59826
59947
  return [4 /*yield*/, mermaid.initialize({
59827
59948
  startOnLoad: false,