diff --git a/app/mail/page.tsx b/app/mail/page.tsx index 1304ded..fbe77f7 100644 --- a/app/mail/page.tsx +++ b/app/mail/page.tsx @@ -319,7 +319,15 @@ function convertCharset(text: string, fromCharset: string): string { .replace(/\xC3\x80/g, 'À') .replace(/\xC3\x89/g, 'É') .replace(/\xC3\x87/g, 'Ç') - .replace(/\xC2\xA0/g, ' '); + // Clean up HTML entities + .replace(/ç/g, 'ç') + .replace(/é/g, 'é') + .replace(/è/g, 'ë') + .replace(/ê/g, 'ª') + .replace(/ë/g, '«') + .replace(/û/g, '»') + .replace(/ /g, ' ') + .replace(/\xA0/g, ' '); } return text; @@ -1306,44 +1314,100 @@ export default function MailPage() { }; const getReplyBody = () => { - const date = new Date(selectedEmail.date); - const formattedDate = date.toLocaleString(); - - // Parse the original email content - const parsed = parseFullEmail(selectedEmail.body); - let originalContent = ''; - - // Get the content from either HTML or text part - if (parsed.html) { - // Convert HTML to plain text for the reply - originalContent = parsed.html - .replace(/]*>[\s\S]*?<\/style>/gi, '') // Remove style tags - .replace(/]*>[\s\S]*?<\/script>/gi, '') // Remove script tags - .replace(/<[^>]+>/g, '') // Remove HTML tags - .replace(/ |‌|»|«|>/g, ' ') // Convert HTML entities to spaces - .replace(/^\s+$/gm, '') // Remove lines with only whitespace - .replace(/\n{3,}/g, '\n\n') // Normalize multiple line breaks - .trim(); - } else if (parsed.text) { - originalContent = parsed.text.trim(); - } else { - // Fallback to raw body if parsing fails - originalContent = selectedEmail.body - .replace(/<[^>]+>/g, '') + try { + const parsed = parseFullEmail(selectedEmail.body); + let originalContent = ''; + + // Get the content from either HTML or text part + if (parsed.html) { + // Convert HTML to plain text for the reply + originalContent = parsed.html + .replace(/]*>[\s\S]*?<\/style>/gi, '') + .replace(/]*>[\s\S]*?<\/script>/gi, '') + .replace(//gi, '\n') + .replace(/]*>/gi, '\n') + .replace(/<\/div>/gi, '') + .replace(/]*>/gi, '\n') + .replace(/<\/p>/gi, '') + .replace(/<[^>]+>/g, '') + .replace(/ |‌|»|«|>|<|&/g, match => { + switch (match) { + case ' ': return ' '; + case '‌': return ''; + case '»': return '»'; + case '«': return '«'; + case '>': return '>'; + case '<': return '<'; + case '&': return '&'; + default: return match; + } + }) + .replace(/^\s+$/gm, '') + .replace(/\n{3,}/g, '\n\n') + .trim(); + } else if (parsed.text) { + originalContent = parsed.text.trim(); + } else { + // Fallback to raw body if parsing fails + originalContent = selectedEmail.body + .replace(/<[^>]+>/g, '') + .trim(); + } + + // Clean up the content + originalContent = originalContent + .split('\n') + .map(line => line.trim()) + .filter(line => { + // Remove email headers and common footer markers + return !line.match(/^(From|To|Sent|Subject|Date|Cc|Bcc):/i) && + !line.match(/^-{2,}/) && + !line.match(/^_{2,}/) && + !line.match(/^={2,}/) && + !line.match(/^This (email|message) has been/i) && + !line.match(/^Disclaimer/i) && + !line.match(/^[*_-]{3,}/); + }) + .join('\n') .trim(); + + // Format the reply + const date = new Date(selectedEmail.date); + const formattedDate = date.toLocaleString('en-GB', { + day: '2-digit', + month: '2-digit', + year: 'numeric', + hour: '2-digit', + minute: '2-digit', + hour12: false + }); + + let replyHeader = ''; + if (type === 'forward') { + replyHeader = `\n\n---------- Forwarded message ----------\n`; + replyHeader += `From: ${selectedEmail.from}\n`; + replyHeader += `Date: ${formattedDate}\n`; + replyHeader += `Subject: ${selectedEmail.subject}\n`; + replyHeader += `To: ${selectedEmail.to}\n`; + if (selectedEmail.cc) { + replyHeader += `Cc: ${selectedEmail.cc}\n`; + } + replyHeader += `\n`; + } else { + replyHeader = `\n\nOn ${formattedDate}, ${selectedEmail.from} wrote:\n`; + } + + // Indent the original content + const indentedContent = originalContent + .split('\n') + .map(line => `> ${line}`) + .join('\n'); + + return `${replyHeader}${indentedContent}`; + } catch (error) { + console.error('Error formatting reply:', error); + return `\n\nOn ${new Date(selectedEmail.date).toLocaleString()}, ${selectedEmail.from} wrote:\n> ${selectedEmail.body}`; } - - // Format the reply - const separator = '\n\n------------ Original Message ------------\n'; - const header = `On ${formattedDate}, ${selectedEmail.from} wrote:\n`; - - // Indent the original content - const indentedContent = originalContent - .split('\n') - .map(line => `> ${line}`) - .join('\n'); - - return `${separator}${header}${indentedContent}`; }; // Open compose modal with reply details