Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tanraya/1d3e0ab778ef73830dac35a35c762f80 to your computer and use it in GitHub Desktop.
Save tanraya/1d3e0ab778ef73830dac35a35c762f80 to your computer and use it in GitHub Desktop.
var data = [
['strong', 0, 5],
['span', 0, 5],
['em', 2, 5]
];
var text = 'hello';
//function vtext(t) {}
//function vnode(name, attrs, chldren) {}
/*[
vnode('strong', {}, [
vtext('he'),
vnode('span', {}, [
vtext('llo')
])
])
]*/
var vtree = [];
var current = null;
data.forEach(function(x) {
if (current == null || current !== null && !(x[1] >= current.start && x[2] <= current.end)) {
current = {
name: x[0],
children: []
};
vtree.push(current);
} else if (current !== null && x[1] >= current.start && x[2] <= current.end) {
var newCurrent = {
name: x[0],
children: []
};
current.children.push(newCurrent);
current = newCurrent;
}
});
console.log(vtree);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment