diff --git a/src/components/frontend/journals/JournalsList.tsx b/src/components/frontend/journals/JournalsList.tsx index d67caf06..b29d19e4 100644 --- a/src/components/frontend/journals/JournalsList.tsx +++ b/src/components/frontend/journals/JournalsList.tsx @@ -78,7 +78,9 @@ 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 4174cc78..fe4d7c55 100644 --- a/src/lib/journals/excerpt.ts +++ b/src/lib/journals/excerpt.ts @@ -9,6 +9,9 @@ interface LexicalNodeLike { children?: LexicalNodeLike[]; } +/** Block-level nodes that should break onto their own line in a preview. */ +const BLOCK_NODE_TYPES = new Set(["paragraph", "heading", "listitem", "quote"]); + function collectText(node: LexicalNodeLike, parts: string[], limit: number): boolean { if (node.type === "text" && typeof node.text === "string") { parts.push(node.text); @@ -17,12 +20,17 @@ function collectText(node: LexicalNodeLike, parts: string[], limit: number): boo for (const child of node.children ?? []) { if (collectText(child, parts, limit)) return true; } + // Separate block-level content so paragraphs do not run together. + if (node.type && BLOCK_NODE_TYPES.has(node.type)) { + parts.push("\n"); + } return false; } /** * Flattens the first `maxLength` characters of visible text out of a Lexical - * editor state. Returns "" for empty/invalid input. + * editor state, keeping one line break per block so paragraphs stay legible in + * a card preview. Returns "" for empty/invalid input. */ export function extractExcerpt(body: unknown, maxLength = 180): string { const root = (body as { root?: LexicalNodeLike } | null)?.root; @@ -30,7 +38,12 @@ export function extractExcerpt(body: unknown, maxLength = 180): string { const parts: string[] = []; collectText(root, parts, maxLength); - const text = parts.join("").replace(/\s+/g, " ").trim(); + 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()}…`; } diff --git a/tests/int/journal-contributions.int.spec.ts b/tests/int/journal-contributions.int.spec.ts index e4ca587f..4c69a253 100644 --- a/tests/int/journal-contributions.int.spec.ts +++ b/tests/int/journal-contributions.int.spec.ts @@ -10,7 +10,7 @@ import { afterAll, beforeAll, describe, expect, it, vi, type MockInstance } from import type { Journal, User } from "@/payload-types"; import { createJournal, deleteJournal, updateJournal } from "@/app/(frontend)/journals/actions"; -import { journalBodyLength } from "@/lib/journals/excerpt"; +import { extractExcerpt, journalBodyLength } from "@/lib/journals/excerpt"; import { recordContribution } from "@/lib/contributions"; // The actions read next/headers and call next/cache outside a request scope. @@ -226,11 +226,25 @@ describe("Journal contribution XP", () => { }); }); -describe("journalBodyLength", () => { - it("counts visible text and treats empty/whitespace bodies as zero", () => { +describe("journal text helpers", () => { + it("journalBodyLength counts visible text and treats empty/whitespace bodies as zero", () => { expect(journalBodyLength(journalBody("hello world"))).toBe(11); expect(journalBodyLength(journalBody(" "))).toBe(0); expect(journalBodyLength(null)).toBe(0); expect(journalBodyLength({ root: { children: [] } })).toBe(0); }); + + it("extractExcerpt keeps a line break between paragraphs", () => { + const twoParagraphs = { + root: { + type: "root", + children: [ + { type: "paragraph", children: [{ type: "text", text: "First para." }] }, + { type: "paragraph", children: [{ type: "text", text: "Second para." }] }, + ], + }, + }; + expect(extractExcerpt(twoParagraphs)).toBe("First para.\nSecond para."); + expect(extractExcerpt(null)).toBe(""); + }); });