Skip to content

Commit bd9fb70

Browse files
committed
Solve the incorrect links in TOC
1 parent 37e19d2 commit bd9fb70

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

generate_toc.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,22 @@ function generateTableOfContents(directory, depth = 0) {
2626

2727
// Get the stats of the file
2828
const stats = fs.statSync(filePath);
29-
29+
const relativePath = path.relative(directory, filePath).replace(/\\/g, "/");
3030
// If the file is a directory, generate the table of contents recursively
31+
3132
if (stats.isDirectory()) {
3233
// Capitalize the first letter of the directory name and add it to the table of contents
3334
const folderName = file.charAt(0).toUpperCase() + file.slice(1);
34-
const relativePath = path.relative(directory, filePath).replace(/\\/g, "/");
3535
tableOfContents += `${" ".repeat(depth * 2)}- [${folderName}](${relativePath})\n`;
36+
traverseDirectory(filePath, depth + 1);
3637

3738
// Recursively traverse the directory
3839
traverseDirectory(filePath, depth + 1);
3940
}
4041
// If the file is a JavaScript file, generate a link to it in the table of contents
4142
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`;
4745
}
4846
}
4947
}

0 commit comments

Comments
 (0)