8000 feat(plugin): optimize plugin code (#160) · chodocs/chodocs@03a3947 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 03a3947

Browse files
feat(plugin): optimize plugin code (#160)
1 parent e6b40e0 commit 03a3947

File tree

1 file changed

+7
-21
lines changed

1 file changed

+7
-21
lines changed

docs/.vitepress/theme/plugins/autoSidebarBeta.ts

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ export interface Options {
1212
export const DIR_ROOT = resolve(__dirname, '../../../../')
1313
export const DIR_SRC = resolve(DIR_ROOT, 'docs')
1414

15-
export function getDir(dir: string, ignore: string[] = []) {
15+
export function fastGlobSync(type: string, dir: string, ignore: string[] = []) {
1616
const files = fg.sync('*', {
17-
onlyDirectories: true,
17+
onlyDirectories: type === 'dir',
18+
onlyFiles: type === 'file',
1819
cwd: dir,
1920
ignore: [
2021
'_*',
@@ -27,32 +28,17 @@ export function getDir(dir: string, ignore: string[] = []) {
2728
return files
2829
}
2930

30-
export function getFiles(dir: string, ignore: string[] = []) {
31-
const files = fg.sync('*', {
32-
onlyFiles: true,
33-
cwd: dir,
34-
ignore: [
35-
'_*',
36-
'dist',
37-
'node_modules',
38-
...ignore,
39-
],
40-
})
41-
files.sort()
42-
return files
43-
}
44-
45-
export const dirs = getDir(DIR_SRC)
31+
export const dirs = fastGlobSync('dir', DIR_SRC)
4632

4733
const getSidebar = (dir: string, title: string | undefined) => {
4834
const curDir = resolve(DIR_SRC, dir)
49-
const dirs = getDir(curDir)
35+
const dirs = fastGlobSync('dir', curDir)
5036
const res = []
5137
if (dirs.length) {
5238
// TODO 多级目录
5339
dirs.forEach((e) => {
5440
const childDir = resolve(curDir, e)
55-
const mdFiles = getFiles(childDir)
41+
const mdFiles = fastGlobSync('file', childDir)
5642
const sidebar = {
5743
text: (e.charAt(0).toUpperCase() + e.slice(1)).replaceAll('-', ' '),
5844
collapsed: false,
@@ -75,7 +61,7 @@ const getSidebar = (dir: string, title: string | undefined) => {
7561
})
7662
}
7763
else {
78-
const mdFiles = getFiles(curDir)
64+
const mdFiles = fastGlobSync('file', curDir)
7965
const sidebar = {
8066
text: title,
8167
collapsed: false,

0 commit comments

Comments
 (0)
0