Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .github/workflows/readme.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Generate Table of Contents

on:
push:
branches: ["main"]
workflow_dispatch:
schedule:
- cron: '0 0 * * *' # Runs at 00:00 UTC every day

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: "20"

- name: Install dependencies
run: npm install
working-directory: scripts

- name: Run index.js
run: node index.js
working-directory: scripts

- name: Commit and push if changed
run: |
git config --global user.email "actions@github.com"
git config --global user.name "GitHub Actions"
git add README.md
git diff --quiet && git diff --staged --quiet || (git commit -m "Update README with table of contents" && git push)
working-directory: ${{ github.workspace }}
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,11 @@
<a href="https://www.instagram.com/yashksaini.codes/"> <img src="https://img.shields.io/badge/Instagram-%23FF006E.svg?style=for-the-badge&logo=Instagram&logoColor=white"> </a>
<a href="https://twitter.com/EasycodesDev"> <img src="https://img.shields.io/badge/X-%23000000.svg?style=for-the-badge&logo=X&logoColor=white"> </a>
</div>
<br>

## Solutions

<!-- SOLUTIONS TABLE BEGIN -->
<!-- SOLUTIONS TABLE END -->

<br>
2 changes: 1 addition & 1 deletion _config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
title: September-Leetcode-Daily-2024
description: A Reposotroy to maintain my September Leetcode Coding session 2024
description: A Repository to maintain my September Leetcode Coding session 2024
show_downloads: false
google_analytics:
theme: jekyll-theme-hacker
76 changes: 76 additions & 0 deletions scripts/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import fetch from "node-fetch";
import fs from "fs";

const toc = async () => {
// Fetch from the GitHubAPI
const data = await fetch(
"https://api.github.com/repos/yashksaini-coder/September-Leetcode-Daily-2024/contents?ref=main"
)
.then((response) => response.json())
.then((data) => data);

var arr = Object.values(data);

// Filter out the folders
const folders = arr.filter(
(item) => item.type === "dir" && item.name[0] !== "."
);

// Make a table of contents
var toc = [];
folders.forEach((item) => {
var num = parseInt(item.name.split("-")[0]);
toc[num] = item.name;
});

// Sort toc by key
var sorted = Object.keys(toc)
.sort()
.reduce((obj, key) => {
obj[key] = toc[key];
return obj;
}, {});

// Generate the table of solutions
let solutionsTable = `
<!-- SOLUTIONS TABLE BEGIN -->
| Leetcode Problem | Problem Statement | Solution |
|---:|:-----|:----:|
`;
for (var key in sorted) {
var str = sorted[key].split("-");
var name = str.slice(1).join(" ");
var num = key;
solutionsTable += `| [${num}](https://leetcode.com/problems/${str.slice(1).join("-")}/) | ${name} | [Solution](./${str.join("-")}/${str.join("-")}.java) |\n`;
}
solutionsTable += "<!-- SOLUTIONS TABLE END -->";

// Read the existing README content
let readmeContent = fs.readFileSync("README.md", "utf8");

// Checking whether the solutions table already exists
if (readmeContent.includes("<!-- SOLUTIONS TABLE BEGIN -->")) {
// Replacing the existing table
readmeContent = readmeContent.replace(
/<!-- SOLUTIONS TABLE BEGIN -->[\s\S]*<!-- SOLUTIONS TABLE END -->/,
solutionsTable
);
} else {
// Find the "## Solutions" heading and insert the table after it. It is necessary the heading matches exactly.
const solutionsHeading = "## Solutions";
const headingIndex = readmeContent.indexOf(solutionsHeading);
if (headingIndex !== -1) {
const insertIndex = headingIndex + solutionsHeading.length;
readmeContent = readmeContent.slice(0, insertIndex) + "\n\n" + solutionsTable + readmeContent.slice(insertIndex);
} else {
console.error("Could not find '## Solutions' heading in README.md");
return;
}
}

// Write the updated content back to README.md
fs.writeFileSync("README.md", readmeContent);
console.log("README.md has been updated with the solutions table!");
};

toc();
6 changes: 6 additions & 0 deletions scripts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "module",
"dependencies": {
"node-fetch": "^3.2.0"
}
}