Neah/lib/mail-parser-wrapper.ts
2025-04-24 16:04:46 +02:00

71 lines
2.2 KiB
TypeScript

import { simpleParser, ParsedMail, Attachment, HeaderValue, AddressObject } from 'mailparser';
export interface DecodedEmail {
html: string | false;
text: string | false;
attachments: Attachment[];
headers: Map<string, HeaderValue>;
subject: string;
from: string;
to: string;
date: Date;
}
function getAddressText(address: AddressObject | AddressObject[] | undefined): string {
if (!address) return '';
if (Array.isArray(address)) {
return address.map(addr => addr.value?.[0]?.address || '').filter(Boolean).join(', ');
}
return address.value?.[0]?.address || '';
}
export async function decodeEmail(rawEmail: string): Promise<DecodedEmail> {
try {
const parsed = await simpleParser(rawEmail);
return {
html: parsed.html || false,
text: parsed.text || false,
attachments: parsed.attachments || [],
headers: parsed.headers,
subject: parsed.subject || '',
from: getAddressText(parsed.from),
to: getAddressText(parsed.to),
date: parsed.date || new Date()
};
} catch (error) {
console.error('Error decoding email:', error);
throw new Error('Failed to decode email');
}
}
export function cleanHtml(html: string): string {
if (!html) return '';
// Detect text direction from the content
const hasRtlChars = /[\u0591-\u07FF\u200F\u202B\u202E\uFB1D-\uFDFD\uFE70-\uFEFC]/.test(html);
const defaultDir = hasRtlChars ? 'rtl' : 'ltr';
// Basic HTML cleaning while preserving structure
const cleaned = html
// Remove script and style tags
.replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '')
.replace(/<style[^>]*>[\s\S]*?<\/style>/gi, '')
// Remove meta tags
.replace(/<meta[^>]*>/gi, '')
// Remove head and title
.replace(/<head[^>]*>[\s\S]*?<\/head>/gi, '')
.replace(/<title[^>]*>[\s\S]*?<\/title>/gi, '')
// Remove body tags
.replace(/<body[^>]*>/gi, '')
.replace(/<\/body>/gi, '')
// Remove html tags
.replace(/<html[^>]*>/gi, '')
.replace(/<\/html>/gi, '')
// Clean up whitespace
.replace(/\s+/g, ' ')
.trim();
// Wrap in a div with the detected direction
return `<div dir="${defaultDir}">${cleaned}</div>`;
}