This repository has been archived by the owner on May 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
88 lines (64 loc) · 2.82 KB
/
index.ts
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const fs = require("fs");
const path = require("path");
function getSidebarItems(docsLocation: string = 'docs') {
let location = docsLocation.replace('/\/$/', '');
// Create array with all folders/files within the 'location' directory
var structure = fs
.readdirSync(path.join(`${process.cwd()}/${location}`))
// Prevents dotfiles from being added to the menu
.filter((item: string) => !item.startsWith('.'))
// Keep or put the README.md file as the first element in the array
.sort((a: string, b: string) => b.includes('README') ? 1 : a.includes('README') ? -1 : 0);
// Itterate over the files/folders
const extendedStructure = structure.map((item: string) => {
// if item=file => add 'location/item' to menu (as a menu item)
// if item=folder => rerun getSidebarItems function with new folder 'location' to get sub folders/files and add this to menu (as a menu group)
if (fs.statSync(path.join(`${process.cwd()}/${location}`, item)).isFile()) {
return '/' + (location + '/').replace('docs/', '') + item
}
return {
text: item,
link: '/' + (location + '/').replace('docs/', '') + item + '/',
children: getSidebarItems(location + '/' + item)
}
}
)
// If folder doesn't contain any sub folders/files or file is empty
const extendedStructureFiltered = extendedStructure.filter((item) => !(item.children && item.children.length == 0)
)
return [
...extendedStructureFiltered
];
}
function getNavbarItems(docsLocation: string = 'docs') {
// Trim slash from location
let location = docsLocation.replace('/\/$/', '');
// Create array with all folders/files within the 'location' directory
var structure = fs.readdirSync(path.join(`${process.cwd()}/${location}`))
// Prevents dotfiles from being added to the menu
.filter((item: string) => !item.startsWith('.'))
// Filter out all files except 'README.md'
.filter((item: string) =>
!fs.statSync(path.join(`${process.cwd()}/${location}`, item)).isFile() || item == 'README.md'
)
//
.map((item: string) => {
if (item == 'README.md') {
return item;
}
return {
text: item,
link: '/' + (location + '/').replace('docs/', '') + item + '/',
children: getNavbarItems(location + '/' + item)
}
}
)
// Remove 'README.md' files from menu
.filter((item) => item !== 'README.md')
// Remove folders that are empty
.filter((item) => fs.readdirSync(path.join(`${process.cwd()}/${location}/${item.text}`)).length > 0);
return [
...structure
];
}
export { getSidebarItems, getNavbarItems };