/** * Converts plain text with newline separators into HTML paragraphs. * If the text already contains HTML block elements, returns as-is. */ export function formatTextToHtml(text: string): string { if (!text) return ""; // If already contains HTML block elements, return as-is if (/<(?:p|div|h[1-6]|ul|ol|blockquote)\b/i.test(text)) { return text; } return text .split(/\n\n+/) .filter((p) => p.trim()) .map((p) => `
${p.trim().replace(/\n/g, "
")}