From 3a9ded0a090ba4e459ec06a9ce06c385df527834 Mon Sep 17 00:00:00 2001 From: Mario Lubenka Date: Fri, 24 Jul 2020 15:26:26 +0200 Subject: [PATCH] feat(repocard): parse emojis in description (#67) * feat: add remove_emojis option Removes emojis in repo card description * feat: parse emojis * feat: rename remove_emojis to show_emojis option for consistency * docs: update docs Co-authored-by: anuraghazra --- api/pin.js | 2 +- package.json | 3 ++- src/renderRepoCard.js | 9 ++++++++- tests/renderRepoCard.test.js | 12 ++++++++++++ 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/api/pin.js b/api/pin.js index da7e9a71..b0172049 100644 --- a/api/pin.js +++ b/api/pin.js @@ -53,7 +53,7 @@ module.exports = async (req, res) => { } res.setHeader("Cache-Control", `public, max-age=${cacheSeconds}`); - + res.send( renderRepoCard(repoData, { title_color, diff --git a/package.json b/package.json index 7c113bb0..574ee681 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,8 @@ "jest": "^26.1.0" }, "dependencies": { - "dotenv": "^8.2.0" + "dotenv": "^8.2.0", + "emoji-name-map": "^1.2.8" }, "husky": { "hooks": { diff --git a/src/renderRepoCard.js b/src/renderRepoCard.js index 9caf807d..a0cf6e2e 100644 --- a/src/renderRepoCard.js +++ b/src/renderRepoCard.js @@ -5,6 +5,7 @@ const { FlexLayout, } = require("../src/utils"); const icons = require("./icons"); +const toEmoji = require("emoji-name-map"); const renderRepoCard = (repo, options = {}) => { const { @@ -34,8 +35,14 @@ const renderRepoCard = (repo, options = {}) => { const shiftText = langName.length > 15 ? 0 : 30; let desc = description || "No description provided"; + + // parse emojis to unicode + desc = desc.replace(/:\w+:/gm, (emoji) => { + return toEmoji.get(emoji) || ""; + }); + if (desc.length > 55) { - desc = `${description.slice(0, 55)}..`; + desc = `${desc.slice(0, 55)}..`; } // returns theme based colors with proper overrides and defaults diff --git a/tests/renderRepoCard.test.js b/tests/renderRepoCard.test.js index 67309406..9aea695e 100644 --- a/tests/renderRepoCard.test.js +++ b/tests/renderRepoCard.test.js @@ -73,6 +73,18 @@ describe("Test renderRepoCard", () => { ); }); + it("should render emojis", () => { + document.body.innerHTML = renderRepoCard({ + ...data_repo.repository, + description: "This is a text with a :poop: poo emoji", + }); + + // poop emoji may not show in all editors but it's there between "a" and "poo" + expect(document.getElementsByClassName("description")[0]).toHaveTextContent( + "This is a text with a 💩 poo emoji" + ); + }); + it("should shift the text position depending on language length", () => { document.body.innerHTML = renderRepoCard({ ...data_repo.repository,