diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d8819b..4a58f50 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,10 @@ Every entry says what changed as a caller sees it, why, and what to watch for - **`next-version` keeps a taken major a major**: with 2.0.0 on npm, `2.0.0` gives `3.0.0`, not `2.1.0`. - **A pre-release is published under npm's `next` tag**, from `bin/release` and from the release workflow. npm refuses a pre-release without a tag, and `latest` must stay a release. +- **A release that collects `changes/` fragments or folds Unreleased no longer leaves a blank line between entries.** + A blank line inside a fragment, or a `###` heading that appears twice in the dated section, used to split the list + in two (ydisk 0.3.0's Fixed) or leave two blank lines in a row. Each heading now gets one list, with one blank line + around the heading. A blank line inside an entry, before its indented second paragraph, stays. ## 0.132.0 — 23.11.2026 diff --git a/src/release/fold.test.ts b/src/release/fold.test.ts index 2dc3087..feb61fb 100644 --- a/src/release/fold.test.ts +++ b/src/release/fold.test.ts @@ -87,6 +87,14 @@ Intro. ) }) + it("leaves no blank line between entries when the dated section ends the file", () => { + const loose = + "# Changelog\n\n## Unreleased\n\n### Fixed\n\n- A.\n\n- B.\n\n\n## 0.9.0 — 14.10.2026\n\n### Fixed\n\n- C.\n" + expect(foldUnreleased(loose, "0.9.0")).toBe( + "# Changelog\n\n## 0.9.0 — 14.10.2026\n\n### Fixed\n\n- A.\n- B.\n- C.\n", + ) + }) + it("merges headings that differ only by trailing spaces", () => { expect(foldUnreleased(changelog.replace("### Fixed\n\n- Older", "### Fixed \n\n- Older"), "0.9.0")).toBe( foldUnreleased(changelog, "0.9.0"), diff --git a/src/release/fold.ts b/src/release/fold.ts index b3c639f..d876c05 100644 --- a/src/release/fold.ts +++ b/src/release/fold.ts @@ -8,6 +8,20 @@ const trimBlank = (lines: string[]) => { return lines.slice(start, end) } +/** + * The entries under one `###` heading, from each place it appears: one list, a blank line only inside an entry. + * A blank line before `- ` would split it into a loose list, as ydisk 0.3.0's Fixed shows. + */ +export const joinBodies = (bodies: string[][]) => + bodies.flatMap(trimBlank).filter( + (line, index, lines) => + line.trim() || + !lines + .slice(index + 1) + .find((next) => next.trim()) + ?.startsWith("- "), + ) + const parseSection = (block: string): Section => { const [title = "", ...lines] = block.split("\n") const chunks = new Map() @@ -20,7 +34,7 @@ const parseSection = (block: string): Section => { } else if (current) current.push(line) else if (line.trim()) throw new Error(`"${title}" has text before its first ### heading: "${line}"`) } - const headings = new Map([...chunks].map(([heading, bodies]) => [heading, bodies.flatMap(trimBlank)])) + const headings = new Map([...chunks].map(([heading, bodies]) => [heading, joinBodies(bodies)])) return { title, headings, repeated: [...chunks.values()].some((bodies) => bodies.length > 1) } } diff --git a/src/release/fragments.test.ts b/src/release/fragments.test.ts index c7946a4..93fbfc9 100644 --- a/src/release/fragments.test.ts +++ b/src/release/fragments.test.ts @@ -63,6 +63,62 @@ describe("collectFragments", () => { ) }) + it("keeps one blank line around each heading and none between entries", () => { + const dated = [ + "# Changelog", + "", + "## 0.3.0 — 19.11.2026", + "", + "### Fixed", + "", + "- Own.", + "", + "### Fixed", + "", + "- Applied under the dated heading by git.", + "", + "## 0.2.0 — 18.11.2026", + "", + "### Added", + "", + "- Shipped.", + "", + ].join("\n") + const fragments = [ + { name: "a.md", text: "### Added\n\n- A.\n\n\n- B,\n\n in two paragraphs.\n\n" }, + { name: "b.md", text: "\n### Fixed\n\n- One.\n- Two.\n\n- The blank line above was ydisk 0.3.0's.\n" }, + ] + expect(collectFragments(dated, "0.3.0", fragments, rules)).toBe( + [ + "# Changelog", + "", + "## 0.3.0 — 19.11.2026", + "", + "### Added", + "", + "- A.", + "- B,", + "", + " in two paragraphs.", + "", + "### Fixed", + "", + "- Own.", + "- Applied under the dated heading by git.", + "- One.", + "- Two.", + "- The blank line above was ydisk 0.3.0's.", + "", + "## 0.2.0 — 18.11.2026", + "", + "### Added", + "", + "- Shipped.", + "", + ].join("\n"), + ) + }) + it("refuses a version without a dated section", () => { expect(() => collectFragments(changelog, "1.4.0", [{ name: "a.md", text: "### Fixed\n\n- a\n" }], rules)).toThrow( /no "## 1\.4\.0 — " section/, diff --git a/src/release/fragments.ts b/src/release/fragments.ts index 6a9a11b..ac955e3 100644 --- a/src/release/fragments.ts +++ b/src/release/fragments.ts @@ -5,6 +5,7 @@ */ import { existsSync, readdirSync, readFileSync } from "node:fs" import { join } from "node:path" +import { joinBodies } from "./fold.js" import { idPattern } from "./markdown.js" export type Fragment = { name: string; text: string } @@ -62,26 +63,18 @@ export const fragmentProblems = ({ name, text }: Fragment, { headings, ids }: Fr return problems } -const trimBlank = (lines: string[]) => { - let start = 0 - let end = lines.length - while (start < end && !lines[start]?.trim()) start++ - while (end > start && !lines[end - 1]?.trim()) end-- - return lines.slice(start, end) -} - const byHeading = (lines: string[], where: string) => { - const found = new Map() + const found = new Map() let current: string[] | undefined for (const line of lines) { if (line.startsWith("### ")) { const heading = line.slice(4).trim() - current = found.get(heading) ?? [] - found.set(heading, current) + current = [] + found.set(heading, [...(found.get(heading) ?? []), current]) } else if (current) current.push(line) else if (line.trim()) throw new Error(`${where} has text before its first ### heading: "${line}"`) } - return new Map([...found].map(([heading, body]) => [heading, trimBlank(body)])) + return new Map([...found].map(([heading, bodies]) => [heading, joinBodies(bodies)])) } /** @@ -103,7 +96,7 @@ export const collectFragments = ( for (const fragment of fragments) { const text = fragment.text.replace(/\]\(\.\.\//g, "](") for (const [heading, body] of byHeading(text.split("\n"), `changes/${fragment.name}`)) - merged.set(heading, [...(merged.get(heading) ?? []), ...body]) + merged.set(heading, joinBodies([merged.get(heading) ?? [], body])) } const rank = (heading: string) => (headings.includes(heading) ? headings.indexOf(heading) : headings.length) const body = [...merged]