function extractMailContent(parsed) { const bodyText = String(parsed.text || '').replace(/\r/g, '').trim().slice(0, 20000); const body = bodyText ? bodyText.split(/\n\s*\n/).map(part => part.trim()).filter(Boolean).slice(0, 40) : ['This message has no plain-text body.']; const html = Array.isArray(parsed.html) ? parsed.html.join('') : String(parsed.html || ''); const links = [...html.matchAll(/]*\bhref\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/gi)] .map(match => (match[1] || match[2] || match[3] || '').replace(/&/gi, '&').replace(///gi, '/')) .filter(Boolean) .slice(0, 30); const files = (parsed.attachments || []) .filter(file => !/^application\/pgp-(signature|encrypted)/i.test(file.contentType || '')); return { bodyText, body, html: html.slice(0, 100000), links, attachments: files.map(file => ({ filename: file.filename || 'attachment', size: file.size || 0 })), attachment: files[0]?.filename || null, size: files[0]?.size ? `${(files[0].size / 1048576).toFixed(1)} MB` : '' }; } module.exports = { extractMailContent };