forked from dundalek/markmap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform.headings.js
More file actions
40 lines (34 loc) · 851 Bytes
/
transform.headings.js
File metadata and controls
40 lines (34 loc) · 851 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
module.exports = function transformHeadings(headings) {
var root = {
name: 'root',
depth: 0,
children: []
};
var node = root;
var stack = [];
var tmp;
headings.forEach(function(h) {
while (h.depth < node.depth + 1) {
node = stack.pop();
}
while (h.depth > node.depth + 1) {
if (!node.children || node.children.length === 0) {
tmp = {
name: '',
depth: node.depth + 1
};
node.children = node.children || [];
node.children.push(tmp);
}
stack.push(node);
node = node.children[node.children.length-1];
}
node.children = node.children || [];
node.children.push(h);
});
if (root.children.length === 1) {
// there is only one child - it is the title, make it root
root = root.children[0];
}
return root;
};