1
0
Fork 0

fix(journals): cut the card excerpt at the first paragraph

This commit is contained in:
Jason Fraley 2026-10-13 15:02:23 -04:00
parent 0a955035a1
commit 420bc06f84
3 changed files with 26 additions and 19 deletions

View file

@ -78,9 +78,7 @@ export function JournalsList({ journals, missions }: JournalsListProps) {
{journal.title}
</h2>
{journal.excerpt && (
<p className="whitespace-pre-line text-sm text-muted-foreground line-clamp-3">
{journal.excerpt}
</p>
<p className="text-sm text-muted-foreground line-clamp-3">{journal.excerpt}</p>
)}
<div className="mt-auto flex items-center gap-2 pt-2 text-xs text-muted-foreground">
<span className="truncate">{journal.authorName}</span>

View file

@ -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}…`;
}
/**

View file

@ -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)}…`);
});
});