Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions src/release/fold.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
16 changes: 15 additions & 1 deletion src/release/fold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string[][]>()
Expand All @@ -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) }
}

Expand Down
56 changes: 56 additions & 0 deletions src/release/fragments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/,
Expand Down
19 changes: 6 additions & 13 deletions src/release/fragments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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<string, string[]>()
const found = new Map<string, string[][]>()
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)]))
}

/**
Expand All @@ -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]
Expand Down