react-ai-renderer 0.1.15 → 0.1.17

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.js CHANGED
@@ -8,7 +8,6 @@ import RemarkGfm from 'remark-gfm';
8
8
  import { run, compile } from '@mdx-js/mdx';
9
9
  import RehypeKatex from 'rehype-katex';
10
10
  import RehypeHighlight from 'rehype-highlight';
11
- import RehypeRaw from 'rehype-raw';
12
11
 
13
12
  // SSR 兼容性 polyfill
14
13
  // 这个文件确保在服务端渲染时不会因为访问 document 而报错
@@ -754,7 +753,6 @@ var MDXStreamingParser = /** @class */function () {
754
753
  // 这比手动转换 JavaScript 对象字面量为 JSON 更可靠
755
754
  props[propName] = new Function('return ' + rawValue)();
756
755
  } catch (e) {
757
- console.warn("Failed to parse prop value for ".concat(propName, ":"), value, e);
758
756
  // 如果 Function 构造器失败,尝试手动转换并解析为 JSON
759
757
  try {
760
758
  // 将 JavaScript 对象字面量转换为有效的 JSON
@@ -58338,13 +58336,13 @@ function renderText(textContent, scope, registeredComponents) {
58338
58336
  children: textContent,
58339
58337
  components: registeredComponents,
58340
58338
  remarkPlugins: [RemarkMath, RemarkGfm, RemarkBreaks],
58341
- rehypePlugins: [RehypeRaw, RehypeKatex, [RehypeHighlight, {
58339
+ rehypePlugins: [RehypeKatex, [RehypeHighlight, {
58342
58340
  detect: false,
58343
58341
  ignoreMissing: true,
58344
58342
  plainText: ['mermaid'] // 将mermaid作为纯文本处理,避免被错误高亮
58345
58343
  }]],
58346
58344
  remarkRehypeOptions: {
58347
- allowDangerousHtml: true // 允许处理 HTML 标签
58345
+ allowDangerousHtml: false // 允许处理 HTML 标签
58348
58346
  }
58349
58347
  })];
58350
58348
  });
@@ -58445,7 +58443,7 @@ function renderMdx(mdxContent, scope, registeredComponents) {
58445
58443
  // 递归解析组件的函数 - 返回结构化对象
58446
58444
  function parseComponentRecursively(allComponentHandlers, item, scope) {
58447
58445
  return __awaiter(this, void 0, void 0, function () {
58448
- var componentHandler, props, propsString, childrenContent_1, parsedChildren_1, componentString, isComplete;
58446
+ var isComplete, componentHandler, props, propsString, childrenContent_1, parsedChildren_1, componentString;
58449
58447
  return __generator(this, function (_a) {
58450
58448
  switch (_a.label) {
58451
58449
  case 0:
@@ -58456,6 +58454,7 @@ function parseComponentRecursively(allComponentHandlers, item, scope) {
58456
58454
  }];
58457
58455
  }
58458
58456
  if (!(item.type === 'component')) return [3 /*break*/, 3];
58457
+ isComplete = item.isComplete === true;
58459
58458
  componentHandler = allComponentHandlers.find(function (c) {
58460
58459
  return c.name === item.value;
58461
58460
  });
@@ -58479,7 +58478,7 @@ function parseComponentRecursively(allComponentHandlers, item, scope) {
58479
58478
  _a.label = 2;
58480
58479
  case 2:
58481
58480
  componentString = (componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.selfClosing) ? "<".concat(item.value, " ").concat(propsString, " />") : ["<".concat(item.value, " ").concat(propsString, ">"), childrenContent_1, "</".concat(item.value, ">")].join('\n');
58482
- isComplete = item.isComplete === true;
58481
+ // 调用组件的渲染钩子
58483
58482
  if (isComplete) {
58484
58483
  // 渲染完成后的钩子
58485
58484
  if (componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.onRenderFinished) {
@@ -58603,18 +58602,533 @@ function MdxLayout(_a) {
58603
58602
  });
58604
58603
  }
58605
58604
 
58605
+ function generateSkeletonByComponentName(componentName, props, style) {
58606
+ var _a, _b;
58607
+ var baseStyle = __assign({
58608
+ borderRadius: '8px',
58609
+ backgroundColor: '#f3f4f6'
58610
+ }, style);
58611
+ // 根据组件名称模式匹配生成不同的骨架图
58612
+ var name = componentName.toLowerCase();
58613
+ // 图表类组件
58614
+ if (name.includes('chart') || name.includes('graph') || name.includes('plot')) {
58615
+ return /*#__PURE__*/jsxs("div", {
58616
+ style: __assign(__assign({}, baseStyle), {
58617
+ padding: '24px',
58618
+ minHeight: (props === null || props === void 0 ? void 0 : props.height) ? "".concat(props.height, "px") : '300px',
58619
+ display: 'flex',
58620
+ flexDirection: 'column',
58621
+ gap: '12px'
58622
+ }),
58623
+ children: [/*#__PURE__*/jsxs("div", {
58624
+ style: {
58625
+ display: 'flex',
58626
+ justifyContent: 'space-between',
58627
+ alignItems: 'center',
58628
+ marginBottom: '8px'
58629
+ },
58630
+ children: [/*#__PURE__*/jsx("div", {
58631
+ style: {
58632
+ width: '120px',
58633
+ height: '20px',
58634
+ backgroundColor: '#e5e7eb',
58635
+ borderRadius: '4px'
58636
+ }
58637
+ }), /*#__PURE__*/jsx("div", {
58638
+ style: {
58639
+ width: '80px',
58640
+ height: '16px',
58641
+ backgroundColor: '#e5e7eb',
58642
+ borderRadius: '4px'
58643
+ }
58644
+ })]
58645
+ }), /*#__PURE__*/jsx("div", {
58646
+ style: {
58647
+ flex: 1,
58648
+ display: 'flex',
58649
+ alignItems: 'flex-end',
58650
+ gap: '8px',
58651
+ height: '200px'
58652
+ },
58653
+ children: Array.from({
58654
+ length: 8
58655
+ }).map(function (_, i) {
58656
+ return /*#__PURE__*/jsx("div", {
58657
+ style: {
58658
+ flex: 1,
58659
+ height: "".concat(Math.random() * 60 + 40, "%"),
58660
+ backgroundColor: '#d1d5db',
58661
+ borderRadius: '4px 4px 0 0',
58662
+ animation: 'pulse 1.5s ease-in-out infinite',
58663
+ animationDelay: "".concat(i * 0.1, "s")
58664
+ }
58665
+ }, i);
58666
+ })
58667
+ }), /*#__PURE__*/jsx("div", {
58668
+ style: {
58669
+ display: 'flex',
58670
+ gap: '12px',
58671
+ marginTop: '8px'
58672
+ },
58673
+ children: Array.from({
58674
+ length: 3
58675
+ }).map(function (_, i) {
58676
+ return /*#__PURE__*/jsxs("div", {
58677
+ style: {
58678
+ display: 'flex',
58679
+ alignItems: 'center',
58680
+ gap: '6px'
58681
+ },
58682
+ children: [/*#__PURE__*/jsx("div", {
58683
+ style: {
58684
+ width: '12px',
58685
+ height: '12px',
58686
+ backgroundColor: '#d1d5db',
58687
+ borderRadius: '2px'
58688
+ }
58689
+ }), /*#__PURE__*/jsx("div", {
58690
+ style: {
58691
+ width: '60px',
58692
+ height: '12px',
58693
+ backgroundColor: '#e5e7eb',
58694
+ borderRadius: '2px'
58695
+ }
58696
+ })]
58697
+ }, i);
58698
+ })
58699
+ }), /*#__PURE__*/jsx("style", {
58700
+ children: "\n @keyframes pulse {\n 0%, 100% { opacity: 1; }\n 50% { opacity: 0.5; }\n }\n "
58701
+ })]
58702
+ });
58703
+ }
58704
+ // 表格类组件
58705
+ if (name.includes('table') || name.includes('grid')) {
58706
+ var rowCount_1 = (props === null || props === void 0 ? void 0 : props.rows) || 5;
58707
+ var colCount_1 = (props === null || props === void 0 ? void 0 : props.columns) || 4;
58708
+ return /*#__PURE__*/jsxs("div", {
58709
+ style: __assign(__assign({}, baseStyle), {
58710
+ padding: '16px',
58711
+ overflow: 'hidden'
58712
+ }),
58713
+ children: [/*#__PURE__*/jsxs("div", {
58714
+ style: {
58715
+ marginBottom: '12px'
58716
+ },
58717
+ children: [/*#__PURE__*/jsx("div", {
58718
+ style: {
58719
+ width: '150px',
58720
+ height: '20px',
58721
+ backgroundColor: '#e5e7eb',
58722
+ borderRadius: '4px',
58723
+ marginBottom: '8px'
58724
+ }
58725
+ }), /*#__PURE__*/jsx("div", {
58726
+ style: {
58727
+ width: '200px',
58728
+ height: '16px',
58729
+ backgroundColor: '#e5e7eb',
58730
+ borderRadius: '4px'
58731
+ }
58732
+ })]
58733
+ }), /*#__PURE__*/jsxs("div", {
58734
+ style: {
58735
+ border: '1px solid #e5e7eb',
58736
+ borderRadius: '4px',
58737
+ overflow: 'hidden'
58738
+ },
58739
+ children: [/*#__PURE__*/jsx("div", {
58740
+ style: {
58741
+ display: 'grid',
58742
+ gridTemplateColumns: "repeat(".concat(colCount_1, ", 1fr)"),
58743
+ backgroundColor: '#f9fafb',
58744
+ borderBottom: '1px solid #e5e7eb'
58745
+ },
58746
+ children: Array.from({
58747
+ length: colCount_1
58748
+ }).map(function (_, i) {
58749
+ return /*#__PURE__*/jsx("div", {
58750
+ style: {
58751
+ padding: '12px',
58752
+ borderRight: i < colCount_1 - 1 ? '1px solid #e5e7eb' : 'none'
58753
+ },
58754
+ children: /*#__PURE__*/jsx("div", {
58755
+ style: {
58756
+ width: '80%',
58757
+ height: '16px',
58758
+ backgroundColor: '#d1d5db',
58759
+ borderRadius: '2px'
58760
+ }
58761
+ })
58762
+ }, i);
58763
+ })
58764
+ }), Array.from({
58765
+ length: rowCount_1
58766
+ }).map(function (_, rowIndex) {
58767
+ return /*#__PURE__*/jsx("div", {
58768
+ style: {
58769
+ display: 'grid',
58770
+ gridTemplateColumns: "repeat(".concat(colCount_1, ", 1fr)"),
58771
+ borderBottom: rowIndex < rowCount_1 - 1 ? '1px solid #e5e7eb' : 'none'
58772
+ },
58773
+ children: Array.from({
58774
+ length: colCount_1
58775
+ }).map(function (_, colIndex) {
58776
+ return /*#__PURE__*/jsx("div", {
58777
+ style: {
58778
+ padding: '12px',
58779
+ borderRight: colIndex < colCount_1 - 1 ? '1px solid #e5e7eb' : 'none'
58780
+ },
58781
+ children: /*#__PURE__*/jsx("div", {
58782
+ style: {
58783
+ width: "".concat(Math.random() * 40 + 60, "%"),
58784
+ height: '14px',
58785
+ backgroundColor: '#e5e7eb',
58786
+ borderRadius: '2px'
58787
+ }
58788
+ })
58789
+ }, colIndex);
58790
+ })
58791
+ }, rowIndex);
58792
+ })]
58793
+ })]
58794
+ });
58795
+ }
58796
+ // 卡片类组件
58797
+ if (name.includes('card')) {
58798
+ return /*#__PURE__*/jsxs("div", {
58799
+ style: __assign(__assign({}, baseStyle), {
58800
+ padding: '20px',
58801
+ minHeight: '200px'
58802
+ }),
58803
+ children: [/*#__PURE__*/jsx("div", {
58804
+ style: {
58805
+ width: '60%',
58806
+ height: '24px',
58807
+ backgroundColor: '#e5e7eb',
58808
+ borderRadius: '4px',
58809
+ marginBottom: '16px'
58810
+ }
58811
+ }), /*#__PURE__*/jsx("div", {
58812
+ style: {
58813
+ width: '100%',
58814
+ height: '16px',
58815
+ backgroundColor: '#e5e7eb',
58816
+ borderRadius: '4px',
58817
+ marginBottom: '8px'
58818
+ }
58819
+ }), /*#__PURE__*/jsx("div", {
58820
+ style: {
58821
+ width: '90%',
58822
+ height: '16px',
58823
+ backgroundColor: '#e5e7eb',
58824
+ borderRadius: '4px',
58825
+ marginBottom: '8px'
58826
+ }
58827
+ }), /*#__PURE__*/jsx("div", {
58828
+ style: {
58829
+ width: '70%',
58830
+ height: '16px',
58831
+ backgroundColor: '#e5e7eb',
58832
+ borderRadius: '4px',
58833
+ marginBottom: '16px'
58834
+ }
58835
+ }), /*#__PURE__*/jsxs("div", {
58836
+ style: {
58837
+ display: 'flex',
58838
+ gap: '8px',
58839
+ marginTop: '16px'
58840
+ },
58841
+ children: [/*#__PURE__*/jsx("div", {
58842
+ style: {
58843
+ width: '80px',
58844
+ height: '32px',
58845
+ backgroundColor: '#d1d5db',
58846
+ borderRadius: '4px'
58847
+ }
58848
+ }), /*#__PURE__*/jsx("div", {
58849
+ style: {
58850
+ width: '80px',
58851
+ height: '32px',
58852
+ backgroundColor: '#d1d5db',
58853
+ borderRadius: '4px'
58854
+ }
58855
+ })]
58856
+ })]
58857
+ });
58858
+ }
58859
+ // 列表类组件
58860
+ if (name.includes('list') || name.includes('todo')) {
58861
+ var itemCount_1 = ((_a = props === null || props === void 0 ? void 0 : props.items) === null || _a === void 0 ? void 0 : _a.length) || (props === null || props === void 0 ? void 0 : props.count) || 3;
58862
+ return /*#__PURE__*/jsx("div", {
58863
+ style: __assign(__assign({}, baseStyle), {
58864
+ padding: '16px'
58865
+ }),
58866
+ children: Array.from({
58867
+ length: itemCount_1
58868
+ }).map(function (_, i) {
58869
+ return /*#__PURE__*/jsxs("div", {
58870
+ style: {
58871
+ display: 'flex',
58872
+ alignItems: 'center',
58873
+ gap: '12px',
58874
+ marginBottom: i < itemCount_1 - 1 ? '12px' : '0',
58875
+ paddingBottom: i < itemCount_1 - 1 ? '12px' : '0',
58876
+ borderBottom: i < itemCount_1 - 1 ? '1px solid #e5e7eb' : 'none'
58877
+ },
58878
+ children: [/*#__PURE__*/jsx("div", {
58879
+ style: {
58880
+ width: '20px',
58881
+ height: '20px',
58882
+ backgroundColor: '#d1d5db',
58883
+ borderRadius: '4px',
58884
+ flexShrink: 0
58885
+ }
58886
+ }), /*#__PURE__*/jsxs("div", {
58887
+ style: {
58888
+ flex: 1
58889
+ },
58890
+ children: [/*#__PURE__*/jsx("div", {
58891
+ style: {
58892
+ width: "".concat(Math.random() * 30 + 60, "%"),
58893
+ height: '16px',
58894
+ backgroundColor: '#e5e7eb',
58895
+ borderRadius: '4px',
58896
+ marginBottom: '6px'
58897
+ }
58898
+ }), /*#__PURE__*/jsx("div", {
58899
+ style: {
58900
+ width: "".concat(Math.random() * 40 + 40, "%"),
58901
+ height: '14px',
58902
+ backgroundColor: '#e5e7eb',
58903
+ borderRadius: '4px'
58904
+ }
58905
+ })]
58906
+ })]
58907
+ }, i);
58908
+ })
58909
+ });
58910
+ }
58911
+ // 表单类组件
58912
+ if (name.includes('form') || name.includes('input') || name.includes('select')) {
58913
+ var fieldCount_1 = ((_b = props === null || props === void 0 ? void 0 : props.fields) === null || _b === void 0 ? void 0 : _b.length) || 3;
58914
+ return /*#__PURE__*/jsxs("div", {
58915
+ style: __assign(__assign({}, baseStyle), {
58916
+ padding: '20px'
58917
+ }),
58918
+ children: [Array.from({
58919
+ length: fieldCount_1
58920
+ }).map(function (_, i) {
58921
+ return /*#__PURE__*/jsxs("div", {
58922
+ style: {
58923
+ marginBottom: i < fieldCount_1 - 1 ? '20px' : '0'
58924
+ },
58925
+ children: [/*#__PURE__*/jsx("div", {
58926
+ style: {
58927
+ width: '100px',
58928
+ height: '14px',
58929
+ backgroundColor: '#e5e7eb',
58930
+ borderRadius: '2px',
58931
+ marginBottom: '8px'
58932
+ }
58933
+ }), /*#__PURE__*/jsx("div", {
58934
+ style: {
58935
+ width: '100%',
58936
+ height: '40px',
58937
+ backgroundColor: '#f3f4f6',
58938
+ border: '1px solid #e5e7eb',
58939
+ borderRadius: '4px'
58940
+ }
58941
+ })]
58942
+ }, i);
58943
+ }), /*#__PURE__*/jsxs("div", {
58944
+ style: {
58945
+ display: 'flex',
58946
+ gap: '12px',
58947
+ marginTop: '20px'
58948
+ },
58949
+ children: [/*#__PURE__*/jsx("div", {
58950
+ style: {
58951
+ width: '100px',
58952
+ height: '36px',
58953
+ backgroundColor: '#d1d5db',
58954
+ borderRadius: '4px'
58955
+ }
58956
+ }), /*#__PURE__*/jsx("div", {
58957
+ style: {
58958
+ width: '100px',
58959
+ height: '36px',
58960
+ backgroundColor: '#e5e7eb',
58961
+ borderRadius: '4px'
58962
+ }
58963
+ })]
58964
+ })]
58965
+ });
58966
+ }
58967
+ // 图片/媒体类组件
58968
+ if (name.includes('image') || name.includes('img') || name.includes('video') || name.includes('media')) {
58969
+ return /*#__PURE__*/jsx("div", {
58970
+ style: __assign(__assign({}, baseStyle), {
58971
+ padding: '16px',
58972
+ display: 'flex',
58973
+ alignItems: 'center',
58974
+ justifyContent: 'center',
58975
+ minHeight: (props === null || props === void 0 ? void 0 : props.height) ? "".concat(props.height, "px") : '200px'
58976
+ }),
58977
+ children: /*#__PURE__*/jsx("div", {
58978
+ style: {
58979
+ width: '100%',
58980
+ height: '100%',
58981
+ backgroundColor: '#e5e7eb',
58982
+ borderRadius: '8px',
58983
+ display: 'flex',
58984
+ alignItems: 'center',
58985
+ justifyContent: 'center'
58986
+ },
58987
+ children: /*#__PURE__*/jsx("div", {
58988
+ style: {
58989
+ width: '48px',
58990
+ height: '48px',
58991
+ backgroundColor: '#d1d5db',
58992
+ borderRadius: '50%',
58993
+ display: 'flex',
58994
+ alignItems: 'center',
58995
+ justifyContent: 'center'
58996
+ },
58997
+ children: /*#__PURE__*/jsx("div", {
58998
+ style: {
58999
+ width: '24px',
59000
+ height: '24px',
59001
+ backgroundColor: '#9ca3af',
59002
+ borderRadius: '4px'
59003
+ }
59004
+ })
59005
+ })
59006
+ })
59007
+ });
59008
+ }
59009
+ // 默认骨架图
59010
+ return /*#__PURE__*/jsxs("div", {
59011
+ style: __assign(__assign({}, baseStyle), {
59012
+ padding: '24px',
59013
+ minHeight: '120px',
59014
+ display: 'flex',
59015
+ flexDirection: 'column',
59016
+ gap: '12px',
59017
+ alignItems: 'center',
59018
+ justifyContent: 'center'
59019
+ }),
59020
+ children: [/*#__PURE__*/jsxs("div", {
59021
+ style: {
59022
+ width: '100%',
59023
+ maxWidth: '400px'
59024
+ },
59025
+ children: [/*#__PURE__*/jsx("div", {
59026
+ style: {
59027
+ width: '60%',
59028
+ height: '20px',
59029
+ backgroundColor: '#e5e7eb',
59030
+ borderRadius: '4px',
59031
+ marginBottom: '12px'
59032
+ }
59033
+ }), /*#__PURE__*/jsx("div", {
59034
+ style: {
59035
+ width: '100%',
59036
+ height: '16px',
59037
+ backgroundColor: '#e5e7eb',
59038
+ borderRadius: '4px',
59039
+ marginBottom: '8px'
59040
+ }
59041
+ }), /*#__PURE__*/jsx("div", {
59042
+ style: {
59043
+ width: '90%',
59044
+ height: '16px',
59045
+ backgroundColor: '#e5e7eb',
59046
+ borderRadius: '4px',
59047
+ marginBottom: '8px'
59048
+ }
59049
+ }), /*#__PURE__*/jsx("div", {
59050
+ style: {
59051
+ width: '75%',
59052
+ height: '16px',
59053
+ backgroundColor: '#e5e7eb',
59054
+ borderRadius: '4px'
59055
+ }
59056
+ })]
59057
+ }), /*#__PURE__*/jsxs("div", {
59058
+ style: {
59059
+ display: 'flex',
59060
+ gap: '8px',
59061
+ marginTop: '12px'
59062
+ },
59063
+ children: [/*#__PURE__*/jsx("div", {
59064
+ style: {
59065
+ width: '60px',
59066
+ height: '32px',
59067
+ backgroundColor: '#d1d5db',
59068
+ borderRadius: '4px'
59069
+ }
59070
+ }), /*#__PURE__*/jsx("div", {
59071
+ style: {
59072
+ width: '60px',
59073
+ height: '32px',
59074
+ backgroundColor: '#d1d5db',
59075
+ borderRadius: '4px'
59076
+ }
59077
+ })]
59078
+ })]
59079
+ });
59080
+ }
59081
+ /**
59082
+ * 创建骨架图组件
59083
+ */
59084
+ function createSkeletonComponent(componentName, componentData, skeletonConfig, style) {
59085
+ // 如果提供了自定义骨架图组件
59086
+ if (skeletonConfig === null || skeletonConfig === void 0 ? void 0 : skeletonConfig.component) {
59087
+ var SkeletonComponent = skeletonConfig.component;
59088
+ return /*#__PURE__*/jsx(SkeletonComponent, {
59089
+ componentName: componentName,
59090
+ componentData: componentData,
59091
+ props: componentData === null || componentData === void 0 ? void 0 : componentData.props,
59092
+ style: style
59093
+ });
59094
+ }
59095
+ // 如果提供了自定义骨架图元素
59096
+ if (skeletonConfig === null || skeletonConfig === void 0 ? void 0 : skeletonConfig.element) {
59097
+ return skeletonConfig.element;
59098
+ }
59099
+ // 如果提供了自定义生成函数
59100
+ if (skeletonConfig === null || skeletonConfig === void 0 ? void 0 : skeletonConfig.generator) {
59101
+ return skeletonConfig.generator({
59102
+ componentName: componentName,
59103
+ componentData: componentData,
59104
+ props: componentData === null || componentData === void 0 ? void 0 : componentData.props,
59105
+ style: style
59106
+ });
59107
+ }
59108
+ // 如果启用了自动生成,或者没有提供任何配置
59109
+ if ((skeletonConfig === null || skeletonConfig === void 0 ? void 0 : skeletonConfig.autoGenerate) !== false) {
59110
+ return generateSkeletonByComponentName(componentName, componentData === null || componentData === void 0 ? void 0 : componentData.props, style);
59111
+ }
59112
+ // 默认返回空
59113
+ return /*#__PURE__*/jsx(React__default.Fragment, {});
59114
+ }
59115
+
58606
59116
  var ComponentPlaceholder = function (_a) {
58607
59117
  var componentName = _a.componentName,
58608
59118
  style = _a.style,
58609
- customPlaceholder = _a.customPlaceholder;
58610
- // 如果有自定义占位内容,优先使用
58611
- if (customPlaceholder) {
59119
+ loader = _a.loader,
59120
+ skeleton = _a.skeleton,
59121
+ componentData = _a.componentData;
59122
+ // 如果有自定义加载内容,优先使用
59123
+ if (loader) {
59124
+ // 如果是布尔值 true,使用默认样式
59125
+ if (typeof loader === 'boolean') ;
58612
59126
  // 如果是 React 元素,直接返回
58613
- if (/*#__PURE__*/React__default.isValidElement(customPlaceholder)) {
58614
- return customPlaceholder;
59127
+ else if (/*#__PURE__*/React__default.isValidElement(loader)) {
59128
+ return loader;
58615
59129
  }
58616
59130
  // 如果是字符串,显示字符串
58617
- if (typeof customPlaceholder === 'string') {
59131
+ else if (typeof loader === 'string') {
58618
59132
  return /*#__PURE__*/jsx("div", {
58619
59133
  style: __assign({
58620
59134
  border: '1px dashed #d1d5db',
@@ -58628,34 +59142,47 @@ var ComponentPlaceholder = function (_a) {
58628
59142
  justifyContent: 'center',
58629
59143
  minHeight: '60px'
58630
59144
  }, style),
58631
- children: customPlaceholder
59145
+ children: loader
58632
59146
  });
58633
59147
  }
58634
59148
  // 如果是函数,调用它
58635
- if (typeof customPlaceholder === 'function') {
59149
+ else if (typeof loader === 'function') {
58636
59150
  try {
58637
59151
  // 检查是否是 React 组件构造函数
58638
- var proto = customPlaceholder.prototype;
59152
+ var proto = loader.prototype;
58639
59153
  if (proto && (proto instanceof React__default.Component || proto.isReactComponent)) {
58640
59154
  // 是 React 组件类,创建元素
58641
- var Component = customPlaceholder;
59155
+ var Component = loader;
58642
59156
  return /*#__PURE__*/jsx(Component, {
58643
59157
  componentName: componentName
58644
59158
  });
58645
59159
  } else {
58646
59160
  // 是普通函数,调用它(类型断言为函数)
58647
- var func = customPlaceholder;
59161
+ var func = loader;
58648
59162
  return func(componentName);
58649
59163
  }
58650
59164
  } catch (e) {
58651
59165
  // 如果出错,尝试作为组件使用
58652
- var Component = customPlaceholder;
59166
+ var Component = loader;
58653
59167
  return /*#__PURE__*/jsx(Component, {
58654
59168
  componentName: componentName
58655
59169
  });
58656
59170
  }
58657
59171
  }
58658
59172
  }
59173
+ // 如果没有 loader,尝试使用骨架图
59174
+ if (skeleton || !loader) {
59175
+ try {
59176
+ var skeletonElement = createSkeletonComponent(componentName, componentData, skeleton, style);
59177
+ // 如果骨架图生成成功且不是空元素,使用骨架图
59178
+ if (skeletonElement && /*#__PURE__*/React__default.isValidElement(skeletonElement)) {
59179
+ return skeletonElement;
59180
+ }
59181
+ } catch (e) {
59182
+ // 如果骨架图生成失败,继续使用默认占位内容
59183
+ console.warn('Failed to generate skeleton:', e);
59184
+ }
59185
+ }
58659
59186
  // 默认占位内容
58660
59187
  return /*#__PURE__*/jsxs("div", {
58661
59188
  style: __assign({
@@ -58745,6 +59272,51 @@ var FallbackView = /** @class */function (_super) {
58745
59272
  return FallbackView;
58746
59273
  }(React__default.Component);
58747
59274
 
59275
+ function isEnhancedComponentConfig(value) {
59276
+ return typeof value === 'object' && value !== null && 'value' in value && typeof value.value !== 'undefined';
59277
+ }
59278
+ /**
59279
+ * 从组件值中提取实际的组件
59280
+ */
59281
+ function extractComponent(value) {
59282
+ return isEnhancedComponentConfig(value) ? value.value : value;
59283
+ }
59284
+ /**
59285
+ * 将 components 转换为 ComponentHandler 数组
59286
+ */
59287
+ function convertComponentsToHandlers(components, componentHandlers) {
59288
+ return Object.entries(components).filter(function (_a) {
59289
+ var _b = __read(_a, 1),
59290
+ name = _b[0];
59291
+ return !componentHandlers.some(function (handler) {
59292
+ return handler.name === name;
59293
+ });
59294
+ }).map(function (_a) {
59295
+ var _b = __read(_a, 2),
59296
+ name = _b[0],
59297
+ componentValue = _b[1];
59298
+ if (isEnhancedComponentConfig(componentValue)) {
59299
+ // 增强模式:从配置对象中提取信息
59300
+ var config = componentValue;
59301
+ return {
59302
+ component: config.value,
59303
+ name: name,
59304
+ selfClosing: config.selfClosing !== undefined ? config.selfClosing : isSelfClosingComponent(config.value),
59305
+ onRenderFinished: config.onRenderFinished,
59306
+ onRenderProcess: config.onRenderProcess,
59307
+ loader: config.loader,
59308
+ label: config.label
59309
+ };
59310
+ } else {
59311
+ // 普通模式:直接使用组件
59312
+ return {
59313
+ component: componentValue,
59314
+ name: name,
59315
+ selfClosing: isSelfClosingComponent(componentValue)
59316
+ };
59317
+ }
59318
+ });
59319
+ }
58748
59320
  function ReactAIRenderer(_a) {
58749
59321
  var _this = this;
58750
59322
  var content = _a.content,
@@ -58767,24 +59339,16 @@ function ReactAIRenderer(_a) {
58767
59339
  });
58768
59340
  }
58769
59341
  content = content || children || '';
58770
- var allComponents = __assign(__assign({}, BuiltInComponents), components);
58771
- var allComponentHandlers = __spreadArray(__spreadArray([], __read(components ? Object.entries(components).filter(function (_a) {
58772
- var _b = __read(_a, 1),
58773
- name = _b[0];
58774
- return !componentHandlers.some(function (handler) {
58775
- return handler.name === name;
58776
- });
58777
- }).map(function (_a) {
59342
+ // 构建 allComponents:从增强配置中提取实际的组件
59343
+ var normalizedComponents = components ? Object.fromEntries(Object.entries(components).map(function (_a) {
58778
59344
  var _b = __read(_a, 2),
58779
59345
  name = _b[0],
58780
- component = _b[1];
58781
- var selfClosing = isSelfClosingComponent(component);
58782
- return {
58783
- component: component,
58784
- name: name,
58785
- selfClosing: selfClosing
58786
- };
58787
- }) : []), false), __read(componentHandlers), false);
59346
+ value = _b[1];
59347
+ return [name, extractComponent(value)];
59348
+ })) : {};
59349
+ var allComponents = __assign(__assign({}, BuiltInComponents), normalizedComponents);
59350
+ // 构建 allComponentHandlers:支持增强模式
59351
+ var allComponentHandlers = __spreadArray(__spreadArray([], __read(components ? convertComponentsToHandlers(components, componentHandlers) : []), false), __read(componentHandlers), false);
58788
59352
  var _e = __read(useState(null), 2),
58789
59353
  component = _e[0],
58790
59354
  setComponent = _e[1];
@@ -58792,6 +59356,8 @@ function ReactAIRenderer(_a) {
58792
59356
  var fallbackErrorRef = useRef({
58793
59357
  hasError: false
58794
59358
  });
59359
+ // 用于缓存已完成的组件,避免重复渲染
59360
+ var completedComponentsCacheRef = useRef(new Map());
58795
59361
  var parser = new MDXStreamingParser(allComponentHandlers);
58796
59362
  var mdxContent = content || '';
58797
59363
  // FallbackView 错误状态变化时的回调
@@ -58804,9 +59370,10 @@ function ReactAIRenderer(_a) {
58804
59370
  useEffect(function () {
58805
59371
  var parseMDX = function () {
58806
59372
  return __awaiter(_this, void 0, void 0, function () {
58807
- var ThinkComponent, ResultComponent, protectedContent, _a, thinkContent, resultContent, parsedData, parsedComponents, finalComponent, fallbackComponent, _loop_1, i, MDXComponent, parsedDataLength, lastItem;
58808
- return __generator(this, function (_b) {
58809
- switch (_b.label) {
59373
+ 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;
59374
+ var e_1, _b;
59375
+ return __generator(this, function (_c) {
59376
+ switch (_c.label) {
58810
59377
  case 0:
58811
59378
  ThinkComponent = null;
58812
59379
  ResultComponent = null;
@@ -58815,8 +59382,8 @@ function ReactAIRenderer(_a) {
58815
59382
  if (!thinkContent) return [3 /*break*/, 2];
58816
59383
  return [4 /*yield*/, renderMdx(thinkContent, scope, allComponents)];
58817
59384
  case 1:
58818
- ThinkComponent = _b.sent();
58819
- _b.label = 2;
59385
+ ThinkComponent = _c.sent();
59386
+ _c.label = 2;
58820
59387
  case 2:
58821
59388
  parsedData = [];
58822
59389
  if (!resultContent) return [3 /*break*/, 8];
@@ -58826,46 +59393,110 @@ function ReactAIRenderer(_a) {
58826
59393
  return parseComponentRecursively(allComponentHandlers, item, scope);
58827
59394
  }))];
58828
59395
  case 3:
58829
- parsedComponents = _b.sent();
59396
+ parsedComponents = _c.sent();
59397
+ currentComponentCount = parsedData.length;
59398
+ cacheKeys = Array.from(completedComponentsCacheRef.current.keys());
59399
+ try {
59400
+ for (cacheKeys_1 = __values(cacheKeys), cacheKeys_1_1 = cacheKeys_1.next(); !cacheKeys_1_1.done; cacheKeys_1_1 = cacheKeys_1.next()) {
59401
+ key = cacheKeys_1_1.value;
59402
+ indexMatch = key.match(/-(\d+)$/);
59403
+ if (indexMatch) {
59404
+ index = parseInt(indexMatch[1], 10);
59405
+ if (index >= currentComponentCount) {
59406
+ // 如果索引超出当前组件数量,删除缓存
59407
+ completedComponentsCacheRef.current.delete(key);
59408
+ }
59409
+ }
59410
+ }
59411
+ } catch (e_1_1) {
59412
+ e_1 = {
59413
+ error: e_1_1
59414
+ };
59415
+ } finally {
59416
+ try {
59417
+ if (cacheKeys_1_1 && !cacheKeys_1_1.done && (_b = cacheKeys_1.return)) _b.call(cacheKeys_1);
59418
+ } finally {
59419
+ if (e_1) throw e_1.error;
59420
+ }
59421
+ }
58830
59422
  finalComponent = [];
58831
59423
  fallbackComponent = [];
58832
59424
  _loop_1 = function (i) {
58833
- var item, _result, placeholderForFallback, componentNameMatch, componentName_1, componentHandler, displayName;
58834
- return __generator(this, function (_c) {
58835
- switch (_c.label) {
59425
+ var item, currentParsedItem, _result, placeholderForFallback, componentNameMatch, componentName_1, componentHandler, displayName, cacheKey;
59426
+ return __generator(this, function (_d) {
59427
+ switch (_d.label) {
58836
59428
  case 0:
58837
59429
  item = parsedComponents[i];
59430
+ currentParsedItem = parsedData[i];
58838
59431
  _result = void 0;
58839
59432
  placeholderForFallback = null;
58840
59433
  if (!(item.type === 'text')) return [3 /*break*/, 2];
58841
59434
  return [4 /*yield*/, renderText(item.value, scope, allComponents)];
58842
59435
  case 1:
58843
- _result = _c.sent();
58844
- return [3 /*break*/, 4];
59436
+ _result = _d.sent();
59437
+ finalComponent.push(_result);
59438
+ return [3 /*break*/, 11];
58845
59439
  case 2:
58846
- return [4 /*yield*/, renderMdx(item.value, scope, allComponents)];
58847
- case 3:
58848
- _result = _c.sent();
58849
59440
  componentNameMatch = item.value.match(/<([A-Z][A-Za-z0-9]*)/);
58850
- if (componentNameMatch) {
58851
- componentName_1 = componentNameMatch[1];
58852
- componentHandler = allComponentHandlers.find(function (c) {
58853
- return c.name === componentName_1;
58854
- });
58855
- displayName = componentName_1;
58856
- if (componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.label) {
58857
- displayName = componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.label;
58858
- }
58859
- placeholderForFallback = /*#__PURE__*/React__default.createElement(ComponentPlaceholder, {
58860
- componentName: displayName,
58861
- customPlaceholder: componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.placeholder,
58862
- key: "fallback-placeholder-".concat(i)
58863
- });
59441
+ if (!componentNameMatch) return [3 /*break*/, 9];
59442
+ componentName_1 = componentNameMatch[1];
59443
+ componentHandler = allComponentHandlers.find(function (c) {
59444
+ return c.name === componentName_1;
59445
+ });
59446
+ displayName = componentName_1;
59447
+ if (componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.label) {
59448
+ displayName = componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.label;
58864
59449
  }
58865
- _c.label = 4;
59450
+ placeholderForFallback = /*#__PURE__*/React__default.createElement(ComponentPlaceholder, {
59451
+ componentName: displayName,
59452
+ loader: componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.loader,
59453
+ key: "fallback-loader-".concat(componentName_1)
59454
+ });
59455
+ if (!(currentParsedItem.isComplete === true || !(componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.loader))) return [3 /*break*/, 6];
59456
+ cacheKey = "mdx-component-".concat(componentName_1, "-").concat(i);
59457
+ if (!completedComponentsCacheRef.current.has(cacheKey)) return [3 /*break*/, 3];
59458
+ // 使用缓存的组件
59459
+ _result = completedComponentsCacheRef.current.get(cacheKey);
59460
+ return [3 /*break*/, 5];
59461
+ case 3:
59462
+ return [4 /*yield*/, renderMdx(item.value, scope, allComponents)];
58866
59463
  case 4:
59464
+ // 缓存不存在,调用 renderMdx 并存入缓存
59465
+ _result = _d.sent();
59466
+ // 为组件设置 key,确保 React 能正确识别
59467
+ if (/*#__PURE__*/React__default.isValidElement(_result)) {
59468
+ _result = /*#__PURE__*/React__default.cloneElement(_result, {
59469
+ key: cacheKey
59470
+ });
59471
+ } else {
59472
+ _result = /*#__PURE__*/React__default.createElement(React__default.Fragment, {
59473
+ key: cacheKey
59474
+ }, _result);
59475
+ }
59476
+ // 存入缓存
59477
+ completedComponentsCacheRef.current.set(cacheKey, _result);
59478
+ _d.label = 5;
59479
+ case 5:
59480
+ finalComponent.push(_result);
59481
+ return [3 /*break*/, 8];
59482
+ case 6:
59483
+ return [4 /*yield*/, renderMdx(item.value, scope, allComponents)];
59484
+ case 7:
59485
+ // 组件未完成,正常渲染
59486
+ _result = _d.sent();
59487
+ finalComponent.push(placeholderForFallback);
59488
+ _d.label = 8;
59489
+ case 8:
59490
+ return [3 /*break*/, 11];
59491
+ case 9:
59492
+ return [4 /*yield*/, renderMdx(item.value, scope, allComponents)];
59493
+ case 10:
59494
+ // 没有匹配到组件名,正常渲染
59495
+ _result = _d.sent();
58867
59496
  finalComponent.push(_result);
58868
- // fallbackComponent 应该是 finalComponent 的前 i 个元素 + 当前 item 的 placeholder
59497
+ _d.label = 11;
59498
+ case 11:
59499
+ // fallbackComponent 应该是 finalComponent 的前 i 个元素 + 当前 item 的 loader
58869
59500
  fallbackComponent.length = 0;
58870
59501
  fallbackComponent.push.apply(fallbackComponent, __spreadArray([], __read(finalComponent.slice(0, i)), false));
58871
59502
  if (placeholderForFallback) {
@@ -58876,13 +59507,13 @@ function ReactAIRenderer(_a) {
58876
59507
  });
58877
59508
  };
58878
59509
  i = 0;
58879
- _b.label = 4;
59510
+ _c.label = 4;
58880
59511
  case 4:
58881
59512
  if (!(i < parsedComponents.length)) return [3 /*break*/, 7];
58882
59513
  return [5 /*yield**/, _loop_1(i)];
58883
59514
  case 5:
58884
- _b.sent();
58885
- _b.label = 6;
59515
+ _c.sent();
59516
+ _c.label = 6;
58886
59517
  case 6:
58887
59518
  i++;
58888
59519
  return [3 /*break*/, 4];
@@ -58897,7 +59528,7 @@ function ReactAIRenderer(_a) {
58897
59528
  children: finalComponent
58898
59529
  })
58899
59530
  });
58900
- _b.label = 8;
59531
+ _c.label = 8;
58901
59532
  case 8:
58902
59533
  MDXComponent = /*#__PURE__*/jsxs(React__default.Fragment, {
58903
59534
  children: [ThinkComponent, ResultComponent]