Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(email): Show plaintext drafts (#1792)

Evan Hutnik5a9aa2e3497f
diff --git a/js/app/packages/block-email/component/Compose.tsx b/js/app/packages/block-email/component/Compose.tsx
index 727185f35a..4c891bf2d9 100644
--- a/js/app/packages/block-email/component/Compose.tsx
+++ b/js/app/packages/block-email/component/Compose.tsx
@@ -68,6 +68,7 @@ import {
import { MACRO_EMAIL_SIGNATURE } from '@block-email/constants';
import { useMaybeEmailContext } from '@block-email/component/EmailContext';
import { decodeBase64Utf8 } from '@block-email/util/decodeBase64';
+import { plainTextToHtml } from '@block-email/util/plainTextToHtml';
import { stickyGate } from '@core/util/debounce';
import { invalidateSoupEntity } from '@queries/soup/cache';
import { WrapUnlessMobile } from '@core/mobile/WrapUnlessMobile';
@@ -724,11 +725,15 @@ export function EmailCompose(props: EmailComposeProps) {
}
const draft = form.draft;
- if (!draft || !draft.body_html_sanitized) return;
+ if (!draft) return;
- const decodedHtml = decodeBase64Utf8(draft.body_html_sanitized);
+ if (draft.body_html_sanitized) {
+ return decodeBase64Utf8(draft.body_html_sanitized);
+ }
- return decodedHtml;
+ if (draft.body_text) {
+ return plainTextToHtml(draft.body_text);
+ }
};
const getRecipientOptions = () => {
diff --git a/js/app/packages/block-email/component/EmailInput.tsx b/js/app/packages/block-email/component/EmailInput.tsx
index bd81c305cd..48cccee3b5 100644
--- a/js/app/packages/block-email/component/EmailInput.tsx
+++ b/js/app/packages/block-email/component/EmailInput.tsx
@@ -5,6 +5,7 @@ import type {
} from '@service-email/generated/schemas';
import { type Accessor, createMemo, type Setter, Show } from 'solid-js';
import { decodeBase64Utf8 } from '../util/decodeBase64';
+import { plainTextToHtml } from '../util/plainTextToHtml';
import { BaseInput } from './BaseInput';
interface EmailInputProps {
@@ -19,7 +20,11 @@ export function EmailInput(props: EmailInputProps) {
const draftHTML = createMemo(() => {
const encoded = props.draft?.body_html_sanitized;
- if (!encoded) return '';
+ if (!encoded) {
+ const plainText = props.draft?.body_text;
+ if (!plainText) return '';
+ return plainTextToHtml(plainText);
+ }
const decodedHtml = decodeBase64Utf8(encoded);
return decodedHtml;
});
diff --git a/js/app/packages/block-email/util/plainTextToHtml.test.ts b/js/app/packages/block-email/util/plainTextToHtml.test.ts
new file mode 100644
index 0000000000..7741b028f0
--- /dev/null
+++ b/js/app/packages/block-email/util/plainTextToHtml.test.ts
@@ -0,0 +1,77 @@
+import { describe, expect, it } from 'vitest';
+import { plainTextToHtml } from './plainTextToHtml';
+
+const span = (text: string) =>
+ `<span style="white-space: pre-wrap;">${text}</span>`;
+
+describe('plainTextToHtml', () => {
+ it('returns a br for empty string', () => {
+ expect(plainTextToHtml('')).toBe('<div><br></div>');
+ });
+
+ it('wraps a single line in a span', () => {
+ expect(plainTextToHtml('hello')).toBe(`<div>${span('hello')}</div>`);
+ });
+
+ it('splits newlines with br separators', () => {
+ expect(plainTextToHtml('line1\nline2')).toBe(
+ `<div>${span('line1')}<br>${span('line2')}</div>`
+ );
+ });
+
+ it('handles consecutive newlines as double br', () => {
+ expect(plainTextToHtml('above\n\nbelow')).toBe(
+ `<div>${span('above')}<br><br><br>${span('below')}</div>`
+ );
+ });
+
+ it('handles only newlines', () => {
+ // '\n\n' splits into ['', '', ''] → <br> joined by <br> = 5 <br>s
+ expect(plainTextToHtml('\n\n')).toBe('<div><br><br><br><br><br></div>');
+ });
+
+ describe('html escaping', () => {
+ it('escapes ampersands', () => {
+ expect(plainTextToHtml('a & b')).toBe(`<div>${span('a &amp; b')}</div>`);
+ });
+
+ it('escapes angle brackets', () => {
+ expect(plainTextToHtml('<script>alert("xss")</script>')).toBe(
+ `<div>${span('&lt;script&gt;alert("xss")&lt;/script&gt;')}</div>`
+ );
+ });
+ });
+
+ describe('no markdown formatting applied', () => {
+ it('preserves asterisks literally', () => {
+ expect(plainTextToHtml('**Key Points:**')).toBe(
+ `<div>${span('**Key Points:**')}</div>`
+ );
+ });
+
+ it('preserves underscores literally', () => {
+ expect(plainTextToHtml('file_name_with_underscores')).toBe(
+ `<div>${span('file_name_with_underscores')}</div>`
+ );
+ });
+
+ it('preserves scope names with dots and underscores', () => {
+ expect(plainTextToHtml('gmail.settings_basic')).toBe(
+ `<div>${span('gmail.settings_basic')}</div>`
+ );
+ });
+
+ it('preserves math expressions with asterisks', () => {
+ expect(plainTextToHtml('3 * 5 = 15')).toBe(
+ `<div>${span('3 * 5 = 15')}</div>`
+ );
+ });
+ });
+
+ it('handles multiline content matching editor format', () => {
+ const input = 'Hi,\n\nSome text here';
+ expect(plainTextToHtml(input)).toBe(
+ `<div>${span('Hi,')}<br><br><br>${span('Some text here')}</div>`
+ );
+ });
+});
diff --git a/js/app/packages/block-email/util/plainTextToHtml.ts b/js/app/packages/block-email/util/plainTextToHtml.ts
new file mode 100644
index 0000000000..963f0eefd2
--- /dev/null
+++ b/js/app/packages/block-email/util/plainTextToHtml.ts
@@ -0,0 +1,17 @@
+function escapeHtml(text: string): string {
+ return text
+ .replace(/&/g, '&amp;')
+ .replace(/</g, '&lt;')
+ .replace(/>/g, '&gt;');
+}
+
+export function plainTextToHtml(text: string): string {
+ const lines = text.split('\n');
+ const inner = lines
+ .map((line) => {
+ if (!line) return '<br>';
+ return `<span style="white-space: pre-wrap;">${escapeHtml(line)}</span>`;
+ })
+ .join('<br>');
+ return `<div>${inner}</div>`;
+}
diff --git a/js/app/vitest.config.ts b/js/app/vitest.config.ts
index ca92862e67..9e00d76081 100644
--- a/js/app/vitest.config.ts
+++ b/js/app/vitest.config.ts
@@ -79,6 +79,12 @@ export default defineConfig({
name: 'notifications',
},
},
+ {
+ test: {
+ include: ['packages/block-email/**/*.{test,spec}.{ts,tsx}'],
+ name: 'block-email',
+ },
+ },
],
},
});