From 420bc06f842eb203e7534640039bab208888c5d5 Mon Sep 17 00:00:00 2001 From: Z8MB1E Date: Tue, 13 Oct 2026 15:02:23 -0400 Subject: [PATCH] fix(journals): cut the card excerpt at the first paragraph --- .../frontend/journals/JournalsList.tsx | 4 +-- src/lib/journals/excerpt.ts | 31 ++++++++++--------- tests/int/journal-contributions.int.spec.ts | 10 ++++-- 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/components/frontend/journals/JournalsList.tsx b/src/components/frontend/journals/JournalsList.tsx index b29d19e4..d67caf06 100644 --- a/src/components/frontend/journals/JournalsList.tsx +++ b/src/components/frontend/journals/JournalsList.tsx @@ -78,9 +78,7 @@ export function JournalsList({ journals, missions }: JournalsListProps) { {journal.title} {journal.excerpt && ( -

- {journal.excerpt} -

+

{journal.excerpt}

)}
{journal.authorName} diff --git a/src/lib/journals/excerpt.ts b/src/lib/journals/excerpt.ts index fe4d7c55..4a6ddb79 100644 --- a/src/lib/journals/excerpt.ts +++ b/src/lib/journals/excerpt.ts @@ -9,7 +9,7 @@ interface LexicalNodeLike { children?: LexicalNodeLike[]; } -/** Block-level nodes that should break onto their own line in a preview. */ +/** Block-level nodes that separate one another in the flattened text. */ const BLOCK_NODE_TYPES = new Set(["paragraph", "heading", "listitem", "quote"]); function collectText(node: LexicalNodeLike, parts: string[], limit: number): boolean { @@ -17,10 +17,14 @@ function collectText(node: LexicalNodeLike, parts: string[], limit: number): boo parts.push(node.text); return parts.join("").length >= limit; } + if (node.type === "linebreak") { + parts.push(" "); + return false; + } for (const child of node.children ?? []) { if (collectText(child, parts, limit)) return true; } - // Separate block-level content so paragraphs do not run together. + // A line break between blocks, so the first one can be found reliably. if (node.type && BLOCK_NODE_TYPES.has(node.type)) { parts.push("\n"); } @@ -28,24 +32,23 @@ function collectText(node: LexicalNodeLike, parts: string[], limit: number): boo } /** - * Flattens the first `maxLength` characters of visible text out of a Lexical - * editor state, keeping one line break per block so paragraphs stay legible in - * a card preview. Returns "" for empty/invalid input. + * Flatten a Lexical body into a one-line card summary: the text up to the first + * block break or `maxLength` characters, whichever comes first, with an + * ellipsis when anything was left out. Returns "" for empty/invalid input. */ export function extractExcerpt(body: unknown, maxLength = 180): string { const root = (body as { root?: LexicalNodeLike } | null)?.root; if (!root || !Array.isArray(root.children)) return ""; const parts: string[] = []; - collectText(root, parts, maxLength); - const text = parts - .join("") - .split("\n") - .map((line) => line.replace(/\s+/g, " ").trim()) - .filter(Boolean) - .join("\n"); - if (text.length <= maxLength) return text; - return `${text.slice(0, maxLength - 1).trimEnd()}…`; + collectText(root, parts, Number.MAX_SAFE_INTEGER); + const full = parts.join("").trim(); + + const firstBreak = full.indexOf("\n"); + const cut = firstBreak >= 0 && firstBreak < maxLength ? firstBreak : maxLength; + const text = full.slice(0, cut).replace(/\s+/g, " ").trim(); + if (full.length <= cut) return text; + return `${text}…`; } /** diff --git a/tests/int/journal-contributions.int.spec.ts b/tests/int/journal-contributions.int.spec.ts index 4c69a253..84fa6590 100644 --- a/tests/int/journal-contributions.int.spec.ts +++ b/tests/int/journal-contributions.int.spec.ts @@ -234,7 +234,7 @@ describe("journal text helpers", () => { expect(journalBodyLength({ root: { children: [] } })).toBe(0); }); - it("extractExcerpt keeps a line break between paragraphs", () => { + it("extractExcerpt stops at the first block break with an ellipsis", () => { const twoParagraphs = { root: { type: "root", @@ -244,7 +244,13 @@ describe("journal text helpers", () => { ], }, }; - expect(extractExcerpt(twoParagraphs)).toBe("First para.\nSecond para."); + expect(extractExcerpt(twoParagraphs)).toBe("First para.…"); + expect(extractExcerpt(journalBody("hello world"))).toBe("hello world"); expect(extractExcerpt(null)).toBe(""); }); + + it("extractExcerpt truncates a long single block at the limit", () => { + const out = extractExcerpt(journalBody("x".repeat(300)), 180); + expect(out).toBe(`${"x".repeat(180)}…`); + }); });