返回 CodeWhale
changelog-lib.mjs
根目录 / web / scripts / changelog-lib.mjs
1 /**
2 * changelog-lib.mjs — parse the repository CHANGELOG.md into the compact,
3 * deterministic shape the website renders (`lib/changelog.generated.ts`).
4 *
5 * Keep a Changelog format: `## [version] - date` (or `## [Unreleased]`),
6 * `### Section` headings, and `- ` bullets that may continue on indented
7 * lines. Compare links live at the bottom as `[version]: url`.
8 *
9 * Pure and dependency-free so the derive script, the drift test, and any
10 * future check can share one parser.
11 */
12
13 const DEFAULT_LIMIT = 6;
14 // Most Keep-a-Changelog bullets in this repository run one to three
15 // sentences; 480 chars keeps the great majority readable in place, and the
16 // per-release "Full notes" link on /changelog carries the rest.
17 const DEFAULT_ITEMS_PER_SECTION = 12;
18 const DEFAULT_ITEM_CHARS = 480;
19
20 /** Collapse Markdown emphasis and links to plain text for a one-line summary. */
21 export function plainText(markdown) {
22 return markdown
23 .replace(/\[([^\]]+)\]\([^)]*\)/g, "$1")
24 .replace(/`([^`]*)`/g, "$1")
25 .replace(/\*\*([^*]+)\*\*/g, "$1")
26 .replace(/\s+/g, " ")
27 .trim();
28 }
29
30 /** Truncate at a word boundary; never mid-word, never past `max` chars. */
31 export function clip(text, max = DEFAULT_ITEM_CHARS) {
32 if (text.length <= max) return text;
33 const cut = text.lastIndexOf(" ", max - 1);
34 return `${text.slice(0, cut > max / 2 ? cut : max - 1).trimEnd()}…`;
35 }
36
37 /**
38 * Parse a CHANGELOG.md string.
39 *
40 * @returns {{ releases: Array<{version: string, date: string | null, unreleased: boolean, compareUrl: string | null, sections: Array<{heading: string, items: string[], itemCount: number}>}> }}
41 */
42 export function parseChangelog(markdown, options = {}) {
43 const limit = options.limit ?? DEFAULT_LIMIT;
44 const itemsPerSection = options.itemsPerSection ?? DEFAULT_ITEMS_PER_SECTION;
45 const itemChars = options.itemChars ?? DEFAULT_ITEM_CHARS;
46
47 const lines = markdown.split(/\r?\n/);
48 const links = new Map();
49 for (const line of lines) {
50 const m = line.match(/^\[([^\]]+)\]:\s*(\S+)\s*$/);
51 if (m) links.set(m[1], m[2]);
52 }
53
54 const releases = [];
55 let release = null;
56 let section = null;
57 let item = null;
58
59 const flushItem = () => {
60 if (section && item !== null) {
61 section.raw.push(plainText(item));
62 }
63 item = null;
64 };
65
66 for (const line of lines) {
67 const heading = line.match(/^## \[([^\]]+)\](?:\s*-\s*(.+))?\s*$/);
68 if (heading) {
69 flushItem();
70 section = null;
71 const label = heading[1].trim();
72 const unreleased = /^unreleased$/i.test(label);
73 const dateText = heading[2]?.trim() ?? null;
74 release = {
75 version: label,
76 date: dateText && /^\d{4}-\d{2}-\d{2}$/.test(dateText) ? dateText : null,
77 unreleased,
78 compareUrl: links.get(label) ?? null,
79 sections: [],
80 };
81 releases.push(release);
82 continue;
83 }
84 if (!release) continue;
85
86 const sub = line.match(/^### (.+?)\s*$/);
87 if (sub) {
88 flushItem();
89 section = { heading: sub[1], raw: [] };
90 release.sections.push(section);
91 continue;
92 }
93 const bullet = line.match(/^- (.*)$/);
94 if (bullet && !section) {
95 section = { heading: "Changes", raw: [] };
96 release.sections.push(section);
97 }
98 if (!section) continue;
99
100 if (bullet) {
101 flushItem();
102 item = bullet[1];
103 continue;
104 }
105 if (item !== null && /^\s{2,}\S/.test(line)) {
106 item += ` ${line.trim()}`;
107 continue;
108 }
109 if (item !== null && line.trim() === "") {
110 flushItem();
111 }
112 }
113 flushItem();
114
115 return {
116 releases: releases.slice(0, limit).map((r) => ({
117 version: r.version,
118 date: r.date,
119 unreleased: r.unreleased,
120 compareUrl: r.compareUrl,
121 sections: r.sections
122 .filter((s) => s.raw.length > 0)
123 .map((s) => ({
124 heading: s.heading,
125 items: s.raw.slice(0, itemsPerSection).map((t) => clip(t, itemChars)),
126 itemCount: s.raw.length,
127 })),
128 })),
129 };
130 }
131
132 /**
133 * The fragment GitHub's Markdown renderer assigns to a release heading such
134 * as `## [0.9.11] - 2026-08-22`: lower-cased, punctuation other than hyphens
135 * dropped, spaces turned to hyphens — so `0911---2026-08-22`. Lets the site
136 * deep-link a version's full notes instead of the top of a 470 KB file.
137 */
138 export function changelogAnchor(release) {
139 const heading = release.unreleased
140 ? "Unreleased"
141 : release.date
142 ? `[${release.version}] - ${release.date}`
143 : `[${release.version}]`;
144 return heading
145 .toLowerCase()
146 .replace(/[^\p{L}\p{N} _-]/gu, "")
147 .replace(/ /g, "-");
148 }
149
150 export function parseReleaseNotes(markdown) {
151 return parseChangelog(markdown, {
152 limit: Infinity,
153 itemsPerSection: Infinity,
154 itemChars: Infinity,
155 }).releases.filter((release) => !release.unreleased);
156 }
157
158 export function renderReleaseNotesModule(releases, sourcePath = "CHANGELOG.md") {
159 return `// AUTO-GENERATED by web/scripts/derive-changelog.mjs at prebuild from ${sourcePath}.
160 // DO NOT EDIT — re-run \`npm run prebuild\` (or just \`npm run build\`) after changing the changelog.
161 // Not tracked in git: every released version with every entry, for /changelog/<version>.
162
163 import type { ChangelogRelease } from "./changelog.generated";
164
165 export const RELEASE_NOTES: ChangelogRelease[] = ${JSON.stringify(releases)};
166 `;
167 }
168
169 /** Render the generated TypeScript module from a parse result. */
170 export function renderChangelogModule(parsed, sourcePath = "CHANGELOG.md") {
171 return `// AUTO-GENERATED by web/scripts/derive-changelog.mjs at prebuild from ${sourcePath}.
172 // DO NOT EDIT — re-run \`npm run prebuild\` (or just \`npm run build\`) after changing the changelog.
173 // Not tracked in git: derived at build and test time so it can never lag a merged CHANGELOG.md.
174
175 export interface ChangelogSection {
176 heading: string;
177 /** Plain-text entries, clipped for the web; \`itemCount\` is the full count. */
178 items: string[];
179 itemCount: number;
180 }
181
182 export interface ChangelogRelease {
183 /** "Unreleased" or a semantic version such as "0.9.11". */
184 version: string;
185 /** ISO date from the heading, or null for the unreleased lane. */
186 date: string | null;
187 unreleased: boolean;
188 /** The changelog's own compare link for this version, when it has one. */
189 compareUrl: string | null;
190 sections: ChangelogSection[];
191 }
192
193 export const CHANGELOG: ChangelogRelease[] = ${JSON.stringify(parsed.releases, null, 2)};
194 `;
195 }
196
196 lines Plain Text