@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
package/src/common/ops.js DELETED
@@ -1,170 +0,0 @@
1
- import toEmoji from "emoji-name-map";
2
-
3
- import { OWNER_AFFILIATIONS } from "./constants.js";
4
- import { CustomError } from "./error.js";
5
-
6
- /**
7
- * Returns boolean if value is either "true" or "false" else the value as it is.
8
- *
9
- * @param {string | boolean} value The value to parse.
10
- * @returns {boolean | undefined } The parsed value.
11
- */
12
- const parseBoolean = (value) => {
13
- if (typeof value === "boolean") {
14
- return value;
15
- }
16
-
17
- if (typeof value === "string") {
18
- if (value.toLowerCase() === "true") {
19
- return true;
20
- } else if (value.toLowerCase() === "false") {
21
- return false;
22
- }
23
- }
24
- return undefined;
25
- };
26
-
27
- /**
28
- * Parse string to array of strings.
29
- *
30
- * @param {string} str The string to parse.
31
- * @returns {string[]} The array of strings.
32
- */
33
- const parseArray = (str) => {
34
- if (!str) {
35
- return [];
36
- }
37
- return str.split(",");
38
- };
39
-
40
- /**
41
- * Clamp the given number between the given range.
42
- *
43
- * @param {number} number The number to clamp.
44
- * @param {number} min The minimum value.
45
- * @param {number} max The maximum value.
46
- * @returns {number} The clamped number.
47
- */
48
- const clampValue = (number, min, max) => {
49
- // @ts-ignore
50
- if (Number.isNaN(parseInt(number, 10))) {
51
- return min;
52
- }
53
- return Math.max(min, Math.min(number, max));
54
- };
55
-
56
- /**
57
- * Lowercase and trim string.
58
- *
59
- * @param {string} name String to lowercase and trim.
60
- * @returns {string} Lowercased and trimmed string.
61
- */
62
- const lowercaseTrim = (name) => name.toLowerCase().trim();
63
-
64
- /**
65
- * Split array of languages in two columns.
66
- *
67
- * @template T Language object.
68
- * @param {Array<T>} arr Array of languages.
69
- * @param {number} perChunk Number of languages per column.
70
- * @returns {Array<T>} Array of languages split in two columns.
71
- */
72
- const chunkArray = (arr, perChunk) => {
73
- return arr.reduce((resultArray, item, index) => {
74
- const chunkIndex = Math.floor(index / perChunk);
75
-
76
- if (!resultArray[chunkIndex]) {
77
- // @ts-ignore
78
- resultArray[chunkIndex] = []; // start a new chunk
79
- }
80
-
81
- // @ts-ignore
82
- resultArray[chunkIndex].push(item);
83
-
84
- return resultArray;
85
- }, []);
86
- };
87
-
88
- /**
89
- * Parse emoji from string.
90
- *
91
- * @param {string} str String to parse emoji from.
92
- * @returns {string} String with emoji parsed.
93
- */
94
- const parseEmojis = (str) => {
95
- if (!str) {
96
- throw new Error("[parseEmoji]: str argument not provided");
97
- }
98
- return str.replace(/:\w+:/gm, (emoji) => {
99
- return toEmoji.get(emoji) || "";
100
- });
101
- };
102
-
103
- /**
104
- * Get diff in minutes between two dates.
105
- *
106
- * @param {Date} d1 First date.
107
- * @param {Date} d2 Second date.
108
- * @returns {number} Number of minutes between the two dates.
109
- */
110
- const dateDiff = (d1, d2) => {
111
- const date1 = new Date(d1);
112
- const date2 = new Date(d2);
113
- const diff = date1.getTime() - date2.getTime();
114
- return Math.round(diff / (1000 * 60));
115
- };
116
-
117
- /**
118
- * Parse owner affiliations.
119
- *
120
- * @param {string[]} affiliations input affiliations to be parsed.
121
- * @returns {string[]} Parsed affiliations.
122
- *
123
- * @throws {CustomError} If affiliations contains invalid values.
124
- */
125
- const parseOwnerAffiliations = (affiliations) => {
126
- // Set default value for ownerAffiliations.
127
- // NOTE: Done here since parseArray() will always return an empty array even nothing
128
- //was specified.
129
- affiliations =
130
- affiliations && affiliations.length > 0
131
- ? affiliations.map((affiliation) => affiliation.toUpperCase())
132
- : ["OWNER"];
133
-
134
- // Check if ownerAffiliations contains valid values.
135
- if (
136
- affiliations.some(
137
- (affiliation) => !OWNER_AFFILIATIONS.includes(affiliation),
138
- )
139
- ) {
140
- throw new CustomError(
141
- "Invalid query parameter",
142
- CustomError.INVALID_AFFILIATION,
143
- );
144
- }
145
- return affiliations;
146
- };
147
-
148
- const buildSearchFilter = (repos = [], owners = []) => {
149
- let repoFilter =
150
- Array.isArray(repos) && repos.length > 0
151
- ? repos.map((r) => `repo:${r} `).join("")
152
- : "";
153
- let orgFilter =
154
- Array.isArray(owners) && owners.length > 0
155
- ? owners.map((o) => `owner:${o} `).join("")
156
- : "";
157
- return repoFilter + orgFilter;
158
- };
159
-
160
- export {
161
- parseBoolean,
162
- parseArray,
163
- clampValue,
164
- lowercaseTrim,
165
- chunkArray,
166
- parseEmojis,
167
- dateDiff,
168
- parseOwnerAffiliations,
169
- buildSearchFilter,
170
- };
@@ -1,440 +0,0 @@
1
- import { getCardColors } from "./color.js";
2
- import { SECONDARY_ERROR_MESSAGES, TRY_AGAIN_LATER } from "./error.js";
3
- import { encodeHTML } from "./html.js";
4
- import { clampValue } from "./ops.js";
5
-
6
- /**
7
- * Auto layout utility, allows us to layout things vertically or horizontally with
8
- * proper gaping.
9
- *
10
- * @param {object} props Function properties.
11
- * @param {string[]} props.items Array of items to layout.
12
- * @param {number} props.gap Gap between items.
13
- * @param {"column" | "row"=} props.direction Direction to layout items.
14
- * @param {number[]=} props.sizes Array of sizes for each item.
15
- * @returns {string[]} Array of items with proper layout.
16
- */
17
- const flexLayout = ({ items, gap, direction, sizes = [] }) => {
18
- let lastSize = 0;
19
- // filter() for filtering out empty strings
20
- return items.filter(Boolean).map((item, i) => {
21
- const size = sizes[i] || 0;
22
- let transform = `translate(${lastSize}, 0)`;
23
- if (direction === "column") {
24
- transform = `translate(0, ${lastSize})`;
25
- }
26
- lastSize += size + gap;
27
- return `<g transform="${transform}">${item}</g>`;
28
- });
29
- };
30
-
31
- /**
32
- * Creates a node to display the primary programming language of the repository/gist.
33
- *
34
- * @param {string} langName Language name.
35
- * @param {string} langColor Language color.
36
- * @returns {string} Language display SVG object.
37
- */
38
- const createLanguageNode = (langName, langColor) => {
39
- return `
40
- <g data-testid="primary-lang">
41
- <circle data-testid="lang-color" cx="0" cy="-5" r="6" fill="${langColor}" />
42
- <text data-testid="lang-name" class="gray" x="15">${langName}</text>
43
- </g>
44
- `;
45
- };
46
-
47
- /**
48
- * Create a node to indicate progress in percentage along a horizontal line.
49
- *
50
- * @param {Object} params Object that contains the createProgressNode parameters.
51
- * @param {number} params.x X-axis position.
52
- * @param {number} params.y Y-axis position.
53
- * @param {number} params.width Width of progress bar.
54
- * @param {string} params.color Progress color.
55
- * @param {number} params.progress Progress value.
56
- * @param {string} params.progressBarBackgroundColor Progress bar bg color.
57
- * @param {number} params.delay Delay before animation starts.
58
- * @returns {string} Progress node.
59
- */
60
- const createProgressNode = ({
61
- x,
62
- y,
63
- width,
64
- color,
65
- progress,
66
- progressBarBackgroundColor,
67
- delay,
68
- }) => {
69
- const progressPercentage = clampValue(progress, 2, 100);
70
-
71
- return `
72
- <svg width="${width}" x="${x}" y="${y}">
73
- <rect data-testid="progress-background" rx="5" ry="5" x="0" y="0" width="${width}" height="8" fill="${progressBarBackgroundColor}"></rect>
74
- <svg data-testid="lang-progress" width="${progressPercentage}%">
75
- <rect
76
- height="8"
77
- fill="${color}"
78
- rx="5" ry="5" x="0" y="0"
79
- class="lang-progress"
80
- style="animation-delay: ${delay}ms;"
81
- />
82
- </svg>
83
- </svg>
84
- `;
85
- };
86
-
87
- /**
88
- * Renders multi-line text via a `foreignObject` so the browser performs
89
- * native, font-aware wrapping. Content overflowing `lineCount` lines is
90
- * clipped (with an ellipsis on the last visible line) by CSS line-clamp.
91
- *
92
- * @param {object} props Function properties.
93
- * @param {string} props.text Text to render (will be HTML-encoded).
94
- * @param {number} props.x X position of the foreignObject.
95
- * @param {number} props.y Y position of the foreignObject.
96
- * @param {number} props.width Width of the wrap box.
97
- * @param {number} props.height Height of the wrap box.
98
- * @param {number} props.lineCount Maximum number of lines to display.
99
- * @param {string} props.className CSS class applied to the inner element.
100
- * @param {string=} props.testId Optional test id for the inner element.
101
- * @returns {string} foreignObject SVG node.
102
- */
103
- const wrappedTextNode = ({
104
- text,
105
- x,
106
- y,
107
- width,
108
- height,
109
- lineCount,
110
- className,
111
- testId,
112
- }) => {
113
- const testIdAttr = testId ? ` data-testid="${testId}"` : "";
114
- return `
115
- <foreignObject x="${x}" y="${y}" width="${width}" height="${height}">
116
- <div xmlns="http://www.w3.org/1999/xhtml" class="${className}" style="--lines: ${lineCount};"${testIdAttr}>${encodeHTML(
117
- text,
118
- )}</div>
119
- </foreignObject>
120
- `;
121
- };
122
-
123
- /**
124
- * CSS rules used to render multi-line text inside a `foreignObject`. Apply this
125
- * to a CSS class (e.g. `.description`) shared with `wrappedTextNode` so the
126
- * browser handles wrapping and the line count is taken from the `--lines`
127
- * custom property set on the element.
128
- *
129
- * @param {string} color Text color (CSS `color` property).
130
- * @returns {string} CSS rules block (without the surrounding selector).
131
- */
132
- const wrappedTextStyles = (color) => `
133
- color: ${color};
134
- margin: 0;
135
- line-height: 1.2;
136
- overflow-wrap: anywhere;
137
- word-break: break-word;
138
- display: -webkit-box;
139
- -webkit-box-orient: vertical;
140
- -webkit-line-clamp: var(--lines);
141
- line-clamp: var(--lines);
142
- overflow: hidden;
143
- text-overflow: ellipsis;
144
- padding-bottom: 0.15em;
145
- `;
146
-
147
- /**
148
- * Creates an icon with label to display repository/gist stats like forks, stars, etc.
149
- *
150
- * @param {string} icon The icon to display.
151
- * @param {number|string} label The label to display.
152
- * @param {string} testid The testid to assign to the label.
153
- * @param {number} iconSize The size of the icon.
154
- * @returns {string} Icon with label SVG object.
155
- */
156
- const iconWithLabel = (icon, label, testid, iconSize) => {
157
- if (typeof label === "number" && label <= 0) {
158
- return "";
159
- }
160
- const iconSvg = `
161
- <svg
162
- class="icon"
163
- y="-12"
164
- viewBox="0 0 16 16"
165
- version="1.1"
166
- width="${iconSize}"
167
- height="${iconSize}"
168
- >
169
- ${icon}
170
- </svg>
171
- `;
172
- const text = `<text data-testid="${testid}" class="gray">${label}</text>`;
173
- return flexLayout({ items: [iconSvg, text], gap: 20 }).join("");
174
- };
175
-
176
- // Script parameters.
177
- const ERROR_CARD_LENGTH = 576.5;
178
-
179
- const UPSTREAM_API_ERRORS = [
180
- TRY_AGAIN_LATER,
181
- SECONDARY_ERROR_MESSAGES.MAX_RETRY,
182
- ];
183
-
184
- /**
185
- * Renders error message on the card.
186
- *
187
- * @param {object} args Function arguments.
188
- * @param {string} args.message Main error message.
189
- * @param {string} [args.secondaryMessage=""] The secondary error message.
190
- * @param {object} [args.renderOptions={}] Render options.
191
- * @param {string=} args.renderOptions.title_color Card title color.
192
- * @param {string=} args.renderOptions.text_color Card text color.
193
- * @param {string=} args.renderOptions.bg_color Card background color.
194
- * @param {string=} args.renderOptions.border_color Card border color.
195
- * @param {Parameters<typeof getCardColors>[0]["theme"]=} args.renderOptions.theme Card theme.
196
- * @param {boolean=} args.renderOptions.show_repo_link Whether to show repo link or not.
197
- * @returns {string} The SVG markup.
198
- */
199
- const renderError = ({
200
- message,
201
- secondaryMessage = "",
202
- renderOptions = {},
203
- }) => {
204
- const {
205
- title_color,
206
- text_color,
207
- bg_color,
208
- border_color,
209
- theme = "default",
210
- show_repo_link = true,
211
- } = renderOptions;
212
-
213
- // returns theme based colors with proper overrides and defaults
214
- const { titleColor, textColor, bgColor, borderColor } = getCardColors({
215
- title_color,
216
- text_color,
217
- icon_color: "",
218
- bg_color,
219
- border_color,
220
- ring_color: "",
221
- theme,
222
- });
223
-
224
- return `
225
- <svg width="${ERROR_CARD_LENGTH}" height="120" viewBox="0 0 ${ERROR_CARD_LENGTH} 120" fill="${bgColor}" xmlns="http://www.w3.org/2000/svg">
226
- <style>
227
- .text { font: 600 16px 'Segoe UI', Ubuntu, Sans-Serif; fill: ${titleColor} }
228
- .small { font: 600 12px 'Segoe UI', Ubuntu, Sans-Serif; fill: ${textColor} }
229
- .gray { fill: #858585 }
230
- </style>
231
- <rect x="0.5" y="0.5" width="${
232
- ERROR_CARD_LENGTH - 1
233
- }" height="99%" rx="4.5" fill="${bgColor}" stroke="${borderColor}"/>
234
- <text x="25" y="45" class="text">Something went wrong!${
235
- UPSTREAM_API_ERRORS.includes(secondaryMessage) || !show_repo_link
236
- ? ""
237
- : " file an issue at https://tinyurl.com/github-stats"
238
- }</text>
239
- <text data-testid="message" x="25" y="55" class="text small">
240
- <tspan x="25" dy="18">${encodeHTML(message)}</tspan>
241
- <tspan x="25" dy="18" class="gray">${secondaryMessage}</tspan>
242
- </text>
243
- </svg>
244
- `;
245
- };
246
-
247
- /**
248
- * Retrieve text length based on Segoe UI font.
249
- *
250
- * @see https://stackoverflow.com/a/48172630/10629172
251
- * @param {string} str String to measure.
252
- * @param {number} fontSize Font size.
253
- * @returns {number} Text length.
254
- */
255
- const measureText = (str, fontSize = 10) => {
256
- // prettier-ignore
257
- const widths = [
258
- 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,
259
- 0, 0, 0, 0, 0, 0, 0, 0.2733333110809326, 0.28499999046325686,
260
- 0.39166667461395266, 0.5900000095367431, 0.5383333206176758,
261
- 0.8183333396911621, 0.8, 0.22999999523162842, 0.30166666507720946,
262
- 0.30166666507720946, 0.41666665077209475, 0.6833333492279052,
263
- 0.21666667461395264, 0.4, 0.21666667461395264, 0.3900000095367432,
264
- 0.5383333206176758, 0.5383333206176758, 0.5383333206176758,
265
- 0.5383333206176758, 0.5383333206176758, 0.5383333206176758,
266
- 0.5383333206176758, 0.5383333206176758, 0.5383333206176758,
267
- 0.5383333206176758, 0.21666667461395264, 0.21666667461395264,
268
- 0.6833333492279052, 0.6833333492279052, 0.6833333492279052,
269
- 0.4483333110809326, 0.9550000190734863, 0.6449999809265137,
270
- 0.5733333110809327, 0.6183333396911621, 0.7016666889190674,
271
- 0.5066666603088379, 0.48833332061767576, 0.6866666793823242,
272
- 0.7099999904632568, 0.26666667461395266, 0.35666666030883787,
273
- 0.5800000190734863, 0.4699999809265137, 0.8983333587646485,
274
- 0.7483333110809326, 0.753333330154419, 0.5599999904632569,
275
- 0.753333330154419, 0.5983333110809326, 0.5316666603088379,
276
- 0.5233333110809326, 0.6866666793823242, 0.621666669845581,
277
- 0.9333333015441895, 0.5900000095367431, 0.553333330154419,
278
- 0.5699999809265137, 0.30166666507720946, 0.37833333015441895,
279
- 0.30166666507720946, 0.6833333492279052, 0.41500000953674315,
280
- 0.26833333969116213, 0.5083333492279053, 0.5883333206176757,
281
- 0.4616666793823242, 0.5883333206176757, 0.5233333110809326,
282
- 0.3133333444595337, 0.5883333206176757, 0.5666666507720948,
283
- 0.24166667461395264, 0.24166667461395264, 0.49666666984558105,
284
- 0.24166667461395264, 0.8616666793823242, 0.5666666507720948,
285
- 0.5866666793823242, 0.5883333206176757, 0.5883333206176757,
286
- 0.3483333349227905, 0.425, 0.33833334445953367, 0.5666666507720948,
287
- 0.4783333301544189, 0.7233333110809326, 0.45833334922790525,
288
- 0.4833333492279053, 0.45166668891906736, 0.30166666507720946,
289
- 0.24000000953674316, 0.30166666507720946, 0.6833333492279052,
290
- ];
291
-
292
- const avg = 0.5131403493881227;
293
- // CJK character range: U+3000–U+9FFF (CJK Symbols/Punctuation, Hiragana,
294
- // Katakana, CJK Unified Ideographs incl. Extension A) plus U+FF00–U+FFEF
295
- // (Halfwidth/Fullwidth Forms — fullwidth ASCII, fullwidth punctuation).
296
- const cjkRange = /[\u3000-\u9FFF\uFF00-\uFFEF]/;
297
-
298
- return (
299
- str
300
- .split("")
301
- .map((c) => {
302
- if (cjkRange.test(c) || c === "\u3000") {
303
- // CJK glyphs and U+3000 IDEOGRAPHIC SPACE are full-width by default;
304
- return 1;
305
- }
306
- if (c.charCodeAt(0) < widths.length) {
307
- return widths[c.charCodeAt(0)];
308
- } else {
309
- return avg;
310
- }
311
- })
312
- .reduce((cur, acc) => acc + cur) * fontSize
313
- );
314
- };
315
-
316
- /**
317
- * Split text into the lines it would wrap to when laid out greedily at the
318
- * given font size inside a box of width `maxWidth`. Uses `measureText` so the
319
- * estimate reflects actual font metrics rather than a fixed character count.
320
- * The browser still does the real wrap inside the foreignObject; this is only
321
- * used to size the SVG.
322
- *
323
- * @param {string} text Text to split.
324
- * @param {number} fontSize Font size in px (matches `measureText`).
325
- * @param {number} maxWidth Available wrap width in px.
326
- * @returns {string[]} Estimated wrapped lines.
327
- */
328
- const splitWrappedText = (text, fontSize, maxWidth) => {
329
- if (!text) {
330
- return [];
331
- }
332
-
333
- // Tokenize the text into atoms representing line-break opportunities:
334
- // - ASCII whitespace runs (collapsed/dropped at line edges per CSS rules);
335
- // - non-ASCII whitespaces
336
- // - a single CJK codepoint (browsers break between any two CJK chars,
337
- // punctuation or not, so each one is its own atom and ~1 em wide);
338
- // - a run of non-whitespace non-CJK characters (a "word" that can only
339
- // break at its boundaries, like Latin script).
340
- // Korean Hangul (U+AC00–U+D7AF) is intentionally NOT in the CJK range
341
- // because Korean wraps at word boundaries by default in HTML.
342
- // ASCII whitespace is collapsed to a single space per CSS `white-space: normal;`
343
- text = text.replace(/[\t\n\r ]+/g, " ");
344
- const tokens = text.match(
345
- /\s|[\u3000-\u9FFF\uFF00-\uFFEF]|[^\s\u3000-\u9FFF\uFF00-\uFFEF]+/g,
346
- );
347
- if (!tokens) {
348
- return [];
349
- }
350
-
351
- const takeFittingSegment = (token, availableWidth) => {
352
- const characters = token.split("");
353
- let segment = "";
354
- let width = 0;
355
-
356
- for (const character of characters) {
357
- const characterWidth = measureText(character, fontSize);
358
- if (segment && width + characterWidth > availableWidth) {
359
- break;
360
- }
361
- segment += character;
362
- width += characterWidth;
363
- }
364
-
365
- return {
366
- segment,
367
- width,
368
- };
369
- };
370
-
371
- const lines = [""];
372
- let currentWidth = 0;
373
-
374
- for (const token of tokens) {
375
- if (token === " ") {
376
- // Whitespace at the start of a line is dropped by browsers.
377
- if (currentWidth === 0) {
378
- continue;
379
- }
380
- lines[lines.length - 1] += token;
381
- currentWidth += measureText(token, fontSize);
382
- continue;
383
- }
384
-
385
- let remaining = token;
386
-
387
- while (remaining) {
388
- const w = measureText(remaining, fontSize);
389
- if (currentWidth + w <= maxWidth) {
390
- lines[lines.length - 1] += remaining;
391
- currentWidth += w;
392
- break;
393
- }
394
-
395
- if (currentWidth > 0) {
396
- lines.push("");
397
- currentWidth = 0;
398
- continue;
399
- }
400
-
401
- // An atom wider than the box wraps mid-glyph (overflow-wrap: anywhere).
402
- const { segment, width } = takeFittingSegment(remaining, maxWidth);
403
- lines[lines.length - 1] += segment;
404
- currentWidth = width;
405
- remaining = remaining.slice(segment.length);
406
- }
407
- }
408
-
409
- return lines.map((line) => line.replace(/[\t\n\r ]+$/, ""));
410
- };
411
-
412
- /**
413
- * Estimate how many lines a string will wrap to when laid out greedily at the
414
- * given font size inside a box of width `maxWidth`, capped at `maxLines`.
415
- *
416
- * @param {string} text Text to estimate.
417
- * @param {number} fontSize Font size in px (matches `measureText`).
418
- * @param {number} maxWidth Available wrap width in px.
419
- * @param {number} maxLines Cap on the returned line count.
420
- * @returns {number} Estimated line count, at least 1, at most `maxLines`.
421
- */
422
- const countWrappedLines = (text, fontSize, maxWidth, maxLines) => {
423
- return Math.min(
424
- Math.max(1, splitWrappedText(text, fontSize, maxWidth).length),
425
- maxLines,
426
- );
427
- };
428
-
429
- export {
430
- renderError,
431
- createLanguageNode,
432
- createProgressNode,
433
- iconWithLabel,
434
- flexLayout,
435
- measureText,
436
- splitWrappedText,
437
- countWrappedLines,
438
- wrappedTextNode,
439
- wrappedTextStyles,
440
- };