Currently, email bodies are stored as raw HTML. The AI intelligence layer and frontend display both struggle with malformed HTML, inline styles, and tracking pixels. We need a robust HTML-to-text converter that produces clean Markdown while preserving critical structures like invoice tables.
Acceptance Criteria
• Create backend/src/services/parser/html-converter.service.ts
• Convert HTML email bodies to clean Markdown using html-to-text + turndown
• Handle malformed HTML gracefully (fallback to plain text)
• Preserve invoice tables in Markdown table format
• Strip all JavaScript, CSS, tracking pixels, and hidden elements
• Configurable max output length (default 10,000 chars)
• Integrate into the email ingestion pipeline (call after receiving raw HTML)
• Update Email model to store cleanedBody alongside bodyHtml
Technical Notes
• Use cheerio for DOM manipulation (already in dependency tree via other packages)
• Must be async and return within <100ms for typical emails
• Test with real-world HTML emails: newsletters, invoices, meeting invites
Why This Matters
Without this, the AI sees messy HTML tags instead of readable text, reducing classification accuracy. The frontend also renders poorly when displaying raw HTML.
Currently, email bodies are stored as raw HTML. The AI intelligence layer and frontend display both struggle with malformed HTML, inline styles, and tracking pixels. We need a robust HTML-to-text converter that produces clean Markdown while preserving critical structures like invoice tables.
Acceptance Criteria
• Create backend/src/services/parser/html-converter.service.ts
• Convert HTML email bodies to clean Markdown using html-to-text + turndown
• Handle malformed HTML gracefully (fallback to plain text)
• Preserve invoice tables in Markdown table format
• Strip all JavaScript, CSS, tracking pixels, and hidden elements
• Configurable max output length (default 10,000 chars)
• Integrate into the email ingestion pipeline (call after receiving raw HTML)
• Update Email model to store cleanedBody alongside bodyHtml
Technical Notes
• Use cheerio for DOM manipulation (already in dependency tree via other packages)
• Must be async and return within <100ms for typical emails
• Test with real-world HTML emails: newsletters, invoices, meeting invites
Why This Matters
Without this, the AI sees messy HTML tags instead of readable text, reducing classification accuracy. The frontend also renders poorly when displaying raw HTML.