Skip to content

📧✨ Beautiful email. For everyone.

fix: render HTML duplicated in email body fields (#48)

Paul Koeck1d7867a226a2
diff --git a/apps/desktop/src/renderer/src/email-html.ts b/apps/desktop/src/renderer/src/email-html.ts
index 287bcba..5c6ca78 100644
--- a/apps/desktop/src/renderer/src/email-html.ts
+++ b/apps/desktop/src/renderer/src/email-html.ts
@@ -1,10 +1,15 @@
import type { Config } from 'dompurify'
import { plainTextHtml, type Message } from '@inlark/core'
-/** Older cached JMAP messages may store the same plain-text part in both fields. */
+// Cached JMAP bodies can match for either MIME type. Look for a document, paired
+// elements, or image markup; a literal <br> or <[email protected]> is still text.
+const htmlMarkup =
+ /<(?:!doctype\s+html|html|head|body)(?:\s|>)|<([a-z][a-z\d:-]*)\b[^>]*>[\s\S]*?<\/\1\s*>|<(?:img|hr)\b[^>]*>/i
+
+/** Repair duplicated plain-text bodies without escaping duplicated HTML bodies. */
export function emailBodyHtml(message: Pick<Message, 'html' | 'text' | 'preview'>): string {
const html = message.html?.trim()
- if (html && html !== message.text?.trim()) return message.html!
+ if (html && (html !== message.text?.trim() || htmlMarkup.test(html))) return message.html!
return plainTextHtml(
(message.text?.trim() ? message.text : message.preview) ||
'No readable message body is available.',
diff --git a/tests/reader-body.test.ts b/tests/reader-body.test.ts
index f259f8d..1aee1ca 100644
--- a/tests/reader-body.test.ts
+++ b/tests/reader-body.test.ts
@@ -20,6 +20,22 @@ describe('email body fallback', () => {
html,
)
})
+ it.each([
+ '<!DOCTYPE html>\n<html xmlns:v="urn:schemas-microsoft-com:vml" style="line-height:1.5"><head><meta charset="utf-8"><title>Weekly database report</title></head><body><table><tr><td>Database report</td></tr></table></body></html>',
+ '<!DOCTYPE html>\n<html style="font-weight: 400"><head><meta charset="utf-8"></head><body><h1>Weekly Report</h1><p>Your activity this week.</p></body></html>',
+ '<p>Hello <strong>there</strong>.</p><p>Another paragraph.</p>',
+ '<img src="cid:[email protected]" alt="Report">',
+ ])('renders HTML duplicated in both body fields: %s', (html) => {
+ expect(emailBodyHtml({ html, text: html, preview: 'Weekly report' })).toBe(html)
+ expect(emailBodyHtml({ html: `\n${html}\n`, text: html, preview: '' })).toBe(`\n${html}\n`)
+ })
+ it('keeps literal markup in duplicated plain text escaped', () => {
+ const text = 'Use <br> literally & keep "quotes".\nSender <[email protected]>'
+ const html = emailBodyHtml({ html: text, text, preview: '' })
+ expect(html).toContain('white-space:pre-wrap')
+ expect(html).toContain('Use &lt;br&gt; literally &amp; keep &quot;quotes&quot;.\n')
+ expect(html).toContain('Sender &lt;[email protected]&gt;')
+ })
it('escapes literal markup and link attributes in plain text', () => {
const html = emailBodyHtml({
text: 'Use <br> literally & keep "quotes".\nhttps://example.test/?a=1&b=2" title="unexpected',