|
1 | 1 | /** |
2 | 2 | * get keys of open sub menu from pathname |
3 | | - * input: /BasicSkill/basis/DOM |
4 | | - * menuOpenKeys: means extra show open keys in config.yml |
| 3 | + * { |
| 4 | + * pathname: /BasicSkill/basis/DOM, |
| 5 | + * menuOpenKeys: means extra show open keys in config.yml |
| 6 | + * } |
5 | 7 | * output: ["/BasicSkill", "/BasicSkill/basis"] |
6 | 8 | */ |
7 | | -function getOpenSubMenuKeys(pathname, menuOpenKeys) { |
| 9 | +// todo: optimize for depth-first traversal |
| 10 | +function getOpenSubMenuKeys({ |
| 11 | + pathname, |
| 12 | + menuSource, |
| 13 | + menuOpenKeys |
| 14 | +}) { |
8 | 15 | const result = [] |
9 | | - if (menuOpenKeys) { |
10 | | - result.push(...menuOpenKeys.split(',')) |
11 | | - } |
| 16 | + getOpenSubMenuKeysForAbbrLink(menuSource, pathname, result) |
12 | 17 | /** |
| 18 | + * logic for not abbrLink |
13 | 19 | * there is no pick item if the length of pathnameSplit is less than or equal to 2. |
14 | 20 | * eg: /README => ["", "README"] |
15 | 21 | */ |
16 | 22 | const pathnameSplit = pathname.split('/') |
17 | 23 | if (pathnameSplit.length <= 2) return result |
18 | 24 | let recordValue = '' |
19 | | - // eslint-disable-next-line no-plusplus |
20 | 25 | for (let i = 1; i < pathnameSplit.length - 1; i++) { |
21 | 26 | recordValue += `/${pathnameSplit[i]}` |
22 | 27 | result.push(recordValue) |
23 | 28 | } |
| 29 | + |
| 30 | + /** default open menu from config.yml */ |
| 31 | + if (menuOpenKeys) { |
| 32 | + result.push(...menuOpenKeys.split(',')) |
| 33 | + } |
| 34 | + |
24 | 35 | return result |
25 | 36 | } |
26 | 37 |
|
| 38 | +function getOpenSubMenuKeysForAbbrLink(source, pathname, result) { |
| 39 | + for (let i = 0; i < source.length; i++) { |
| 40 | + const { type, path, mdconf } = source[i] |
| 41 | + if (type === 'directory') { |
| 42 | + result.push(path) |
| 43 | + const ifFind = getOpenSubMenuKeysForAbbrLink(source[i].children, pathname, result) |
| 44 | + if (ifFind) return true |
| 45 | + result.pop() |
| 46 | + } else { |
| 47 | + if (pathname.indexOf(mdconf.abbrlink) > -1) { |
| 48 | + return true |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
27 | 54 | export { getOpenSubMenuKeys } |
0 commit comments