From 02e714aeb441de64ab4df271491db145d69c4d8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Tyrkk=C3=B6?= Date: Mon, 18 Oct 2021 16:41:50 +0300 Subject: [PATCH] fix: word-wrap bug (#1378) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fixed word-wrap bug * ci(workflow): add 'npm' cache for actions/setup-node in .github/workflows (#1382) * Revert "ci(workflow): add 'npm' cache for actions/setup-node in .github/workflows (#1382)" This reverts commit 2723f00cb8bf069d3b9b4c8482df042482e9c8de. * chore: remove action cache * chore: minor change Co-authored-by: Markus Tyrkkö Co-authored-by: Oscar Dominguez Co-authored-by: Anurag --- src/cards/repo-card.js | 2 +- src/common/utils.js | 20 +++++++++++++++----- tests/renderRepoCard.test.js | 11 +++++++++++ tests/utils.test.js | 7 +++++++ 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/cards/repo-card.js b/src/cards/repo-card.js index 5e6a28d..5295b17 100644 --- a/src/cards/repo-card.js +++ b/src/cards/repo-card.js @@ -139,7 +139,7 @@ const renderRepoCard = (repo, options = {}) => { }).join(""); const card = new Card({ - defaultTitle: header, + defaultTitle: header.length > 35 ? `${header.slice(0, 35)}...` : header, titlePrefixIcon: icons.contribs, width: 400, height, diff --git a/src/common/utils.js b/src/common/utils.js index 79d6af0..92018a6 100644 --- a/src/common/utils.js +++ b/src/common/utils.js @@ -220,12 +220,22 @@ function getCardColors({ * @param {number} maxLines * @returns {string[]} */ -function wrapTextMultiline(text, width = 60, maxLines = 3) { - const wrapped = wrap(encodeHTML(text), { width }) - .split("\n") // Split wrapped lines to get an array of lines - .map((line) => line.trim()); // Remove leading and trailing whitespace of each line +function wrapTextMultiline(text, width = 59, maxLines = 3) { + const fullWidthComma = ","; + const encoded = encodeHTML(text); + const isChinese = encoded.includes(fullWidthComma); - const lines = wrapped.slice(0, maxLines); // Only consider maxLines lines + let wrapped = []; + + if (isChinese) { + wrapped = encoded.split(fullWidthComma); // Chinese full punctuation + } else { + wrapped = wrap(encoded, { + width, + }).split("\n"); // Split wrapped lines to get an array of lines + } + + const lines = wrapped.map((line) => line.trim()).slice(0, maxLines); // Only consider maxLines lines // Add "..." to the last line if the text exceeds maxLines if (wrapped.length > maxLines) { diff --git a/tests/renderRepoCard.test.js b/tests/renderRepoCard.test.js index 4b7060a..8f1d6ef 100644 --- a/tests/renderRepoCard.test.js +++ b/tests/renderRepoCard.test.js @@ -51,6 +51,17 @@ describe("Test renderRepoCard", () => { ); }); + it("should trim header", () => { + document.body.innerHTML = renderRepoCard({ + ...data_repo.repository, + name: "some-really-long-repo-name-for-test-purposes", + }); + + expect(document.getElementsByClassName("header")[0].textContent).toBe( + "some-really-long-repo-name-for-test...", + ); + }); + it("should trim description", () => { document.body.innerHTML = renderRepoCard({ ...data_repo.repository, diff --git a/tests/utils.test.js b/tests/utils.test.js index 15c4d97..4fdd6a8 100644 --- a/tests/utils.test.js +++ b/tests/utils.test.js @@ -117,4 +117,11 @@ describe("wrapTextMultiline", () => { ); expect(multiLineText).toEqual(["Hello", "world long..."]); }); + it("should wrap chinese by punctuation", () => { + let multiLineText = wrapTextMultiline( + "专门为刚开始刷题的同学准备的算法基地,没有最细只有更细,立志用动画将晦涩难懂的算法说的通俗易懂!", + ); + expect(multiLineText.length).toEqual(3); + expect(multiLineText[0].length).toEqual(18 * 8); // &#xxxxx; x 8 + }); });