1
0
mirror of https://github.com/sahadev/vue-component-creater-ui.git synced 2025-06-13 10:24:56 +08:00
vue-component-creater-ui/src/libs/bundle-html2json-esm.js
2021-04-09 20:06:44 +08:00

75 lines
1.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//该文件用于解析HTML输出为Object对象
const htmlparser2 = require("htmlparser2");
function getNodeContent(node) {
return node[Object.keys(node)[0]];
}
/**每个节点的表示方法为:
{
tagname: {
key1: value1,
key2: value2,
__children: [
{
}
]
}
}*/
function generateNewNode(tagName, attributes = {}) {
// 构建新节点
const newNode = {};
newNode[tagName] = attributes;
attributes.__children = [];
return newNode;
}
function parseHtml(htmlData) {
return new Promise((resolve, reject) => {
// 根节点
const root = generateNewNode('root');
// 当前访问的节点
let currentAccessObject = root;
// 之前访问的节点数组
let lastAccessStack = [root];
// options docment: https://github.com/fb55/htmlparser2/wiki/Parser-options
const parser = new htmlparser2.Parser({
onopentag(tagname, attributes) {
const newNode = generateNewNode(tagname, attributes);
lastAccessStack.push(newNode);
getNodeContent(currentAccessObject).__children.push(newNode);
currentAccessObject = newNode;
},
ontext(text) {
if (text.trim()) {
getNodeContent(currentAccessObject).__text__ = text.trim();
}
},
onclosetag(tagname) {
lastAccessStack.pop();
currentAccessObject = lastAccessStack[lastAccessStack.length - 1];
},
onend(){
resolve(root);
},
onerror(error) {
reject(error);
}
});
parser.write(
htmlData
);
parser.end();
})
}
async function html2Json(htmlData) {
return await parseHtml(htmlData);
}
export { html2Json };