Vytvořené narychlo, takže určitě ne ideální:
function parse(input) {
const result = [];
const stack = [];
const lines = input.split("\n");
let indent = 0;
let col = result;
let item;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
let curInd = 0;
while (line[curInd] === " ") curInd++;
if (curInd > indent) {
stack.push(col);
col = item.children;
}
if (curInd < indent) {
col = stack.pop();
}
indent = curInd;
item = {
description: line.substr(curInd),
children: []
};
col.push(item);
}
console.log(JSON.stringify(result, null, 2));
}