/** * Simple MIME decoder for compose message box * Handles basic email content and text direction */ export function decodeComposeContent(content: string): string { if (!content) return ''; // Debug logging console.log("Before decode:", content); // Basic HTML cleaning without any string manipulation that could reverse text let cleaned = content // Remove script and style tags .replace(/]*>[\s\S]*?<\/script>/gi, '') .replace(/]*>[\s\S]*?<\/style>/gi, '') // Remove meta tags .replace(/]*>/gi, '') // Remove head and title .replace(/]*>[\s\S]*?<\/head>/gi, '') .replace(/]*>[\s\S]*?<\/title>/gi, '') // Remove body tags .replace(/]*>/gi, '') .replace(/<\/body>/gi, '') // Remove html tags .replace(/]*>/gi, '') .replace(/<\/html>/gi, '') // Handle basic formatting .replace(//gi, '\n') .replace(/]*>/gi, '\n') .replace(/<\/p>/gi, '\n') .replace(/]*>/gi, '\n') .replace(/<\/div>/gi, '\n') // Handle lists .replace(/]*>/gi, '\n') .replace(/<\/ul>/gi, '\n') .replace(/]*>/gi, '\n') .replace(/<\/ol>/gi, '\n') .replace(/]*>/gi, '• ') .replace(/<\/li>/gi, '\n') // Handle basic text formatting .replace(/]*>/gi, '**') .replace(/<\/strong>/gi, '**') .replace(/]*>/gi, '**') .replace(/<\/b>/gi, '**') .replace(/]*>/gi, '*') .replace(/<\/em>/gi, '*') .replace(/]*>/gi, '*') .replace(/<\/i>/gi, '*') // Handle links .replace(/]*href="([^"]*)"[^>]*>(.*?)<\/a>/gi, '$2 ($1)') // Handle basic entities .replace(/ /g, ' ') .replace(/&/g, '&') .replace(/</g, '<') .replace(/>/g, '>') .replace(/"/g, '"') .replace(/'/g, "'") // Clean up whitespace .replace(/\s+/g, ' ') .trim(); // Debug logging console.log("After decode:", cleaned); // Ensure all content has proper direction cleaned = `
${cleaned}
`; return cleaned; } export function encodeComposeContent(content: string): string { if (!content) return ''; // Debug logging console.log("Before encode:", content); // Basic HTML encoding without reversing const encoded = content .replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, '"') .replace(/'/g, ''') .replace(/\n/g, '
'); // Debug logging console.log("After encode:", encoded); return encoded; }