diff --git a/lib/text-highlight.ts b/lib/text-highlight.ts index 44714b66..21f528b1 100644 --- a/lib/text-highlight.ts +++ b/lib/text-highlight.ts @@ -78,9 +78,14 @@ export function extractMatchedText( fragment: { prefix: string; suffix: string; length: number }, paragraphs: string[], ): string | null { - // Strip HTML tags to get plain text + // Strip HTML tags and entities to get plain text. The entity regex must be + // strict — `&[^;]+;` would greedily pair a stray `&` (e.g. "24 & 25 Oct") + // with a far-away `;` (e.g. "my beloved son;"), eating everything between + // them. Only match real entities: named (&), decimal ('), hex ('). const plainParagraphs = paragraphs.map((p) => - p.replace(/<[^>]*>/g, "").replace(/&[^;]+;/g, " "), + p + .replace(/<[^>]*>/g, "") + .replace(/&(?:[a-zA-Z][a-zA-Z0-9]*|#\d+|#x[0-9a-fA-F]+);/g, " "), ); // Build a single string with paragraph breaks