forked from docsifyjs/docsify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen-tree.js
More file actions
30 lines (25 loc) 路 709 Bytes
/
Copy pathgen-tree.js
File metadata and controls
30 lines (25 loc) 路 709 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
/**
* Gen toc tree
* @link https://github.com/killercup/grock/blob/5280ae63e16c5739e9233d9009bc235ed7d79a50/styles/solarized/assets/js/behavior.coffee#L54-L81
* @param {Array} toc List of TOC elements
* @param {Number} maxLevel Deep level
* @return {Array} Headlines
*/
export function genTree(toc, maxLevel) {
const headlines = [];
const last = {};
toc.forEach(headline => {
const level = headline.level || 1;
const len = level - 1;
if (level > maxLevel) {
return;
}
if (last[len]) {
last[len].children = (last[len].children || []).concat(headline);
} else {
headlines.push(headline);
}
last[level] = headline;
});
return headlines;
}