fastchar-appjs
Version:
快速搭建VUE项目工具类的基本库,主要用于每个功能页面独立生成html,不使用vue单页面功能。
89 lines (88 loc) • 2.67 kB
JavaScript
;
const htmlparser2 = require("htmlparser2");
const singleTag = ["br", "hr", "img", "input", "param", "meta", "link", "slot"];
/**
* 标签package-builder插件,用来处理是否排除标签内的代码
* @author Janesen
* @param source
*/
module.exports = function (source) {
//对应配置的options
let legacyLoaderConfig = this.query;
let parseNewHtml = "";
let isBreak = false, hasResolve = false;
const tagName = legacyLoaderConfig.buildLevelTagName;
const parser = new htmlparser2.Parser({
onopentag(name, attributes) {
if (isBreak) {
return;
}
let attr = "";
let buildConfig = {
level: 1,
};
for (let attributesKey in attributes) {
let attributesValue = attributes[attributesKey];
if (attributesValue.length === 0) {
attr += " " + attributesKey;
continue;
}
if (attributesKey === "level") {
buildConfig.level = parseInt(attributesValue);
}
attr += " " + attributesKey + "=\"" + attributesValue + "\"";
}
if (name === tagName) {
hasResolve = true;
if (legacyLoaderConfig.buildLevel < buildConfig.level) {
isBreak = true;
}
}
else {
if (attr.length > 0) {
parseNewHtml += "<" + name + " " + attr + " ";
}
else {
parseNewHtml += "<" + name;
}
if (singleTag.indexOf(name) >= 0) {
parseNewHtml += "/>";
}
else {
parseNewHtml += ">";
}
}
},
ontext(text) {
if (isBreak) {
return;
}
parseNewHtml += text;
},
onclosetag(name) {
if (name === tagName) {
isBreak = false;
}
if (isBreak) {
return;
}
if (singleTag.indexOf(name) >= 0) {
return;
}
if (name === tagName) {
return;
}
parseNewHtml += "</" + name + ">";
},
}, {
recognizeSelfClosing: true,
lowerCaseTags: false,
lowerCaseAttributeNames: false
});
parser.write(source);
parser.end();
if (hasResolve) {
return parseNewHtml;
}
return source;
};