feat(changelog): render section headers as Discord markdown headings
This commit is contained in:
parent
bf50dd99a9
commit
6df285e2e8
3 changed files with 44 additions and 18 deletions
|
|
@ -59,7 +59,7 @@ bot/
|
|||
|
||||
## Feature flow: changelogs
|
||||
|
||||
`/changelog` (guild-only, staff only) posts a `CHANGELOG.md` release entry to the releases channel (`DISCORD_CHANGELOG_CHANNEL_ID`, falling back to `DISCORD_ANNOUNCE_CHANNEL_ID`). `/changelog` targets the newest entry; `/changelog v0.4.0` targets a specific version (for catching up on missed posts); `/changelog v0.3.0..v0.5.0` targets a range (inclusive slice, output oldest-to-newest). Either range end is open (`v0.3.0..` to the latest, `..v0.5.0` from the oldest) and a reversed range is normalized. Rendering lives in `bot/lib/changelogRender.ts` (pure, no Discord/Payload deps): entries are split on `# vX.Y.Z` headers, `### ` sections become `**bold**`, bullets become `•`, and entries over Discord's 4096-char limit are split across messages at section boundaries. The command reads `CHANGELOG.md` from the repo root relative to the file (works from the deploy server) and posts each version as its own amber embed, the first message of each version carrying the `Changelog vX.Y.Z` title and multi-message versions adding a page footer. Ranges spanning more than `WIDE_RANGE_THRESHOLD` (10) versions post an ephemeral warning but proceed. Idempotency ("don't repost") is intentionally out of scope for the manual command; the same rendering can be reused by a future automatic trigger.
|
||||
`/changelog` (guild-only, staff only) posts a `CHANGELOG.md` release entry to the releases channel (`DISCORD_CHANGELOG_CHANNEL_ID`, falling back to `DISCORD_ANNOUNCE_CHANNEL_ID`). `/changelog` targets the newest entry; `/changelog v0.4.0` targets a specific version (for catching up on missed posts); `/changelog v0.3.0..v0.5.0` targets a range (inclusive slice, output oldest-to-newest). Either range end is open (`v0.3.0..` to the latest, `..v0.5.0` from the oldest) and a reversed range is normalized. Rendering lives in `bot/lib/changelogRender.ts` (pure, no Discord/Payload deps): entries are split on `# vX.Y.Z` headers, `### ` sections become Discord markdown headings (`### `, the deepest level Discord renders), bullets become `•`, and entries over Discord's 4096-char limit are split across messages at section boundaries. The command reads `CHANGELOG.md` from the repo root relative to the file (works from the deploy server) and posts each version as its own amber embed, the first message of each version carrying the `Changelog vX.Y.Z` title and multi-message versions adding a page footer. Ranges spanning more than `WIDE_RANGE_THRESHOLD` (10) versions post an ephemeral warning but proceed. Idempotency ("don't repost") is intentionally out of scope for the manual command; the same rendering can be reused by a future automatic trigger.
|
||||
|
||||
## Feature flow: attendance
|
||||
|
||||
|
|
|
|||
|
|
@ -3,11 +3,13 @@
|
|||
* messages. No Discord or Payload dependencies, so the logic is trivially
|
||||
* testable in isolation.
|
||||
*
|
||||
* Discord renders `**bold**`, *italic*, `code`, [links](url) and :emoji:, but
|
||||
* NOT markdown headings or list markers. Section headers therefore become bold
|
||||
* and list bullets become the "•" glyph. Entries longer than Discord's message
|
||||
* limit are split into multiple messages at section/bullet boundaries so a
|
||||
* chunk never lands mid-item.
|
||||
* Discord renders `**bold**`, *italic*, `code`, [links](url) and :emoji:, plus
|
||||
* markdown headings and lists inside embed descriptions (its renderer stops at
|
||||
* `###`, and a heading needs a blank line before it, which the block join
|
||||
* supplies). Section headers therefore become real markdown headings and list
|
||||
* bullets become the "•" glyph. Entries longer than Discord's message limit are
|
||||
* split into multiple messages at section/bullet boundaries so a chunk never
|
||||
* lands mid-item.
|
||||
*/
|
||||
|
||||
/** Discord allows 4096 chars per message and per embed description; leave headroom. */
|
||||
|
|
@ -29,6 +31,15 @@ const HORIZONTAL_RULE = /^\s*-{3,}\s*$/;
|
|||
/** The fallback rendered as a list item, matching how Fixes footnotes look. */
|
||||
const FALLBACK_LINE = `- ${EMPTY_SECTION_FALLBACK}`;
|
||||
|
||||
/**
|
||||
* Discord's renderer supports headings only up to `###`; a section header is
|
||||
* rendered at that level (the version itself is the embed title). Change this
|
||||
* one constant to `## ` for a larger heading.
|
||||
*/
|
||||
const SECTION_HEADER_PREFIX = "### ";
|
||||
|
||||
const sectionHeader = (header: string): string => `${SECTION_HEADER_PREFIX}${header}`;
|
||||
|
||||
/** A parsed `# vX.Y.Z` entry: the version and the raw markdown body under it. */
|
||||
export interface ChangelogEntry {
|
||||
version: string;
|
||||
|
|
@ -88,6 +99,13 @@ function intoBlocks(rawBody: string): Block[] {
|
|||
if (HORIZONTAL_RULE.test(line)) continue;
|
||||
const section = line.match(SECTION_HEADER);
|
||||
if (section) {
|
||||
// A single-hash heading is the entry's own "# vX.Y.Z" title line, which
|
||||
// the embed title already carries. parseChangelog excludes it from the
|
||||
// body it hands over; drop it here too so a raw body renders the same.
|
||||
if (section[1] === "#") {
|
||||
current = null;
|
||||
continue;
|
||||
}
|
||||
current = { header: section[2].trim(), lines: [] };
|
||||
blocks.push(current);
|
||||
continue;
|
||||
|
|
@ -135,7 +153,7 @@ const formatLine = (raw: string): string => {
|
|||
|
||||
const blockToString = (block: Block): string => {
|
||||
const lines: string[] = [];
|
||||
if (block.header !== null) lines.push(`**${block.header}**`);
|
||||
if (block.header !== null) lines.push(sectionHeader(block.header));
|
||||
for (const line of block.lines) lines.push(formatLine(line));
|
||||
return lines.join("\n");
|
||||
};
|
||||
|
|
@ -176,7 +194,7 @@ export function splitForDiscord(rawBody: string): string[] {
|
|||
let current = "";
|
||||
for (const block of intoBlocks(rawBody)) {
|
||||
const blockLines: string[] = [];
|
||||
if (block.header !== null) blockLines.push(`**${block.header}**`);
|
||||
if (block.header !== null) blockLines.push(sectionHeader(block.header));
|
||||
for (const line of block.lines) blockLines.push(formatLine(line));
|
||||
if (blockLines.length === 0) continue;
|
||||
|
||||
|
|
@ -197,7 +215,7 @@ export function splitForDiscord(rawBody: string): string[] {
|
|||
continue;
|
||||
}
|
||||
|
||||
const headerPrefix = block.header !== null ? `**${block.header}**` : null;
|
||||
const headerPrefix = block.header !== null ? sectionHeader(block.header) : null;
|
||||
// Reserve room for the re-attached header + separator on continuation chunks.
|
||||
const headroom = headerPrefix ? headerPrefix.length + "\n\n".length : 0;
|
||||
const blockChunks = splitLinesToMessages(blockLines, MAX_MESSAGE_CHARS - headroom);
|
||||
|
|
|
|||
|
|
@ -131,12 +131,13 @@ describe("resolveChangelogRange", () => {
|
|||
});
|
||||
|
||||
describe("formatEntryDiscord", () => {
|
||||
it("turns section headers bold and bullets into the bullet glyph", () => {
|
||||
it("turns section headers into markdown headings and bullets into the bullet glyph", () => {
|
||||
const formatted = formatEntryDiscord(SAMPLE);
|
||||
expect(formatted).toContain("**Features**");
|
||||
expect(formatted).toContain("**Fixes**");
|
||||
expect(formatted).toContain("### Features");
|
||||
expect(formatted).toContain("### Fixes");
|
||||
expect(formatted).toContain("• **Feature one:** does a thing.");
|
||||
expect(formatted).not.toContain("# ");
|
||||
// The entry's own "# vX.Y.Z" version header stays out (the embed title has it).
|
||||
expect(formatted).not.toContain("# v0.4.0");
|
||||
});
|
||||
|
||||
it("preserves inline markdown", () => {
|
||||
|
|
@ -164,8 +165,8 @@ describe("splitForDiscord", () => {
|
|||
.flatMap((message) => message.split("\n"))
|
||||
.filter((line) => line.startsWith("• "));
|
||||
expect(bulletsInMessages).toHaveLength(300);
|
||||
// The first chunk leads with the section header.
|
||||
expect(messages[0]).toContain("**Features**");
|
||||
// The first chunk leads with the section heading.
|
||||
expect(messages[0]).toContain("### Features");
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -189,14 +190,14 @@ describe("empty-section fallback", () => {
|
|||
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("### 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("### Improvements");
|
||||
expect(formatted).toContain(`• ${EMPTY_SECTION_FALLBACK}`);
|
||||
expect(formatted).toContain("• A real fix.");
|
||||
});
|
||||
|
|
@ -214,6 +215,13 @@ describe("empty-section fallback", () => {
|
|||
expect(formatted).not.toContain(EMPTY_SECTION_FALLBACK);
|
||||
});
|
||||
|
||||
it("drops a raw body's own # version line (the embed title carries it)", () => {
|
||||
const formatted = formatEntryDiscord("# v9.9.9\n\nA summary.\n\n### Fixes\n\n- A fix.\n");
|
||||
expect(formatted).not.toContain("v9.9.9");
|
||||
expect(formatted).toContain("A summary.");
|
||||
expect(formatted).toContain("### Fixes");
|
||||
});
|
||||
|
||||
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");
|
||||
|
|
@ -238,7 +246,7 @@ describe("real CHANGELOG.md", () => {
|
|||
const v040 = resolveChangelog(content, "0.4.0");
|
||||
expect(v040.found).toBe(true);
|
||||
expect(v040.messages.length).toBeGreaterThan(0);
|
||||
expect(v040.messages[0]).toContain("**Features**");
|
||||
expect(v040.messages[0]).toContain("### Features");
|
||||
expect(v040.messages[0]).toContain("• **Mission planning maps**:");
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue