Skip to content

Commit 63d3360

Browse files
committed
fix:优化代码,增加错误提示
1 parent c75388f commit 63d3360

File tree

3 files changed

+59
-39
lines changed

3 files changed

+59
-39
lines changed

core/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export type CodeBlockData = {
1212
export default function (source: string) {
1313
const options = this.getOptions();
1414
const result = getCodeBlockString(source, options.lang || ['tsx', 'jsx']);
15+
1516
return `
1617
${result}
1718
export default {

core/src/utils/index.ts

+47-29
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,24 @@ import remark from 'remark';
99

1010
/** 转换 代码*/
1111
const getProcessor = (scope: string) => {
12-
const child = remark.parse(scope) as MarkDownTreeType;
13-
return child.children;
12+
try {
13+
const child = remark.parse(scope) as MarkDownTreeType;
14+
return child.children;
15+
} catch (err) {
16+
console.warn(err);
17+
}
1418
};
1519

1620
const getMeta = (meta: string | null): Record<string, string | boolean> => {
1721
let metaData: Record<string, string | boolean> = {};
18-
if (meta) {
19-
const [metaItem] = /mdx:(.[\w|:]+)/i.exec(meta) || [];
20-
const [_, field, val] = (metaItem || '').split(':').map((item) => item.trim());
21-
metaData[field] = val || true;
22+
try {
23+
if (meta) {
24+
const [metaItem] = /mdx:(.[\w|:]+)/i.exec(meta) || [];
25+
const [_, field, val] = (metaItem || '').split(':').map((item) => item.trim());
26+
metaData[field] = val || true;
27+
}
28+
} catch (err) {
29+
console.warn(err);
2230
}
2331
return metaData;
2432
};
@@ -27,23 +35,27 @@ const getMeta = (meta: string | null): Record<string, string | boolean> => {
2735
const getCodeBlock = (child: MarkDownTreeType['children'], lang: string[] = ['jsx', 'tsx']) => {
2836
// 获取渲染部分
2937
const codeBlock: Record<string | number, CodeBlockItemType> = {};
30-
child.forEach((item) => {
31-
if (item && item.type === 'code' && lang.includes(item.lang)) {
32-
const line = item.position.start.line;
33-
const metaData = getMeta(item.meta);
34-
if (metaData.preview) {
35-
let name = typeof metaData.preview === 'string' ? metaData.preview : line;
36-
const funName = `BaseCode${line}`;
37-
const returnCode = getTransformValue(item.value, `${funName}.${lang}`, funName);
38-
codeBlock[line] = {
39-
code: returnCode,
40-
name,
41-
language: item.lang,
42-
value: item.value,
43-
};
38+
try {
39+
child.forEach((item) => {
40+
if (item && item.type === 'code' && lang.includes(item.lang)) {
41+
const line = item.position.start.line;
42+
const metaData = getMeta(item.meta);
43+
if (metaData.preview) {
44+
let name = typeof metaData.preview === 'string' ? metaData.preview : line;
45+
const funName = `BaseCode${line}`;
46+
const returnCode = getTransformValue(item.value, `${funName}.${lang}`, funName);
47+
codeBlock[line] = {
48+
code: returnCode,
49+
name,
50+
language: item.lang,
51+
value: item.value,
52+
};
53+
}
4454
}
45-
}
46-
});
55+
});
56+
} catch (err) {
57+
console.warn(err);
58+
}
4759
return codeBlock;
4860
};
4961

@@ -52,13 +64,19 @@ const createStr = (codeBlock: Record<string | number, CodeBlockItemType>) => {
5264
let baseCodeObjStr = ``;
5365
let codeBlockValue = ``;
5466
let languageStr = ``;
55-
Object.entries(codeBlock).forEach(([key, item]) => {
56-
const { code, value, language, name } = item;
57-
baseCodeStr += `${code};\n`;
58-
baseCodeObjStr += `${name}:BaseCode${key},\n`;
59-
codeBlockValue += `${name}:${JSON.stringify(value)},\n`;
60-
languageStr += `${name}:\`${language}\`,\n`;
61-
});
67+
68+
try {
69+
Object.entries(codeBlock).forEach(([key, item]) => {
70+
const { code, value, language, name } = item;
71+
baseCodeStr += `${code};\n`;
72+
baseCodeObjStr += `${name}:BaseCode${key},\n`;
73+
codeBlockValue += `${name}:${JSON.stringify(value)},\n`;
74+
languageStr += `${name}:\`${language}\`,\n`;
75+
});
76+
} catch (err) {
77+
console.warn(err);
78+
}
79+
6280
let indexStr = `${baseCodeStr} const languages={${languageStr}};\n const codeBlock={${codeBlockValue}};\n const components={${baseCodeObjStr}}`;
6381
return indexStr;
6482
};

core/src/utils/transform.ts

+11-10
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ export function babelTransform(input: string, filename: string) {
88
}
99

1010
export const getTransformValue = (str: string, filename: string, funName: string) => {
11-
const isReact = /import\x20+React(\x20+|[\x20+,]+({[a-zA-Z0-9,\s]+}|{})\x20+)from\x20+('|")react('|")/.test(str);
12-
// 先判断 是否引入 react
13-
const tran = isReact ? str : `import React from "react"\n ${str}`;
14-
/** 先把默认导出 export default 进行替换 **/
15-
const newCode = `${tran.replace(/export\x20+default/, 'const _default = ')}\n`;
16-
const code = `${babelTransform(newCode, `${filename}`).code}\n return _react["default"].createElement(_default)`;
17-
18-
return `function ${funName}(){
19-
${code}
20-
}`;
11+
try {
12+
const isReact = /import\x20+React(\x20+|[\x20+,]+({[a-zA-Z0-9,\s]+}|{})\x20+)from\x20+('|")react('|")/.test(str);
13+
// 先判断 是否引入 react
14+
const tran = isReact ? str : `import React from "react"\n ${str}`;
15+
/** 先把默认导出 export default 进行替换 **/
16+
const newCode = `${tran.replace(/export\x20+default/, 'const _default = ')}\n`;
17+
const code = `${babelTransform(newCode, `${filename}`).code}\n return _react["default"].createElement(_default)`;
18+
return `function ${funName}(){\n${code}\n};`;
19+
} catch (err) {
20+
console.warn(err);
21+
}
2122
};

0 commit comments

Comments
 (0)