@@ -26,24 +26,22 @@ function generateTableOfContents(directory, depth = 0) {
26
26
27
27
// Get the stats of the file
28
28
const stats = fs . statSync ( filePath ) ;
29
-
29
+ const relativePath = path . relative ( directory , filePath ) . replace ( / \\ / g , "/" ) ;
30
30
// If the file is a directory, generate the table of contents recursively
31
+
31
32
if ( stats . isDirectory ( ) ) {
32
33
// Capitalize the first letter of the directory name and add it to the table of contents
33
34
const folderName = file . charAt ( 0 ) . toUpperCase ( ) + file . slice ( 1 ) ;
34
- const relativePath = path . relative ( directory , filePath ) . replace ( / \\ / g, "/" ) ;
35
35
tableOfContents += `${ " " . repeat ( depth * 2 ) } - [${ folderName } ](${ relativePath } )\n` ;
36
+ traverseDirectory ( filePath , depth + 1 ) ;
36
37
37
38
// Recursively traverse the directory
38
39
traverseDirectory ( filePath , depth + 1 ) ;
39
40
}
40
41
// If the file is a JavaScript file, generate a link to it in the table of contents
41
42
else if ( file . endsWith ( ".js" ) ) {
42
- // Get the relative path of the file
43
- const relativePath = path . relative ( directory , filePath ) . replace ( / \\ / g, "/" ) ;
44
-
45
- // Add a link to the file in the table of contents
46
- tableOfContents += `${ " " . repeat ( ( depth + 1 ) * 2 ) } - [${ file } ](${ relativePath } )\n` ;
43
+ const fileName = path . basename ( file , '.js' ) ;
44
+ tableOfContents += `${ " " . repeat ( ( depth + 1 ) * 2 ) } - [${ fileName } ](${ relativePath } )\n` ;
47
45
}
48
46
}
49
47
}
0 commit comments