1
0
Fork 0

fix(journals): preserve paragraph breaks in the card excerpt

This commit is contained in:
Jason Fraley 2026-10-13 14:59:21 -04:00
parent f1634fb335
commit 0a955035a1
3 changed files with 35 additions and 6 deletions

View file

@ -78,7 +78,9 @@ export function JournalsList({ journals, missions }: JournalsListProps) {
{journal.title}
</h2>
{journal.excerpt && (
<p className="text-sm text-muted-foreground line-clamp-3">{journal.excerpt}</p>
<p className="whitespace-pre-line 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,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()}…`;
}

View file

@ -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("");
});
});