From 4c39241d3cf799bf3e44d8480da90e22be3a1f3c Mon Sep 17 00:00:00 2001
From: alma
Date: Wed, 30 Apr 2025 20:32:24 +0200
Subject: [PATCH] courrier formatting
---
components/email/EmailPreview.tsx | 45 +++++++++++-----
lib/utils/email-content.ts | 86 ++++++++++++++++++++++++++++---
2 files changed, 110 insertions(+), 21 deletions(-)
diff --git a/components/email/EmailPreview.tsx b/components/email/EmailPreview.tsx
index fc4bbc9b..2ce03bcf 100644
--- a/components/email/EmailPreview.tsx
+++ b/components/email/EmailPreview.tsx
@@ -222,27 +222,46 @@ export default function EmailPreview({ email, loading = false, onReply }: EmailP
-
+
+ {formattedContent ? (
+
+ ) : (
+
+
This email does not contain any content.
+
+ )}
+
+
+ {process.env.NODE_ENV === 'development' && (
+
+ Email Debug Info
+
+
Email ID: {email.id}
+
Content Type: {
+ typeof email.content === 'object' && email.content?.html
+ ? 'HTML'
+ : 'Plain Text'
+ }
+
Content Size: {
+ typeof email.content === 'object'
+ ? `HTML: ${email.content?.html?.length || 0} chars, Text: ${email.content?.text?.length || 0} chars`
+ : `${typeof email.content === 'string' ? email.content.length : 0} chars`
+ }
+
+
+ )}
diff --git a/lib/utils/email-content.ts b/lib/utils/email-content.ts
index 0c30aa81..b6ceb09a 100644
--- a/lib/utils/email-content.ts
+++ b/lib/utils/email-content.ts
@@ -46,22 +46,50 @@ export function formatEmailContent(email: any): string {
textContent = '';
}
+ // Log what we found for debugging
+ console.log(`Email content detected: isHtml=${isHtml}, contentLength=${content.length}, textLength=${textContent.length}`);
+
// If we have HTML content, sanitize and standardize it
if (isHtml && content) {
+ // Make sure we have a complete HTML structure
+ const hasHtmlTag = content.includes('
- ${sanitizedContent}
+
`;
}
// If we only have text content, format it properly
else if (textContent) {
+ // Check for RTL content and set appropriate direction
+ const rtlLangPattern = /[\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC]/;
+ const containsRtlText = rtlLangPattern.test(textContent);
+ const dirAttribute = containsRtlText ? 'dir="rtl"' : 'dir="ltr"';
+
+ // Escape HTML characters to prevent XSS
+ const escapedText = textContent
+ .replace(/&/g, '&')
+ .replace(//g, '>')
+ .replace(/"/g, '"')
+ .replace(/'/g, ''');
+
// Format plain text with proper line breaks and paragraphs
- const formattedText = textContent
+ const formattedText = escapedText
.replace(/\r\n|\r|\n/g, '
') // Convert all newlines to
.replace(/((?:
){2,})/g, '
-
${formattedText}
+
`;
}
@@ -97,6 +162,11 @@ export function formatEmailContent(email: any): string {
return '
No content available
';
} catch (error) {
console.error('formatEmailContent: Error formatting email content:', error);
- return '
Error displaying email content
';
+ return `
+
+
Error displaying email content
+
${error instanceof Error ? error.message : 'Unknown error'}
+
+ `;
}
}
\ No newline at end of file