Neah/lib/compose-mime-decoder.ts
2025-04-24 19:38:44 +02:00

49 lines
1.2 KiB
TypeScript

/**
* Simple MIME decoder for compose message box
* Handles basic email content without creating nested structures
*/
import { simpleParser } from 'mailparser';
interface ParsedContent {
html: string | null;
text: string | null;
}
export async function decodeComposeContent(content: string): Promise<ParsedContent> {
if (!content.trim()) {
return { html: null, text: null };
}
try {
const parsed = await simpleParser(content);
return {
html: parsed.html || null,
text: parsed.text || null
};
} catch (error) {
console.error('Error parsing email content:', error);
return {
html: content,
text: content
};
}
}
export async function encodeComposeContent(content: string): Promise<string> {
if (!content.trim()) {
throw new Error('Email content is empty');
}
// Create MIME headers
const mimeHeaders = {
'MIME-Version': '1.0',
'Content-Type': 'text/html; charset="utf-8"',
'Content-Transfer-Encoding': 'quoted-printable'
};
// Combine headers and content
return Object.entries(mimeHeaders)
.map(([key, value]) => `${key}: ${value}`)
.join('\n') + '\n\n' + content;
}