From 9af56df2c122e76f38994f572ac114bbbeaafbd8 Mon Sep 17 00:00:00 2001 From: anuraghazra Date: Sun, 12 Jul 2020 12:46:08 +0530 Subject: [PATCH] feat: added bg_color options, added RepoCard color options too --- api/index.js | 2 ++ api/pin.js | 18 +++++++++++-- src/renderRepoCard.js | 32 ++++++++++++++--------- src/renderStatsCard.js | 5 +++- tests/renderRepoCard.test.js | 48 +++++++++++++++++++++++++++++++++++ tests/renderStatsCard.test.js | 9 +++++++ 6 files changed, 99 insertions(+), 15 deletions(-) diff --git a/api/index.js b/api/index.js index 46253793..e6684f2c 100644 --- a/api/index.js +++ b/api/index.js @@ -13,6 +13,7 @@ module.exports = async (req, res) => { title_color, icon_color, text_color, + bg_color, } = req.query; let stats; @@ -32,6 +33,7 @@ module.exports = async (req, res) => { title_color, icon_color, text_color, + bg_color, }) ); }; diff --git a/api/pin.js b/api/pin.js index c509fe29..2d69c0e7 100644 --- a/api/pin.js +++ b/api/pin.js @@ -4,7 +4,14 @@ const fetchRepo = require("../src/fetchRepo"); const renderRepoCard = require("../src/renderRepoCard"); module.exports = async (req, res) => { - const { username, repo } = req.query; + const { + username, + repo, + title_color, + icon_color, + text_color, + bg_color, + } = req.query; let repoData; res.setHeader("Content-Type", "image/svg+xml"); @@ -16,5 +23,12 @@ module.exports = async (req, res) => { return res.send(renderError(err.message)); } - res.send(renderRepoCard(repoData)); + res.send( + renderRepoCard(repoData, { + title_color, + icon_color, + text_color, + bg_color, + }) + ); }; diff --git a/src/renderRepoCard.js b/src/renderRepoCard.js index 97412c35..a2288668 100644 --- a/src/renderRepoCard.js +++ b/src/renderRepoCard.js @@ -1,7 +1,9 @@ -const { kFormatter, encodeHTML } = require("../src/utils"); +const { kFormatter, encodeHTML, isValidHexColor } = require("../src/utils"); -const renderRepoCard = (repo) => { +const renderRepoCard = (repo, options = {}) => { const { name, description, primaryLanguage, stargazers, forkCount } = repo; + const { title_color, icon_color, text_color, bg_color } = options; + const height = 120; const shiftText = primaryLanguage.name.length > 15 ? 0 : 30; @@ -10,20 +12,26 @@ const renderRepoCard = (repo) => { desc = `${description.slice(0, 55)}..`; } + const titleColor = + (isValidHexColor(title_color) && `#${title_color}`) || "#2f80ed"; + const iconColor = + (isValidHexColor(icon_color) && `#${icon_color}`) || "#586069"; + const bgColor = + (isValidHexColor(bg_color) && `#${bg_color}`) || "rgba(255, 255, 255, 0)"; + const textColor = (isValidHexColor(text_color) && `#${text_color}`) || "#333"; + const totalStars = kFormatter(stargazers.totalCount); const totalForks = kFormatter(forkCount); return ` - - + + @@ -40,14 +48,14 @@ const renderRepoCard = (repo) => { - + ${totalStars} - + ${totalForks} diff --git a/src/renderStatsCard.js b/src/renderStatsCard.js index 07910140..40b03a6d 100644 --- a/src/renderStatsCard.js +++ b/src/renderStatsCard.js @@ -27,6 +27,7 @@ const renderStatsCard = (stats = {}, options = { hide: [] }) => { title_color, icon_color, text_color, + bg_color, } = options; const lheight = parseInt(line_height); @@ -36,6 +37,8 @@ const renderStatsCard = (stats = {}, options = { hide: [] }) => { const iconColor = (isValidHexColor(icon_color) && `#${icon_color}`) || "#4c71f2"; const textColor = (isValidHexColor(text_color) && `#${text_color}`) || "#333"; + const bgColor = + (isValidHexColor(bg_color) && `#${bg_color}`) || "rgba(255, 255, 255, 0)"; const STAT_MAP = { stars: createTextNode({ @@ -81,7 +84,7 @@ const renderStatsCard = (stats = {}, options = { hide: [] }) => { const height = 45 + (statItems.length + 1) * lheight; - const border = ``; + const border = ``; return `