-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathbuildHierarchy.mjs
More file actions
63 lines (55 loc) · 1.85 KB
/
Copy pathbuildHierarchy.mjs
File metadata and controls
63 lines (55 loc) · 1.85 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* We need the files to be in a hierarchy based off of depth, but they're
* given to us flattened. So, let's fix that.
*
* Assuming that {@link entries} is in the same order as the elements are in
* the markdown, we can use the entry's depth property to reassemble the
* hierarchy.
*
* If depth <= 1, it's a top-level element (aka a root).
*
* If it's depth is greater than the previous entry's depth, it's a child of
* the previous entry. Otherwise (if it's less than or equal to the previous
* entry's depth), we need to find the entry that it was the greater than. We
* can do this by just looping through entries in reverse starting at the
* current index - 1.
*
* @param {Array<ApiDocMetadataEntry>} entries
* @returns {Array<import('../types.d.ts').HierarchizedEntry>}
*/
export function buildHierarchy(entries) {
const roots = [];
for (let i = 0; i < entries.length; i++) {
const entry = entries[i];
const currentDepth = entry.heading.depth;
if (currentDepth <= 1) {
// We're a top-level entry
roots.push(entry);
continue;
}
const previousEntry = entries[i - 1];
const previousDepth = previousEntry.heading.depth;
if (currentDepth > previousDepth) {
// We're a child of the previous one
if (previousEntry.hierarchyChildren === undefined) {
previousEntry.hierarchyChildren = [];
}
previousEntry.hierarchyChildren.push(entry);
} else {
if (i < 2) {
throw new Error(`can't find parent since i < 2 (${i})`);
}
// Loop to find the entry we're a child of
for (let j = i - 2; j >= 0; j--) {
const jEntry = entries[j];
const jDepth = jEntry.heading.depth;
if (currentDepth > jDepth) {
// Found it
jEntry.hierarchyChildren.push(entry);
break;
}
}
}
}
return roots;
}