diff --git a/.github/workflows/readme.yml b/.github/workflows/readme.yml new file mode 100644 index 0000000..c7359de --- /dev/null +++ b/.github/workflows/readme.yml @@ -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 }} diff --git a/README.md b/README.md index b0a923b..7a9a8a8 100644 --- a/README.md +++ b/README.md @@ -33,4 +33,11 @@ +
+ +## Solutions + + + +
\ No newline at end of file diff --git a/_config.yml b/_config.yml index 4ba4562..5ade8f6 100644 --- a/_config.yml +++ b/_config.yml @@ -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 diff --git a/scripts/index.js b/scripts/index.js new file mode 100644 index 0000000..df3f2cf --- /dev/null +++ b/scripts/index.js @@ -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 = ` + +| 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 += ""; + + // Read the existing README content + let readmeContent = fs.readFileSync("README.md", "utf8"); + + // Checking whether the solutions table already exists + if (readmeContent.includes("")) { + // Replacing the existing table + readmeContent = readmeContent.replace( + /[\s\S]*/, + 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(); \ No newline at end of file diff --git a/scripts/package.json b/scripts/package.json new file mode 100644 index 0000000..cb2eb3f --- /dev/null +++ b/scripts/package.json @@ -0,0 +1,6 @@ +{ + "type": "module", + "dependencies": { + "node-fetch": "^3.2.0" + } +}