fix(person-mention): truncate notes excerpt at last word boundary

Leonie FINDING-04 + Elicit E5: notes.slice(0, 120) cuts mid-word, especially
ugly in German compound nouns ("…Familienzu…"). Sara #7: the assertion
.toBeLessThanOrEqual(122) was a magic number that hid this bug.

Add truncateAtWordBoundary(text, max): cut at the last space inside the
window unless it'd shrink the excerpt below 70% (single-word fallback).
Single-word case still produces hard-cut + ellipsis so a 150-char word
shows the first 120 chars + … rather than nothing.

Tests pinned to exact strings.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Marcel
2026-04-29 09:01:39 +02:00
parent 6dd60571e3
commit 558e1e6b22
2 changed files with 50 additions and 5 deletions

View File

@@ -34,12 +34,29 @@ const dateRange = $derived(
: ''
);
/**
* Cut the notes excerpt at the last word boundary inside the NOTES_MAX
* window. Mid-word truncation is especially ugly in German compound nouns
* ("…Familienzu…"), so prefer the previous space if there is one within
* a reasonable distance. Fall back to a hard cut for strings with no
* spaces at all (e.g. a single 150-char word). Leonie FINDING-04 / Elicit E5.
*/
function truncateAtWordBoundary(text: string, max: number): string {
if (text.length <= max) return text;
const window = text.slice(0, max);
const lastSpace = window.lastIndexOf(' ');
// If the last space is too close to the start (< 70% of the window) we'd
// produce a near-empty excerpt — fall back to the hard cut instead.
const minBoundary = Math.floor(max * 0.7);
const cut = lastSpace >= minBoundary ? window.slice(0, lastSpace) : window;
return cut + '…';
}
const notesExcerpt = $derived.by(() => {
if (state.status !== 'loaded') return null;
const notes = state.person.notes;
if (!notes) return null;
if (notes.length <= NOTES_MAX) return notes;
return notes.slice(0, NOTES_MAX) + '…';
return truncateAtWordBoundary(notes, NOTES_MAX);
});
// Accessible name for the region landmark — required by WCAG 1.3.1.