mirror of
https://github.com/anuraghazra/github-readme-stats.git
synced 2025-03-07 15:08:07 +08:00
Merge pull request #6 from anuraghazra/refactor
refactor: refactored code & added line_height option
This commit is contained in:
commit
7f5a598d15
76
api/index.js
76
api/index.js
@ -66,6 +66,16 @@ async function fetchStats(username) {
|
|||||||
return stats;
|
return stats;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const createTextNode = (icon, label, value, lheight) => {
|
||||||
|
return `
|
||||||
|
<tspan x="25" dy="${lheight}" class="stat bold">
|
||||||
|
<tspan class="icon ${
|
||||||
|
icon === "★" && "star-icon"
|
||||||
|
}" fill="#4C71F2">${icon}</tspan> ${label}:</tspan>
|
||||||
|
<tspan x="155" dy="0" class="stat">${value}</tspan>
|
||||||
|
`;
|
||||||
|
};
|
||||||
|
|
||||||
const renderSVG = (stats, options) => {
|
const renderSVG = (stats, options) => {
|
||||||
const {
|
const {
|
||||||
name,
|
name,
|
||||||
@ -75,51 +85,34 @@ const renderSVG = (stats, options) => {
|
|||||||
totalPRs,
|
totalPRs,
|
||||||
contributedTo,
|
contributedTo,
|
||||||
} = stats;
|
} = stats;
|
||||||
const { hide, show_icons, hide_border } = options || {};
|
const { hide, show_icons, hide_border, line_height } = options || {};
|
||||||
|
|
||||||
|
const lheight = line_height || 25;
|
||||||
|
|
||||||
const STAT_MAP = {
|
const STAT_MAP = {
|
||||||
stars: `
|
stars: createTextNode("★", "Total Stars", totalStars, lheight),
|
||||||
<tspan x="25" dy="25" class="stat bold">
|
commits: createTextNode("🕗", "Total Commits", totalCommits, lheight),
|
||||||
<tspan class="icon star-icon" fill="#4C71F2">★</tspan> Total Stars:</tspan>
|
prs: createTextNode("🔀", "Total PRs", totalPRs, lheight),
|
||||||
<tspan x="155" dy="0" class="stat">${totalStars}</tspan>
|
issues: createTextNode("ⓘ", "Total Issues", totalIssues, lheight),
|
||||||
`,
|
contribs: createTextNode("📕", "Contributed to", contributedTo, lheight),
|
||||||
commits: `
|
|
||||||
<tspan x="25" dy="25" class="stat bold">
|
|
||||||
<tspan class="icon" fill="#4C71F2">🕗</tspan> Total Commits:</tspan>
|
|
||||||
<tspan x="155" dy="0" class="stat">${totalCommits}</tspan>
|
|
||||||
`,
|
|
||||||
prs: `
|
|
||||||
<tspan x="25" dy="25" class="stat bold">
|
|
||||||
<tspan class="icon" fill="#4C71F2">🔀</tspan> Total PRs:</tspan>
|
|
||||||
<tspan x="155" dy="0" class="stat">${totalPRs}</tspan>
|
|
||||||
`,
|
|
||||||
issues: `
|
|
||||||
<tspan x="25" dy="25" class="stat bold">
|
|
||||||
<tspan class="icon" fill="#4C71F2">ⓘ</tspan> Total Issues:</tspan>
|
|
||||||
<tspan x="155" dy="0" class="stat">${totalIssues}</tspan>
|
|
||||||
`,
|
|
||||||
contribs: `
|
|
||||||
<tspan x="25" dy="25" class="stat bold"><tspan class="icon" fill="#4C71F2">📕</tspan> Contributed to:</tspan>
|
|
||||||
<tspan x="155" dy="0" class="stat">${contributedTo} repos</tspan>
|
|
||||||
`,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const statItems = Object.keys(STAT_MAP)
|
const statItems = Object.keys(STAT_MAP)
|
||||||
.filter((key) => !hide.includes(key))
|
.filter((key) => !hide.includes(key))
|
||||||
.map((key) => STAT_MAP[key]);
|
.map((key) => STAT_MAP[key]);
|
||||||
|
|
||||||
const height = 45 + (statItems.length + 1) * 25;
|
const height = 45 + (statItems.length + 1) * lheight;
|
||||||
|
|
||||||
return `
|
return `
|
||||||
<svg width="495" height="${height}" viewBox="0 0 495 ${height}" fill="none" xmlns="http://www.w3.org/2000/svg">
|
<svg width="495" height="${height}" viewBox="0 0 495 ${height}" fill="none" xmlns="http://www.w3.org/2000/svg">
|
||||||
<style>
|
<style>
|
||||||
.header { font: 600 18px 'Segoe UI', Ubuntu, Sans-Serif; fill: #2F80ED }
|
.header { font: 600 18px 'Segoe UI', Ubuntu, Sans-Serif; fill: #2F80ED }
|
||||||
.stat { font: 600 14px 'Segoe UI', Ubuntu, Sans-Serif; fill: #333 }
|
.stat { font: 600 14px 'Segoe UI', Ubuntu, Sans-Serif; fill: #333 }
|
||||||
.star-icon { font: 600 17px 'Segoe UI', Ubuntu, Sans-Serif; }
|
.star-icon { font: 600 18px 'Segoe UI', Ubuntu, Sans-Serif; }
|
||||||
.bold { font-weight: 700 }
|
.bold { font-weight: 700 }
|
||||||
.icon {
|
.icon {
|
||||||
display: none;
|
display: none;
|
||||||
${show_icons && "display: block"}
|
${!!show_icons && "display: block"}
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
${
|
${
|
||||||
@ -140,28 +133,17 @@ module.exports = async (req, res) => {
|
|||||||
const hide = req.query.hide;
|
const hide = req.query.hide;
|
||||||
const hide_border = req.query.hide_border;
|
const hide_border = req.query.hide_border;
|
||||||
const show_icons = req.query.show_icons;
|
const show_icons = req.query.show_icons;
|
||||||
|
const line_height = req.query.line_height;
|
||||||
|
|
||||||
let {
|
const stats = await fetchStats(username);
|
||||||
name,
|
|
||||||
totalPRs,
|
|
||||||
totalCommits,
|
|
||||||
totalStars,
|
|
||||||
totalIssues,
|
|
||||||
contributedTo,
|
|
||||||
} = await fetchStats(username);
|
|
||||||
|
|
||||||
res.setHeader("Content-Type", "image/svg+xml");
|
res.setHeader("Content-Type", "image/svg+xml");
|
||||||
res.send(
|
res.send(
|
||||||
renderSVG(
|
renderSVG(stats, {
|
||||||
{
|
hide: JSON.parse(hide || "[]"),
|
||||||
name,
|
show_icons,
|
||||||
totalStars,
|
hide_border,
|
||||||
totalCommits,
|
line_height,
|
||||||
totalIssues,
|
})
|
||||||
totalPRs,
|
|
||||||
contributedTo,
|
|
||||||
},
|
|
||||||
{ hide: JSON.parse(hide || "[]"), show_icons, hide_border }
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@ -33,6 +33,7 @@ To enable icons you can pass `show_icons=true` in the query param like so
|
|||||||
Other options:
|
Other options:
|
||||||
|
|
||||||
- `&hide_border=true` hide the border box if you don't like it :D.
|
- `&hide_border=true` hide the border box if you don't like it :D.
|
||||||
|
- `&line_height=30` control the line-height between text.
|
||||||
|
|
||||||
## Demo
|
## Demo
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user