courrier preview

This commit is contained in:
alma 2025-05-01 12:37:35 +02:00
parent af8ba0b7b5
commit 9c2c909f71
2 changed files with 43 additions and 4 deletions

View File

@ -278,15 +278,35 @@ export function processHtmlContent(htmlContent: string, textContent?: string): s
textNodes.forEach(node => {
if (node.nodeValue) {
// Replace sequences of whitespace with a single space
node.nodeValue = node.nodeValue.replace(/\s+/g, ' ').trim();
// But don't change whitespace in <pre> tags
if (!isInPreTag(node)) {
node.nodeValue = node.nodeValue.replace(/\s+/g, ' ').trim();
}
}
});
// Helper function to check if a node is inside a pre tag
function isInPreTag(node: Node): boolean {
let parent = node.parentNode;
while (parent) {
if (parent.nodeName.toLowerCase() === 'pre') {
return true;
}
parent = parent.parentNode;
}
return false;
}
// 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 === '&nbsp;') {
el.parentNode?.removeChild(el);
// Only remove if not a child of a layout container like table
if (el.parentNode &&
el.parentNode.nodeName.toLowerCase() !== 'td' &&
el.parentNode.nodeName.toLowerCase() !== 'th') {
el.parentNode?.removeChild(el);
}
}
});
@ -312,7 +332,7 @@ export function processHtmlContent(htmlContent: string, textContent?: string): s
.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
// Remove excessive whitespace from the HTML string itself, but be careful not to break preformatted text
.replace(/>\s+</g, '> <');
} catch (error) {
console.error('Error processing HTML content:', error);

View File

@ -372,8 +372,27 @@ export function formatForwardedEmail(originalEmail: EmailMessage | LegacyEmailMe
`;
// Use the original HTML content if available, otherwise format the text
const contentHtml = html || (text ? `<p>${text.replace(/\n/g, '</p><p>')}</p>` : '<p>No content available</p>');
let contentHtml = '';
if (html) {
// Preserve the original HTML structure
// The key is to preserve all formatting without trying to modify it
contentHtml = `<div class="original-email-content" style="margin-top: 10px;">${html}</div>`;
} else if (text) {
// For plain text, convert to HTML with proper line breaks
const formattedText = text
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/\n/g, '<br>');
contentHtml = `<div class="original-email-content" style="margin-top: 10px; font-family: monospace; white-space: pre-wrap;">${formattedText}</div>`;
} else {
contentHtml = '<div class="no-content" style="color: #777; font-style: italic;">No content available</div>';
}
// Just concatenate the header with the original content
// Don't try to process or reformat the original content
const cleanHtml = `${forwardHeader}${contentHtml}`;
// Plain text version - with clearer formatting