|
| 1 | +export type HighlightMedia = |
| 2 | + | { type: "video"; src: string } |
| 3 | + | { type: "image"; src: string; width: string; height: string } |
| 4 | + |
| 5 | +export type HighlightItem = { |
| 6 | + title: string |
| 7 | + description: string |
| 8 | + shortDescription?: string |
| 9 | + media?: HighlightMedia |
| 10 | +} |
| 11 | + |
| 12 | +export type HighlightGroup = { |
| 13 | + source: string |
| 14 | + items: HighlightItem[] |
| 15 | +} |
| 16 | + |
| 17 | +export type ChangelogSection = { title: string; items: string[] } |
| 18 | + |
| 19 | +export type ChangelogRelease = { |
| 20 | + tag: string |
| 21 | + name: string |
| 22 | + date: string |
| 23 | + url: string |
| 24 | + highlights: HighlightGroup[] |
| 25 | + sections: ChangelogSection[] |
| 26 | +} |
| 27 | + |
| 28 | +export type ChangelogData = { |
| 29 | + ok: boolean |
| 30 | + releases: ChangelogRelease[] |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Parses one GitHub release body into the highlight groups and detail sections the feed carries. V2 notes open with a |
| 35 | + * `### Highlights` bullet list and group details under `### <Product>` sections; older notes use `<highlight |
| 36 | + * source="…">` HTML blocks and `## <Product>` sections. Both shapes are kept, as the feed still serves old releases. |
| 37 | + */ |
| 38 | +export function parseReleaseBody(body: string): { highlights: HighlightGroup[]; sections: ChangelogSection[] } { |
| 39 | + const htmlHighlights = parseHtmlHighlights(body) |
| 40 | + const { highlights, sections } = parseMarkdown(body) |
| 41 | + |
| 42 | + const groups = [...htmlHighlights] |
| 43 | + |
| 44 | + // V2 notes carry no <highlight> HTML: their `### Highlights` bullets are what the desktop app shows. |
| 45 | + if (htmlHighlights.length === 0) groups.push({ source: "Desktop", items: highlights }) |
| 46 | + |
| 47 | + const hasDesktop = groups.some( |
| 48 | + (group) => group.source.toLowerCase().includes("desktop") && group.items.length > 0, |
| 49 | + ) |
| 50 | + |
| 51 | + // Backfill from the Desktop section when nothing names Desktop, so a release whose Highlights bullets could not be |
| 52 | + // attributed to Desktop still shows its Desktop bullets in What's New. |
| 53 | + if (!hasDesktop) { |
| 54 | + const desktop = sections.find((section) => section.title.toLowerCase() === "desktop") |
| 55 | + |
| 56 | + if (desktop) groups.push({ source: "Desktop", items: desktop.items.flatMap(sectionHighlight) }) |
| 57 | + } |
| 58 | + |
| 59 | + return { highlights: mergeGroups(groups), sections } |
| 60 | +} |
| 61 | + |
| 62 | +/** Splits a `- **Title**: description.` detail bullet into a highlight when it carries both parts. */ |
| 63 | +function sectionHighlight(item: string): HighlightItem[] { |
| 64 | + const match = /^\*\*(.+?)\*\*:?\s*(.*)$/.exec(item.trim()) |
| 65 | + |
| 66 | + if (!match) return [] |
| 67 | + |
| 68 | + const title = inlineText(match[1].replace(/:$/, "")) |
| 69 | + const description = stripTrailingReferences(inlineText(match[2])) |
| 70 | + |
| 71 | + if (!title || !description) return [] |
| 72 | + |
| 73 | + return [{ title, description }] |
| 74 | +} |
| 75 | + |
| 76 | +function mergeGroups(groups: HighlightGroup[]): HighlightGroup[] { |
| 77 | + const merged = new Map<string, HighlightItem[]>() |
| 78 | + |
| 79 | + for (const group of groups) { |
| 80 | + if (!group.items.length) continue |
| 81 | + |
| 82 | + const items = merged.get(group.source) ?? [] |
| 83 | + items.push(...group.items) |
| 84 | + merged.set(group.source, items) |
| 85 | + } |
| 86 | + |
| 87 | + return Array.from(merged, ([source, items]) => ({ source, items: dedupeItems(items) })) |
| 88 | +} |
| 89 | + |
| 90 | +function dedupeItems(items: HighlightItem[]): HighlightItem[] { |
| 91 | + const seen = new Set<string>() |
| 92 | + |
| 93 | + return items.filter((item) => { |
| 94 | + const key = `${item.title}\n${item.description}\n${item.media?.src ?? ""}` |
| 95 | + |
| 96 | + if (seen.has(key)) return false |
| 97 | + |
| 98 | + seen.add(key) |
| 99 | + |
| 100 | + return true |
| 101 | + }) |
| 102 | +} |
| 103 | + |
| 104 | +function parseHtmlHighlights(body: string): HighlightGroup[] { |
| 105 | + const groups = new Map<string, HighlightItem[]>() |
| 106 | + const regex = /<highlight\s+source="([^"]+)">([\s\S]*?)<\/highlight>/g |
| 107 | + let match |
| 108 | + |
| 109 | + while ((match = regex.exec(body)) !== null) { |
| 110 | + const source = match[1] |
| 111 | + const content = match[2] |
| 112 | + |
| 113 | + const titleMatch = content.match(/<h2>([^<]+)<\/h2>/) |
| 114 | + const pMatch = content.match(/<p(?:\s+short="([^"]*)")?>([^<]+)<\/p>/) |
| 115 | + const imgMatch = content.match(/<img\s+width="([^"]+)"\s+height="([^"]+)"\s+alt="[^"]*"\s+src="([^"]+)"/) |
| 116 | + const videoMatch = content.match(/^\s*(https:\/\/github\.com\/user-attachments\/assets\/[a-f0-9-]+)\s*$/m) |
| 117 | + |
| 118 | + const media = (() => { |
| 119 | + if (videoMatch) return { type: "video", src: videoMatch[1] } satisfies HighlightMedia |
| 120 | + if (imgMatch) { |
| 121 | + return { |
| 122 | + type: "image", |
| 123 | + src: imgMatch[3], |
| 124 | + width: imgMatch[1], |
| 125 | + height: imgMatch[2], |
| 126 | + } satisfies HighlightMedia |
| 127 | + } |
| 128 | + })() |
| 129 | + |
| 130 | + if (!titleMatch || !media) continue |
| 131 | + |
| 132 | + const item: HighlightItem = { |
| 133 | + title: titleMatch[1], |
| 134 | + description: pMatch?.[2] || "", |
| 135 | + shortDescription: pMatch?.[1], |
| 136 | + media, |
| 137 | + } |
| 138 | + |
| 139 | + if (!groups.has(source)) groups.set(source, []) |
| 140 | + groups.get(source)!.push(item) |
| 141 | + } |
| 142 | + |
| 143 | + return Array.from(groups.entries()).map(([source, items]) => ({ source, items })) |
| 144 | +} |
| 145 | + |
| 146 | +/** |
| 147 | + * Parses the Markdown portions of a release body. V2 notes use `### Highlights` and `### <Product>` headings; old |
| 148 | + * notes use `## <Product>` headings, so any heading from level two down starts a block. |
| 149 | + */ |
| 150 | +function parseMarkdown(body: string): { highlights: HighlightItem[]; sections: ChangelogSection[] } { |
| 151 | + const lines = body.replace(/\r\n?/g, "\n").split("\n") |
| 152 | + const blocks: { level: number; title: string; items: string[] }[] = [] |
| 153 | + let current: (typeof blocks)[number] | undefined |
| 154 | + let skip = false |
| 155 | + |
| 156 | + for (const line of lines) { |
| 157 | + const heading = /^(#{2,3})\s+(.+?)\s*$/.exec(line) |
| 158 | + |
| 159 | + if (heading) { |
| 160 | + current = { level: heading[1].length, title: inlineText(heading[2]), items: [] } |
| 161 | + blocks.push(current) |
| 162 | + skip = false |
| 163 | + continue |
| 164 | + } |
| 165 | + |
| 166 | + if (line.startsWith("**Thank you")) { |
| 167 | + skip = true |
| 168 | + continue |
| 169 | + } |
| 170 | + |
| 171 | + if (/^[-*]\s+/.test(line) && !skip) current?.items.push(line.replace(/^[-*]\s+/, "").trim()) |
| 172 | + } |
| 173 | + |
| 174 | + const highlights = blocks |
| 175 | + .filter((block) => block.level === 3 && block.title.toLowerCase() === "highlights") |
| 176 | + .flatMap((block) => block.items.flatMap(parseHighlightBullet)) |
| 177 | + const sections = blocks |
| 178 | + .filter((block) => !(block.level === 3 && block.title.toLowerCase() === "highlights")) |
| 179 | + .map((block) => ({ title: block.title, items: block.items })) |
| 180 | + |
| 181 | + return { highlights, sections } |
| 182 | +} |
| 183 | + |
| 184 | +/** Reads one `### Highlights` bullet as `- **Title**: One or two sentences. ([#1](https://…))`. */ |
| 185 | +function parseHighlightBullet(item: string): HighlightItem[] { |
| 186 | + const match = /^\*\*(.+?)\*\*:?\s*([\s\S]*)$/.exec(item.trim()) |
| 187 | + |
| 188 | + if (!match) return [] |
| 189 | + |
| 190 | + const title = inlineText(match[1].replace(/:$/, "")) |
| 191 | + const description = stripTrailingReferences(inlineText(match[2])) |
| 192 | + |
| 193 | + if (!title || !description) return [] |
| 194 | + |
| 195 | + return [{ title, description }] |
| 196 | +} |
| 197 | + |
| 198 | +/** Reduces inline Markdown to plain text, keeping the words behind links, images and emphasis. */ |
| 199 | +function inlineText(value: string) { |
| 200 | + return value |
| 201 | + .replace(/!\[([^\]]*)\]\([^)]*\)/g, "$1") |
| 202 | + .replace(/\[([^\]]+)\]\([^)]+\)/g, "$1") |
| 203 | + .replace(/`([^`]+)`/g, "$1") |
| 204 | + .replace(/\*\*([^*]+)\*\*/g, "$1") |
| 205 | + .replace(/\s+/g, " ") |
| 206 | + .trim() |
| 207 | +} |
| 208 | + |
| 209 | +/** Removes a trailing pull request reference such as `([#1](https://…), [#2](https://…))`. */ |
| 210 | +function stripTrailingReferences(value: string) { |
| 211 | + const reference = /(?:\[[^\]]*\]\([^)]*\)|<[^>]+>|#\d+)/ |
| 212 | + |
| 213 | + return value.replace( |
| 214 | + new RegExp(`\\s*\\(\\s*${reference.source}(?:\\s*,\\s*${reference.source})*\\s*\\)\\s*$`), |
| 215 | + "", |
| 216 | + ) |
| 217 | +} |
0 commit comments