@@ -9,6 +9,10 @@ interface SidebarItem {
99 collapsed ?: boolean ;
1010}
1111
12+ interface SidebarConfig {
13+ [ key : string ] : SidebarItem [ ] ;
14+ }
15+
1216const EXCLUDE_FILES = [
1317 '.vitepress' ,
1418 'node_modules' ,
@@ -17,49 +21,45 @@ const EXCLUDE_FILES = [
1721 'assets' ,
1822] ;
1923
20- export function generateSidebar ( ) : SidebarItem [ ] {
24+ export function generateSidebar ( ) : SidebarConfig {
2125 const docsPath = path . resolve ( __dirname , '../../' ) ;
22- const items : SidebarItem [ ] = [ ] ;
26+ const sidebarConfig : SidebarConfig = { } ;
27+
28+ // 获取所有一级目录
29+ const directories = fs . readdirSync ( docsPath ) . filter ( file => {
30+ const filePath = path . join ( docsPath , file ) ;
31+ return fs . statSync ( filePath ) . isDirectory ( ) && ! EXCLUDE_FILES . includes ( file ) ;
32+ } ) ;
33+
34+ // 为每个一级目录生成侧边栏配置
35+ directories . forEach ( dir => {
36+ const dirPath = path . join ( docsPath , dir ) ;
37+ sidebarConfig [ `/${ dir } /` ] = walkDir ( dirPath ) ;
38+ } ) ;
2339
24- // 读取docs目录
40+ // 根目录的侧边栏配置
41+ sidebarConfig [ '/' ] = generateRootSidebar ( docsPath ) ;
42+
43+ return sidebarConfig ;
44+ }
45+
46+ function generateRootSidebar ( docsPath : string ) : SidebarItem [ ] {
47+ const items : SidebarItem [ ] = [ ] ;
2548 const files = fs
2649 . readdirSync ( docsPath )
27- . filter ( file => ! EXCLUDE_FILES . includes ( file ) )
28- . sort ( ( a , b ) => {
29- // 目录优先,同类型按字母排序
30- const aStats = fs . statSync ( path . join ( docsPath , a ) ) ;
31- const bStats = fs . statSync ( path . join ( docsPath , b ) ) ;
32- if ( aStats . isDirectory ( ) && ! bStats . isDirectory ( ) ) return - 1 ;
33- if ( ! aStats . isDirectory ( ) && bStats . isDirectory ( ) ) return 1 ;
34- return a . localeCompare ( b ) ;
35- } ) ;
50+ . filter (
51+ file =>
52+ ! EXCLUDE_FILES . includes ( file ) &&
53+ file . endsWith ( '.md' ) &&
54+ file !== 'README.md' ,
55+ )
56+ . sort ( ) ;
3657
3758 files . forEach ( file => {
38- const filePath = path . join ( docsPath , file ) ;
39- const stat = fs . statSync ( filePath ) ;
40-
41- // 忽略以.开头的文件和README.md
42- if ( file . startsWith ( '.' ) || file === 'README.md' ) {
43- return ;
44- }
45-
46- if ( stat . isDirectory ( ) ) {
47- const children = walkDir ( filePath ) ;
48- // 只有当目录非空时才添加
49- if ( children . length > 0 ) {
50- items . push ( {
51- text : formatText ( file ) ,
52- items : children ,
53- collapsible : true ,
54- collapsed : false ,
55- } ) ;
56- }
57- } else if ( file . endsWith ( '.md' ) ) {
58- items . push ( {
59- text : formatText ( file . replace ( '.md' , '' ) ) ,
60- link : `/${ file . replace ( '.md' , '' ) } ` ,
61- } ) ;
62- }
59+ items . push ( {
60+ text : formatText ( file . replace ( '.md' , '' ) ) ,
61+ link : `/${ file . replace ( '.md' , '' ) } ` ,
62+ } ) ;
6363 } ) ;
6464
6565 return items ;
@@ -108,7 +108,6 @@ function walkDir(dir: string): SidebarItem[] {
108108}
109109
110110function formatText ( text : string ) : string {
111- // 将文件名转换为更友好的显示文本
112111 return text
113112 . replace ( / - / g, ' ' )
114113 . split ( ' ' )
0 commit comments