@stats-organization/github-readme-stats-core 2.1.3 → 2.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/api/gist.d.ts.map +1 -1
- package/build/api/gist.js +34 -1
- package/build/api/index.d.ts.map +1 -1
- package/build/api/index.js +18 -1
- package/build/api/pin.d.ts.map +1 -1
- package/build/api/pin.js +17 -1
- package/build/api/top-langs.d.ts.map +1 -1
- package/build/api/top-langs.js +36 -5
- package/build/api/wakatime.d.ts.map +1 -1
- package/build/api/wakatime.js +34 -1
- package/build/calculateRank.d.ts +12 -11
- package/build/calculateRank.d.ts.map +1 -1
- package/build/calculateRank.js +18 -18
- package/build/cards/gist.d.ts.map +1 -1
- package/build/cards/gist.js +0 -1
- package/build/cards/repo.d.ts.map +1 -1
- package/build/cards/repo.js +29 -21
- package/build/cards/stats.d.ts +4 -2
- package/build/cards/stats.d.ts.map +1 -1
- package/build/cards/stats.js +21 -9
- package/build/cards/top-languages.d.ts.map +1 -1
- package/build/cards/top-languages.js +39 -12
- package/build/cards/wakatime.d.ts.map +1 -1
- package/build/cards/wakatime.js +29 -9
- package/build/common/Card.d.ts +54 -60
- package/build/common/Card.d.ts.map +1 -1
- package/build/common/Card.js +88 -61
- package/build/common/I18n.d.ts +13 -14
- package/build/common/I18n.d.ts.map +1 -1
- package/build/common/I18n.js +13 -12
- package/build/common/color.d.ts +56 -32
- package/build/common/color.d.ts.map +1 -1
- package/build/common/color.js +87 -50
- package/build/common/config.d.ts +18 -2
- package/build/common/config.d.ts.map +1 -1
- package/build/common/config.js +17 -26
- package/build/common/constants.d.ts +2 -2
- package/build/common/constants.d.ts.map +1 -1
- package/build/common/constants.js +5 -3
- package/build/common/error.d.ts +36 -29
- package/build/common/error.d.ts.map +1 -1
- package/build/common/error.js +14 -10
- package/build/common/fmt.d.ts +14 -13
- package/build/common/fmt.d.ts.map +1 -1
- package/build/common/fmt.js +19 -15
- package/build/common/html.d.ts +2 -4
- package/build/common/html.d.ts.map +1 -1
- package/build/common/html.js +2 -6
- package/build/common/http.d.ts +13 -4
- package/build/common/http.d.ts.map +1 -1
- package/build/common/http.js +3 -4
- package/build/common/icons.d.ts +22 -21
- package/build/common/icons.d.ts.map +1 -1
- package/build/common/icons.js +4 -5
- package/build/common/languageColors.json +9 -1
- package/build/common/ops.d.ts +30 -29
- package/build/common/ops.d.ts.map +1 -1
- package/build/common/ops.js +32 -38
- package/build/common/render.d.ts +115 -111
- package/build/common/render.d.ts.map +1 -1
- package/build/common/render.js +138 -88
- package/build/common/retryer.d.ts +24 -13
- package/build/common/retryer.d.ts.map +1 -1
- package/build/common/retryer.js +22 -30
- package/build/fetchers/gist.d.ts +6 -21
- package/build/fetchers/gist.d.ts.map +1 -1
- package/build/fetchers/gist.js +30 -36
- package/build/fetchers/repo.d.ts.map +1 -1
- package/build/fetchers/repo.js +4 -7
- package/build/fetchers/stats.d.ts.map +1 -1
- package/build/fetchers/stats.js +3 -7
- package/build/fetchers/top-languages.d.ts.map +1 -1
- package/build/fetchers/top-languages.js +0 -1
- package/build/fetchers/types.d.ts +126 -0
- package/build/fetchers/types.d.ts.map +1 -0
- package/build/fetchers/types.js +1 -0
- package/build/fetchers/wakatime.d.ts.map +1 -1
- package/build/fetchers/wakatime.js +0 -1
- package/build/translations.d.ts +627 -24
- package/build/translations.d.ts.map +1 -1
- package/build/translations.js +7 -8
- package/package.json +4 -4
- package/src/_emoji-name-map.d.ts +10 -0
- package/src/api/gist.js +36 -2
- package/src/api/index.js +19 -2
- package/src/api/pin.js +18 -2
- package/src/api/top-langs.js +38 -6
- package/src/api/wakatime.js +36 -2
- package/src/{calculateRank.js → calculateRank.ts} +29 -19
- package/src/cards/gist.js +0 -2
- package/src/cards/repo.js +30 -22
- package/src/cards/stats.js +22 -10
- package/src/cards/top-languages.js +49 -13
- package/src/cards/wakatime.js +33 -10
- package/src/common/Card.ts +338 -0
- package/src/common/I18n.ts +49 -0
- package/src/common/color.ts +212 -0
- package/src/common/config.ts +83 -0
- package/src/common/constants.ts +10 -0
- package/src/common/{error.js → error.ts} +20 -14
- package/src/common/{fmt.js → fmt.ts} +27 -19
- package/src/common/{html.js → html.ts} +3 -8
- package/src/common/http.ts +31 -0
- package/src/common/{icons.js → icons.ts} +9 -7
- package/src/common/languageColors.json +9 -1
- package/src/common/{ops.js → ops.ts} +48 -52
- package/src/common/{render.js → render.ts} +208 -98
- package/src/common/retryer.ts +125 -0
- package/src/fetchers/gist.ts +136 -0
- package/src/fetchers/repo.js +4 -8
- package/src/fetchers/stats.js +3 -8
- package/src/fetchers/top-languages.js +0 -2
- package/src/fetchers/{types.d.ts → types.ts} +1 -1
- package/src/fetchers/wakatime.js +0 -2
- package/src/{translations.js → translations.ts} +14 -10
- package/src/common/Card.js +0 -275
- package/src/common/I18n.js +0 -42
- package/src/common/color.js +0 -145
- package/src/common/config.js +0 -78
- package/src/common/constants.js +0 -9
- package/src/common/http.js +0 -21
- package/src/common/retryer.js +0 -104
- package/src/fetchers/gist.js +0 -114
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
// @ts-check
|
|
2
|
-
|
|
3
1
|
import toEmoji from "emoji-name-map";
|
|
4
2
|
|
|
5
3
|
import { OWNER_AFFILIATIONS } from "./constants.js";
|
|
@@ -8,10 +6,10 @@ import { CustomError } from "./error.js";
|
|
|
8
6
|
/**
|
|
9
7
|
* Returns boolean if value is either "true" or "false" else the value as it is.
|
|
10
8
|
*
|
|
11
|
-
* @param
|
|
12
|
-
* @returns
|
|
9
|
+
* @param value The value to parse.
|
|
10
|
+
* @returns The parsed value.
|
|
13
11
|
*/
|
|
14
|
-
const parseBoolean = (value) => {
|
|
12
|
+
const parseBoolean = (value: string | boolean): boolean | undefined => {
|
|
15
13
|
if (typeof value === "boolean") {
|
|
16
14
|
return value;
|
|
17
15
|
}
|
|
@@ -29,10 +27,10 @@ const parseBoolean = (value) => {
|
|
|
29
27
|
/**
|
|
30
28
|
* Parse string to array of strings.
|
|
31
29
|
*
|
|
32
|
-
* @param
|
|
33
|
-
* @returns
|
|
30
|
+
* @param str The string to parse.
|
|
31
|
+
* @returns The array of strings.
|
|
34
32
|
*/
|
|
35
|
-
const parseArray = (str) => {
|
|
33
|
+
const parseArray = (str: string): Array<string> => {
|
|
36
34
|
if (!str) {
|
|
37
35
|
return [];
|
|
38
36
|
}
|
|
@@ -42,47 +40,44 @@ const parseArray = (str) => {
|
|
|
42
40
|
/**
|
|
43
41
|
* Clamp the given number between the given range.
|
|
44
42
|
*
|
|
45
|
-
* @param
|
|
46
|
-
* @param
|
|
47
|
-
* @param
|
|
48
|
-
* @returns
|
|
43
|
+
* @param number The number to clamp.
|
|
44
|
+
* @param min The minimum value.
|
|
45
|
+
* @param max The maximum value.
|
|
46
|
+
* @returns The clamped number.
|
|
49
47
|
*/
|
|
50
|
-
const clampValue = (
|
|
51
|
-
|
|
52
|
-
|
|
48
|
+
const clampValue = (
|
|
49
|
+
number: string | number,
|
|
50
|
+
min: number,
|
|
51
|
+
max: number,
|
|
52
|
+
): number => {
|
|
53
|
+
if (Number.isNaN(parseInt(String(number), 10))) {
|
|
53
54
|
return min;
|
|
54
55
|
}
|
|
55
|
-
return Math.max(min, Math.min(number, max));
|
|
56
|
+
return Math.max(min, Math.min(Number(number), max));
|
|
56
57
|
};
|
|
57
58
|
|
|
58
59
|
/**
|
|
59
60
|
* Lowercase and trim string.
|
|
60
61
|
*
|
|
61
|
-
* @param
|
|
62
|
-
* @returns
|
|
62
|
+
* @param name String to lowercase and trim.
|
|
63
|
+
* @returns Lowercased and trimmed string.
|
|
63
64
|
*/
|
|
64
|
-
const lowercaseTrim = (name) => name.toLowerCase().trim();
|
|
65
|
+
const lowercaseTrim = (name: string): string => name.toLowerCase().trim();
|
|
65
66
|
|
|
66
67
|
/**
|
|
67
68
|
* Split array of languages in two columns.
|
|
68
69
|
*
|
|
69
70
|
* @template T Language object.
|
|
70
|
-
* @param
|
|
71
|
-
* @param
|
|
72
|
-
* @returns
|
|
71
|
+
* @param arr Array of languages.
|
|
72
|
+
* @param perChunk Number of languages per column.
|
|
73
|
+
* @returns Array of languages split in two columns.
|
|
73
74
|
*/
|
|
74
|
-
const chunkArray = (arr
|
|
75
|
-
return arr.reduce((resultArray, item, index) => {
|
|
75
|
+
const chunkArray = <T>(arr: Array<T>, perChunk: number): Array<Array<T>> => {
|
|
76
|
+
return arr.reduce<Array<Array<T>>>((resultArray, item, index) => {
|
|
76
77
|
const chunkIndex = Math.floor(index / perChunk);
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
resultArray[chunkIndex] = []; // start a new chunk
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// @ts-ignore
|
|
84
|
-
resultArray[chunkIndex].push(item);
|
|
85
|
-
|
|
78
|
+
const chunk = resultArray[chunkIndex] ?? [];
|
|
79
|
+
chunk.push(item);
|
|
80
|
+
resultArray[chunkIndex] = chunk;
|
|
86
81
|
return resultArray;
|
|
87
82
|
}, []);
|
|
88
83
|
};
|
|
@@ -90,10 +85,10 @@ const chunkArray = (arr, perChunk) => {
|
|
|
90
85
|
/**
|
|
91
86
|
* Parse emoji from string.
|
|
92
87
|
*
|
|
93
|
-
* @param
|
|
94
|
-
* @returns
|
|
88
|
+
* @param str String to parse emoji from.
|
|
89
|
+
* @returns String with emoji parsed.
|
|
95
90
|
*/
|
|
96
|
-
const parseEmojis = (str) => {
|
|
91
|
+
const parseEmojis = (str: string): string => {
|
|
97
92
|
if (!str) {
|
|
98
93
|
throw new Error("[parseEmoji]: str argument not provided");
|
|
99
94
|
}
|
|
@@ -105,11 +100,11 @@ const parseEmojis = (str) => {
|
|
|
105
100
|
/**
|
|
106
101
|
* Get diff in minutes between two dates.
|
|
107
102
|
*
|
|
108
|
-
* @param
|
|
109
|
-
* @param
|
|
110
|
-
* @returns
|
|
103
|
+
* @param d1 First date.
|
|
104
|
+
* @param d2 Second date.
|
|
105
|
+
* @returns Number of minutes between the two dates.
|
|
111
106
|
*/
|
|
112
|
-
const dateDiff = (d1, d2) => {
|
|
107
|
+
const dateDiff = (d1: Date, d2: Date): number => {
|
|
113
108
|
const date1 = new Date(d1);
|
|
114
109
|
const date2 = new Date(d2);
|
|
115
110
|
const diff = date1.getTime() - date2.getTime();
|
|
@@ -119,40 +114,41 @@ const dateDiff = (d1, d2) => {
|
|
|
119
114
|
/**
|
|
120
115
|
* Parse owner affiliations.
|
|
121
116
|
*
|
|
122
|
-
* @param
|
|
123
|
-
* @returns
|
|
117
|
+
* @param affiliations input affiliations to be parsed.
|
|
118
|
+
* @returns Parsed affiliations.
|
|
124
119
|
*
|
|
125
120
|
* @throws {CustomError} If affiliations contains invalid values.
|
|
126
121
|
*/
|
|
127
|
-
const parseOwnerAffiliations = (affiliations) => {
|
|
122
|
+
const parseOwnerAffiliations = (affiliations: Array<string>): Array<string> => {
|
|
128
123
|
// Set default value for ownerAffiliations.
|
|
129
124
|
// NOTE: Done here since parseArray() will always return an empty array even nothing
|
|
130
125
|
//was specified.
|
|
131
|
-
|
|
132
|
-
affiliations
|
|
126
|
+
const normalized =
|
|
127
|
+
affiliations.length > 0
|
|
133
128
|
? affiliations.map((affiliation) => affiliation.toUpperCase())
|
|
134
129
|
: ["OWNER"];
|
|
135
130
|
|
|
136
131
|
// Check if ownerAffiliations contains valid values.
|
|
137
132
|
if (
|
|
138
|
-
|
|
139
|
-
(affiliation) => !OWNER_AFFILIATIONS.includes(affiliation),
|
|
140
|
-
)
|
|
133
|
+
normalized.some((affiliation) => !OWNER_AFFILIATIONS.includes(affiliation))
|
|
141
134
|
) {
|
|
142
135
|
throw new CustomError(
|
|
143
136
|
"Invalid query parameter",
|
|
144
137
|
CustomError.INVALID_AFFILIATION,
|
|
145
138
|
);
|
|
146
139
|
}
|
|
147
|
-
return
|
|
140
|
+
return normalized;
|
|
148
141
|
};
|
|
149
142
|
|
|
150
|
-
const buildSearchFilter = (
|
|
151
|
-
|
|
143
|
+
const buildSearchFilter = (
|
|
144
|
+
repos: Array<string> | string = [],
|
|
145
|
+
owners: Array<string> | string = [],
|
|
146
|
+
): string => {
|
|
147
|
+
const repoFilter =
|
|
152
148
|
Array.isArray(repos) && repos.length > 0
|
|
153
149
|
? repos.map((r) => `repo:${r} `).join("")
|
|
154
150
|
: "";
|
|
155
|
-
|
|
151
|
+
const orgFilter =
|
|
156
152
|
Array.isArray(owners) && owners.length > 0
|
|
157
153
|
? owners.map((o) => `owner:${o} `).join("")
|
|
158
154
|
: "";
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { getCardColors } from "./color.js";
|
|
1
|
+
import { getCardColors, isPrefixedHexColor } from "./color.js";
|
|
4
2
|
import { SECONDARY_ERROR_MESSAGES, TRY_AGAIN_LATER } from "./error.js";
|
|
5
3
|
import { encodeHTML } from "./html.js";
|
|
6
4
|
import { clampValue } from "./ops.js";
|
|
@@ -9,14 +7,30 @@ import { clampValue } from "./ops.js";
|
|
|
9
7
|
* Auto layout utility, allows us to layout things vertically or horizontally with
|
|
10
8
|
* proper gaping.
|
|
11
9
|
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* @param
|
|
15
|
-
* @param
|
|
16
|
-
* @param
|
|
17
|
-
* @
|
|
10
|
+
* The caller must ensure that the passed `items` are properly sanitized!
|
|
11
|
+
*
|
|
12
|
+
* @param props Function properties.
|
|
13
|
+
* @param props.items Array of sanitized items to layout.
|
|
14
|
+
* @param props.gap Gap between items.
|
|
15
|
+
* @param props.direction Direction to layout items.
|
|
16
|
+
* @param props.sizes Array of sizes for each item.
|
|
17
|
+
* @returns Array of items with proper layout.
|
|
18
18
|
*/
|
|
19
|
-
const flexLayout = ({
|
|
19
|
+
const flexLayout = ({
|
|
20
|
+
items,
|
|
21
|
+
gap,
|
|
22
|
+
direction,
|
|
23
|
+
sizes = [],
|
|
24
|
+
}: {
|
|
25
|
+
items: Array<string>;
|
|
26
|
+
gap: number;
|
|
27
|
+
direction?: "column" | "row";
|
|
28
|
+
sizes?: Array<number>;
|
|
29
|
+
}): Array<string> => {
|
|
30
|
+
if (sizes.some((size) => !Number.isFinite(size))) {
|
|
31
|
+
throw new Error("flexLayout: `sizes` must contain only numbers");
|
|
32
|
+
}
|
|
33
|
+
|
|
20
34
|
let lastSize = 0;
|
|
21
35
|
// filter() for filtering out empty strings
|
|
22
36
|
return items.filter(Boolean).map((item, i) => {
|
|
@@ -33,15 +47,19 @@ const flexLayout = ({ items, gap, direction, sizes = [] }) => {
|
|
|
33
47
|
/**
|
|
34
48
|
* Creates a node to display the primary programming language of the repository/gist.
|
|
35
49
|
*
|
|
36
|
-
* @param
|
|
37
|
-
* @param
|
|
38
|
-
* @returns
|
|
50
|
+
* @param langName Language name.
|
|
51
|
+
* @param langColor Language color.
|
|
52
|
+
* @returns Language display SVG object.
|
|
39
53
|
*/
|
|
40
|
-
const createLanguageNode = (langName, langColor) => {
|
|
54
|
+
const createLanguageNode = (langName: string, langColor: string): string => {
|
|
55
|
+
if (!isPrefixedHexColor(langColor)) {
|
|
56
|
+
throw new Error(`Invalid language color: "${langColor}"`);
|
|
57
|
+
}
|
|
58
|
+
|
|
41
59
|
return `
|
|
42
60
|
<g data-testid="primary-lang">
|
|
43
61
|
<circle data-testid="lang-color" cx="0" cy="-5" r="6" fill="${langColor}" />
|
|
44
|
-
<text data-testid="lang-name" class="gray" x="15">${langName}</text>
|
|
62
|
+
<text data-testid="lang-name" class="gray" x="15">${encodeHTML(langName)}</text>
|
|
45
63
|
</g>
|
|
46
64
|
`;
|
|
47
65
|
};
|
|
@@ -49,15 +67,15 @@ const createLanguageNode = (langName, langColor) => {
|
|
|
49
67
|
/**
|
|
50
68
|
* Create a node to indicate progress in percentage along a horizontal line.
|
|
51
69
|
*
|
|
52
|
-
* @param
|
|
53
|
-
* @param
|
|
54
|
-
* @param
|
|
55
|
-
* @param
|
|
56
|
-
* @param
|
|
57
|
-
* @param
|
|
58
|
-
* @param
|
|
59
|
-
* @param
|
|
60
|
-
* @returns
|
|
70
|
+
* @param params Object that contains the createProgressNode parameters.
|
|
71
|
+
* @param params.x X-axis position.
|
|
72
|
+
* @param params.y Y-axis position.
|
|
73
|
+
* @param params.width Width of progress bar.
|
|
74
|
+
* @param params.color Progress color.
|
|
75
|
+
* @param params.progress Progress value.
|
|
76
|
+
* @param params.progressBarBackgroundColor Progress bar bg color.
|
|
77
|
+
* @param params.delay Delay before animation starts.
|
|
78
|
+
* @returns Progress node.
|
|
61
79
|
*/
|
|
62
80
|
const createProgressNode = ({
|
|
63
81
|
x,
|
|
@@ -67,7 +85,36 @@ const createProgressNode = ({
|
|
|
67
85
|
progress,
|
|
68
86
|
progressBarBackgroundColor,
|
|
69
87
|
delay,
|
|
70
|
-
}
|
|
88
|
+
}: {
|
|
89
|
+
x: number;
|
|
90
|
+
y: number;
|
|
91
|
+
width: number;
|
|
92
|
+
color: string;
|
|
93
|
+
progress: number;
|
|
94
|
+
progressBarBackgroundColor: string;
|
|
95
|
+
delay: number;
|
|
96
|
+
}): string => {
|
|
97
|
+
if (!isPrefixedHexColor(color)) {
|
|
98
|
+
throw new Error(`Invalid progress color: "${color}"`);
|
|
99
|
+
}
|
|
100
|
+
if (!isPrefixedHexColor(progressBarBackgroundColor)) {
|
|
101
|
+
throw new Error(
|
|
102
|
+
`Invalid progress bar background color: "${progressBarBackgroundColor}"`,
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
if (!Number.isFinite(width)) {
|
|
106
|
+
throw new Error(`Invalid width: "${width}"`);
|
|
107
|
+
}
|
|
108
|
+
if (!Number.isFinite(x)) {
|
|
109
|
+
throw new Error(`Invalid x: "${x}"`);
|
|
110
|
+
}
|
|
111
|
+
if (!Number.isFinite(y)) {
|
|
112
|
+
throw new Error(`Invalid y: "${y}"`);
|
|
113
|
+
}
|
|
114
|
+
if (!Number.isFinite(delay)) {
|
|
115
|
+
throw new Error(`Invalid delay: "${delay}"`);
|
|
116
|
+
}
|
|
117
|
+
|
|
71
118
|
const progressPercentage = clampValue(progress, 2, 100);
|
|
72
119
|
|
|
73
120
|
return `
|
|
@@ -91,16 +138,16 @@ const createProgressNode = ({
|
|
|
91
138
|
* native, font-aware wrapping. Content overflowing `lineCount` lines is
|
|
92
139
|
* clipped (with an ellipsis on the last visible line) by CSS line-clamp.
|
|
93
140
|
*
|
|
94
|
-
* @param
|
|
95
|
-
* @param
|
|
96
|
-
* @param
|
|
97
|
-
* @param
|
|
98
|
-
* @param
|
|
99
|
-
* @param
|
|
100
|
-
* @param
|
|
101
|
-
* @param
|
|
102
|
-
* @param
|
|
103
|
-
* @returns
|
|
141
|
+
* @param props Function properties.
|
|
142
|
+
* @param props.text Text to render (will be HTML-encoded).
|
|
143
|
+
* @param props.x X position of the foreignObject.
|
|
144
|
+
* @param props.y Y position of the foreignObject.
|
|
145
|
+
* @param props.width Width of the wrap box.
|
|
146
|
+
* @param props.height Height of the wrap box.
|
|
147
|
+
* @param props.lineCount Maximum number of lines to display.
|
|
148
|
+
* @param props.className CSS class applied to the inner element.
|
|
149
|
+
* @param props.testId Optional test id for the inner element.
|
|
150
|
+
* @returns foreignObject SVG node.
|
|
104
151
|
*/
|
|
105
152
|
const wrappedTextNode = ({
|
|
106
153
|
text,
|
|
@@ -111,11 +158,36 @@ const wrappedTextNode = ({
|
|
|
111
158
|
lineCount,
|
|
112
159
|
className,
|
|
113
160
|
testId,
|
|
114
|
-
}
|
|
115
|
-
|
|
161
|
+
}: {
|
|
162
|
+
text: string;
|
|
163
|
+
x: number;
|
|
164
|
+
y: number;
|
|
165
|
+
width: number;
|
|
166
|
+
height: number;
|
|
167
|
+
lineCount: number;
|
|
168
|
+
className: string;
|
|
169
|
+
testId?: string;
|
|
170
|
+
}): string => {
|
|
171
|
+
if (!Number.isFinite(x)) {
|
|
172
|
+
throw new Error(`Invalid x: "${x}"`);
|
|
173
|
+
}
|
|
174
|
+
if (!Number.isFinite(y)) {
|
|
175
|
+
throw new Error(`Invalid y: "${y}"`);
|
|
176
|
+
}
|
|
177
|
+
if (!Number.isFinite(width)) {
|
|
178
|
+
throw new Error(`Invalid width: "${width}"`);
|
|
179
|
+
}
|
|
180
|
+
if (!Number.isFinite(height)) {
|
|
181
|
+
throw new Error(`Invalid height: "${height}"`);
|
|
182
|
+
}
|
|
183
|
+
if (!Number.isFinite(lineCount)) {
|
|
184
|
+
throw new Error(`Invalid lineCount: "${lineCount}"`);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const testIdAttr = testId ? ` data-testid="${encodeHTML(testId)}"` : "";
|
|
116
188
|
return `
|
|
117
189
|
<foreignObject x="${x}" y="${y}" width="${width}" height="${height}">
|
|
118
|
-
<div xmlns="http://www.w3.org/1999/xhtml" class="${className}" style="--lines: ${lineCount};"${testIdAttr}>${encodeHTML(
|
|
190
|
+
<div xmlns="http://www.w3.org/1999/xhtml" class="${encodeHTML(className)}" style="--lines: ${lineCount};"${testIdAttr}>${encodeHTML(
|
|
119
191
|
text,
|
|
120
192
|
)}</div>
|
|
121
193
|
</foreignObject>
|
|
@@ -128,37 +200,55 @@ const wrappedTextNode = ({
|
|
|
128
200
|
* browser handles wrapping and the line count is taken from the `--lines`
|
|
129
201
|
* custom property set on the element.
|
|
130
202
|
*
|
|
131
|
-
* @param
|
|
132
|
-
* @returns
|
|
203
|
+
* @param color Text color (CSS `color` property).
|
|
204
|
+
* @returns CSS rules block (without the surrounding selector).
|
|
133
205
|
*/
|
|
134
|
-
const wrappedTextStyles = (color) =>
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
206
|
+
const wrappedTextStyles = (color: string): string => {
|
|
207
|
+
if (!isPrefixedHexColor(color)) {
|
|
208
|
+
throw new Error(`Invalid text color: "${color}"`);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return `
|
|
212
|
+
color: ${color};
|
|
213
|
+
margin: 0;
|
|
214
|
+
line-height: 1.2;
|
|
215
|
+
overflow-wrap: anywhere;
|
|
216
|
+
word-break: break-word;
|
|
217
|
+
display: -webkit-box;
|
|
218
|
+
-webkit-box-orient: vertical;
|
|
219
|
+
-webkit-line-clamp: var(--lines);
|
|
220
|
+
line-clamp: var(--lines);
|
|
221
|
+
overflow: hidden;
|
|
222
|
+
text-overflow: ellipsis;
|
|
223
|
+
padding-bottom: 0.15em;
|
|
224
|
+
`;
|
|
225
|
+
};
|
|
148
226
|
|
|
149
227
|
/**
|
|
150
228
|
* Creates an icon with label to display repository/gist stats like forks, stars, etc.
|
|
151
229
|
*
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
* @param
|
|
155
|
-
* @param
|
|
156
|
-
* @
|
|
230
|
+
* The caller must ensure that the passed `icon` is properly sanitized!
|
|
231
|
+
*
|
|
232
|
+
* @param icon The sanitized icon to display.
|
|
233
|
+
* @param label The label to display.
|
|
234
|
+
* @param testid The testid to assign to the label.
|
|
235
|
+
* @param iconSize The size of the icon.
|
|
236
|
+
* @returns Icon with label SVG object.
|
|
157
237
|
*/
|
|
158
|
-
const iconWithLabel = (
|
|
238
|
+
const iconWithLabel = (
|
|
239
|
+
icon: string,
|
|
240
|
+
label: number | string,
|
|
241
|
+
testid: string,
|
|
242
|
+
iconSize: number,
|
|
243
|
+
): string => {
|
|
159
244
|
if (typeof label === "number" && label <= 0) {
|
|
160
245
|
return "";
|
|
161
246
|
}
|
|
247
|
+
|
|
248
|
+
if (!Number.isFinite(iconSize)) {
|
|
249
|
+
throw new Error(`Invalid iconSize: "${iconSize}"`);
|
|
250
|
+
}
|
|
251
|
+
|
|
162
252
|
const iconSvg = `
|
|
163
253
|
<svg
|
|
164
254
|
class="icon"
|
|
@@ -171,7 +261,7 @@ const iconWithLabel = (icon, label, testid, iconSize) => {
|
|
|
171
261
|
${icon}
|
|
172
262
|
</svg>
|
|
173
263
|
`;
|
|
174
|
-
const text = `<text data-testid="${testid}" class="gray">${label}</text>`;
|
|
264
|
+
const text = `<text data-testid="${encodeHTML(testid)}" class="gray">${encodeHTML(String(label))}</text>`;
|
|
175
265
|
return flexLayout({ items: [iconSvg, text], gap: 20 }).join("");
|
|
176
266
|
};
|
|
177
267
|
|
|
@@ -186,23 +276,34 @@ const UPSTREAM_API_ERRORS = [
|
|
|
186
276
|
/**
|
|
187
277
|
* Renders error message on the card.
|
|
188
278
|
*
|
|
189
|
-
* @param
|
|
190
|
-
* @param
|
|
191
|
-
* @param
|
|
192
|
-
* @param
|
|
193
|
-
* @param
|
|
194
|
-
* @param
|
|
195
|
-
* @param
|
|
196
|
-
* @param
|
|
197
|
-
* @param
|
|
198
|
-
* @param
|
|
199
|
-
* @returns
|
|
279
|
+
* @param args Function arguments.
|
|
280
|
+
* @param args.message Main error message.
|
|
281
|
+
* @param args.secondaryMessage The secondary error message.
|
|
282
|
+
* @param args.renderOptions Render options.
|
|
283
|
+
* @param args.renderOptions.title_color Card title color.
|
|
284
|
+
* @param args.renderOptions.text_color Card text color.
|
|
285
|
+
* @param args.renderOptions.bg_color Card background color.
|
|
286
|
+
* @param args.renderOptions.border_color Card border color.
|
|
287
|
+
* @param args.renderOptions.theme Card theme.
|
|
288
|
+
* @param args.renderOptions.show_repo_link Whether to show repo link or not.
|
|
289
|
+
* @returns The SVG markup.
|
|
200
290
|
*/
|
|
201
291
|
const renderError = ({
|
|
202
292
|
message,
|
|
203
293
|
secondaryMessage = "",
|
|
204
294
|
renderOptions = {},
|
|
205
|
-
}
|
|
295
|
+
}: {
|
|
296
|
+
message: string;
|
|
297
|
+
secondaryMessage?: string;
|
|
298
|
+
renderOptions?: {
|
|
299
|
+
title_color?: string;
|
|
300
|
+
text_color?: string;
|
|
301
|
+
bg_color?: string;
|
|
302
|
+
border_color?: string;
|
|
303
|
+
theme?: string;
|
|
304
|
+
show_repo_link?: boolean;
|
|
305
|
+
};
|
|
306
|
+
}): string => {
|
|
206
307
|
const {
|
|
207
308
|
title_color,
|
|
208
309
|
text_color,
|
|
@@ -224,7 +325,7 @@ const renderError = ({
|
|
|
224
325
|
});
|
|
225
326
|
|
|
226
327
|
return `
|
|
227
|
-
<svg width="${ERROR_CARD_LENGTH}" height="120" viewBox="0 0 ${ERROR_CARD_LENGTH} 120" fill="${bgColor}" xmlns="http://www.w3.org/2000/svg">
|
|
328
|
+
<svg width="${ERROR_CARD_LENGTH}" height="120" viewBox="0 0 ${ERROR_CARD_LENGTH} 120" fill="${String(bgColor)}" xmlns="http://www.w3.org/2000/svg">
|
|
228
329
|
<style>
|
|
229
330
|
.text { font: 600 16px 'Segoe UI', Ubuntu, Sans-Serif; fill: ${titleColor} }
|
|
230
331
|
.small { font: 600 12px 'Segoe UI', Ubuntu, Sans-Serif; fill: ${textColor} }
|
|
@@ -232,7 +333,7 @@ const renderError = ({
|
|
|
232
333
|
</style>
|
|
233
334
|
<rect x="0.5" y="0.5" width="${
|
|
234
335
|
ERROR_CARD_LENGTH - 1
|
|
235
|
-
}" height="99%" rx="4.5" fill="${bgColor}" stroke="${borderColor}"/>
|
|
336
|
+
}" height="99%" rx="4.5" fill="${String(bgColor)}" stroke="${borderColor}"/>
|
|
236
337
|
<text x="25" y="45" class="text">Something went wrong!${
|
|
237
338
|
UPSTREAM_API_ERRORS.includes(secondaryMessage) || !show_repo_link
|
|
238
339
|
? ""
|
|
@@ -240,7 +341,7 @@ const renderError = ({
|
|
|
240
341
|
}</text>
|
|
241
342
|
<text data-testid="message" x="25" y="55" class="text small">
|
|
242
343
|
<tspan x="25" dy="18">${encodeHTML(message)}</tspan>
|
|
243
|
-
<tspan x="25" dy="18" class="gray">${secondaryMessage}</tspan>
|
|
344
|
+
<tspan x="25" dy="18" class="gray">${encodeHTML(secondaryMessage)}</tspan>
|
|
244
345
|
</text>
|
|
245
346
|
</svg>
|
|
246
347
|
`;
|
|
@@ -250,11 +351,11 @@ const renderError = ({
|
|
|
250
351
|
* Retrieve text length based on Segoe UI font.
|
|
251
352
|
*
|
|
252
353
|
* @see https://stackoverflow.com/a/48172630/10629172
|
|
253
|
-
* @param
|
|
254
|
-
* @param
|
|
255
|
-
* @returns
|
|
354
|
+
* @param str String to measure.
|
|
355
|
+
* @param fontSize Font size.
|
|
356
|
+
* @returns Text length.
|
|
256
357
|
*/
|
|
257
|
-
const measureText = (str, fontSize = 10) => {
|
|
358
|
+
const measureText = (str: string, fontSize = 10): number => {
|
|
258
359
|
// prettier-ignore
|
|
259
360
|
const widths = [
|
|
260
361
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
@@ -306,7 +407,7 @@ const measureText = (str, fontSize = 10) => {
|
|
|
306
407
|
return 1;
|
|
307
408
|
}
|
|
308
409
|
if (c.charCodeAt(0) < widths.length) {
|
|
309
|
-
return widths[c.charCodeAt(0)];
|
|
410
|
+
return widths[c.charCodeAt(0)] ?? avg;
|
|
310
411
|
} else {
|
|
311
412
|
return avg;
|
|
312
413
|
}
|
|
@@ -322,12 +423,16 @@ const measureText = (str, fontSize = 10) => {
|
|
|
322
423
|
* The browser still does the real wrap inside the foreignObject; this is only
|
|
323
424
|
* used to size the SVG.
|
|
324
425
|
*
|
|
325
|
-
* @param
|
|
326
|
-
* @param
|
|
327
|
-
* @param
|
|
328
|
-
* @returns
|
|
426
|
+
* @param text Text to split.
|
|
427
|
+
* @param fontSize Font size in px (matches `measureText`).
|
|
428
|
+
* @param maxWidth Available wrap width in px.
|
|
429
|
+
* @returns Estimated wrapped lines.
|
|
329
430
|
*/
|
|
330
|
-
const splitWrappedText = (
|
|
431
|
+
const splitWrappedText = (
|
|
432
|
+
text: string,
|
|
433
|
+
fontSize: number,
|
|
434
|
+
maxWidth: number,
|
|
435
|
+
): Array<string> => {
|
|
331
436
|
if (!text) {
|
|
332
437
|
return [];
|
|
333
438
|
}
|
|
@@ -342,15 +447,15 @@ const splitWrappedText = (text, fontSize, maxWidth) => {
|
|
|
342
447
|
// Korean Hangul (U+AC00–U+D7AF) is intentionally NOT in the CJK range
|
|
343
448
|
// because Korean wraps at word boundaries by default in HTML.
|
|
344
449
|
// ASCII whitespace is collapsed to a single space per CSS `white-space: normal;`
|
|
345
|
-
|
|
346
|
-
const tokens =
|
|
450
|
+
const normalizedText = text.replace(/[\t\n\r ]+/g, " ");
|
|
451
|
+
const tokens = normalizedText.match(
|
|
347
452
|
/\s|[\u3000-\u9FFF\uFF00-\uFFEF]|[^\s\u3000-\u9FFF\uFF00-\uFFEF]+/g,
|
|
348
453
|
);
|
|
349
454
|
if (!tokens) {
|
|
350
455
|
return [];
|
|
351
456
|
}
|
|
352
457
|
|
|
353
|
-
const takeFittingSegment = (token, availableWidth) => {
|
|
458
|
+
const takeFittingSegment = (token: string, availableWidth: number) => {
|
|
354
459
|
const characters = token.split("");
|
|
355
460
|
let segment = "";
|
|
356
461
|
let width = 0;
|
|
@@ -379,7 +484,7 @@ const splitWrappedText = (text, fontSize, maxWidth) => {
|
|
|
379
484
|
if (currentWidth === 0) {
|
|
380
485
|
continue;
|
|
381
486
|
}
|
|
382
|
-
lines[lines.length - 1]
|
|
487
|
+
lines[lines.length - 1] = (lines[lines.length - 1] ?? "") + token;
|
|
383
488
|
currentWidth += measureText(token, fontSize);
|
|
384
489
|
continue;
|
|
385
490
|
}
|
|
@@ -389,7 +494,7 @@ const splitWrappedText = (text, fontSize, maxWidth) => {
|
|
|
389
494
|
while (remaining) {
|
|
390
495
|
const w = measureText(remaining, fontSize);
|
|
391
496
|
if (currentWidth + w <= maxWidth) {
|
|
392
|
-
lines[lines.length - 1]
|
|
497
|
+
lines[lines.length - 1] = (lines[lines.length - 1] ?? "") + remaining;
|
|
393
498
|
currentWidth += w;
|
|
394
499
|
break;
|
|
395
500
|
}
|
|
@@ -402,7 +507,7 @@ const splitWrappedText = (text, fontSize, maxWidth) => {
|
|
|
402
507
|
|
|
403
508
|
// An atom wider than the box wraps mid-glyph (overflow-wrap: anywhere).
|
|
404
509
|
const { segment, width } = takeFittingSegment(remaining, maxWidth);
|
|
405
|
-
lines[lines.length - 1]
|
|
510
|
+
lines[lines.length - 1] = (lines[lines.length - 1] ?? "") + segment;
|
|
406
511
|
currentWidth = width;
|
|
407
512
|
remaining = remaining.slice(segment.length);
|
|
408
513
|
}
|
|
@@ -415,13 +520,18 @@ const splitWrappedText = (text, fontSize, maxWidth) => {
|
|
|
415
520
|
* Estimate how many lines a string will wrap to when laid out greedily at the
|
|
416
521
|
* given font size inside a box of width `maxWidth`, capped at `maxLines`.
|
|
417
522
|
*
|
|
418
|
-
* @param
|
|
419
|
-
* @param
|
|
420
|
-
* @param
|
|
421
|
-
* @param
|
|
422
|
-
* @returns
|
|
523
|
+
* @param text Text to estimate.
|
|
524
|
+
* @param fontSize Font size in px (matches `measureText`).
|
|
525
|
+
* @param maxWidth Available wrap width in px.
|
|
526
|
+
* @param maxLines Cap on the returned line count.
|
|
527
|
+
* @returns Estimated line count, at least 1, at most `maxLines`.
|
|
423
528
|
*/
|
|
424
|
-
const countWrappedLines = (
|
|
529
|
+
const countWrappedLines = (
|
|
530
|
+
text: string,
|
|
531
|
+
fontSize: number,
|
|
532
|
+
maxWidth: number,
|
|
533
|
+
maxLines: number,
|
|
534
|
+
): number => {
|
|
425
535
|
return Math.min(
|
|
426
536
|
Math.max(1, splitWrappedText(text, fontSize, maxWidth).length),
|
|
427
537
|
maxLines,
|