/** * Strip HTML tags from a string and return the plain text. * Uses DOMParser in the browser, falls back to a regex strip on the server * (where DOMParser is not available without isomorphic-dompurify's JSDOM). */ export function stripHtml(html: string | null | undefined): string { if (!html) return ''; if (typeof DOMParser === 'function') { const doc = new DOMParser().parseFromString(html, 'text/html'); return (doc.body.textContent ?? '').trim(); } return html.replace(/<[^>]*>/g, '').trim(); } /** * Strip HTML and truncate to a maximum length, appending an ellipsis when * the source exceeds it. Used for editorial story excerpts. */ export function plainExcerpt(html: string | null | undefined, max = 80): string { const text = stripHtml(html); if (text.length <= max) return text; return text.slice(0, max).replace(/\s+\S*$/, '') + '…'; }