90 lines
2.6 KiB
TypeScript
90 lines
2.6 KiB
TypeScript
/**
|
|
* Simple MIME decoder for compose message box
|
|
* Handles basic email content and text direction
|
|
*/
|
|
|
|
export function decodeComposeContent(content: string): string {
|
|
if (!content) return '';
|
|
|
|
// Debug logging
|
|
console.log("Before decode:", content);
|
|
|
|
// Basic HTML cleaning without any string manipulation that could reverse text
|
|
let cleaned = content
|
|
// 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, '')
|
|
// Handle basic formatting
|
|
.replace(/<br\s*\/?>/gi, '\n')
|
|
.replace(/<p[^>]*>/gi, '\n')
|
|
.replace(/<\/p>/gi, '\n')
|
|
.replace(/<div[^>]*>/gi, '\n')
|
|
.replace(/<\/div>/gi, '\n')
|
|
// Handle lists
|
|
.replace(/<ul[^>]*>/gi, '\n')
|
|
.replace(/<\/ul>/gi, '\n')
|
|
.replace(/<ol[^>]*>/gi, '\n')
|
|
.replace(/<\/ol>/gi, '\n')
|
|
.replace(/<li[^>]*>/gi, '• ')
|
|
.replace(/<\/li>/gi, '\n')
|
|
// Handle basic text formatting
|
|
.replace(/<strong[^>]*>/gi, '**')
|
|
.replace(/<\/strong>/gi, '**')
|
|
.replace(/<b[^>]*>/gi, '**')
|
|
.replace(/<\/b>/gi, '**')
|
|
.replace(/<em[^>]*>/gi, '*')
|
|
.replace(/<\/em>/gi, '*')
|
|
.replace(/<i[^>]*>/gi, '*')
|
|
.replace(/<\/i>/gi, '*')
|
|
// Handle links
|
|
.replace(/<a[^>]*href="([^"]*)"[^>]*>(.*?)<\/a>/gi, '$2 ($1)')
|
|
// Handle basic entities
|
|
.replace(/ /g, ' ')
|
|
.replace(/&/g, '&')
|
|
.replace(/</g, '<')
|
|
.replace(/>/g, '>')
|
|
.replace(/"/g, '"')
|
|
.replace(/'/g, "'")
|
|
// Clean up whitespace
|
|
.replace(/\s+/g, ' ')
|
|
.trim();
|
|
|
|
// Debug logging
|
|
console.log("After decode:", cleaned);
|
|
|
|
// Ensure all content has proper direction
|
|
cleaned = `<div dir="ltr" style="direction: ltr; text-align: left; unicode-bidi: normal;">${cleaned}</div>`;
|
|
return cleaned;
|
|
}
|
|
|
|
export function encodeComposeContent(content: string): string {
|
|
if (!content) return '';
|
|
|
|
// Debug logging
|
|
console.log("Before encode:", content);
|
|
|
|
// Basic HTML encoding without reversing
|
|
const encoded = content
|
|
.replace(/&/g, '&')
|
|
.replace(/</g, '<')
|
|
.replace(/>/g, '>')
|
|
.replace(/"/g, '"')
|
|
.replace(/'/g, ''')
|
|
.replace(/\n/g, '<br>');
|
|
|
|
// Debug logging
|
|
console.log("After encode:", encoded);
|
|
|
|
return encoded;
|
|
}
|