mail page rest

This commit is contained in:
alma 2025-04-21 16:01:59 +02:00
parent 02668624c0
commit 273442d42c

View File

@ -6,21 +6,25 @@ export function decodeQuotedPrintable(text: string, charset: string): string {
// Replace soft line breaks (=\r\n or =\n or =\r)
let decoded = text.replace(/=(?:\r\n|\n|\r)/g, '');
// Replace quoted-printable encoded characters (including non-ASCII characters)
decoded = decoded.replace(/=([0-9A-F]{2})/gi, (match, p1) => {
return String.fromCharCode(parseInt(p1, 16));
});
// Replace quoted-printable encoded characters
decoded = decoded
// Handle common encoded characters
.replace(/=3D/g, '=')
.replace(/=20/g, ' ')
.replace(/=09/g, '\t')
.replace(/=0A/g, '\n')
.replace(/=0D/g, '\r')
// Handle other quoted-printable encoded characters
.replace(/=([0-9A-F]{2})/gi, (match, p1) => {
return String.fromCharCode(parseInt(p1, 16));
});
// Handle character encoding
try {
// For browsers with TextDecoder support
if (typeof TextDecoder !== 'undefined') {
// Convert string to array of byte values
const bytes = new Uint8Array(Array.from(decoded).map(c => c.charCodeAt(0)));
return new TextDecoder(charset).decode(bytes);
}
// Fallback for older browsers or when charset handling is not critical
return decoded;
} catch (e) {
console.warn('Charset conversion error:', e);
@ -61,14 +65,17 @@ export function convertCharset(text: string, charset: string): string {
if (!text) return '';
try {
// For browsers with TextDecoder support
if (typeof TextDecoder !== 'undefined') {
// Convert string to array of byte values
// Handle common charset aliases
const normalizedCharset = charset.toLowerCase()
.replace(/^iso-8859-1$/, 'windows-1252')
.replace(/^iso-8859-15$/, 'windows-1252')
.replace(/^utf-8$/, 'utf-8')
.replace(/^us-ascii$/, 'utf-8');
const bytes = new Uint8Array(Array.from(text).map(c => c.charCodeAt(0)));
return new TextDecoder(charset).decode(bytes);
return new TextDecoder(normalizedCharset).decode(bytes);
}
// Fallback for older browsers
return text;
} catch (e) {
console.warn('Charset conversion error:', e);
@ -158,6 +165,24 @@ export function cleanHtml(html: string): string {
.replace(/&lt;/g, '<')
.replace(/&amp;/g, '&')
.replace(/&quot;/g, '"')
.replace(/&eacute;/g, 'é')
.replace(/&egrave;/g, 'è')
.replace(/&ecirc;/g, 'ê')
.replace(/&euml;/g, 'ë')
.replace(/&agrave;/g, 'à')
.replace(/&acirc;/g, 'â')
.replace(/&auml;/g, 'ä')
.replace(/&icirc;/g, 'î')
.replace(/&iuml;/g, 'ï')
.replace(/&ocirc;/g, 'ô')
.replace(/&ouml;/g, 'ö')
.replace(/&ucirc;/g, 'û')
.replace(/&uuml;/g, 'ü')
.replace(/&ccedil;/g, 'ç')
.replace(/&OElig;/g, 'Œ')
.replace(/&oelig;/g, 'œ')
.replace(/&AElig;/g, 'Æ')
.replace(/&aelig;/g, 'æ')
// Clean up whitespace
.replace(/^\s+$/gm, '')
.replace(/\n{3,}/g, '\n\n')