Update generate-theme-doc.js (#3308)

* Update generate-theme-doc.js

Optimize README generation code for efficiency and readability.

* fix(scripts): fix some small bugs

This commit fixes some small bugs that were still present in the script
code.

---------

Co-authored-by: rickstaa <rick.staa@outlook.com>
This commit is contained in:
sahilpawar01
2023-10-13 13:36:06 +02:00
committed by GitHub
co-authored by rickstaa
parent 428e97bfcc
commit 28b65928d2
+46 -40
View File
@@ -2,11 +2,14 @@ import fs from "fs";
import { themes } from "../themes/index.js";
const TARGET_FILE = "./themes/README.md";
const REPO_CARD_LINKS_FLAG = "<!-- REPO_CARD_LINKS -->";
const STAT_CARD_LINKS_FLAG = "<!-- STATS_CARD_LINKS -->";
const STAT_CARD_TABLE_FLAG = "<!-- STATS_CARD_TABLE -->";
const REPO_CARD_TABLE_FLAG = "<!-- REPO_CARD_TABLE -->";
const LINKS_FLAG_MAP = {
repo: "<!-- REPO_CARD_LINKS -->",
stats: "<!-- STATS_CARD_LINKS -->",
};
const TABLE_FLAG_MAP = {
repo: "<!-- REPO_CARD_TABLE -->",
stats: "<!-- STATS_CARD_TABLE -->",
};
const THEME_TEMPLATE = `## Available Themes
@@ -14,7 +17,7 @@ const THEME_TEMPLATE = `## Available Themes
With inbuilt themes, you can customize the look of the card without doing any manual customization.
Use \`?theme=THEME_NAME\` parameter like so :-
Use \`?theme=THEME_NAME\` parameter like so:
\`\`\`md
![Anurag's GitHub stats](https://github-readme-stats.vercel.app/api?username=anuraghazra&theme=dark&show_icons=true)
@@ -26,7 +29,7 @@ Use \`?theme=THEME_NAME\` parameter like so :-
| | | |
| :--: | :--: | :--: |
${STAT_CARD_TABLE_FLAG}
${TABLE_FLAG_MAP.stats}
## Repo Card
@@ -34,36 +37,36 @@ ${STAT_CARD_TABLE_FLAG}
| | | |
| :--: | :--: | :--: |
${REPO_CARD_TABLE_FLAG}
${TABLE_FLAG_MAP.repo}
${STAT_CARD_LINKS_FLAG}
${REPO_CARD_LINKS_FLAG}
${LINKS_FLAG_MAP.stats}
${LINKS_FLAG_MAP.repo}
[add-theme]: https://github.com/anuraghazra/github-readme-stats/edit/master/themes/index.js
Want to add a new theme? Consider reading the [contribution guidelines](../CONTRIBUTING.md#themes-contribution) :D
`;
const createRepoMdLink = (theme) => {
return `\n[${theme}_repo]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=${theme}`;
};
const createStatMdLink = (theme) => {
return `\n[${theme}]: https://github-readme-stats.vercel.app/api?username=anuraghazra&show_icons=true&hide=contribs,prs&cache_seconds=86400&theme=${theme}`;
const createMdLink = (theme, type) => {
const baseLink =
type === "repo"
? "api/pin/?username=anuraghazra&repo=github-readme-stats"
: "api?username=anuraghazra";
return `\n[${theme}]: https://github-readme-stats.vercel.app/${baseLink}&cache_seconds=86400&theme=${theme}`;
};
const generateLinks = (fn) => {
const generateLinks = (type) => {
return Object.keys(themes)
.map((name) => fn(name))
.map((name) => createMdLink(name, type))
.join("");
};
const createTableItem = ({ link, label, isRepoCard }) => {
const createTableItem = ({ link, label }) => {
if (!link || !label) {
return "";
}
return `\`${label}\` ![${link}][${link}${isRepoCard ? "_repo" : ""}]`;
return `\`${label}\` ![${link}][${link}]`;
};
const generateTable = ({ isRepoCard }) => {
@@ -73,22 +76,23 @@ const generateTable = ({ isRepoCard }) => {
);
for (let i = 0; i < themesFiltered.length; i += 3) {
const one = themesFiltered[i];
const two = themesFiltered[i + 1];
const three = themesFiltered[i + 2];
const [one, two, three] = themesFiltered.slice(i, i + 3);
let tableItem1 = createTableItem({ link: one, label: one, isRepoCard });
let tableItem2 = createTableItem({ link: two, label: two, isRepoCard });
let tableItem3 = createTableItem({ link: three, label: three, isRepoCard });
const tableItem1 = createTableItem({ link: one, label: one });
const tableItem2 = createTableItem({ link: two, label: two });
const tableItem3 = createTableItem({ link: three, label: three });
if (three === undefined) {
tableItem3 = `[Add your theme][add-theme]`;
}
rows.push(`| ${tableItem1} | ${tableItem2} | ${tableItem3} |`);
// if it's the last row & the row has no empty space push a new row
if (three && i + 3 === themesFiltered.length) {
rows.push(`| [Add your theme][add-theme] | | |`);
if (i + 3 >= themesFiltered.length) {
// If last row add your theme placeholder.
if (!three) {
rows.push(
` ${tableItem1} | ${tableItem2} | [Add your theme][add-theme] |`,
);
} else {
rows.push(`| [Add your theme][add-theme] | | |`);
}
} else {
rows.push(`| ${tableItem1} | ${tableItem2} | ${tableItem3} |`);
}
}
@@ -98,16 +102,16 @@ const generateTable = ({ isRepoCard }) => {
const buildReadme = () => {
return THEME_TEMPLATE.split("\n")
.map((line) => {
if (line.includes(REPO_CARD_LINKS_FLAG)) {
return generateLinks(createRepoMdLink);
if (line.includes(LINKS_FLAG_MAP.repo)) {
return generateLinks("repo");
}
if (line.includes(STAT_CARD_LINKS_FLAG)) {
return generateLinks(createStatMdLink);
if (line.includes(LINKS_FLAG_MAP.stats)) {
return generateLinks("stats");
}
if (line.includes(REPO_CARD_TABLE_FLAG)) {
if (line.includes(TABLE_FLAG_MAP.repo)) {
return generateTable({ isRepoCard: true });
}
if (line.includes(STAT_CARD_TABLE_FLAG)) {
if (line.includes(TABLE_FLAG_MAP.stats)) {
return generateTable({ isRepoCard: false });
}
return line;
@@ -116,3 +120,5 @@ const buildReadme = () => {
};
fs.writeFileSync(TARGET_FILE, buildReadme());
console.log("README.md updated successfully!");