@stats-organization/github-readme-stats-core 2.1.4 → 2.2.0

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.
Files changed (182) hide show
  1. package/build/api/gist.d.ts +2 -7
  2. package/build/api/gist.d.ts.map +1 -1
  3. package/build/api/gist.js +28 -26
  4. package/build/api/index.d.ts +3 -8
  5. package/build/api/index.d.ts.map +1 -1
  6. package/build/api/index.js +32 -36
  7. package/build/api/pin.d.ts +2 -7
  8. package/build/api/pin.d.ts.map +1 -1
  9. package/build/api/pin.js +18 -33
  10. package/build/api/top-langs.d.ts +2 -7
  11. package/build/api/top-langs.d.ts.map +1 -1
  12. package/build/api/top-langs.js +32 -44
  13. package/build/api/wakatime.d.ts +2 -7
  14. package/build/api/wakatime.d.ts.map +1 -1
  15. package/build/api/wakatime.js +28 -26
  16. package/build/calculateRank.d.ts +12 -11
  17. package/build/calculateRank.d.ts.map +1 -1
  18. package/build/calculateRank.js +18 -18
  19. package/build/cards/common-options.d.ts +13 -0
  20. package/build/cards/common-options.d.ts.map +1 -0
  21. package/build/cards/common-options.js +1 -0
  22. package/build/cards/gist.d.ts +11 -16
  23. package/build/cards/gist.d.ts.map +1 -1
  24. package/build/cards/gist.js +26 -31
  25. package/build/cards/repo.d.ts +19 -16
  26. package/build/cards/repo.d.ts.map +1 -1
  27. package/build/cards/repo.js +79 -72
  28. package/build/cards/stats.d.ts +29 -44
  29. package/build/cards/stats.d.ts.map +1 -1
  30. package/build/cards/stats.js +118 -173
  31. package/build/cards/top-languages.d.ts +81 -77
  32. package/build/cards/top-languages.d.ts.map +1 -1
  33. package/build/cards/top-languages.js +186 -184
  34. package/build/cards/wakatime.d.ts +21 -11
  35. package/build/cards/wakatime.d.ts.map +1 -1
  36. package/build/cards/wakatime.js +92 -109
  37. package/build/common/Card.d.ts +41 -14
  38. package/build/common/Card.d.ts.map +1 -1
  39. package/build/common/Card.js +92 -31
  40. package/build/common/I18n.d.ts +4 -2
  41. package/build/common/I18n.d.ts.map +1 -1
  42. package/build/common/color.d.ts +91 -15
  43. package/build/common/color.d.ts.map +1 -1
  44. package/build/common/color.js +163 -17
  45. package/build/common/config.d.ts +2 -1
  46. package/build/common/config.d.ts.map +1 -1
  47. package/build/common/config.js +16 -26
  48. package/build/common/constants.d.ts +2 -1
  49. package/build/common/constants.d.ts.map +1 -1
  50. package/build/common/date.d.ts +37 -0
  51. package/build/common/date.d.ts.map +1 -0
  52. package/build/common/date.js +31 -0
  53. package/build/common/html.js +1 -1
  54. package/build/common/http.d.ts +37 -5
  55. package/build/common/http.d.ts.map +1 -1
  56. package/build/common/http.js +29 -6
  57. package/build/common/icons.d.ts +1 -0
  58. package/build/common/icons.d.ts.map +1 -1
  59. package/build/common/icons.js +1 -0
  60. package/build/common/languageColors.d.ts +11 -0
  61. package/build/common/languageColors.d.ts.map +1 -0
  62. package/build/common/languageColors.js +15 -0
  63. package/build/common/languageColors.json +10 -1
  64. package/build/common/ops.d.ts +27 -33
  65. package/build/common/ops.d.ts.map +1 -1
  66. package/build/common/ops.js +31 -48
  67. package/build/common/render.d.ts +154 -113
  68. package/build/common/render.d.ts.map +1 -1
  69. package/build/common/render.js +209 -92
  70. package/build/common/retryer.d.ts +15 -6
  71. package/build/common/retryer.d.ts.map +1 -1
  72. package/build/common/retryer.js +2 -0
  73. package/build/fetchers/gist.d.ts +6 -21
  74. package/build/fetchers/gist.d.ts.map +1 -1
  75. package/build/fetchers/gist.js +28 -64
  76. package/build/fetchers/repo.d.ts +12 -11
  77. package/build/fetchers/repo.d.ts.map +1 -1
  78. package/build/fetchers/repo.js +27 -68
  79. package/build/fetchers/stats.d.ts +24 -17
  80. package/build/fetchers/stats.d.ts.map +1 -1
  81. package/build/fetchers/stats.js +302 -184
  82. package/build/fetchers/top-languages.d.ts +10 -15
  83. package/build/fetchers/top-languages.d.ts.map +1 -1
  84. package/build/fetchers/top-languages.js +52 -103
  85. package/build/fetchers/types.d.ts +130 -0
  86. package/build/fetchers/types.d.ts.map +1 -0
  87. package/build/fetchers/types.js +1 -0
  88. package/build/fetchers/wakatime.d.ts +9 -5
  89. package/build/fetchers/wakatime.d.ts.map +1 -1
  90. package/build/fetchers/wakatime.js +8 -4
  91. package/build/graphql/contributionsDocument.d.ts +18 -0
  92. package/build/graphql/contributionsDocument.d.ts.map +1 -0
  93. package/build/graphql/contributionsDocument.js +30 -0
  94. package/build/graphql/generated/common.d.ts +42 -0
  95. package/build/graphql/generated/common.d.ts.map +1 -0
  96. package/build/graphql/generated/common.js +2 -0
  97. package/build/graphql/generated/gist.d.ts +44 -0
  98. package/build/graphql/generated/gist.d.ts.map +1 -0
  99. package/build/graphql/generated/gist.js +27 -0
  100. package/build/graphql/generated/repo.d.ts +67 -0
  101. package/build/graphql/generated/repo.d.ts.map +1 -0
  102. package/build/graphql/generated/repo.js +30 -0
  103. package/build/graphql/generated/stats.d.ts +150 -0
  104. package/build/graphql/generated/stats.d.ts.map +1 -0
  105. package/build/graphql/generated/stats.js +95 -0
  106. package/build/graphql/generated/top-languages.d.ts +54 -0
  107. package/build/graphql/generated/top-languages.d.ts.map +1 -0
  108. package/build/graphql/generated/top-languages.js +27 -0
  109. package/build/graphql/graphqlDocument.d.ts +22 -0
  110. package/build/graphql/graphqlDocument.d.ts.map +1 -0
  111. package/build/graphql/graphqlDocument.js +8 -0
  112. package/build/graphql/reposContributedToDocument.d.ts +27 -0
  113. package/build/graphql/reposContributedToDocument.d.ts.map +1 -0
  114. package/build/graphql/reposContributedToDocument.js +59 -0
  115. package/build/index.d.ts +3 -1
  116. package/build/index.d.ts.map +1 -1
  117. package/build/index.js +2 -1
  118. package/build/themes/index.d.ts +32 -0
  119. package/build/themes/index.d.ts.map +1 -1
  120. package/build/themes/index.js +28 -0
  121. package/build/translations.d.ts +17 -0
  122. package/build/translations.d.ts.map +1 -1
  123. package/build/translations.js +17 -0
  124. package/package.json +14 -8
  125. package/src/_emoji-name-map.d.ts +10 -0
  126. package/src/_github-username-regex.d.ts +4 -0
  127. package/src/api/gist.js +31 -31
  128. package/src/api/index.js +40 -42
  129. package/src/api/pin.js +20 -38
  130. package/src/api/top-langs.js +36 -49
  131. package/src/api/wakatime.js +31 -31
  132. package/src/{calculateRank.js → calculateRank.ts} +29 -19
  133. package/src/cards/common-options.ts +13 -0
  134. package/src/cards/{gist.js → gist.ts} +37 -37
  135. package/src/cards/{repo.js → repo.ts} +107 -79
  136. package/src/cards/{stats.js → stats.ts} +173 -210
  137. package/src/cards/{top-languages.js → top-languages.ts} +325 -243
  138. package/src/cards/{wakatime.js → wakatime.ts} +158 -127
  139. package/src/common/Card.ts +123 -43
  140. package/src/common/I18n.ts +6 -2
  141. package/src/common/color.ts +240 -28
  142. package/src/common/config.ts +22 -29
  143. package/src/common/constants.ts +3 -1
  144. package/src/common/date.ts +45 -0
  145. package/src/common/html.ts +1 -1
  146. package/src/common/http.ts +71 -0
  147. package/src/common/icons.ts +1 -0
  148. package/src/common/languageColors.json +10 -1
  149. package/src/common/languageColors.ts +19 -0
  150. package/src/common/ops.ts +158 -0
  151. package/src/common/render.ts +652 -0
  152. package/src/common/retryer.ts +21 -12
  153. package/src/fetchers/gist.ts +79 -0
  154. package/src/fetchers/repo.ts +114 -0
  155. package/src/fetchers/stats.ts +743 -0
  156. package/src/fetchers/top-languages.ts +135 -0
  157. package/src/fetchers/{types.d.ts → types.ts} +28 -20
  158. package/src/fetchers/{wakatime.js → wakatime.ts} +19 -5
  159. package/src/graphql/contributionsDocument.ts +44 -0
  160. package/src/graphql/generated/common.ts +26 -0
  161. package/src/graphql/generated/gist.ts +59 -0
  162. package/src/graphql/generated/repo.ts +90 -0
  163. package/src/graphql/generated/stats.ts +194 -0
  164. package/src/graphql/generated/top-languages.ts +75 -0
  165. package/src/graphql/graphqlDocument.ts +25 -0
  166. package/src/graphql/queries/gist.graphql +25 -0
  167. package/src/graphql/queries/repo.graphql +28 -0
  168. package/src/graphql/queries/stats.graphql +120 -0
  169. package/src/graphql/queries/top-languages.graphql +34 -0
  170. package/src/graphql/reposContributedToDocument.ts +86 -0
  171. package/src/index.ts +3 -1
  172. package/src/themes/index.ts +33 -0
  173. package/src/translations.ts +17 -0
  174. package/src/cards/types.d.ts +0 -78
  175. package/src/common/http.js +0 -19
  176. package/src/common/ops.js +0 -170
  177. package/src/common/render.js +0 -440
  178. package/src/fetchers/gist.js +0 -112
  179. package/src/fetchers/repo.js +0 -161
  180. package/src/fetchers/stats.js +0 -474
  181. package/src/fetchers/top-languages.js +0 -175
  182. package/src/themes/README.md +0 -229
@@ -0,0 +1,652 @@
1
+ import { getCardColors, isPrefixedHexColor } from "./color.js";
2
+ import { SECONDARY_ERROR_MESSAGES, TRY_AGAIN_LATER } from "./error.js";
3
+ import { kFormatter } from "./fmt.js";
4
+ import { encodeHTML } from "./html.js";
5
+ import { clampValue } from "./ops.js";
6
+
7
+ /**
8
+ * Auto layout utility, allows us to layout things vertically or horizontally with
9
+ * proper gaping.
10
+ *
11
+ * The caller must ensure that the passed `items` are properly sanitized!
12
+ *
13
+ * @param props Function properties.
14
+ * @param props.items Array of sanitized items to layout.
15
+ * @param props.gap Gap between items.
16
+ * @param props.direction Direction to layout items.
17
+ * @param props.sizes Array of sizes for each item.
18
+ * @returns Array of items with proper layout.
19
+ */
20
+ const flexLayout = ({
21
+ items,
22
+ gap,
23
+ direction,
24
+ sizes = [],
25
+ }: {
26
+ items: Array<string>;
27
+ gap: number;
28
+ direction?: "column" | "row";
29
+ sizes?: Array<number>;
30
+ }): Array<string> => {
31
+ if (sizes.some((size) => !Number.isFinite(size))) {
32
+ throw new Error("flexLayout: `sizes` must contain only numbers");
33
+ }
34
+
35
+ let lastSize = 0;
36
+ // filter() for filtering out empty strings
37
+ return items.filter(Boolean).map((item, i) => {
38
+ const size = sizes[i] || 0;
39
+ let transform = `translate(${lastSize}, 0)`;
40
+ if (direction === "column") {
41
+ transform = `translate(0, ${lastSize})`;
42
+ }
43
+ lastSize += size + gap;
44
+ return `<g transform="${transform}">${item}</g>`;
45
+ });
46
+ };
47
+
48
+ /**
49
+ * Creates a node to display the primary programming language of the repository/gist.
50
+ *
51
+ * @param langName Language name.
52
+ * @param langColor Language color.
53
+ * @returns Language display SVG object.
54
+ */
55
+ const createLanguageNode = (langName: string, langColor: string): string => {
56
+ if (!isPrefixedHexColor(langColor)) {
57
+ throw new Error(`Invalid language color: "${langColor}"`);
58
+ }
59
+
60
+ return `
61
+ <g data-testid="primary-lang">
62
+ <circle data-testid="lang-color" cx="0" cy="-5" r="6" fill="${langColor}" />
63
+ <text data-testid="lang-name" class="gray" x="15">${encodeHTML(langName)}</text>
64
+ </g>
65
+ `;
66
+ };
67
+
68
+ /**
69
+ * Create a node to indicate progress in percentage along a horizontal line.
70
+ *
71
+ * @param params Object that contains the createProgressNode parameters.
72
+ * @param params.x X-axis position.
73
+ * @param params.y Y-axis position.
74
+ * @param params.width Width of progress bar.
75
+ * @param params.color Optional foreground color as an inline fill fallback.
76
+ * When omitted, the color must be set via a `.lang-progress` CSS rule.
77
+ * @param params.progress Progress value.
78
+ * @param params.delay Delay before animation starts.
79
+ * @returns Progress node.
80
+ */
81
+ const createProgressNode = ({
82
+ x,
83
+ y,
84
+ width,
85
+ color,
86
+ progress,
87
+ delay,
88
+ }: {
89
+ x: number;
90
+ y: number;
91
+ width: number;
92
+ color?: string;
93
+ progress: number;
94
+ delay: number;
95
+ }): string => {
96
+ if (color !== undefined && !isPrefixedHexColor(color)) {
97
+ throw new Error(`Invalid progress color: "${color}"`);
98
+ }
99
+ if (!Number.isFinite(width)) {
100
+ throw new Error(`Invalid width: "${width}"`);
101
+ }
102
+ if (!Number.isFinite(x)) {
103
+ throw new Error(`Invalid x: "${x}"`);
104
+ }
105
+ if (!Number.isFinite(y)) {
106
+ throw new Error(`Invalid y: "${y}"`);
107
+ }
108
+ if (!Number.isFinite(delay)) {
109
+ throw new Error(`Invalid delay: "${delay}"`);
110
+ }
111
+
112
+ const progressPercentage = clampValue(progress, 2, 100);
113
+
114
+ return `
115
+ <svg width="${width}" x="${x}" y="${y}">
116
+ <rect data-testid="progress-background" class="progress-background" rx="5" ry="5" x="0" y="0" width="${width}" height="8"></rect>
117
+ <svg data-testid="lang-progress" width="${progressPercentage}%">
118
+ <rect
119
+ height="8"
120
+ ${color === undefined ? "" : `fill="${color}"`}
121
+ rx="5" ry="5" x="0" y="0"
122
+ class="lang-progress"
123
+ style="animation-delay: ${delay}ms;"
124
+ />
125
+ </svg>
126
+ </svg>
127
+ `;
128
+ };
129
+
130
+ /**
131
+ * Renders multi-line text via a `foreignObject` so the browser performs
132
+ * native, font-aware wrapping. Content overflowing `lineCount` lines is
133
+ * clipped (with an ellipsis on the last visible line) by CSS line-clamp.
134
+ *
135
+ * @param props Function properties.
136
+ * @param props.text Text to render (will be HTML-encoded).
137
+ * @param props.x X position of the foreignObject.
138
+ * @param props.y Y position of the foreignObject.
139
+ * @param props.width Width of the wrap box.
140
+ * @param props.height Height of the wrap box.
141
+ * @param props.lineCount Maximum number of lines to display.
142
+ * @param props.className CSS class applied to the inner element.
143
+ * @param props.testId Optional test id for the inner element.
144
+ * @returns foreignObject SVG node.
145
+ */
146
+ const wrappedTextNode = ({
147
+ text,
148
+ x,
149
+ y,
150
+ width,
151
+ height,
152
+ lineCount,
153
+ className,
154
+ testId,
155
+ }: {
156
+ text: string;
157
+ x: number;
158
+ y: number;
159
+ width: number;
160
+ height: number;
161
+ lineCount: number;
162
+ className: string;
163
+ testId?: string;
164
+ }): string => {
165
+ if (!Number.isFinite(x)) {
166
+ throw new Error(`Invalid x: "${x}"`);
167
+ }
168
+ if (!Number.isFinite(y)) {
169
+ throw new Error(`Invalid y: "${y}"`);
170
+ }
171
+ if (!Number.isFinite(width)) {
172
+ throw new Error(`Invalid width: "${width}"`);
173
+ }
174
+ if (!Number.isFinite(height)) {
175
+ throw new Error(`Invalid height: "${height}"`);
176
+ }
177
+ if (!Number.isFinite(lineCount)) {
178
+ throw new Error(`Invalid lineCount: "${lineCount}"`);
179
+ }
180
+
181
+ const testIdAttr = testId ? ` data-testid="${encodeHTML(testId)}"` : "";
182
+ return `
183
+ <foreignObject x="${x}" y="${y}" width="${width}" height="${height}">
184
+ <div xmlns="http://www.w3.org/1999/xhtml" class="${encodeHTML(className)}" style="--lines: ${lineCount};"${testIdAttr}>${encodeHTML(
185
+ text,
186
+ )}</div>
187
+ </foreignObject>
188
+ `;
189
+ };
190
+
191
+ /**
192
+ * CSS rules used to render multi-line text inside a `foreignObject`. Apply this
193
+ * to a CSS class (e.g. `.description`) shared with `wrappedTextNode` so the
194
+ * browser handles wrapping and the line count is taken from the `--lines`
195
+ * custom property set on the element.
196
+ *
197
+ * @param color Text color (CSS `color` property).
198
+ * @returns CSS rules block (without the surrounding selector).
199
+ */
200
+ const wrappedTextStyles = (color: string): string => {
201
+ if (!isPrefixedHexColor(color)) {
202
+ throw new Error(`Invalid text color: "${color}"`);
203
+ }
204
+
205
+ return `
206
+ color: ${color};
207
+ margin: 0;
208
+ line-height: 1.2;
209
+ overflow-wrap: anywhere;
210
+ word-break: break-word;
211
+ display: -webkit-box;
212
+ -webkit-box-orient: vertical;
213
+ -webkit-line-clamp: var(--lines);
214
+ line-clamp: var(--lines);
215
+ overflow: hidden;
216
+ text-overflow: ellipsis;
217
+ padding-bottom: 0.15em;
218
+ `;
219
+ };
220
+
221
+ /**
222
+ * Creates an icon with label to display repository/gist stats like forks, stars, etc.
223
+ *
224
+ * The caller must ensure that the passed `icon` is properly sanitized!
225
+ *
226
+ * @param icon The sanitized icon to display.
227
+ * @param label The label to display.
228
+ * @param testid The testid to assign to the label.
229
+ * @param iconSize The size of the icon.
230
+ * @returns Icon with label SVG object.
231
+ */
232
+ const iconWithLabel = (
233
+ icon: string,
234
+ label: number | string,
235
+ testid: string,
236
+ iconSize: number,
237
+ ): string => {
238
+ if (typeof label === "number" && label <= 0) {
239
+ return "";
240
+ }
241
+
242
+ if (!Number.isFinite(iconSize)) {
243
+ throw new Error(`Invalid iconSize: "${iconSize}"`);
244
+ }
245
+
246
+ const iconSvg = `
247
+ <svg
248
+ class="icon"
249
+ y="-12"
250
+ viewBox="0 0 16 16"
251
+ version="1.1"
252
+ width="${iconSize}"
253
+ height="${iconSize}"
254
+ >
255
+ ${icon}
256
+ </svg>
257
+ `;
258
+ const text = `<text data-testid="${encodeHTML(testid)}" class="gray">${encodeHTML(String(label))}</text>`;
259
+ return flexLayout({ items: [iconSvg, text], gap: 20 }).join("");
260
+ };
261
+
262
+ /**
263
+ * Create a labelled stat text item
264
+ * (a label and its value, optionally with an icon and link).
265
+ * Shared by the stats and repo cards.
266
+ *
267
+ * The caller must ensure that the passed `icon` and `link` are properly sanitized!
268
+ *
269
+ * @param params Object that contains the createTextNode parameters.
270
+ * @param params.icon The sanitized icon to display.
271
+ * @param params.label The label to display.
272
+ * @param params.value The value to display.
273
+ * @param params.id The id of the stat.
274
+ * @param params.unitSymbol The unit symbol of the stat.
275
+ * @param params.index The index of the stat.
276
+ * @param params.showIcons Whether to show icons.
277
+ * @param params.shiftValuePos Number of pixels the value has to be shifted to the right.
278
+ * @param params.bold Whether to bold the label.
279
+ * @param params.numberFormat The format of numbers on card.
280
+ * @param params.numberPrecision The precision of numbers on card.
281
+ * @param params.link Sanitized url to link to.
282
+ * @param params.labelXOffset horizontal offset for label.
283
+ * @returns The stats card text item SVG object.
284
+ */
285
+ const createTextNode = ({
286
+ icon,
287
+ label,
288
+ value,
289
+ id,
290
+ unitSymbol,
291
+ index,
292
+ showIcons,
293
+ shiftValuePos,
294
+ bold,
295
+ numberFormat,
296
+ numberPrecision,
297
+ link,
298
+ labelXOffset = 25,
299
+ }: {
300
+ icon: string;
301
+ label: string;
302
+ value: number | string;
303
+ id: string;
304
+ // `| undefined`: card callers forward possibly-undefined per-stat fields
305
+ unitSymbol?: string | undefined;
306
+ index: number;
307
+ showIcons: boolean;
308
+ shiftValuePos: number;
309
+ bold: boolean;
310
+ numberFormat: string;
311
+ numberPrecision?: number | undefined;
312
+ link?: string | undefined;
313
+ labelXOffset?: number;
314
+ }): string => {
315
+ if (!Number.isFinite(labelXOffset)) {
316
+ throw new Error(`Invalid labelXOffset: "${labelXOffset}"`);
317
+ }
318
+ if (!Number.isFinite(shiftValuePos)) {
319
+ throw new Error(`Invalid shiftValuePos: "${shiftValuePos}"`);
320
+ }
321
+ if (!Number.isFinite(index)) {
322
+ throw new Error(`Invalid index: "${index}"`);
323
+ }
324
+
325
+ const precision =
326
+ numberPrecision !== undefined && Number.isFinite(numberPrecision)
327
+ ? clampValue(numberPrecision, 0, 2)
328
+ : undefined;
329
+ // `long` format and the percentage stat keep the raw value; everything else
330
+ // (always a number here) is abbreviated via kFormatter.
331
+ const rawValue =
332
+ numberFormat.toLowerCase() === "long" || id === "prs_merged_percentage";
333
+ const kValue =
334
+ rawValue || typeof value !== "number"
335
+ ? value
336
+ : kFormatter(value, precision);
337
+
338
+ const staggerDelay = (index + 3) * 150;
339
+ const boldClass = bold ? " bold" : "not_bold";
340
+ const valueX = (showIcons ? 140 : 120) + (bold ? 5 : 0) + shiftValuePos;
341
+ const unit = unitSymbol ? ` ${unitSymbol}` : "";
342
+ const labelOffset = showIcons ? `x="${labelXOffset}"` : "";
343
+ const iconSvg = showIcons
344
+ ? `
345
+ <svg data-testid="icon" class="icon" viewBox="0 0 16 16" version="1.1" width="16" height="16">
346
+ ${icon}
347
+ </svg>
348
+ `
349
+ : "";
350
+
351
+ const content = `
352
+ ${iconSvg}
353
+ <text class="stat ${boldClass}" ${labelOffset} y="12.5">${encodeHTML(label)}:</text>
354
+ <text
355
+ class="stat ${boldClass}"
356
+ x="${valueX}"
357
+ y="12.5"
358
+ data-testid="${id}"
359
+ >${kValue}${unit}</text>`;
360
+ const inner = link ? `<a href="${link}">${content}</a>` : content;
361
+
362
+ return `
363
+ <g class="stagger" style="animation-delay: ${staggerDelay}ms" transform="translate(25, 0)">${inner}
364
+ </g>
365
+ `;
366
+ };
367
+
368
+ // Script parameters.
369
+ const ERROR_CARD_LENGTH = 576.5;
370
+
371
+ const UPSTREAM_API_ERRORS = [
372
+ TRY_AGAIN_LATER,
373
+ SECONDARY_ERROR_MESSAGES.MAX_RETRY,
374
+ ];
375
+
376
+ /**
377
+ * Renders error message on the card.
378
+ *
379
+ * @param args Function arguments.
380
+ * @param args.message Main error message.
381
+ * @param args.secondaryMessage The secondary error message.
382
+ * @param args.renderOptions Render options.
383
+ * @param args.renderOptions.title_color Card title color.
384
+ * @param args.renderOptions.text_color Card text color.
385
+ * @param args.renderOptions.bg_color Card background color.
386
+ * @param args.renderOptions.border_color Card border color.
387
+ * @param args.renderOptions.theme Card theme.
388
+ * @param args.renderOptions.show_repo_link Whether to show repo link or not.
389
+ * @returns The SVG markup.
390
+ */
391
+ const renderError = ({
392
+ message,
393
+ secondaryMessage = "",
394
+ renderOptions = {},
395
+ }: {
396
+ message: string;
397
+ secondaryMessage?: string;
398
+ renderOptions?: {
399
+ title_color?: string;
400
+ text_color?: string;
401
+ bg_color?: string;
402
+ border_color?: string;
403
+ theme?: string;
404
+ show_repo_link?: boolean;
405
+ };
406
+ }): string => {
407
+ const {
408
+ title_color,
409
+ text_color,
410
+ bg_color,
411
+ border_color,
412
+ theme = "default",
413
+ show_repo_link = true,
414
+ } = renderOptions;
415
+
416
+ const { titleColor, textColor, bgColor, borderColor } = getCardColors({
417
+ title_color,
418
+ text_color,
419
+ icon_color: "",
420
+ bg_color,
421
+ border_color,
422
+ ring_color: "",
423
+ theme,
424
+ });
425
+
426
+ return `
427
+ <svg width="${ERROR_CARD_LENGTH}" height="120" viewBox="0 0 ${ERROR_CARD_LENGTH} 120" fill="${String(bgColor)}" xmlns="http://www.w3.org/2000/svg">
428
+ <style>
429
+ .text { font: 600 16px 'Segoe UI', Ubuntu, Sans-Serif; fill: ${titleColor} }
430
+ .small { font: 600 12px 'Segoe UI', Ubuntu, Sans-Serif; fill: ${textColor} }
431
+ .gray { fill: #858585 }
432
+ </style>
433
+ <rect x="0.5" y="0.5" width="${
434
+ ERROR_CARD_LENGTH - 1
435
+ }" height="99%" rx="4.5" fill="${String(bgColor)}" stroke="${borderColor}"/>
436
+ <text x="25" y="45" class="text">Something went wrong!${
437
+ UPSTREAM_API_ERRORS.includes(secondaryMessage) || !show_repo_link
438
+ ? ""
439
+ : " file an issue at https://tinyurl.com/github-stats"
440
+ }</text>
441
+ <text data-testid="message" x="25" y="55" class="text small">
442
+ <tspan x="25" dy="18">${encodeHTML(message)}</tspan>
443
+ <tspan x="25" dy="18" class="gray">${encodeHTML(secondaryMessage)}</tspan>
444
+ </text>
445
+ </svg>
446
+ `;
447
+ };
448
+
449
+ /**
450
+ * Retrieve text length based on Segoe UI font.
451
+ *
452
+ * @see https://stackoverflow.com/a/48172630/10629172
453
+ * @param str String to measure.
454
+ * @param fontSize Font size.
455
+ * @returns Text length.
456
+ */
457
+ const measureText = (str: string, fontSize = 10): number => {
458
+ // prettier-ignore
459
+ const widths = [
460
+ 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,
461
+ 0, 0, 0, 0, 0, 0, 0, 0.2733333110809326, 0.28499999046325686,
462
+ 0.39166667461395266, 0.5900000095367431, 0.5383333206176758,
463
+ 0.8183333396911621, 0.8, 0.22999999523162842, 0.30166666507720946,
464
+ 0.30166666507720946, 0.41666665077209475, 0.6833333492279052,
465
+ 0.21666667461395264, 0.4, 0.21666667461395264, 0.3900000095367432,
466
+ 0.5383333206176758, 0.5383333206176758, 0.5383333206176758,
467
+ 0.5383333206176758, 0.5383333206176758, 0.5383333206176758,
468
+ 0.5383333206176758, 0.5383333206176758, 0.5383333206176758,
469
+ 0.5383333206176758, 0.21666667461395264, 0.21666667461395264,
470
+ 0.6833333492279052, 0.6833333492279052, 0.6833333492279052,
471
+ 0.4483333110809326, 0.9550000190734863, 0.6449999809265137,
472
+ 0.5733333110809327, 0.6183333396911621, 0.7016666889190674,
473
+ 0.5066666603088379, 0.48833332061767576, 0.6866666793823242,
474
+ 0.7099999904632568, 0.26666667461395266, 0.35666666030883787,
475
+ 0.5800000190734863, 0.4699999809265137, 0.8983333587646485,
476
+ 0.7483333110809326, 0.753333330154419, 0.5599999904632569,
477
+ 0.753333330154419, 0.5983333110809326, 0.5316666603088379,
478
+ 0.5233333110809326, 0.6866666793823242, 0.621666669845581,
479
+ 0.9333333015441895, 0.5900000095367431, 0.553333330154419,
480
+ 0.5699999809265137, 0.30166666507720946, 0.37833333015441895,
481
+ 0.30166666507720946, 0.6833333492279052, 0.41500000953674315,
482
+ 0.26833333969116213, 0.5083333492279053, 0.5883333206176757,
483
+ 0.4616666793823242, 0.5883333206176757, 0.5233333110809326,
484
+ 0.3133333444595337, 0.5883333206176757, 0.5666666507720948,
485
+ 0.24166667461395264, 0.24166667461395264, 0.49666666984558105,
486
+ 0.24166667461395264, 0.8616666793823242, 0.5666666507720948,
487
+ 0.5866666793823242, 0.5883333206176757, 0.5883333206176757,
488
+ 0.3483333349227905, 0.425, 0.33833334445953367, 0.5666666507720948,
489
+ 0.4783333301544189, 0.7233333110809326, 0.45833334922790525,
490
+ 0.4833333492279053, 0.45166668891906736, 0.30166666507720946,
491
+ 0.24000000953674316, 0.30166666507720946, 0.6833333492279052,
492
+ ];
493
+
494
+ const avg = 0.5131403493881227;
495
+ // CJK character range: U+3000–U+9FFF (CJK Symbols/Punctuation, Hiragana,
496
+ // Katakana, CJK Unified Ideographs incl. Extension A) plus U+FF00–U+FFEF
497
+ // (Halfwidth/Fullwidth Forms — fullwidth ASCII, fullwidth punctuation).
498
+ const cjkRange = /[\u3000-\u9FFF\uFF00-\uFFEF]/;
499
+
500
+ return (
501
+ str
502
+ .split("")
503
+ .map((c) => {
504
+ if (cjkRange.test(c) || c === "\u3000") {
505
+ // CJK glyphs and U+3000 IDEOGRAPHIC SPACE are full-width by default;
506
+ return 1;
507
+ }
508
+ if (c.charCodeAt(0) < widths.length) {
509
+ return widths[c.charCodeAt(0)] ?? avg;
510
+ } else {
511
+ return avg;
512
+ }
513
+ })
514
+ .reduce((cur, acc) => acc + cur) * fontSize
515
+ );
516
+ };
517
+
518
+ /**
519
+ * Split text into the lines it would wrap to when laid out greedily at the
520
+ * given font size inside a box of width `maxWidth`. Uses `measureText` so the
521
+ * estimate reflects actual font metrics rather than a fixed character count.
522
+ * The browser still does the real wrap inside the foreignObject; this is only
523
+ * used to size the SVG.
524
+ *
525
+ * @param text Text to split.
526
+ * @param fontSize Font size in px (matches `measureText`).
527
+ * @param maxWidth Available wrap width in px.
528
+ * @returns Estimated wrapped lines.
529
+ */
530
+ const splitWrappedText = (
531
+ text: string,
532
+ fontSize: number,
533
+ maxWidth: number,
534
+ ): Array<string> => {
535
+ if (!text) {
536
+ return [];
537
+ }
538
+
539
+ // Tokenize the text into atoms representing line-break opportunities:
540
+ // - ASCII whitespace runs (collapsed/dropped at line edges per CSS rules);
541
+ // - non-ASCII whitespaces
542
+ // - a single CJK codepoint (browsers break between any two CJK chars,
543
+ // punctuation or not, so each one is its own atom and ~1 em wide);
544
+ // - a run of non-whitespace non-CJK characters (a "word" that can only
545
+ // break at its boundaries, like Latin script).
546
+ // Korean Hangul (U+AC00–U+D7AF) is intentionally NOT in the CJK range
547
+ // because Korean wraps at word boundaries by default in HTML.
548
+ // ASCII whitespace is collapsed to a single space per CSS `white-space: normal;`
549
+ const normalizedText = text.replace(/[\t\n\r ]+/g, " ");
550
+ const tokens = normalizedText.match(
551
+ /\s|[\u3000-\u9FFF\uFF00-\uFFEF]|[^\s\u3000-\u9FFF\uFF00-\uFFEF]+/g,
552
+ );
553
+ if (!tokens) {
554
+ return [];
555
+ }
556
+
557
+ const takeFittingSegment = (token: string, availableWidth: number) => {
558
+ const characters = token.split("");
559
+ let segment = "";
560
+ let width = 0;
561
+
562
+ for (const character of characters) {
563
+ const characterWidth = measureText(character, fontSize);
564
+ if (segment && width + characterWidth > availableWidth) {
565
+ break;
566
+ }
567
+ segment += character;
568
+ width += characterWidth;
569
+ }
570
+
571
+ return {
572
+ segment,
573
+ width,
574
+ };
575
+ };
576
+
577
+ const lines = [""];
578
+ let currentWidth = 0;
579
+
580
+ for (const token of tokens) {
581
+ if (token === " ") {
582
+ // Whitespace at the start of a line is dropped by browsers.
583
+ if (currentWidth === 0) {
584
+ continue;
585
+ }
586
+ lines[lines.length - 1] = (lines[lines.length - 1] ?? "") + token;
587
+ currentWidth += measureText(token, fontSize);
588
+ continue;
589
+ }
590
+
591
+ let remaining = token;
592
+
593
+ while (remaining) {
594
+ const w = measureText(remaining, fontSize);
595
+ if (currentWidth + w <= maxWidth) {
596
+ lines[lines.length - 1] = (lines[lines.length - 1] ?? "") + remaining;
597
+ currentWidth += w;
598
+ break;
599
+ }
600
+
601
+ if (currentWidth > 0) {
602
+ lines.push("");
603
+ currentWidth = 0;
604
+ continue;
605
+ }
606
+
607
+ // An atom wider than the box wraps mid-glyph (overflow-wrap: anywhere).
608
+ const { segment, width } = takeFittingSegment(remaining, maxWidth);
609
+ lines[lines.length - 1] = (lines[lines.length - 1] ?? "") + segment;
610
+ currentWidth = width;
611
+ remaining = remaining.slice(segment.length);
612
+ }
613
+ }
614
+
615
+ return lines.map((line) => line.replace(/[\t\n\r ]+$/, ""));
616
+ };
617
+
618
+ /**
619
+ * Estimate how many lines a string will wrap to when laid out greedily at the
620
+ * given font size inside a box of width `maxWidth`, capped at `maxLines`.
621
+ *
622
+ * @param text Text to estimate.
623
+ * @param fontSize Font size in px (matches `measureText`).
624
+ * @param maxWidth Available wrap width in px.
625
+ * @param maxLines Cap on the returned line count.
626
+ * @returns Estimated line count, at least 1, at most `maxLines`.
627
+ */
628
+ const countWrappedLines = (
629
+ text: string,
630
+ fontSize: number,
631
+ maxWidth: number,
632
+ maxLines: number,
633
+ ): number => {
634
+ return Math.min(
635
+ Math.max(1, splitWrappedText(text, fontSize, maxWidth).length),
636
+ maxLines,
637
+ );
638
+ };
639
+
640
+ export {
641
+ renderError,
642
+ createLanguageNode,
643
+ createProgressNode,
644
+ createTextNode,
645
+ iconWithLabel,
646
+ flexLayout,
647
+ measureText,
648
+ splitWrappedText,
649
+ countWrappedLines,
650
+ wrappedTextNode,
651
+ wrappedTextStyles,
652
+ };