react-ai-renderer 0.1.20 → 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;
@@ -59313,37 +59422,55 @@ function extractComponent(value) {
59313
59422
  * 将 components 转换为 ComponentHandler 数组
59314
59423
  */
59315
59424
  function convertComponentsToHandlers(components, componentHandlers) {
59316
- return Object.entries(components).filter(function (_a) {
59317
- var _b = __read(_a, 1),
59318
- name = _b[0];
59319
- return !componentHandlers.some(function (handler) {
59320
- return handler.name === name;
59321
- });
59322
- }).map(function (_a) {
59323
- var _b = __read(_a, 2),
59324
- name = _b[0],
59325
- 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";
59326
59432
  if (isEnhancedComponentConfig(componentValue)) {
59327
59433
  var config = componentValue;
59328
- return {
59434
+ if (!config.value) {
59435
+ return "continue";
59436
+ }
59437
+ handlers.push({
59329
59438
  component: config.value,
59330
- name: name,
59439
+ name: name_1,
59331
59440
  selfClosing: config.selfClosing !== undefined ? config.selfClosing : isSelfClosingComponent(config.value),
59332
59441
  onRenderStart: config.onRenderStart,
59333
59442
  onRenderProcess: config.onRenderProcess,
59334
59443
  onRenderFinished: config.onRenderFinished,
59335
59444
  loader: config.loader,
59336
59445
  label: config.label
59337
- };
59446
+ });
59338
59447
  } else {
59339
- // 普通模式:直接使用组件
59340
- return {
59448
+ handlers.push({
59341
59449
  component: componentValue,
59342
- name: name,
59450
+ name: name_1,
59343
59451
  selfClosing: isSelfClosingComponent(componentValue)
59344
- };
59452
+ });
59345
59453
  }
59346
- });
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;
59347
59474
  }
59348
59475
  function ReactAIRenderer(_a) {
59349
59476
  var _this = this;
@@ -59358,34 +59485,28 @@ function ReactAIRenderer(_a) {
59358
59485
  useGithubStyles = _a.useGithubStyles,
59359
59486
  mdxLayoutClassName = _a.mdxLayoutClassName,
59360
59487
  mdxLayoutStyle = _a.mdxLayoutStyle;
59361
- // 检查是否在浏览器环境中
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());
59362
59495
  var isBrowser = typeof window !== 'undefined';
59363
- // 在服务端直接返回空内容,避免使用 hooks
59364
- if (!isBrowser) {
59365
- return /*#__PURE__*/runtime.jsx(React.Fragment, {
59366
- children: null
59367
- });
59368
- }
59369
59496
  content = content || children || '';
59370
- // 构建 allComponents:从增强配置中提取实际的组件
59371
- 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) {
59372
59503
  var _b = __read(_a, 2),
59373
59504
  name = _b[0],
59374
59505
  value = _b[1];
59375
59506
  return [name, extractComponent(value)];
59376
59507
  })) : {};
59377
59508
  var allComponents = __assign(__assign({}, BuiltInComponents), normalizedComponents);
59378
- // 构建 allComponentHandlers:支持增强模式
59379
59509
  var allComponentHandlers = __spreadArray(__spreadArray([], __read(components ? convertComponentsToHandlers(components, componentHandlers) : []), false), __read(componentHandlers), false);
59380
- var _e = __read(React.useState(null), 2),
59381
- component = _e[0],
59382
- setComponent = _e[1];
59383
- // 用于跟踪 FallbackView 的错误状态
59384
- var fallbackErrorRef = React.useRef({
59385
- hasError: false
59386
- });
59387
- // 用于缓存已完成的组件,避免重复渲染
59388
- var completedComponentsCacheRef = React.useRef(new Map());
59389
59510
  var parser = new MDXStreamingParser(allComponentHandlers);
59390
59511
  var mdxContent = content || '';
59391
59512
  // FallbackView 错误状态变化时的回调
@@ -59396,10 +59517,11 @@ function ReactAIRenderer(_a) {
59396
59517
  };
59397
59518
  };
59398
59519
  React.useEffect(function () {
59520
+ if (!isBrowser) return;
59399
59521
  var parseMDX = function () {
59400
59522
  return __awaiter(_this, void 0, void 0, function () {
59401
- 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;
59402
- 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;
59403
59525
  return __generator(this, function (_c) {
59404
59526
  switch (_c.label) {
59405
59527
  case 0:
@@ -59436,20 +59558,20 @@ function ReactAIRenderer(_a) {
59436
59558
  }
59437
59559
  }
59438
59560
  }
59439
- } catch (e_1_1) {
59440
- e_1 = {
59441
- error: e_1_1
59561
+ } catch (e_2_1) {
59562
+ e_2 = {
59563
+ error: e_2_1
59442
59564
  };
59443
59565
  } finally {
59444
59566
  try {
59445
59567
  if (cacheKeys_1_1 && !cacheKeys_1_1.done && (_b = cacheKeys_1.return)) _b.call(cacheKeys_1);
59446
59568
  } finally {
59447
- if (e_1) throw e_1.error;
59569
+ if (e_2) throw e_2.error;
59448
59570
  }
59449
59571
  }
59450
59572
  finalComponent = [];
59451
59573
  fallbackComponent = [];
59452
- _loop_1 = function (i) {
59574
+ _loop_2 = function (i) {
59453
59575
  var item, currentParsedItem, _result, placeholderForFallback, componentNameMatch, componentName_1, componentHandler, displayName, cacheKey;
59454
59576
  return __generator(this, function (_d) {
59455
59577
  switch (_d.label) {
@@ -59538,7 +59660,7 @@ function ReactAIRenderer(_a) {
59538
59660
  _c.label = 4;
59539
59661
  case 4:
59540
59662
  if (!(i < parsedComponents.length)) return [3 /*break*/, 7];
59541
- return [5 /*yield**/, _loop_1(i)];
59663
+ return [5 /*yield**/, _loop_2(i)];
59542
59664
  case 5:
59543
59665
  _c.sent();
59544
59666
  _c.label = 6;
@@ -59582,6 +59704,9 @@ function ReactAIRenderer(_a) {
59582
59704
  };
59583
59705
  parseMDX();
59584
59706
  }, [content]);
59707
+ if (!isBrowser) {
59708
+ return null;
59709
+ }
59585
59710
  return /*#__PURE__*/runtime.jsx(ErrorBoundary, {
59586
59711
  content: content,
59587
59712
  children: /*#__PURE__*/runtime.jsx(React.Fragment, {