react-ai-renderer 0.1.14 → 0.1.16
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 +173 -66
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +22 -5
- package/dist/index.js +173 -66
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -776,7 +776,6 @@ var MDXStreamingParser = /** @class */function () {
|
|
|
776
776
|
// 这比手动转换 JavaScript 对象字面量为 JSON 更可靠
|
|
777
777
|
props[propName] = new Function('return ' + rawValue)();
|
|
778
778
|
} catch (e) {
|
|
779
|
-
console.warn("Failed to parse prop value for ".concat(propName, ":"), value, e);
|
|
780
779
|
// 如果 Function 构造器失败,尝试手动转换并解析为 JSON
|
|
781
780
|
try {
|
|
782
781
|
// 将 JavaScript 对象字面量转换为有效的 JSON
|
|
@@ -58389,7 +58388,8 @@ function preprocessMdxContent(content) {
|
|
|
58389
58388
|
// 将其他自闭合 HTML 标签转换为 JSX 格式
|
|
58390
58389
|
.replace(/<(hr|img|input)([^>]*)(?<!\/)>/gi, '<$1$2 />');
|
|
58391
58390
|
// 转义非标准格式的内容
|
|
58392
|
-
|
|
58391
|
+
// 支持以下划线开头的组件名(如 _THINK)
|
|
58392
|
+
return processedContent.replace(/<(?!\/|[A-Z_][a-zA-Z0-9_]*\b|[a-z][a-zA-Z0-9]*\b)/g, '<');
|
|
58393
58393
|
}
|
|
58394
58394
|
// 辅助函数:格式化props
|
|
58395
58395
|
function formatProps(props) {
|
|
@@ -58466,7 +58466,7 @@ function renderMdx(mdxContent, scope, registeredComponents) {
|
|
|
58466
58466
|
// 递归解析组件的函数 - 返回结构化对象
|
|
58467
58467
|
function parseComponentRecursively(allComponentHandlers, item, scope) {
|
|
58468
58468
|
return __awaiter(this, void 0, void 0, function () {
|
|
58469
|
-
var componentHandler, props, propsString, childrenContent_1, parsedChildren_1, componentString
|
|
58469
|
+
var isComplete, componentHandler, props, propsString, childrenContent_1, parsedChildren_1, componentString;
|
|
58470
58470
|
return __generator(this, function (_a) {
|
|
58471
58471
|
switch (_a.label) {
|
|
58472
58472
|
case 0:
|
|
@@ -58477,6 +58477,7 @@ function parseComponentRecursively(allComponentHandlers, item, scope) {
|
|
|
58477
58477
|
}];
|
|
58478
58478
|
}
|
|
58479
58479
|
if (!(item.type === 'component')) return [3 /*break*/, 3];
|
|
58480
|
+
isComplete = item.isComplete === true;
|
|
58480
58481
|
componentHandler = allComponentHandlers.find(function (c) {
|
|
58481
58482
|
return c.name === item.value;
|
|
58482
58483
|
});
|
|
@@ -58500,7 +58501,7 @@ function parseComponentRecursively(allComponentHandlers, item, scope) {
|
|
|
58500
58501
|
_a.label = 2;
|
|
58501
58502
|
case 2:
|
|
58502
58503
|
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');
|
|
58503
|
-
|
|
58504
|
+
// 调用组件的渲染钩子
|
|
58504
58505
|
if (isComplete) {
|
|
58505
58506
|
// 渲染完成后的钩子
|
|
58506
58507
|
if (componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.onRenderFinished) {
|
|
@@ -58627,15 +58628,17 @@ function MdxLayout(_a) {
|
|
|
58627
58628
|
var ComponentPlaceholder = function (_a) {
|
|
58628
58629
|
var componentName = _a.componentName,
|
|
58629
58630
|
style = _a.style,
|
|
58630
|
-
|
|
58631
|
-
//
|
|
58632
|
-
if (
|
|
58631
|
+
loader = _a.loader;
|
|
58632
|
+
// 如果有自定义加载内容,优先使用
|
|
58633
|
+
if (loader) {
|
|
58634
|
+
// 如果是布尔值 true,使用默认样式
|
|
58635
|
+
if (typeof loader === 'boolean') ;
|
|
58633
58636
|
// 如果是 React 元素,直接返回
|
|
58634
|
-
if (/*#__PURE__*/React.isValidElement(
|
|
58635
|
-
return
|
|
58637
|
+
else if (/*#__PURE__*/React.isValidElement(loader)) {
|
|
58638
|
+
return loader;
|
|
58636
58639
|
}
|
|
58637
58640
|
// 如果是字符串,显示字符串
|
|
58638
|
-
if (typeof
|
|
58641
|
+
else if (typeof loader === 'string') {
|
|
58639
58642
|
return /*#__PURE__*/runtime.jsx("div", {
|
|
58640
58643
|
style: __assign({
|
|
58641
58644
|
border: '1px dashed #d1d5db',
|
|
@@ -58649,28 +58652,28 @@ var ComponentPlaceholder = function (_a) {
|
|
|
58649
58652
|
justifyContent: 'center',
|
|
58650
58653
|
minHeight: '60px'
|
|
58651
58654
|
}, style),
|
|
58652
|
-
children:
|
|
58655
|
+
children: loader
|
|
58653
58656
|
});
|
|
58654
58657
|
}
|
|
58655
58658
|
// 如果是函数,调用它
|
|
58656
|
-
if (typeof
|
|
58659
|
+
else if (typeof loader === 'function') {
|
|
58657
58660
|
try {
|
|
58658
58661
|
// 检查是否是 React 组件构造函数
|
|
58659
|
-
var proto =
|
|
58662
|
+
var proto = loader.prototype;
|
|
58660
58663
|
if (proto && (proto instanceof React.Component || proto.isReactComponent)) {
|
|
58661
58664
|
// 是 React 组件类,创建元素
|
|
58662
|
-
var Component =
|
|
58665
|
+
var Component = loader;
|
|
58663
58666
|
return /*#__PURE__*/runtime.jsx(Component, {
|
|
58664
58667
|
componentName: componentName
|
|
58665
58668
|
});
|
|
58666
58669
|
} else {
|
|
58667
58670
|
// 是普通函数,调用它(类型断言为函数)
|
|
58668
|
-
var func =
|
|
58671
|
+
var func = loader;
|
|
58669
58672
|
return func(componentName);
|
|
58670
58673
|
}
|
|
58671
58674
|
} catch (e) {
|
|
58672
58675
|
// 如果出错,尝试作为组件使用
|
|
58673
|
-
var Component =
|
|
58676
|
+
var Component = loader;
|
|
58674
58677
|
return /*#__PURE__*/runtime.jsx(Component, {
|
|
58675
58678
|
componentName: componentName
|
|
58676
58679
|
});
|
|
@@ -58766,6 +58769,51 @@ var FallbackView = /** @class */function (_super) {
|
|
|
58766
58769
|
return FallbackView;
|
|
58767
58770
|
}(React.Component);
|
|
58768
58771
|
|
|
58772
|
+
function isEnhancedComponentConfig(value) {
|
|
58773
|
+
return typeof value === 'object' && value !== null && 'value' in value && typeof value.value !== 'undefined';
|
|
58774
|
+
}
|
|
58775
|
+
/**
|
|
58776
|
+
* 从组件值中提取实际的组件
|
|
58777
|
+
*/
|
|
58778
|
+
function extractComponent(value) {
|
|
58779
|
+
return isEnhancedComponentConfig(value) ? value.value : value;
|
|
58780
|
+
}
|
|
58781
|
+
/**
|
|
58782
|
+
* 将 components 转换为 ComponentHandler 数组
|
|
58783
|
+
*/
|
|
58784
|
+
function convertComponentsToHandlers(components, componentHandlers) {
|
|
58785
|
+
return Object.entries(components).filter(function (_a) {
|
|
58786
|
+
var _b = __read(_a, 1),
|
|
58787
|
+
name = _b[0];
|
|
58788
|
+
return !componentHandlers.some(function (handler) {
|
|
58789
|
+
return handler.name === name;
|
|
58790
|
+
});
|
|
58791
|
+
}).map(function (_a) {
|
|
58792
|
+
var _b = __read(_a, 2),
|
|
58793
|
+
name = _b[0],
|
|
58794
|
+
componentValue = _b[1];
|
|
58795
|
+
if (isEnhancedComponentConfig(componentValue)) {
|
|
58796
|
+
// 增强模式:从配置对象中提取信息
|
|
58797
|
+
var config = componentValue;
|
|
58798
|
+
return {
|
|
58799
|
+
component: config.value,
|
|
58800
|
+
name: name,
|
|
58801
|
+
selfClosing: config.selfClosing !== undefined ? config.selfClosing : isSelfClosingComponent(config.value),
|
|
58802
|
+
onRenderFinished: config.onRenderFinished,
|
|
58803
|
+
onRenderProcess: config.onRenderProcess,
|
|
58804
|
+
loader: config.loader,
|
|
58805
|
+
label: config.label
|
|
58806
|
+
};
|
|
58807
|
+
} else {
|
|
58808
|
+
// 普通模式:直接使用组件
|
|
58809
|
+
return {
|
|
58810
|
+
component: componentValue,
|
|
58811
|
+
name: name,
|
|
58812
|
+
selfClosing: isSelfClosingComponent(componentValue)
|
|
58813
|
+
};
|
|
58814
|
+
}
|
|
58815
|
+
});
|
|
58816
|
+
}
|
|
58769
58817
|
function ReactAIRenderer(_a) {
|
|
58770
58818
|
var _this = this;
|
|
58771
58819
|
var content = _a.content,
|
|
@@ -58788,24 +58836,16 @@ function ReactAIRenderer(_a) {
|
|
|
58788
58836
|
});
|
|
58789
58837
|
}
|
|
58790
58838
|
content = content || children || '';
|
|
58791
|
-
|
|
58792
|
-
var
|
|
58793
|
-
var _b = __read(_a, 1),
|
|
58794
|
-
name = _b[0];
|
|
58795
|
-
return !componentHandlers.some(function (handler) {
|
|
58796
|
-
return handler.name === name;
|
|
58797
|
-
});
|
|
58798
|
-
}).map(function (_a) {
|
|
58839
|
+
// 构建 allComponents:从增强配置中提取实际的组件
|
|
58840
|
+
var normalizedComponents = components ? Object.fromEntries(Object.entries(components).map(function (_a) {
|
|
58799
58841
|
var _b = __read(_a, 2),
|
|
58800
58842
|
name = _b[0],
|
|
58801
|
-
|
|
58802
|
-
|
|
58803
|
-
|
|
58804
|
-
|
|
58805
|
-
|
|
58806
|
-
|
|
58807
|
-
};
|
|
58808
|
-
}) : []), false), __read(componentHandlers), false);
|
|
58843
|
+
value = _b[1];
|
|
58844
|
+
return [name, extractComponent(value)];
|
|
58845
|
+
})) : {};
|
|
58846
|
+
var allComponents = __assign(__assign({}, BuiltInComponents), normalizedComponents);
|
|
58847
|
+
// 构建 allComponentHandlers:支持增强模式
|
|
58848
|
+
var allComponentHandlers = __spreadArray(__spreadArray([], __read(components ? convertComponentsToHandlers(components, componentHandlers) : []), false), __read(componentHandlers), false);
|
|
58809
58849
|
var _e = __read(React.useState(null), 2),
|
|
58810
58850
|
component = _e[0],
|
|
58811
58851
|
setComponent = _e[1];
|
|
@@ -58813,6 +58853,8 @@ function ReactAIRenderer(_a) {
|
|
|
58813
58853
|
var fallbackErrorRef = React.useRef({
|
|
58814
58854
|
hasError: false
|
|
58815
58855
|
});
|
|
58856
|
+
// 用于缓存已完成的组件,避免重复渲染
|
|
58857
|
+
var completedComponentsCacheRef = React.useRef(new Map());
|
|
58816
58858
|
var parser = new MDXStreamingParser(allComponentHandlers);
|
|
58817
58859
|
var mdxContent = content || '';
|
|
58818
58860
|
// FallbackView 错误状态变化时的回调
|
|
@@ -58825,9 +58867,10 @@ function ReactAIRenderer(_a) {
|
|
|
58825
58867
|
React.useEffect(function () {
|
|
58826
58868
|
var parseMDX = function () {
|
|
58827
58869
|
return __awaiter(_this, void 0, void 0, function () {
|
|
58828
|
-
var ThinkComponent, ResultComponent, protectedContent, _a, thinkContent, resultContent, parsedData, parsedComponents, finalComponent, fallbackComponent, _loop_1, i, MDXComponent, parsedDataLength, lastItem;
|
|
58829
|
-
|
|
58830
|
-
|
|
58870
|
+
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;
|
|
58871
|
+
var e_1, _b;
|
|
58872
|
+
return __generator(this, function (_c) {
|
|
58873
|
+
switch (_c.label) {
|
|
58831
58874
|
case 0:
|
|
58832
58875
|
ThinkComponent = null;
|
|
58833
58876
|
ResultComponent = null;
|
|
@@ -58836,8 +58879,8 @@ function ReactAIRenderer(_a) {
|
|
|
58836
58879
|
if (!thinkContent) return [3 /*break*/, 2];
|
|
58837
58880
|
return [4 /*yield*/, renderMdx(thinkContent, scope, allComponents)];
|
|
58838
58881
|
case 1:
|
|
58839
|
-
ThinkComponent =
|
|
58840
|
-
|
|
58882
|
+
ThinkComponent = _c.sent();
|
|
58883
|
+
_c.label = 2;
|
|
58841
58884
|
case 2:
|
|
58842
58885
|
parsedData = [];
|
|
58843
58886
|
if (!resultContent) return [3 /*break*/, 8];
|
|
@@ -58847,46 +58890,110 @@ function ReactAIRenderer(_a) {
|
|
|
58847
58890
|
return parseComponentRecursively(allComponentHandlers, item, scope);
|
|
58848
58891
|
}))];
|
|
58849
58892
|
case 3:
|
|
58850
|
-
parsedComponents =
|
|
58893
|
+
parsedComponents = _c.sent();
|
|
58894
|
+
currentComponentCount = parsedData.length;
|
|
58895
|
+
cacheKeys = Array.from(completedComponentsCacheRef.current.keys());
|
|
58896
|
+
try {
|
|
58897
|
+
for (cacheKeys_1 = __values(cacheKeys), cacheKeys_1_1 = cacheKeys_1.next(); !cacheKeys_1_1.done; cacheKeys_1_1 = cacheKeys_1.next()) {
|
|
58898
|
+
key = cacheKeys_1_1.value;
|
|
58899
|
+
indexMatch = key.match(/-(\d+)$/);
|
|
58900
|
+
if (indexMatch) {
|
|
58901
|
+
index = parseInt(indexMatch[1], 10);
|
|
58902
|
+
if (index >= currentComponentCount) {
|
|
58903
|
+
// 如果索引超出当前组件数量,删除缓存
|
|
58904
|
+
completedComponentsCacheRef.current.delete(key);
|
|
58905
|
+
}
|
|
58906
|
+
}
|
|
58907
|
+
}
|
|
58908
|
+
} catch (e_1_1) {
|
|
58909
|
+
e_1 = {
|
|
58910
|
+
error: e_1_1
|
|
58911
|
+
};
|
|
58912
|
+
} finally {
|
|
58913
|
+
try {
|
|
58914
|
+
if (cacheKeys_1_1 && !cacheKeys_1_1.done && (_b = cacheKeys_1.return)) _b.call(cacheKeys_1);
|
|
58915
|
+
} finally {
|
|
58916
|
+
if (e_1) throw e_1.error;
|
|
58917
|
+
}
|
|
58918
|
+
}
|
|
58851
58919
|
finalComponent = [];
|
|
58852
58920
|
fallbackComponent = [];
|
|
58853
58921
|
_loop_1 = function (i) {
|
|
58854
|
-
var item, _result, placeholderForFallback, componentNameMatch, componentName_1, componentHandler, displayName;
|
|
58855
|
-
return __generator(this, function (
|
|
58856
|
-
switch (
|
|
58922
|
+
var item, currentParsedItem, _result, placeholderForFallback, componentNameMatch, componentName_1, componentHandler, displayName, cacheKey;
|
|
58923
|
+
return __generator(this, function (_d) {
|
|
58924
|
+
switch (_d.label) {
|
|
58857
58925
|
case 0:
|
|
58858
58926
|
item = parsedComponents[i];
|
|
58927
|
+
currentParsedItem = parsedData[i];
|
|
58859
58928
|
_result = void 0;
|
|
58860
58929
|
placeholderForFallback = null;
|
|
58861
58930
|
if (!(item.type === 'text')) return [3 /*break*/, 2];
|
|
58862
58931
|
return [4 /*yield*/, renderText(item.value, scope, allComponents)];
|
|
58863
58932
|
case 1:
|
|
58864
|
-
_result =
|
|
58865
|
-
|
|
58933
|
+
_result = _d.sent();
|
|
58934
|
+
finalComponent.push(_result);
|
|
58935
|
+
return [3 /*break*/, 11];
|
|
58866
58936
|
case 2:
|
|
58867
|
-
return [4 /*yield*/, renderMdx(item.value, scope, allComponents)];
|
|
58868
|
-
case 3:
|
|
58869
|
-
_result = _c.sent();
|
|
58870
58937
|
componentNameMatch = item.value.match(/<([A-Z][A-Za-z0-9]*)/);
|
|
58871
|
-
if (componentNameMatch)
|
|
58872
|
-
|
|
58873
|
-
|
|
58874
|
-
|
|
58875
|
-
|
|
58876
|
-
|
|
58877
|
-
|
|
58878
|
-
|
|
58879
|
-
}
|
|
58880
|
-
placeholderForFallback = /*#__PURE__*/React.createElement(ComponentPlaceholder, {
|
|
58881
|
-
componentName: displayName,
|
|
58882
|
-
customPlaceholder: componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.placeholder,
|
|
58883
|
-
key: "fallback-placeholder-".concat(i)
|
|
58884
|
-
});
|
|
58938
|
+
if (!componentNameMatch) return [3 /*break*/, 9];
|
|
58939
|
+
componentName_1 = componentNameMatch[1];
|
|
58940
|
+
componentHandler = allComponentHandlers.find(function (c) {
|
|
58941
|
+
return c.name === componentName_1;
|
|
58942
|
+
});
|
|
58943
|
+
displayName = componentName_1;
|
|
58944
|
+
if (componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.label) {
|
|
58945
|
+
displayName = componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.label;
|
|
58885
58946
|
}
|
|
58886
|
-
|
|
58947
|
+
placeholderForFallback = /*#__PURE__*/React.createElement(ComponentPlaceholder, {
|
|
58948
|
+
componentName: displayName,
|
|
58949
|
+
loader: componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.loader,
|
|
58950
|
+
key: "fallback-loader-".concat(componentName_1)
|
|
58951
|
+
});
|
|
58952
|
+
if (!(currentParsedItem.isComplete === true || !(componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.loader))) return [3 /*break*/, 6];
|
|
58953
|
+
cacheKey = "mdx-component-".concat(componentName_1, "-").concat(i);
|
|
58954
|
+
if (!completedComponentsCacheRef.current.has(cacheKey)) return [3 /*break*/, 3];
|
|
58955
|
+
// 使用缓存的组件
|
|
58956
|
+
_result = completedComponentsCacheRef.current.get(cacheKey);
|
|
58957
|
+
return [3 /*break*/, 5];
|
|
58958
|
+
case 3:
|
|
58959
|
+
return [4 /*yield*/, renderMdx(item.value, scope, allComponents)];
|
|
58887
58960
|
case 4:
|
|
58961
|
+
// 缓存不存在,调用 renderMdx 并存入缓存
|
|
58962
|
+
_result = _d.sent();
|
|
58963
|
+
// 为组件设置 key,确保 React 能正确识别
|
|
58964
|
+
if (/*#__PURE__*/React.isValidElement(_result)) {
|
|
58965
|
+
_result = /*#__PURE__*/React.cloneElement(_result, {
|
|
58966
|
+
key: cacheKey
|
|
58967
|
+
});
|
|
58968
|
+
} else {
|
|
58969
|
+
_result = /*#__PURE__*/React.createElement(React.Fragment, {
|
|
58970
|
+
key: cacheKey
|
|
58971
|
+
}, _result);
|
|
58972
|
+
}
|
|
58973
|
+
// 存入缓存
|
|
58974
|
+
completedComponentsCacheRef.current.set(cacheKey, _result);
|
|
58975
|
+
_d.label = 5;
|
|
58976
|
+
case 5:
|
|
58977
|
+
finalComponent.push(_result);
|
|
58978
|
+
return [3 /*break*/, 8];
|
|
58979
|
+
case 6:
|
|
58980
|
+
return [4 /*yield*/, renderMdx(item.value, scope, allComponents)];
|
|
58981
|
+
case 7:
|
|
58982
|
+
// 组件未完成,正常渲染
|
|
58983
|
+
_result = _d.sent();
|
|
58984
|
+
finalComponent.push(placeholderForFallback);
|
|
58985
|
+
_d.label = 8;
|
|
58986
|
+
case 8:
|
|
58987
|
+
return [3 /*break*/, 11];
|
|
58988
|
+
case 9:
|
|
58989
|
+
return [4 /*yield*/, renderMdx(item.value, scope, allComponents)];
|
|
58990
|
+
case 10:
|
|
58991
|
+
// 没有匹配到组件名,正常渲染
|
|
58992
|
+
_result = _d.sent();
|
|
58888
58993
|
finalComponent.push(_result);
|
|
58889
|
-
|
|
58994
|
+
_d.label = 11;
|
|
58995
|
+
case 11:
|
|
58996
|
+
// fallbackComponent 应该是 finalComponent 的前 i 个元素 + 当前 item 的 loader
|
|
58890
58997
|
fallbackComponent.length = 0;
|
|
58891
58998
|
fallbackComponent.push.apply(fallbackComponent, __spreadArray([], __read(finalComponent.slice(0, i)), false));
|
|
58892
58999
|
if (placeholderForFallback) {
|
|
@@ -58897,13 +59004,13 @@ function ReactAIRenderer(_a) {
|
|
|
58897
59004
|
});
|
|
58898
59005
|
};
|
|
58899
59006
|
i = 0;
|
|
58900
|
-
|
|
59007
|
+
_c.label = 4;
|
|
58901
59008
|
case 4:
|
|
58902
59009
|
if (!(i < parsedComponents.length)) return [3 /*break*/, 7];
|
|
58903
59010
|
return [5 /*yield**/, _loop_1(i)];
|
|
58904
59011
|
case 5:
|
|
58905
|
-
|
|
58906
|
-
|
|
59012
|
+
_c.sent();
|
|
59013
|
+
_c.label = 6;
|
|
58907
59014
|
case 6:
|
|
58908
59015
|
i++;
|
|
58909
59016
|
return [3 /*break*/, 4];
|
|
@@ -58918,7 +59025,7 @@ function ReactAIRenderer(_a) {
|
|
|
58918
59025
|
children: finalComponent
|
|
58919
59026
|
})
|
|
58920
59027
|
});
|
|
58921
|
-
|
|
59028
|
+
_c.label = 8;
|
|
58922
59029
|
case 8:
|
|
58923
59030
|
MDXComponent = /*#__PURE__*/runtime.jsxs(React.Fragment, {
|
|
58924
59031
|
children: [ThinkComponent, ResultComponent]
|