Skip to content

Commit 1f89f1b

Browse files
huhu-1981360151219
andauthored
fix: 修复CodeBlock为空时报错的bug (#14)
* fix: 修复CodeBlock为空时报错的bug * fix: no attribute tag cause lastindex incorrect --------- Co-authored-by: 1360151219 <[email protected]>
1 parent 57a97bf commit 1f89f1b

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

packages/demo/loaders/index.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,20 @@ module.exports = function loader(source) {
4545
const headSource = newSource.slice(0, m.index);
4646
let restSource = newSource.slice(m.index);
4747

48+
if (!m[2].match(/<.*>/gi)) {
49+
// 没有合法的标签元素,直接跳过
50+
continue;
51+
}
4852
// 根据>的出现次数,判断是否自动导入,只出现一次>,自动引入闭合标签内的元素,元素名需要和demo中的文件名相同
4953
const isImport = m[2].match(/>/gi).length === 1;
5054

5155
// 获取需要引入的组件名,自动匹配demo文件夹中组件
5256
// 如 <demo />
5357
const importName = isImport
5458
? m[2]
55-
.match(/<([\s\S]*?)\/>/g)[0]
56-
.slice(1, -2)
57-
.trim()
59+
.match(/<([\s\S]*?)\/>/g)[0]
60+
.slice(1, -2)
61+
.trim()
5862
: null;
5963

6064
if (isImport) {
@@ -67,11 +71,9 @@ module.exports = function loader(source) {
6771
CodeBlockReg.lastIndex = m.index + addStr.length;
6872
} else {
6973
// 解决当m[1]=''的时候无法replace的问题
70-
const addStr = isImport
71-
? ` code={${importName}Code}` + restSource.slice(10)
72-
: ` code={\`${getDemo(component, m[2])}\`}` + restSource.slice(10);
73-
restSource = restSource.slice(0, 10) + addStr;
74-
CodeBlockReg.lastIndex = m.index + addStr.length;
74+
const addStr = isImport ? ` code={${importName}Code}` : ` code={\`${getDemo(component, m[2])}\`}`;
75+
restSource = restSource.slice(0, 10) + addStr + restSource.slice(10);
76+
CodeBlockReg.lastIndex = headSource.length + m[0].length + addStr.length;
7577
}
7678
newSource = headSource + restSource;
7779
}

0 commit comments

Comments
 (0)