-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_toc.js
100 lines (77 loc) · 3.97 KB
/
generate_toc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// This JavaScript file is responsible for generating a table of contents (TOC)
// for a given directory and inserting it into a README.md file. The TOC
// lists the directories and JavaScript files within the given directory.
// Load the built-in 'fs' module to interact with the file system
const fs = require('fs');
// Load the built-in 'path' module to manipulate file paths
const path = require('path');
// Define a function to generate a table of contents for a given directory
function generateTableOfContents(directory, depth = 0) {
// Initialize the table of contents with an empty string
let tableOfContents = "";
// Define a helper function to recursively traverse the directory and generate the table of contents
function traverseDirectory(currentPath, depth) {
// Get the files in the current directory
const files = fs.readdirSync(currentPath);
// Traverse each file in the directory
for (const file of files) {
// Get the full path of the file
const filePath = path.join(currentPath, file);
// Get the stats of the file
const stats = fs.statSync(filePath);
// If the file is a directory, generate the table of contents recursively
const prefix = "Tutorials" + "/";
if (stats.isDirectory()) {
// Capitalize the first letter of the directory name and add it to the table of contents
const folderName = file.charAt(0).toUpperCase() + file.slice(1);
const relativePath = prefix + file;
tableOfContents += `${" ".repeat(depth * 2)}- [${folderName}](${relativePath})\n`;
// Recursively traverse the directory
traverseDirectory(filePath, depth + 1);
}
// If the file is a JavaScript file, generate a link to it in the table of contents
else if (file.endsWith(".js")) {
// Get the relative path of the file
const relativePath = prefix + file;
// Add a link to the file in the table of contents
tableOfContents += `${" ".repeat((depth + 1) * 2)}- [${file}](${relativePath})\n`;
}
}
}
// Start traversing the given directory
traverseDirectory(directory, depth);
// Return the generated table of contents
return tableOfContents;
}
// Define the directory to generate the table of contents for
const directory = 'Tutorials';
// Generate the table of contents
const tableOfContents = generateTableOfContents(directory);
// Define the path to the README.md file
const readmePath = path.join(process.env.GITHUB_WORKSPACE, 'README.md');
console.log(readmePath);
// Read the content of the README.md file
let readmeContent = fs.readFileSync(readmePath, 'utf8');
// Define unique markers to indicate the start and end of the TOC in the README content
const startMarker = '<!--TOC_START-->';
const endMarker = '<!--TOC_END-->';
// Find the range of the start and end markers in the README content
const startIndex = readmeContent.indexOf(startMarker);
const endIndex = readmeContent.indexOf(endMarker);
// If the start and end markers are found, update the content with the generated table of contents
if (startIndex !== -1 && endIndex !== -1) {
const updatedContent = readmeContent.slice(0, startIndex) + startMarker + '\n' + tableOfContents + '\n' + endMarker + readmeContent.slice(endIndex + endMarker.length);
// Write the updated content back to the README file
fs.writeFile(readmePath, updatedContent, { encoding: 'utf8' }, (err) => {
if (err) {
console.error("Error writing to README file:", err);
} else {
console.log("README file updated successfully!");
console.log("Changes committed successfully!");
}
});
}
// If the start or end markers are not found, log an error message
else {
console.error("Unable to find the start or end markers in the README content. Update failed.");
}