Skip to content

Commit e7b0697

Browse files
author
yashksaini-coder
committed
Update README generation and add GitHub Actions workflow
- Modified scripts/index.js to generate LeetCode solutions table - Added GitHub Actions workflow to automatically update README - Updated README.md with placeholder for solutions table - Updated _config.yml for Jekyll configuration
1 parent 2b379d6 commit e7b0697

File tree

5 files changed

+127
-1
lines changed

5 files changed

+127
-1
lines changed

.github/workflows/readme.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Generate Table of Contents
2+
3+
on:
4+
push:
5+
branches: ["main"]
6+
workflow_dispatch:
7+
schedule:
8+
- cron: '0 0 * * *' # Runs at 00:00 UTC every day
9+
10+
jobs:
11+
build:
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Checkout code
16+
uses: actions/checkout@v2
17+
18+
- name: Set up Node.js
19+
uses: actions/setup-node@v2
20+
with:
21+
node-version: "20"
22+
23+
- name: Install dependencies
24+
run: npm install
25+
working-directory: scripts
26+
27+
- name: Run index.js
28+
run: node index.js
29+
working-directory: scripts
30+
31+
- name: Commit and push if changed
32+
run: |
33+
git config --global user.email "actions@github.com"
34+
git config --global user.name "GitHub Actions"
35+
git add README.md
36+
git diff --quiet && git diff --staged --quiet || (git commit -m "Update README with table of contents" && git push)
37+
working-directory: ${{ github.workspace }}

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,11 @@
3333
<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>
3434
<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>
3535
</div>
36+
<br>
37+
38+
## Solutions
39+
40+
<!-- SOLUTIONS TABLE BEGIN -->
41+
<!-- SOLUTIONS TABLE END -->
42+
3643
<br>

_config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
title: September-Leetcode-Daily-2024
2-
description: A Reposotroy to maintain my September Leetcode Coding session 2024
2+
description: A Repository to maintain my September Leetcode Coding session 2024
33
show_downloads: false
44
google_analytics:
55
theme: jekyll-theme-hacker

scripts/index.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import fetch from "node-fetch";
2+
import fs from "fs";
3+
4+
const toc = async () => {
5+
// Fetch from the GitHubAPI
6+
const data = await fetch(
7+
"https://api.github.com/repos/yashksaini-coder/September-Leetcode-Daily-2024/contents?ref=main"
8+
)
9+
.then((response) => response.json())
10+
.then((data) => data);
11+
12+
var arr = Object.values(data);
13+
14+
// Filter out the folders
15+
const folders = arr.filter(
16+
(item) => item.type === "dir" && item.name[0] !== "."
17+
);
18+
19+
// Make a table of contents
20+
var toc = [];
21+
folders.forEach((item) => {
22+
var num = parseInt(item.name.split("-")[0]);
23+
toc[num] = item.name;
24+
});
25+
26+
// Sort toc by key
27+
var sorted = Object.keys(toc)
28+
.sort()
29+
.reduce((obj, key) => {
30+
obj[key] = toc[key];
31+
return obj;
32+
}, {});
33+
34+
// Generate the table of solutions
35+
let solutionsTable = `
36+
<!-- SOLUTIONS TABLE BEGIN -->
37+
| Leetcode Problem | Problem Statement | Solution |
38+
|---:|:-----|:----:|
39+
`;
40+
for (var key in sorted) {
41+
var str = sorted[key].split("-");
42+
var name = str.slice(1).join(" ");
43+
var num = key;
44+
solutionsTable += `| [${num}](https://leetcode.com/problems/${str.slice(1).join("-")}/) | ${name} | [Solution](./${str.join("-")}/${str.join("-")}.java) |\n`;
45+
}
46+
solutionsTable += "<!-- SOLUTIONS TABLE END -->";
47+
48+
// Read the existing README content
49+
let readmeContent = fs.readFileSync("README.md", "utf8");
50+
51+
// Checking whether the solutions table already exists
52+
if (readmeContent.includes("<!-- SOLUTIONS TABLE BEGIN -->")) {
53+
// Replacing the existing table
54+
readmeContent = readmeContent.replace(
55+
/<!-- SOLUTIONS TABLE BEGIN -->[\s\S]*<!-- SOLUTIONS TABLE END -->/,
56+
solutionsTable
57+
);
58+
} else {
59+
// Find the "## Solutions" heading and insert the table after it. It is necessary the heading matches exactly.
60+
const solutionsHeading = "## Solutions";
61+
const headingIndex = readmeContent.indexOf(solutionsHeading);
62+
if (headingIndex !== -1) {
63+
const insertIndex = headingIndex + solutionsHeading.length;
64+
readmeContent = readmeContent.slice(0, insertIndex) + "\n\n" + solutionsTable + readmeContent.slice(insertIndex);
65+
} else {
66+
console.error("Could not find '## Solutions' heading in README.md");
67+
return;
68+
}
69+
}
70+
71+
// Write the updated content back to README.md
72+
fs.writeFileSync("README.md", readmeContent);
73+
console.log("README.md has been updated with the solutions table!");
74+
};
75+
76+
toc();

scripts/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "module",
3+
"dependencies": {
4+
"node-fetch": "^3.2.0"
5+
}
6+
}

0 commit comments

Comments
 (0)