courrier preview

This commit is contained in:
alma 2025-05-01 12:36:23 +02:00
parent 0813a70184
commit af8ba0b7b5

View File

@ -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 <br> tags (more than 2)
let html = tempDiv.innerHTML;
html = html.replace(/(<br\s*\/?>\s*){3,}/gi, '<br><br>');
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+</g, '> <');
} catch (error) {
console.error('Error processing HTML content:', error);
return htmlContent;