diff --git a/lib/utils/email-content.ts b/lib/utils/email-content.ts
index 8c06fc09..dac643c4 100644
--- a/lib/utils/email-content.ts
+++ b/lib/utils/email-content.ts
@@ -213,7 +213,7 @@ export function processHtmlContent(htmlContent: string, textContent?: string): s
// Use the centralized sanitizeHtml function
let sanitizedContent = sanitizeHtml(htmlContent);
- // Fix URL encoding issues
+ // Fix URL encoding issues and clean up content
try {
if (typeof window !== 'undefined' && typeof document !== 'undefined') {
// Temporary element to manipulate the HTML
@@ -261,11 +261,45 @@ export function processHtmlContent(htmlContent: string, textContent?: string): s
}
});
+ // Clean up excessive whitespace and empty elements
+ // Find all text nodes and normalize whitespace
+ const walker = document.createTreeWalker(
+ tempDiv,
+ NodeFilter.SHOW_TEXT,
+ null
+ );
+
+ const textNodes = [];
+ while (walker.nextNode()) {
+ textNodes.push(walker.currentNode);
+ }
+
+ // Process text nodes to normalize whitespace
+ textNodes.forEach(node => {
+ if (node.nodeValue) {
+ // Replace sequences of whitespace with a single space
+ node.nodeValue = node.nodeValue.replace(/\s+/g, ' ').trim();
+ }
+ });
+
+ // Remove empty paragraphs and divs that contain only whitespace
+ const emptyElements = tempDiv.querySelectorAll('p, div, span');
+ emptyElements.forEach(el => {
+ if (el.innerHTML.trim() === '' || el.innerHTML === ' ') {
+ el.parentNode?.removeChild(el);
+ }
+ });
+
+ // Remove excessive consecutive
tags (more than 2)
+ let html = tempDiv.innerHTML;
+ html = html.replace(/(
\s*){3,}/gi, '
');
+ tempDiv.innerHTML = html;
+
// Get the fixed HTML
sanitizedContent = tempDiv.innerHTML;
}
} catch (e) {
- console.error('Error fixing URLs in content:', e);
+ console.error('Error fixing content:', e);
}
// Fix common email client quirks without breaking cid: URLs
@@ -277,7 +311,9 @@ export function processHtmlContent(htmlContent: string, textContent?: string): s
// Fix for base64 images that might be broken across lines
.replace(/src="data:image\/[^;]+;base64,\s*([^"]+)\s*"/gi, (match, p1) => {
return `src="data:image/png;base64,${p1.replace(/\s+/g, '')}"`;
- });
+ })
+ // Remove excessive whitespace from the HTML string itself
+ .replace(/>\s+ <');
} catch (error) {
console.error('Error processing HTML content:', error);
return htmlContent;