1
0
Fork 0

feat(changelog): fall back to a generic line for empty entries and sections

This commit is contained in:
Jason Fraley 2026-10-11 16:40:44 -04:00
parent 0ef3da018e
commit 88d1d1f6f1
2 changed files with 88 additions and 4 deletions

View file

@ -13,6 +13,22 @@
/** Discord allows 4096 chars per message and per embed description; leave headroom. */
const MAX_MESSAGE_CHARS = 4000;
/**
* Shown in place of an empty release entry or an empty section, so a post never
* renders as a bare heading (or a stray separator). Players read it as "there
* were other changes we did not detail".
*/
export const EMPTY_SECTION_FALLBACK = "Various bug fixes and improvements.";
/** Section the fallback goes under when an entire entry has no content. */
const FALLBACK_SECTION_HEADER = "Fixes";
/** The `---` separator CHANGELOG.md puts between entries: file formatting, never content. */
const HORIZONTAL_RULE = /^\s*-{3,}\s*$/;
/** The fallback rendered as a list item, matching how Fixes footnotes look. */
const FALLBACK_LINE = `- ${EMPTY_SECTION_FALLBACK}`;
/** A parsed `# vX.Y.Z` entry: the version and the raw markdown body under it. */
export interface ChangelogEntry {
version: string;
@ -68,6 +84,8 @@ function intoBlocks(rawBody: string): Block[] {
const blocks: Block[] = [];
let current: Block | null = null;
for (const line of rawBody.split("\n")) {
// Entry separators are file formatting; they are not player-facing content.
if (HORIZONTAL_RULE.test(line)) continue;
const section = line.match(SECTION_HEADER);
if (section) {
current = { header: section[2].trim(), lines: [] };
@ -88,10 +106,21 @@ function intoBlocks(rawBody: string): Block[] {
while (start < end && /^\s*$/.test(block.lines[start])) start++;
while (end > start && /^\s*$/.test(block.lines[end - 1])) end--;
const lines = block.lines.slice(start, end);
// Drop a leading/trailing all-blank intro block; keep section blocks even
// if empty so their bold header still renders.
if (lines.length === 0 && block.header === null) continue;
result.push({ header: block.header, lines });
if (block.header === null) {
// Drop a leading/trailing all-blank intro block.
if (lines.length === 0) continue;
result.push({ header: null, lines });
continue;
}
// Keep every section, falling back to the generic line when it has no
// items, so a post never shows a heading with nothing under it.
result.push({ header: block.header, lines: lines.length > 0 ? lines : [FALLBACK_LINE] });
}
// An entry with no content at all still tells players something shipped.
if (result.length === 0) {
return [{ header: FALLBACK_SECTION_HEADER, lines: [FALLBACK_LINE] }];
}
return result;
}

View file

@ -3,6 +3,7 @@ import path from "node:path";
import { fileURLToPath } from "node:url";
import { describe, expect, it } from "vitest";
import {
EMPTY_SECTION_FALLBACK,
compareVersions,
formatEntryDiscord,
parseChangelog,
@ -168,6 +169,60 @@ describe("splitForDiscord", () => {
});
});
describe("empty-section fallback", () => {
it("renders a fully empty entry as a Fixes section carrying the generic line", () => {
const content = [
"# Changelog",
"",
"# v0.8.3",
"",
"---",
"",
"# v0.8.2",
"",
"### Fixes",
"",
"- A real fix.",
"",
].join("\n");
const resolved = resolveChangelog(content, "0.8.3");
expect(resolved.found).toBe(true);
expect(resolved.messages).toHaveLength(1);
expect(resolved.messages[0]).toContain("**Fixes**");
expect(resolved.messages[0]).toContain(EMPTY_SECTION_FALLBACK);
expect(resolved.messages[0]).not.toContain("---");
});
it("fills a section heading that has no items, leaving real sections alone", () => {
const formatted = formatEntryDiscord("### Improvements\n\n### Fixes\n\n- A real fix.\n");
expect(formatted).toContain("**Improvements**");
expect(formatted).toContain(`• ${EMPTY_SECTION_FALLBACK}`);
expect(formatted).toContain("• A real fix.");
});
it("does not invent content for entries that already have some", () => {
const formatted = formatEntryDiscord(SAMPLE);
expect(formatted).not.toContain(EMPTY_SECTION_FALLBACK);
// Entry separators never leak into the post.
expect(formatted).not.toContain("---");
});
it("keeps a real summary paragraph even without sections", () => {
const formatted = formatEntryDiscord("Just a summary sentence.\n");
expect(formatted).toBe("Just a summary sentence.");
expect(formatted).not.toContain(EMPTY_SECTION_FALLBACK);
});
it("renders the shipped empty v0.8.3 entry from the real file", () => {
const content = readFileSync(CHANGELOG_PATH, "utf8");
const resolved = resolveChangelog(content, "0.8.3");
expect(resolved.found).toBe(true);
expect(resolved.messages[0]).toContain(EMPTY_SECTION_FALLBACK);
expect(resolved.messages[0]).not.toContain("---");
});
});
describe("real CHANGELOG.md", () => {
it("parses and renders the shipped entries", () => {
const content = readFileSync(CHANGELOG_PATH, "utf8");