@@ -8,7 +8,6 @@ const fs = require('fs');
8
8
// Load the built-in 'path' module to manipulate file paths
9
9
const path = require ( 'path' ) ;
10
10
11
-
12
11
// Define a function to generate a table of contents for a given directory
13
12
function generateTableOfContents ( directory , depth = 0 ) {
14
13
// Initialize the table of contents with an empty string
@@ -26,22 +25,24 @@ function generateTableOfContents(directory, depth = 0) {
26
25
27
26
// Get the stats of the file
28
27
const stats = fs . statSync ( filePath ) ;
29
- const relativePath = path . relative ( directory , filePath ) . replace ( / \\ / g, "/" ) ;
30
28
// If the file is a directory, generate the table of contents recursively
31
-
29
+ const prefix = path . relative ( directory , currentPath ) . replace ( / \\ / g , "/" ) + "/" ;
32
30
if ( stats . isDirectory ( ) ) {
33
31
// Capitalize the first letter of the directory name and add it to the table of contents
34
32
const folderName = file . charAt ( 0 ) . toUpperCase ( ) + file . slice ( 1 ) ;
33
+ const relativePath = prefix + file ;
35
34
tableOfContents += `${ " " . repeat ( depth * 2 ) } - [${ folderName } ](${ relativePath } )\n` ;
36
- traverseDirectory ( filePath , depth + 1 ) ;
37
35
38
36
// Recursively traverse the directory
39
37
traverseDirectory ( filePath , depth + 1 ) ;
40
38
}
41
39
// If the file is a JavaScript file, generate a link to it in the table of contents
42
40
else if ( file . endsWith ( ".js" ) ) {
43
- const fileName = path . basename ( file , '.js' ) ;
44
- tableOfContents += `${ " " . repeat ( ( depth + 1 ) * 2 ) } - [${ fileName } ](${ relativePath } )\n` ;
41
+ // Get the relative path of the file
42
+ const relativePath = prefix + file ;
43
+
44
+ // Add a link to the file in the table of contents
45
+ tableOfContents += `${ " " . repeat ( ( depth + 1 ) * 2 ) } - [${ file } ](${ relativePath } )\n` ;
45
46
}
46
47
}
47
48
}
@@ -95,3 +96,5 @@ if (startIndex !== -1 && endIndex !== -1) {
95
96
else {
96
97
console . error ( "Unable to find the start or end markers in the README content. Update failed." ) ;
97
98
}
99
+
100
+
0 commit comments