Open History to see the last commit
1import type { DesktopUpdateChannel, DesktopUpdateReleaseNote } from "@t3tools/contracts";
2
3import { resolveDefaultDesktopUpdateChannel } from "./updateChannels.ts";
4
5interface ElectronReleaseNoteInfo {
6 readonly version: string;
7 readonly note: string | null | undefined;
8}
9
10function isElectronReleaseNoteInfo(value: unknown): value is ElectronReleaseNoteInfo {
11 if (typeof value !== "object" || value === null) return false;
12 const candidate = value as { readonly version?: unknown; readonly note?: unknown };
13 return (
14 typeof candidate.version === "string" &&
15 (typeof candidate.note === "string" || candidate.note === null || candidate.note === undefined)
16 );
17}
18
19const MAX_RELEASE_NOTE_GROUPS = 6;
20const MAX_RELEASE_NOTE_ITEMS_PER_GROUP = 8;
21const MAX_RELEASE_NOTE_ITEM_LENGTH = 220;
22
23const HTML_ENTITY_REPLACEMENTS: Readonly<Record<string, string>> = {
24 amp: "&",
25 apos: "'",
26 gt: ">",
27 lt: "<",
28 nbsp: " ",
29 quot: '"',
30};
31
32function decodeCodePoint(codePoint: number, entity: string): string {
33 if (!Number.isInteger(codePoint) || codePoint < 0 || codePoint > 0x10ffff) {
34 return `&${entity};`;
35 }
36 return String.fromCodePoint(codePoint);
37}
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};`;
49}
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 );
55}
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(/<h([1-6])\b[^>]*>/gi, (_, level: string) => `\n${"#".repeat(Number(level))} `)
63 .replace(/<\/(?:p|div|li|h[1-6]|ul|ol|blockquote)>/gi, "\n")
64 .replace(/<[^>]*>/g, "")
65 .replace(/\[([^\]]+)\]\([^)]+\)/g, "$1")
66 .replace(/\*\*([^*]+)\*\*/g, "$1"),
67 );
68}
69
70function truncateReleaseNoteItem(item: string): string {
71 if (item.length <= MAX_RELEASE_NOTE_ITEM_LENGTH) return item;
72 return `${item.slice(0, MAX_RELEASE_NOTE_ITEM_LENGTH - 3).trimEnd()}...`;
73}
74
75function normalizeReleaseNoteLine(line: string): string {
76 return line
77 .toLowerCase()
78 .replace(/[*_`#]/g, "")
79 .trim();
80}
81
82function isIgnoredReleaseNoteLine(line: string): boolean {
83 const normalized = normalizeReleaseNoteLine(line);
84 return (
85 normalized === "" ||
86 normalized === "what's changed" ||
87 normalized === "whats changed" ||
88 normalized.startsWith("compare: ") ||
89 normalized.includes("/compare/")
90 );
91}
92
93interface ExtractedReleaseNoteItems {
94 readonly items: ReadonlyArray<string>;
95 readonly totalItems: number;
96}
97
98function extractReleaseNoteItems(note: string | null | undefined): ExtractedReleaseNoteItems {
99 if (!note) return { items: [], totalItems: 0 };
100
101 const items: string[] = [];
102 let totalItems = 0;
103 for (const rawLine of stripMarkup(note).split("\n")) {
104 const item = rawLine
105 .trim()
106 .replace(/^[-*]\s+/, "")
107 .replace(/^\d+[.)]\s+/, "")
108 .replace(/\s+/g, " ");
109 const normalized = normalizeReleaseNoteLine(item);
110 if (normalized === "new contributors" || normalized === "full changelog") break;
111 if (/^#{1,6}\s+/.test(item)) continue;
112 if (isIgnoredReleaseNoteLine(item)) continue;
113 totalItems += 1;
114 items.push(truncateReleaseNoteItem(item));
115 if (items.length > MAX_RELEASE_NOTE_ITEMS_PER_GROUP) items.shift();
116 }
117 return { items: items.toReversed(), totalItems };
118}
119
120interface NormalizedDesktopUpdateReleaseNotes {
121 readonly releaseNotes: ReadonlyArray<DesktopUpdateReleaseNote>;
122 readonly omittedReleaseCount: number;
123}
124
125/** fullChangelog includes all greater semvers, so preview cuts sort above nightlies of the same base version. */
126export function normalizeDesktopUpdateReleaseNotes(
127 releaseNotes: unknown,
128 fallbackVersion: string,
129 channel: DesktopUpdateChannel,
130): NormalizedDesktopUpdateReleaseNotes {
131 const rawNotes = (
132 typeof releaseNotes === "string"
133 ? [{ version: fallbackVersion, note: releaseNotes }]
134 : Array.isArray(releaseNotes)
135 ? releaseNotes.filter(isElectronReleaseNoteInfo)
136 : []
137 ).filter((entry) => resolveDefaultDesktopUpdateChannel(entry.version) === channel);
138
139 const normalizedNotes = rawNotes.flatMap((entry) => {
140 const { items, totalItems } = extractReleaseNoteItems(entry.note);
141 if (totalItems === 0) return [];
142 return [
143 {
144 version: entry.version,
145 items,
146 totalItems,
147 },
148 ];
149 });
150
151 return {
152 releaseNotes: normalizedNotes.slice(0, MAX_RELEASE_NOTE_GROUPS),
153 omittedReleaseCount: Math.max(0, normalizedNotes.length - MAX_RELEASE_NOTE_GROUPS),
154 };
155}