@@ -45,16 +45,20 @@ module.exports = function loader(source) {
45
45
const headSource = newSource . slice ( 0 , m . index ) ;
46
46
let restSource = newSource . slice ( m . index ) ;
47
47
48
+ if ( ! m [ 2 ] . match ( / < .* > / gi) ) {
49
+ // 没有合法的标签元素,直接跳过
50
+ continue ;
51
+ }
48
52
// 根据>的出现次数,判断是否自动导入,只出现一次>,自动引入闭合标签内的元素,元素名需要和demo中的文件名相同
49
53
const isImport = m [ 2 ] . match ( / > / gi) . length === 1 ;
50
54
51
55
// 获取需要引入的组件名,自动匹配demo文件夹中组件
52
56
// 如 <demo />
53
57
const importName = isImport
54
58
? 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 ( )
58
62
: null ;
59
63
60
64
if ( isImport ) {
@@ -67,11 +71,9 @@ module.exports = function loader(source) {
67
71
CodeBlockReg . lastIndex = m . index + addStr . length ;
68
72
} else {
69
73
// 解决当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 ;
75
77
}
76
78
newSource = headSource + restSource ;
77
79
}
0 commit comments