Skip to content

Commit

Permalink
feat: receives text, title and icon colors from URL
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoVSouto committed Jul 11, 2020
1 parent c25319e commit 59b8bde
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
14 changes: 13 additions & 1 deletion api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@ const fetchStats = require("../src/fetchStats");
const renderStatsCard = require("../src/renderStatsCard");

module.exports = async (req, res) => {
const { username, hide, hide_border, show_icons, line_height } = req.query;
const {
username,
hide,
hide_border,
show_icons,
line_height,
title_color,
icon_color,
text_color,
} = req.query;
let stats;

res.setHeader("Content-Type", "image/svg+xml");
Expand All @@ -20,6 +29,9 @@ module.exports = async (req, res) => {
show_icons,
hide_border,
line_height,
title_color,
icon_color,
text_color,
})
);
};
18 changes: 14 additions & 4 deletions src/renderStatsCard.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const { kFormatter } = require("../src/utils");
const { kFormatter, isValidHexColor } = require("../src/utils");

const createTextNode = ({ icon, label, value, lineHeight, id }) => {
const classname = icon === "★" && "star-icon";
const kValue = kFormatter(value);
return `
<tspan x="25" dy="${lineHeight}" class="stat bold">
<tspan data-testid="icon" class="icon ${classname}" fill="#4C71F2">${icon}</tspan> ${label}:</tspan>
<tspan data-testid="icon" class="icon ${classname}">${icon}</tspan> ${label}:</tspan>
<tspan data-testid="${id}" x="155" dy="0" class="stat">${kValue}</tspan>
`;
};
Expand All @@ -24,10 +24,19 @@ const renderStatsCard = (stats = {}, options = { hide: [] }) => {
show_icons = false,
hide_border = false,
line_height = 25,
title_color,
icon_color,
text_color,
} = options;

const lheight = parseInt(line_height);

const titleColor =
(isValidHexColor(title_color) && `#${title_color}`) || "#2f80ed";
const iconColor =
(isValidHexColor(icon_color) && `#${icon_color}`) || "#4c71f2";
const textColor = (isValidHexColor(text_color) && `#${text_color}`) || "#333";

const STAT_MAP = {
stars: createTextNode({
icon: "★",
Expand Down Expand Up @@ -76,11 +85,12 @@ const renderStatsCard = (stats = {}, options = { hide: [] }) => {
return `
<svg width="495" height="${height}" viewBox="0 0 495 ${height}" fill="none" xmlns="http://www.w3.org/2000/svg">
<style>
.header { font: 600 18px 'Segoe UI', Ubuntu, Sans-Serif; fill: #2F80ED }
.stat { font: 600 14px 'Segoe UI', Ubuntu, Sans-Serif; fill: #333 }
.header { font: 600 18px 'Segoe UI', Ubuntu, Sans-Serif; fill: ${titleColor}; }
.stat { font: 600 14px 'Segoe UI', Ubuntu, Sans-Serif; fill: ${textColor}; }
.star-icon { font: 600 18px 'Segoe UI', Ubuntu, Sans-Serif; }
.bold { font-weight: 700 }
.icon {
fill: ${iconColor};
display: ${!!show_icons ? "block" : "none"};
}
</style>
Expand Down
8 changes: 7 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ function kFormatter(num) {
: Math.sign(num) * Math.abs(num);
}

module.exports = { renderError, kFormatter, encodeHTML };
function isValidHexColor(hexColor) {
return new RegExp(
/^([A-Fa-f0-9]{8}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{3}|[A-Fa-f0-9]{4})$/
).test(hexColor);
}

module.exports = { renderError, kFormatter, encodeHTML, isValidHexColor };

0 comments on commit 59b8bde

Please sign in to comment.