import type { Email } from "@/lib/email-data" import { cleanSenderName } from "@/lib/sender-display" function escapeHtml(s: string): string { return s .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """) } type PrintSegment = { fromName: string fromEmail: string date: string bodyHtml: string } function buildSegments(email: Email): PrintSegment[] { const conv = email.conversation ?? [] const segments: PrintSegment[] = [] for (const msg of conv) { segments.push({ fromName: cleanSenderName(msg.sender), fromEmail: msg.senderEmail, date: msg.date, bodyHtml: msg.body, }) } const mainName = cleanSenderName(email.sender) const mainEmail = email.senderEmail || `${mainName.toLowerCase().replace(/\s+/g, ".")}@example.com` segments.push({ fromName: mainName, fromEmail: mainEmail, date: email.date, bodyHtml: email.body ?? `
${escapeHtml(email.preview)}
`, }) return segments } function buildPrintHtml(email: Email): string { const subject = escapeHtml(email.subject) const segments = buildSegments(email) const blocks = segments .map( (seg) => `