@@ -3,55 +3,54 @@ import fs from 'fs';
33import matter from 'gray-matter' ;
44
55interface StarlightSidebarFolder {
6- label : string ;
7- items : StarlightSidebarList ;
8- collapsed ?: boolean ;
6+ label : string ;
7+ items : StarlightSidebarList ;
8+ collapsed ?: boolean ;
99}
1010
1111type StarlightSidebarFile = string ;
1212
1313export type StarlightSidebarList = Array < StarlightSidebarFile | StarlightSidebarFolder > ;
1414
1515/**
16- * Recursively walk through a documentation directory and build a sidebar structure.
17- * Handles both folders (nested sections) and Markdown files.
18- */
16+ * Recursively walk through a documentation directory and build a sidebar structure.
17+ * Handles both folders (nested sections) and Markdown files.
18+ */
1919export function walkDocsRecursive ( dir : string ) : StarlightSidebarList {
20- const entries = fs . readdirSync ( dir ) ;
21- const result : StarlightSidebarList = [ ] ;
22-
23- // Sort all entry file (If named index.*, it will be first)
24- entries . sort ( ( a , b ) => {
25- const aIsIndex = a . startsWith ( 'index.' ) ;
26- const bIsIndex = b . startsWith ( 'index.' ) ;
27- if ( aIsIndex && ! bIsIndex ) return - 1 ;
28- if ( ! aIsIndex && bIsIndex ) return 1 ;
29- return a . localeCompare ( b ) ;
30- } ) ;
31-
32- for ( const entry of entries ) {
33- const entryPath = path . join ( dir , entry ) ;
34- const stat = fs . statSync ( entryPath ) ;
35-
36- if ( stat . isDirectory ( ) ) {
37- // Clean label by removing numeric prefix (if present)
38- const label = entry . replace ( / ^ \d + \s + / , '' ) ; // Remove leading numbers and spaces
39-
40- result . push ( {
41- label,
42- items : walkDocsRecursive ( entryPath ) // Recurse into subdirectory
43- } ) ;
44- } else {
45- // Read file contents and extract frontmatter using gray-matter
46- const fileContent = fs . readFileSync ( entryPath , 'utf8' ) ;
47- const parsed = matter ( fileContent ) ;
48-
49- if ( parsed . data . hideInSidebar && parsed . data . hideInSidebar === true ) continue ;
50-
51- result . push ( String ( parsed . data . slug ?? '' ) ) ;
52- }
20+ const entries = fs . readdirSync ( dir ) ;
21+ const result : StarlightSidebarList = [ ] ;
22+
23+ // Sort all entry file (If named index.*, it will be first)
24+ entries . sort ( ( a , b ) => {
25+ const aIsIndex = a . startsWith ( 'index.' ) ;
26+ const bIsIndex = b . startsWith ( 'index.' ) ;
27+ if ( aIsIndex && ! bIsIndex ) return - 1 ;
28+ if ( ! aIsIndex && bIsIndex ) return 1 ;
29+ return a . localeCompare ( b ) ;
30+ } ) ;
31+
32+ for ( const entry of entries ) {
33+ const entryPath = path . join ( dir , entry ) ;
34+ const stat = fs . statSync ( entryPath ) ;
35+
36+ if ( stat . isDirectory ( ) ) {
37+ // Clean label by removing numeric prefix (if present)
38+ const label = entry . replace ( / ^ \d + \s + / , '' ) ; // Remove leading numbers and spaces
39+
40+ result . push ( {
41+ label,
42+ items : walkDocsRecursive ( entryPath ) // Recurse into subdirectory
43+ } ) ;
44+ } else {
45+ // Read file contents and extract frontmatter using gray-matter
46+ const fileContent = fs . readFileSync ( entryPath , 'utf8' ) ;
47+ const parsed = matter ( fileContent ) ;
48+
49+ if ( parsed . data . hideInSidebar && parsed . data . hideInSidebar === true ) continue ;
50+
51+ result . push ( String ( parsed . data . slug ?? '' ) ) ;
5352 }
53+ }
5454
55- return result ;
55+ return result ;
5656}
57-
0 commit comments