forked from PalisadoesFoundation/talawa-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added script to generate table of content (PalisadoesFoundation#1587)
* Added script to generate table of content * git hook to generate toc when markdown files are changed. * script for toc
- Loading branch information
Showing
9 changed files
with
154 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const { execSync } = require("child_process"); | ||
|
||
// Getting the current working directory | ||
const directory = process.cwd(); | ||
|
||
/** | ||
* This function finds all markdown files at the root level of a given directory. | ||
* @param {string} dir - The directory to search in. | ||
* @returns {string[]} - An array of markdown file names. | ||
*/ | ||
const findMarkdownFiles = (dir) => { | ||
// Reading the directory | ||
const dirents = fs.readdirSync(dir, { withFileTypes: true }); | ||
|
||
// Filtering out the files | ||
const files = dirents | ||
.filter((dirent) => dirent.isFile()) | ||
.map((dirent) => dirent.name); | ||
|
||
// Returning only the markdown files | ||
return files.filter((file) => path.extname(file) === ".md"); | ||
}; | ||
|
||
/** | ||
* This function generates a table of contents for a given markdown file. | ||
* @param {string} file - The markdown file to generate a table of contents for. | ||
*/ | ||
const generateTOC = (file) => { | ||
// Constructing the file path | ||
const filePath = path.join(directory, file); | ||
|
||
// Generating the table of contents | ||
execSync(`markdown-toc -i ${filePath}`); | ||
|
||
// Logging the result | ||
console.log(`Table of contents generated for ${file}`); | ||
}; | ||
|
||
/** | ||
* The main function of the script. | ||
* It finds all markdown files in the current directory and generates a table of contents for each one. | ||
*/ | ||
function main() { | ||
// Finding all markdown files | ||
const markdownFiles = findMarkdownFiles(directory); | ||
|
||
// Checking if any markdown files were found | ||
if (markdownFiles.length === 0) { | ||
// Logging an error message and returning if no markdown files were found | ||
console.error("No Markdown files found in the current directory."); | ||
return; | ||
} | ||
|
||
// Generating a table of contents for each markdown file | ||
markdownFiles.forEach(generateTOC); | ||
} | ||
|
||
main(); |