commit d8137dfef34d3506f2138319cfc4c1cd69d312e4
parent a7e11bd21458300f4eeb86672acf8850e40c1e20
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date: Wed, 18 Nov 2026 04:17:25 -0500
common: changelogPreview never cuts inside a fenced block or ends on a bare heading
A line starting with a dash inside a fenced block is code, not a bullet:
cutting there left the fence open over everything after it. And when the
bullet that does not fit is the first under a sub-heading, the heading
was left as the preview's last line. Both pinned.
Co-Authored-By: Claude <noreply@anthropic.com>
Diffstat:
2 files changed, 91 insertions(+), 6 deletions(-)
diff --git a/common/lib/changelog.test.ts b/common/lib/changelog.test.ts
@@ -382,3 +382,61 @@ test("changelogPreview is the top of the newest entry, cut on a bullet", () => {
assert.equal(changelogPreview(one, 6000), one);
assert.equal(changelogPreview("", 6000), "");
});
+
+test("changelogPreview never cuts inside a fenced block", () => {
+ const filler = "words ".repeat(40);
+ const fenced = [
+ "## [Unreleased]",
+ `- **First.** ${filler}`,
+ " ```diff",
+ "- a removed line that starts like a bullet",
+ "+ an added line",
+ `- another removed line ${filler}`,
+ " ```",
+ `- **Second.** ${filler}`,
+ `- **Third.** ${filler}`,
+ "",
+ ].join("\n");
+ const source = `# Changelog\n\n${fenced}`;
+ // A budget that ends inside the fence: the only places to cut are before
+ // Second and before Third, so the whole first bullet, fence closed, is kept.
+ const insideFence = source.indexOf("another removed line");
+ const cut = changelogPreview(source, insideFence);
+ assert.ok(cut.includes("- another removed line"), "the fenced lines are kept whole");
+ assert.ok(cut.trimEnd().endsWith("```"), cut.slice(-40));
+ assert.ok(!cut.includes("Second."));
+ assert.equal(cut.split("```").length - 1, 2, "the fence is closed");
+ // The same with a tilde fence, and a fence char that differs inside it.
+ const tilde = source.replaceAll(" ```diff", "~~~").replace(" ```\n- **Second", "~~~\n- **Second");
+ const cutTilde = changelogPreview(tilde, tilde.indexOf("another removed line"));
+ assert.ok(cutTilde.trimEnd().endsWith("~~~"), cutTilde.slice(-40));
+});
+
+test("changelogPreview never ends on a heading with nothing under it", () => {
+ const filler = "words ".repeat(40);
+ const source = [
+ "# Changelog",
+ "",
+ "## [Unreleased]",
+ "### Added",
+ `- **One.** ${filler}`,
+ `- **Two.** ${filler}`,
+ "",
+ "### Fixed",
+ `- **Three.** ${filler}`,
+ `- **Four.** ${filler}`,
+ "",
+ ].join("\n");
+ // The budget ends just past the start of Three: the cut is before Three,
+ // which would leave "### Fixed" as the last line.
+ const cut = changelogPreview(source, source.indexOf("**Three.**") + 5);
+ assert.ok(cut.includes("**Two.**"));
+ assert.ok(!cut.includes("Fixed"), cut.slice(-60));
+ assert.ok(!cut.includes("Three"));
+ assert.ok(cut.endsWith(`- **Two.** ${filler}`.trimEnd() + "\n"), JSON.stringify(cut.slice(-30)));
+ // A sub-heading WITH a bullet under it stays.
+ assert.ok(cut.includes("### Added\n- **One.**"));
+ const more = changelogPreview(source, source.indexOf("**Four.**") + 5);
+ assert.ok(more.includes("### Fixed\n- **Three.**"));
+ assert.ok(!more.includes("Four"));
+});
diff --git a/common/lib/changelog.ts b/common/lib/changelog.ts
@@ -31,19 +31,46 @@ export function sliceTopEntries(source: string, limit: number): string {
// heading and as many of its top-level bullets as fit in `maxChars`, cut ON a
// bullet boundary so no bullet is ever shown half-written — and always at
// least the first bullet, however long. An entry that fits is returned whole.
+//
+// Two things a cut never does:
+// - land inside a fenced code block: a line starting `- ` between two fence
+// lines is code (a diff, a list in an example), not a bullet, and cutting
+// there would leave the fence open over everything after it;
+// - end on a heading with nothing under it: when the next bullet that does
+// not fit is the first under a `### …` sub-heading, the heading goes too.
export function changelogPreview(source: string, maxChars = 6000): string {
const top = sliceTopEntries(source, 1);
if (top.length <= maxChars) return top;
- // Top-level bullets only: a nested bullet is indented.
+ // Where each top-level bullet starts (a nested bullet is indented), outside
+ // any fenced block.
const starts: number[] = [];
- for (const m of top.matchAll(/^[-*] /gm)) starts.push(m.index ?? 0);
+ let fence: string | null = null;
+ let at = 0;
+ for (const line of top.split("\n")) {
+ const mark = /^\s*(`{3,}|~{3,})/.exec(line)?.[1];
+ if (mark) {
+ if (fence === null) fence = mark[0];
+ else if (mark[0] === fence) fence = null;
+ } else if (fence === null && /^[-*] /.test(line)) {
+ starts.push(at);
+ }
+ at += line.length + 1;
+ }
if (starts.length < 2) return top;
let cut = starts[1];
- for (const at of starts.slice(1)) {
- if (at > maxChars) break;
- cut = at;
+ for (const start of starts.slice(1)) {
+ if (start > maxChars) break;
+ cut = start;
+ }
+ let kept = top.slice(0, cut).trimEnd();
+ // A sub-heading left with nothing under it. Never the entry's own `## `
+ // heading: the first bullet is always kept, so that one is never last.
+ for (;;) {
+ const lastLine = kept.slice(kept.lastIndexOf("\n") + 1);
+ if (!/^#{3,}\s/.test(lastLine)) break;
+ kept = kept.slice(0, kept.length - lastLine.length).trimEnd();
}
- return top.slice(0, cut).trimEnd() + "\n";
+ return kept + "\n";
}
export function getLatestReleasedVersion(source: string): string | null {