1 回答
TA贡献1893条经验 获得超10个赞
不需要正则表达式。
var log = console.log;
var data = `・First Level 1 list item
– First Level 2 list item, which is a subset of the first Level 1 list item.
– Second Level 2 list item, which is a subset of the first Level 1 list item.
♦ First Level 3 list item, which is a subset of the second Level 2 list item.
・Second Level 1 list item.`;
//split text to array of string. One item per line
data = data.split("\n");
var firstChar,prevFirstChar = "";
//our output struct
var struct = [];
var cursor = struct;
//we need only one token for return to first level
var lvl1Key = "・";
var prevnode = {};
data.forEach(line=>{
//get token
firstChar = line.charAt(0);
let node = {
str : line.slice(1),
child : []
};
if (firstChar == lvl1Key) {
//return to root
cursor = struct;
} else if (firstChar != prevFirstChar) {
//move up if token change and it is not root token
cursor = prevnode.child;
}
cursor.push(node);
prevnode = node;
prevFirstChar = firstChar;
});
log(struct);
//Ok, we get struct, convert this to html
//offset for formating
const offsetSize = 2;
//recursive function node - array of { str : "string", childs : [nodes]}
var toHtml = function(node, offset = "") {
var ret = offset + "<ul>\n";
offset += " ".repeat(offsetSize);
node.forEach(rec=>{
ret += offset + "<li>" + rec.str + "</li>\n";
//if array not empty add html for childs
if (rec.child.length) {
ret += toHtml(rec.child, offset + " ".repeat(offsetSize));
}
});
offset = offset.slice(offsetSize);
ret += offset + "</ul>\n";
return ret;
}
log(toHtml(struct));
添加回答
举报