Skip to content

Commit 7a0b660

Browse files
committed
Correct link generation (bd9fb was incorrect)
1 parent 0bad9ff commit 7a0b660

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

generate_toc.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const fs = require('fs');
88
// Load the built-in 'path' module to manipulate file paths
99
const path = require('path');
1010

11-
1211
// Define a function to generate a table of contents for a given directory
1312
function generateTableOfContents(directory, depth = 0) {
1413
// Initialize the table of contents with an empty string
@@ -26,22 +25,24 @@ function generateTableOfContents(directory, depth = 0) {
2625

2726
// Get the stats of the file
2827
const stats = fs.statSync(filePath);
29-
const relativePath = path.relative(directory, filePath).replace(/\\/g, "/");
3028
// If the file is a directory, generate the table of contents recursively
31-
29+
const prefix = path.relative(directory, currentPath).replace(/\\/g, "/") + "/";
3230
if (stats.isDirectory()) {
3331
// Capitalize the first letter of the directory name and add it to the table of contents
3432
const folderName = file.charAt(0).toUpperCase() + file.slice(1);
33+
const relativePath = prefix + file;
3534
tableOfContents += `${" ".repeat(depth * 2)}- [${folderName}](${relativePath})\n`;
36-
traverseDirectory(filePath, depth + 1);
3735

3836
// Recursively traverse the directory
3937
traverseDirectory(filePath, depth + 1);
4038
}
4139
// If the file is a JavaScript file, generate a link to it in the table of contents
4240
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`;
4546
}
4647
}
4748
}
@@ -95,3 +96,5 @@ if (startIndex !== -1 && endIndex !== -1) {
9596
else {
9697
console.error("Unable to find the start or end markers in the README content. Update failed.");
9798
}
99+
100+

0 commit comments

Comments
 (0)