Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

releaseNotes.ts

125 lines3.8 KB

1import type { DesktopUpdateReleaseNote } from "@t3tools/contracts";
2​
3interface ElectronReleaseNoteInfo {
4 readonly version: string;
5 readonly note: string | null | undefined;
6}
7​
8function isElectronReleaseNoteInfo(value: unknown): value is ElectronReleaseNoteInfo {
9 if (typeof value !== "object" || value === null) return false;
10 const candidate = value as { readonly version?: unknown; readonly note?: unknown };
11 return (
12 typeof candidate.version === "string" &&
13 (typeof candidate.note === "string" || candidate.note === null || candidate.note === undefined)
14 );
16​
17const MAX_RELEASE_NOTE_GROUPS = 6;
18const MAX_RELEASE_NOTE_ITEMS_PER_GROUP = 8;
19const MAX_RELEASE_NOTE_ITEM_LENGTH = 220;
20​
21const HTML_ENTITY_REPLACEMENTS: Readonly<Record<string, string>> = {
22 amp: "&",
23 apos: "'",
24 gt: ">",
25 lt: "<",
26 nbsp: " ",
27 quot: '"',
28};
29​
30function decodeCodePoint(codePoint: number, entity: string): string {
31 // String.fromCodePoint throws RangeError outside the valid Unicode range, and
32 // Number.isFinite alone lets oversized values (e.g. &#9999999999;) through.
33 if (!Number.isInteger(codePoint) || codePoint < 0 || codePoint > 0x10ffff) {
34 return `&${entity};`;
35 }
36 return String.fromCodePoint(codePoint);
38​
39function decodeHtmlEntity(entity: string): string {
40 const named = HTML_ENTITY_REPLACEMENTS[entity];
41 if (named) return named;
42 if (entity.startsWith("#x")) {
43 return decodeCodePoint(Number.parseInt(entity.slice(2), 16), entity);
44 }
45 if (entity.startsWith("#")) {
46 return decodeCodePoint(Number.parseInt(entity.slice(1), 10), entity);
47 }
48 return `&${entity};`;
50​
51function decodeHtmlEntities(input: string): string {
52 return input.replace(/&([a-zA-Z]+|#\d+|#x[0-9a-fA-F]+);/g, (_, entity: string) =>
53 decodeHtmlEntity(entity),
54 );
56​
57function stripMarkup(input: string): string {
58 return decodeHtmlEntities(
59 input
60 .replace(/<br\s*\/?>/gi, "\n")
61 .replace(/<li\b[^>]*>/gi, "\n- ")
62 .replace(/<\/(?:p|div|li|h[1-6]|ul|ol|blockquote)>/gi, "\n")
63 .replace(/<[^>]*>/g, "")
64 .replace(/\[([^\]]+)\]\([^)]+\)/g, "$1")
65 .replace(/\*\*([^*]+)\*\*/g, "$1"),
66 );
68​
69function truncateReleaseNoteItem(item: string): string {
70 if (item.length <= MAX_RELEASE_NOTE_ITEM_LENGTH) return item;
71 return `${item.slice(0, MAX_RELEASE_NOTE_ITEM_LENGTH - 3).trimEnd()}...`;
73​
74function isIgnoredReleaseNoteLine(line: string): boolean {
75 const normalized = line
76 .toLowerCase()
77 .replace(/[*_`#]/g, "")
78 .trim();
79 return (
80 normalized === "" ||
81 normalized === "what's changed" ||
82 normalized === "whats changed" ||
83 normalized === "full changelog" ||
84 normalized === "new contributors" ||
85 normalized.startsWith("compare: ") ||
86 normalized.includes("/compare/")
87 );
89​
90function extractReleaseNoteItems(note: string | null | undefined): ReadonlyArray<string> {
91 if (!note) return [];
92​
93 const items: string[] = [];
94 for (const rawLine of stripMarkup(note).split("\n")) {
95 const item = rawLine
96 .trim()
97 .replace(/^[-*]\s+/, "")
98 .replace(/^\d+[.)]\s+/, "")
99 .replace(/\s+/g, " ");
100 if (isIgnoredReleaseNoteLine(item)) continue;
101 items.push(truncateReleaseNoteItem(item));
102 if (items.length >= MAX_RELEASE_NOTE_ITEMS_PER_GROUP) break;
103 }
104 return items;
106​
107export function normalizeDesktopUpdateReleaseNotes(
108 releaseNotes: unknown,
109 fallbackVersion: string,
110): ReadonlyArray<DesktopUpdateReleaseNote> {
111 const rawNotes =
112 typeof releaseNotes === "string"
113 ? [{ version: fallbackVersion, note: releaseNotes }]
114 : Array.isArray(releaseNotes)
115 ? releaseNotes.filter(isElectronReleaseNoteInfo)
116 : [];
117​
118 return rawNotes
119 .map((entry) => ({
120 version: entry.version,
121 items: extractReleaseNoteItems(entry.note),
122 }))
123 .filter((entry) => entry.items.length > 0)
124 .slice(0, MAX_RELEASE_NOTE_GROUPS);