Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update generate-theme-doc.js #3308

Merged
merged 2 commits into from
Oct 13, 2023
Merged
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
90 changes: 48 additions & 42 deletions scripts/generate-theme-doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ 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

<!-- 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 :-
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)
Expand All @@ -26,44 +29,44 @@ Use \`?theme=THEME_NAME\` parameter like so :-

| | | |
| :--: | :--: | :--: |
${STAT_CARD_TABLE_FLAG}
${TABLE_FLAG_MAP.stats}

## Repo Card

> These themes work both for the Stats Card and Repo Card.

| | | |
| :--: | :--: | :--: |
${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 }) => {
Expand All @@ -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];

let tableItem1 = createTableItem({ link: one, label: one, isRepoCard });
let tableItem2 = createTableItem({ link: two, label: two, isRepoCard });
let tableItem3 = createTableItem({ link: three, label: three, isRepoCard });

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] | | |`);
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} |`);
}
}

Expand All @@ -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;
Expand All @@ -116,3 +120,5 @@ const buildReadme = () => {
};

fs.writeFileSync(TARGET_FILE, buildReadme());

console.log("README.md updated successfully!");