github-readme-stats/scripts/generate-theme-doc.js
sahilpawar01 28b65928d2
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>
2023-10-13 13:36:06 +02:00

124 lines
3.3 KiB
JavaScript

import fs from "fs";
import { themes } from "../themes/index.js";
const TARGET_FILE = "./themes/README.md";
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
<!-- DO NOT EDIT THIS FILE DIRECTLY -->
With inbuilt themes, you can customize the look of the card without doing any manual customization.
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)
\`\`\`
## Stats
> These themes work both for the Stats Card and Repo Card.
| | | |
| :--: | :--: | :--: |
${TABLE_FLAG_MAP.stats}
## Repo Card
> These themes work both for the Stats Card and Repo Card.
| | | |
| :--: | :--: | :--: |
${TABLE_FLAG_MAP.repo}
${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 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 = (type) => {
return Object.keys(themes)
.map((name) => createMdLink(name, type))
.join("");
};
const createTableItem = ({ link, label }) => {
if (!link || !label) {
return "";
}
return `\`${label}\` ![${link}][${link}]`;
};
const generateTable = ({ isRepoCard }) => {
const rows = [];
const themesFiltered = Object.keys(themes).filter(
(name) => name !== (!isRepoCard ? "default_repocard" : "default"),
);
for (let i = 0; i < themesFiltered.length; i += 3) {
const [one, two, three] = themesFiltered.slice(i, i + 3);
const tableItem1 = createTableItem({ link: one, label: one });
const tableItem2 = createTableItem({ link: two, label: two });
const tableItem3 = createTableItem({ link: three, label: three });
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} |`);
}
}
return rows.join("\n");
};
const buildReadme = () => {
return THEME_TEMPLATE.split("\n")
.map((line) => {
if (line.includes(LINKS_FLAG_MAP.repo)) {
return generateLinks("repo");
}
if (line.includes(LINKS_FLAG_MAP.stats)) {
return generateLinks("stats");
}
if (line.includes(TABLE_FLAG_MAP.repo)) {
return generateTable({ isRepoCard: true });
}
if (line.includes(TABLE_FLAG_MAP.stats)) {
return generateTable({ isRepoCard: false });
}
return line;
})
.join("\n");
};
fs.writeFileSync(TARGET_FILE, buildReadme());
console.log("README.md updated successfully!");