Skip to content

Commit ddcc30d

Browse files
authored
feat(repo card): add description lines count query parameter (#3453)
* feature(repo card): add description lines count query parameter * dev * dev * docs * test
1 parent d4f1a5d commit ddcc30d

File tree

5 files changed

+52
-3
lines changed

5 files changed

+52
-3
lines changed

api/pin.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export default async (req, res) => {
2424
locale,
2525
border_radius,
2626
border_color,
27+
description_lines_count,
2728
} = req.query;
2829

2930
res.setHeader("Content-Type", "image/svg+xml");
@@ -96,6 +97,7 @@ export default async (req, res) => {
9697
border_color,
9798
show_owner: parseBoolean(show_owner),
9899
locale: locale ? locale.toLowerCase() : null,
100+
description_lines_count,
99101
}),
100102
);
101103
} catch (err) {

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ If we don't support your language, please consider contributing! You can find mo
386386
#### Repo Card Exclusive Options
387387

388388
* `show_owner` - Shows the repo's owner name *(boolean)*. Default: `false`.
389+
* `description_lines_count` - Manually set the number of lines for the description *(number)*. Specified value will be clamped between 1 and 3. If this parameter is not specified, the number of lines will be automatically adjusted according to the actual length of the description. Default: `undefined`.
389390

390391
#### Gist Card Exclusive Options
391392

src/cards/repo-card.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ import {
1212
wrapTextMultiline,
1313
iconWithLabel,
1414
createLanguageNode,
15+
clampValue,
1516
} from "../common/utils.js";
1617
import { repoCardLocales } from "../translations.js";
1718

1819
const ICON_SIZE = 16;
20+
const DESCRIPTION_LINE_WIDTH = 59;
21+
const DESCRIPTION_MAX_LINES = 3;
1922

2023
/**
2124
* Retrieves the repository description and wraps it to fit the card width.
@@ -73,22 +76,34 @@ const renderRepoCard = (repo, options = {}) => {
7376
border_radius,
7477
border_color,
7578
locale,
79+
description_lines_count,
7680
} = options;
7781

7882
const lineHeight = 10;
7983
const header = show_owner ? nameWithOwner : name;
8084
const langName = (primaryLanguage && primaryLanguage.name) || "Unspecified";
8185
const langColor = (primaryLanguage && primaryLanguage.color) || "#333";
86+
const descriptionMaxLines = description_lines_count
87+
? clampValue(description_lines_count, 1, DESCRIPTION_MAX_LINES)
88+
: DESCRIPTION_MAX_LINES;
8289

8390
const desc = parseEmojis(description || "No description provided");
84-
const multiLineDescription = wrapTextMultiline(desc);
85-
const descriptionLines = multiLineDescription.length;
91+
const multiLineDescription = wrapTextMultiline(
92+
desc,
93+
DESCRIPTION_LINE_WIDTH,
94+
descriptionMaxLines,
95+
);
96+
const descriptionLinesCount = description_lines_count
97+
? clampValue(description_lines_count, 1, DESCRIPTION_MAX_LINES)
98+
: multiLineDescription.length;
99+
86100
const descriptionSvg = multiLineDescription
87101
.map((line) => `<tspan dy="1.2em" x="25">${encodeHTML(line)}</tspan>`)
88102
.join("");
89103

90104
const height =
91-
(descriptionLines > 1 ? 120 : 110) + descriptionLines * lineHeight;
105+
(descriptionLinesCount > 1 ? 120 : 110) +
106+
descriptionLinesCount * lineHeight;
92107

93108
const i18n = new I18n({
94109
locale,

src/cards/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export type StatCardOptions = CommonOptions & {
3232

3333
export type RepoCardOptions = CommonOptions & {
3434
show_owner: boolean;
35+
description_lines_count: number;
3536
};
3637

3738
export type TopLangOptions = CommonOptions & {

tests/renderRepoCard.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,4 +339,34 @@ describe("Test renderRepoCard", () => {
339339
"No description provided",
340340
);
341341
});
342+
343+
it("should have correct height with specified `description_lines_count` parameter", () => {
344+
// Testing short description
345+
document.body.innerHTML = renderRepoCard(data_repo.repository, {
346+
description_lines_count: 1,
347+
});
348+
expect(document.querySelector("svg")).toHaveAttribute("height", "120");
349+
document.body.innerHTML = renderRepoCard(data_repo.repository, {
350+
description_lines_count: 3,
351+
});
352+
expect(document.querySelector("svg")).toHaveAttribute("height", "150");
353+
354+
// Testing long description
355+
const longDescription =
356+
"A tool that will make a lot of iPhone/iPad developers' life easier. It shares your app over-the-air in a WiFi network. Bonjour is used and no configuration is needed.";
357+
document.body.innerHTML = renderRepoCard(
358+
{ ...data_repo.repository, description: longDescription },
359+
{
360+
description_lines_count: 3,
361+
},
362+
);
363+
expect(document.querySelector("svg")).toHaveAttribute("height", "150");
364+
document.body.innerHTML = renderRepoCard(
365+
{ ...data_repo.repository, description: longDescription },
366+
{
367+
description_lines_count: 1,
368+
},
369+
);
370+
expect(document.querySelector("svg")).toHaveAttribute("height", "120");
371+
});
342372
});

0 commit comments

Comments
 (0)