/** * CENTRAL EMAIL FORMATTER * * This is the primary and only email formatting utility to be used. * All email formatting should go through here to ensure consistent * handling of text direction and HTML sanitization. */ import DOMPurify from 'isomorphic-dompurify'; // Configure DOMPurify to preserve direction attributes DOMPurify.addHook('afterSanitizeAttributes', function(node) { // Preserve direction attributes if (node.hasAttribute('dir')) { node.setAttribute('dir', node.getAttribute('dir') || 'ltr'); } // Preserve text-align in styles if (node.hasAttribute('style')) { const style = node.getAttribute('style') || ''; if (style.includes('text-align')) { // Keep existing alignment } else if (node.hasAttribute('dir') && node.getAttribute('dir') === 'rtl') { node.setAttribute('style', style + '; text-align: right;'); } } }); // Interface definitions export interface EmailAddress { name: string; address: string; } export interface EmailMessage { id: string; messageId?: string; subject: string; from: EmailAddress[]; to: EmailAddress[]; cc?: EmailAddress[]; bcc?: EmailAddress[]; date: Date | string; flags?: { seen: boolean; flagged: boolean; answered: boolean; deleted: boolean; draft: boolean; }; preview?: string; content?: string; html?: string; text?: string; hasAttachments?: boolean; attachments?: any[]; folder?: string; size?: number; contentFetched?: boolean; } /** * Format email addresses for display */ export function formatEmailAddresses(addresses: EmailAddress[]): string { if (!addresses || addresses.length === 0) return ''; return addresses.map(addr => addr.name && addr.name !== addr.address ? `${addr.name} <${addr.address}>` : addr.address ).join(', '); } /** * Format date for display */ export function formatEmailDate(date: Date | string | undefined): string { if (!date) return ''; try { const dateObj = typeof date === 'string' ? new Date(date) : date; return dateObj.toLocaleString('en-US', { weekday: 'short', year: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' }); } catch (e) { return typeof date === 'string' ? date : date.toString(); } } /** * Sanitize HTML content before processing or displaying * @param content HTML content to sanitize * @returns Sanitized HTML */ export function sanitizeHtml(content: string): string { if (!content) return ''; // Sanitize the HTML using our configured DOMPurify return DOMPurify.sanitize(content); } /** * Format an email for forwarding - CENTRAL IMPLEMENTATION * All other formatting functions should be deprecated in favor of this one */ export function formatForwardedEmail(email: EmailMessage): { subject: string; content: string; } { // Format subject with Fwd: prefix if needed const subjectBase = email.subject || '(No subject)'; const subject = subjectBase.match(/^(Fwd|FW|Forward):/i) ? subjectBase : `Fwd: ${subjectBase}`; // Get sender and recipient information const fromString = formatEmailAddresses(email.from || []); const toString = formatEmailAddresses(email.to || []); const dateString = formatEmailDate(email.date); // Get and sanitize original content const originalContent = sanitizeHtml(email.content || email.html || email.text || ''); // Check if the content already has a forwarded message header const hasExistingHeader = originalContent.includes('---------- Forwarded message ---------'); // If there's already a forwarded message header, don't add another one if (hasExistingHeader) { // Just wrap the content in appropriate styling without adding another header const content = `