Skip to content

Commit d752036

Browse files
author
Huang ShuWei
committed
update doc style match rule
1 parent bad46a0 commit d752036

File tree

1 file changed

+53
-51
lines changed

1 file changed

+53
-51
lines changed

examples/build/md-loader/util.js

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,69 @@
1-
const { compileTemplate } = require('@vue/component-compiler-utils');
2-
const compiler = require('vue-template-compiler');
1+
const { compileTemplate } = require("@vue/component-compiler-utils");
2+
const compiler = require("vue-template-compiler");
33

44
function stripScript(content) {
5-
const result = content.match(/<(script)>([\s\S]+)<\/\1>/);
6-
return result && result[2] ? result[2].trim() : '';
5+
const result = content.match(/<(script)>([\s\S]+)<\/\1>/);
6+
return result && result[2] ? result[2].trim() : "";
77
}
88

99
function stripStyle(content) {
10-
const result = content.match(/<(style)\s*>([\s\S]+)<\/\1>/);
11-
return result && result[2] ? result[2].trim() : '';
10+
const result = content.match(/<(style)[\s\S]*>([\s\S]+)<\/\1>/);
11+
return result && result[2] ? result[2].trim() : "";
1212
}
1313

1414
// 编写例子时不一定有 template。所以采取的方案是剔除其他的内容
1515
function stripTemplate(content) {
16-
content = content.trim();
17-
if (!content) {
18-
return content;
19-
}
20-
return content.replace(/<(script|style)[\s\S]+<\/\1>/g, '').trim();
16+
content = content.trim();
17+
if (!content) {
18+
return content;
19+
}
20+
return content.replace(/<(script|style)[\s\S]+<\/\1>/g, "").trim();
2121
}
2222

2323
function pad(source) {
24-
return source
25-
.split(/\r?\n/)
26-
.map(line => ` ${line}`)
27-
.join('\n');
24+
return source
25+
.split(/\r?\n/)
26+
.map(line => ` ${line}`)
27+
.join("\n");
2828
}
2929

3030
function genInlineComponentText(template, script) {
31-
32-
// https://github.com/vuejs/vue-loader/blob/423b8341ab368c2117931e909e2da9af74503635/lib/loaders/templateLoader.js#L46
33-
const finalOptions = {
34-
source: `<div>${template}</div>`,
35-
filename: 'inline-component', // TODO:这里有待调整
36-
compiler
37-
};
38-
const compiled = compileTemplate(finalOptions);
39-
// tips
40-
if (compiled.tips && compiled.tips.length) {
41-
compiled.tips.forEach(tip => {
42-
console.warn(tip);
43-
});
44-
}
45-
// errors
46-
if (compiled.errors && compiled.errors.length) {
47-
console.error(
48-
`\n Error compiling template:\n${pad(compiled.source)}\n` +
49-
compiled.errors.map(e => ` - ${e}`).join('\n') +
50-
'\n'
51-
);
52-
}
53-
let demoComponentContent = `
31+
// https://github.com/vuejs/vue-loader/blob/423b8341ab368c2117931e909e2da9af74503635/lib/loaders/templateLoader.js#L46
32+
const finalOptions = {
33+
source: `<div>${template}</div>`,
34+
filename: "inline-component", // TODO:这里有待调整
35+
compiler
36+
};
37+
const compiled = compileTemplate(finalOptions);
38+
// tips
39+
if (compiled.tips && compiled.tips.length) {
40+
compiled.tips.forEach(tip => {
41+
console.warn(tip);
42+
});
43+
}
44+
// errors
45+
if (compiled.errors && compiled.errors.length) {
46+
console.error(
47+
`\n Error compiling template:\n${pad(compiled.source)}\n` +
48+
compiled.errors.map(e => ` - ${e}`).join("\n") +
49+
"\n"
50+
);
51+
}
52+
let demoComponentContent = `
5453
${compiled.code}
5554
`;
5655

57-
// todo: 这里采用了硬编码有待改进
58-
script = script.trim();
59-
if (script) {
60-
script = script.replace(/export\s+default/, 'const democomponentExport =');
61-
} else {
62-
script = 'const democomponentExport = {}';
63-
}
64-
demoComponentContent = `(function() {
56+
// todo: 这里采用了硬编码有待改进
57+
script = script.trim();
58+
if (script) {
59+
script = script.replace(
60+
/export\s+default/,
61+
"const democomponentExport ="
62+
);
63+
} else {
64+
script = "const democomponentExport = {}";
65+
}
66+
demoComponentContent = `(function() {
6567
${demoComponentContent}
6668
${script}
6769
return {
@@ -70,12 +72,12 @@ function genInlineComponentText(template, script) {
7072
...democomponentExport
7173
}
7274
})()`;
73-
return demoComponentContent;
75+
return demoComponentContent;
7476
}
7577

7678
module.exports = {
77-
stripScript,
78-
stripStyle,
79-
stripTemplate,
80-
genInlineComponentText
79+
stripScript,
80+
stripStyle,
81+
stripTemplate,
82+
genInlineComponentText
8183
};

0 commit comments

Comments
 (0)