Skip to content

Commit

Permalink
Added script to generate table of content (PalisadoesFoundation#1587)
Browse files Browse the repository at this point in the history
* Added script to generate table of content

* git hook to generate toc when markdown files are changed.

* script for toc
  • Loading branch information
Veer0x1 authored and AVtheking committed Jan 6, 2024
1 parent 6583443 commit 3d7de50
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 84 deletions.
8 changes: 6 additions & 2 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ npm run lint:fix

npm run gen:schema

# Generate table of contents for markdown files in root directory
npm run update:toc
# Check if any markdown files have been changed
if git diff --cached --name-only | grep -E '\.md$'
then
# Generate table of contents for markdown files in root directory
npm run generate:toc
fi

# Add the changes made to the stage
git add .
8 changes: 4 additions & 4 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
- [Scope](#scope)
- [Enforcement](#enforcement)
- [Enforcement Guidelines](#enforcement-guidelines)
- [1. Correction](#1-correction)
- [2. Warning](#2-warning)
- [3. Temporary Ban](#3-temporary-ban)
- [4. Permanent Ban](#4-permanent-ban)
* [1. Correction](#1-correction)
* [2. Warning](#2-warning)
* [3. Temporary Ban](#3-temporary-ban)
* [4. Permanent Ban](#4-permanent-ban)
- [Attribution](#attribution)

<!-- tocstop -->
Expand Down
39 changes: 19 additions & 20 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ If you are new to contributing to open source, please read the Open Source Guide

- [Code of Conduct](#code-of-conduct)
- [Ways to Contribute](#ways-to-contribute)
- [Our Development Process](#our-development-process)
- [Issues](#issues)
- [Pull Requests](#pull-requests)
- [Branching Strategy](#branching-strategy)
- [Conflict Resolution](#conflict-resolution)
- [Contributing Code](#contributing-code)
* [Our Development Process](#our-development-process)
+ [Issues](#issues)
+ [Pull Requests](#pull-requests)
+ [Branching Strategy](#branching-strategy)
+ [Conflict Resolution](#conflict-resolution)
* [Contributing Code](#contributing-code)
- [Upgrading Code](#upgrading-code)
- [Setting up Upstream and Origin](#setting-up-upstream-and-origin)
- [Creating a Pull Request - Process Overview](#creating-a-pull-request---process-overview)
+ [Setting up Upstream and Origin](#setting-up-upstream-and-origin)
+ [Creating a Pull Request - Process Overview](#creating-a-pull-request---process-overview)
- [Type checking and code quality](#type-checking-and-code-quality)
- [Type checking code files](#type-checking-code-files)
- [Linting code files](#linting-code-files)
- [Formatting code files](#formatting-code-files)
- [Generating Table of Contents](#generating-table-of-contents)
- [Automation using husky](#automation-using-husky)
- [Pre-Commit hook](#pre-commit-hook)
- [Post-Merge hook](#post-merge-hook)
- [GraphQL Voyager](#graphql-voyager)
- [GraphQL Markdown](#graphql-markdown)
- [Running Queries with talawa-api](#running-queries-with-talawa-api)
* [Type checking code files](#type-checking-code-files)
* [Linting code files](#linting-code-files)
* [Formatting code files](#formatting-code-files)
* [Generating Table of Contents](#generating-table-of-contents)
* [Automation using husky](#automation-using-husky)
+ [Pre-Commit hook](#pre-commit-hook)
+ [Post-Merge hook](#post-merge-hook)
* [GraphQL Voyager](#graphql-voyager)
* [GraphQL Markdown](#graphql-markdown)
* [Running Queries with talawa-api](#running-queries-with-talawa-api)
- [Internships](#internships)
- [Community](#community)

Expand Down Expand Up @@ -243,7 +243,7 @@ To fix formatting issues in code use this command:-
### Generating Table of Contents
To generate table of contents for markdown files use this command:-
```bash
npm run update:toc
npm run generate:toc
```
And if you want to generate table of content for specific file you can use the `markdown-toc` package directly as shown:-
```bash
Expand All @@ -253,7 +253,6 @@ Remember to replace `README.md` with the name of the file you want to generate t
</br>
For more information on how to use `markdown-toc` package, visit [this link](https://www.npmjs.com/package/markdown-toc).


### Automation using husky

<br/>
Expand Down
87 changes: 43 additions & 44 deletions INSTALLATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,57 +13,56 @@ This document provides instructions on how to set up and start a running instanc
- [Install the Required Packages](#install-the-required-packages)
- [Installation Using Docker](#installation-using-docker)
- [Installation without Docker](#installation-without-docker)
- [Install MongoDB](#install-mongodb)
- [Setting up the mongoDB database](#setting-up-the-mongodb-database)
- [Install Redis](#install-redis)
- [Performance Benefits](#performance-benefits)
- [Setting Up Redis](#setting-up-redis)
* [Install MongoDB](#install-mongodb)
+ [Setting up the mongoDB database](#setting-up-the-mongodb-database)
* [Install Redis](#install-redis)
+ [Performance Benefits](#performance-benefits)
+ [Setting Up Redis](#setting-up-redis)
- [Configuration](#configuration)
- [Automated Configuration of `.env`](#automated-configuration-of-env)
- [Manual Configuration of `.env`](#manual-configuration-of-env)
- [The Environment Variables in `.env`](#the-environment-variables-in-env)
- [Changing the Development Environment in the .env file](#changing-the-development-environment-in-the-env-file)
- [Generating Token Secrets](#generating-token-secrets)
- [Setting up ACCESS_TOKEN_SECRET in the .env file](#setting-up-access_token_secret-in-the-env-file)
- [Linux](#linux)
- [Windows](#windows)
- [Setting up REFRESH_TOKEN_SECRET in the .env file](#setting-up-refresh_token_secret-in-the-env-file)
- [Configuring MongoDB in the .env file](#configuring-mongodb-in-the-env-file)
- [Using the CLI to get the MONGODB_URL Connection String](#using-the-cli-to-get-the-mongodb_url-connection-string)
- [Using Microsoft Windows to get the MONGODB_URL Connection String](#using-microsoft-windows-to-get-the-mongodb_url-connection-string)
- [Configuring Redis in .env file](#configuring-redis-in-env-file)
- [For Local Setup (Linux and WSL)](#for-local-setup-linux-and-wsl)
- [For Remote Setup (Redis Cloud)](#for-remote-setup-redis-cloud)
- [Setting up .env LAST_RESORT_SUPERADMIN_EMAIL parameter in the .env file](#setting-up-env-last_resort_superadmin_email-parameter-in-the-env-file)
- [Configuring Google ReCAPTCHA in the .env file](#configuring-google-recaptcha-in-the-env-file)
- [Setting up the RECAPTCHA_SECRET_KEY](#setting-up-the-recaptcha_secret_key)
- [Setting up .env MAIL_USERNAME and MAIL_PASSWORD ReCAPTCHA Parameters](#setting-up-env-mail_username-and-mail_password-recaptcha-parameters)
- [Setting up SMTP Email Variables in the .env File](#setting-up-smtp-email-variables-in-the-env-file)
- [Setting up Logger configurations _(optional)_](#setting-up-logger-configurations-_optional_)
- [Setting up COLORIZE_LOGS in .env file](#setting-up-colorize_logs-in-env-file)
- [Setting up LOG_LEVEL in .env file](#setting-up-log_level-in-env-file)
* [The .env Configuration File](#the-env-configuration-file)
* [Changing the environment of talawa-api](#changing-the-environment-of-talawa-api)
* [Generating Token Secrets](#generating-token-secrets)
+ [Setting up ACCESS_TOKEN_SECRET in .env file](#setting-up-access_token_secret-in-env-file)
- [Linux](#linux)
- [Windows](#windows)
+ [Setting up REFRESH_TOKEN_SECRET in .env file](#setting-up-refresh_token_secret-in-env-file)
* [Configuring MongoDB](#configuring-mongodb)
+ [Setting up the MONGODB_URL in .env file](#setting-up-the-mongodb_url-in-env-file)
+ [Using the CLI to get the MONGODB_URL Connection String](#using-the-cli-to-get-the-mongodb_url-connection-string)
+ [Using Microsoft Windows to get the MONGODB_URL Connection String](#using-microsoft-windows-to-get-the-mongodb_url-connection-string)
* [Configuring Redis](#configuring-redis)
+ [For Local Setup (Linux and WSL)](#for-local-setup-linux-and-wsl)
+ [For Remote Setup (Redis Cloud)](#for-remote-setup-redis-cloud)
* [Setting up .env LAST_RESORT_SUPERADMIN_EMAIL parameter](#setting-up-env-last_resort_superadmin_email-parameter)
* [Configuring Google ReCAPTCHA](#configuring-google-recaptcha)
+ [Setting up RECAPTCHA_SECRET_KEY in .env file](#setting-up-recaptcha_secret_key-in-env-file)
+ [Setting up .env MAIL_USERNAME and MAIL_PASSWORD ReCAPTCHA Parameters](#setting-up-env-mail_username-and-mail_password-recaptcha-parameters)
+ [Setting up .env SMTP Variables](#setting-up-env-smtp-variables)
* [Setting up Logger configurations _(optional)_](#setting-up-logger-configurations-_optional_)
+ [Setting up COLORIZE_LOGS in .env file](#setting-up-colorize_logs-in-env-file)
+ [Setting up LOG_LEVEL in .env file](#setting-up-log_level-in-env-file)
- [Importing Sample Database](#importing-sample-database)
- [Syntax:](#syntax)
- [Examples:](#examples)
- [Sample Data Overview:](#sample-data-overview)
- [User Accounts and Organizations:](#user-accounts-and-organizations)
- [Posts](#posts)
- [Events](#events)
* [Syntax:](#syntax)
* [Examples:](#examples)
* [Sample Data Overview:](#sample-data-overview)
+ [User Accounts and Organizations:](#user-accounts-and-organizations)
+ [Posts](#posts)
+ [Events](#events)
- [Running Talawa-API](#running-talawa-api)
- [How to Access the Talawa-API URL](#how-to-access-the-talawa-api-url)
- [For Talawa-API Developers](#for-talawa-api-developers)
- [For Mobile App Developers](#for-mobile-app-developers)
- [On Android Virtual Device](#on-android-virtual-device)
- [On a Real Mobile Device](#on-a-real-mobile-device)
- [For Talawa-Admin Developers](#for-talawa-admin-developers)
* [For Talawa-API Developers](#for-talawa-api-developers)
* [For Mobile App Developers](#for-mobile-app-developers)
+ [On Android Virtual Device](#on-android-virtual-device)
+ [On a Real Mobile Device](#on-a-real-mobile-device)
* [For Talawa-Admin Developers](#for-talawa-admin-developers)
- [Accessing MongoDB](#accessing-mongodb)
- [Managing MongoDB using the MongoDB Compass GUI](#managing-mongodb-using-the-mongodb-compass-gui)
- [Managing MongoDB using the VSCode extension](#managing-mongodb-using-the-vscode-extension)
* [Managing MongoDB using the MongoDB Compass GUI](#managing-mongodb-using-the-mongodb-compass-gui)
* [Managing MongoDB using the VSCode extension](#managing-mongodb-using-the-vscode-extension)
- [Manually Adding The First Super Admin User](#manually-adding-the-first-super-admin-user)
- [Using MongoDB Compass](#using-mongodb-compass)
- [Using Mongo Shell](#using-mongo-shell)
* [Using MongoDB Compass](#using-mongodb-compass)
* [Using Mongo Shell](#using-mongo-shell)
- [Other](#other)
- [Changing default talawa-api port](#changing-default-talawa-api-port)
* [Changing default talawa-api port](#changing-default-talawa-api-port)
- [Testing](#testing)

<!-- tocstop -->
Expand Down
8 changes: 4 additions & 4 deletions ISSUE_GUIDELINES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ ___
<!-- toc -->

- [Issue Management](#issue-management)
- [New Issues](#new-issues)
- [Existing Issues](#existing-issues)
- [Feature Request Issues](#feature-request-issues)
- [Monitoring the Creation of New Issues](#monitoring-the-creation-of-new-issues)
* [New Issues](#new-issues)
* [Existing Issues](#existing-issues)
* [Feature Request Issues](#feature-request-issues)
* [Monitoring the Creation of New Issues](#monitoring-the-creation-of-new-issues)
- [General Guidelines](#general-guidelines)

<!-- tocstop -->
Expand Down
8 changes: 4 additions & 4 deletions PR_GUIDELINES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
<!-- toc -->

- [Pull Request Guidelines](#pull-request-guidelines)
- [Pull Requests and Issues](#pull-requests-and-issues)
- [Testing](#testing)
- [Pull Request Processing](#pull-request-processing)
- [Only submit PRs against our `develop` branch, not the default `main` branch](#only-submit-prs-against-our-develop-branch-not-the-default-main-branch)
* [Pull Requests and Issues](#pull-requests-and-issues)
* [Testing](#testing)
* [Pull Request Processing](#pull-request-processing)
+ [Only submit PRs against our `develop` branch, not the default `main` branch](#only-submit-prs-against-our-develop-branch-not-the-default-main-branch)

<!-- tocstop -->

Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
## Table of Contents


<!-- toc -->

- [Talawa API](#talawa-api)
- [Talawa Components](#talawa-components)
- [Documentation](#documentation)
- [Installation](#installation)
- [Image Upload](#image-upload)
* [Talawa Components](#talawa-components)
* [Documentation](#documentation)
* [Installation](#installation)
* [Image Upload](#image-upload)

<!-- tocstop -->

Expand Down
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"import:sample-data": "ts-node ./src/utilities/loadSampleData.ts",
"import:sample-data:prod": "node ./build/utilities/loadSampleData.js",
"gen:schema": "graphql-inspector introspect ./src/typeDefs/**/**/*.ts --write ./schema.graphql ",
"update:toc": "node scripts/githooks/update-toc.js"
"generate:toc": "node src/scripts/githooks/generate-toc"
},
"repository": {
"type": "git",
Expand Down
60 changes: 60 additions & 0 deletions src/scripts/githooks/generate-toc.js
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();

0 comments on commit 3d7de50

Please sign in to comment.