@emailens/engine 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Emailens
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,285 @@
1
+ # @emailens/engine
2
+
3
+ Email compatibility engine that transforms CSS per email client, analyzes compatibility, scores results, simulates dark mode, and provides framework-aware fix snippets.
4
+
5
+ Supports **12 email clients**: Gmail (Web, Android, iOS), Outlook (365, Windows), Apple Mail (macOS, iOS), Yahoo Mail, Samsung Mail, Thunderbird, HEY Mail, and Superhuman.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm install @emailens/engine
11
+ # or
12
+ bun add @emailens/engine
13
+ ```
14
+
15
+ ## Quick Start
16
+
17
+ ```typescript
18
+ import {
19
+ analyzeEmail,
20
+ generateCompatibilityScore,
21
+ transformForAllClients,
22
+ simulateDarkMode,
23
+ } from "@emailens/engine";
24
+
25
+ const html = `
26
+ <html>
27
+ <head>
28
+ <style>
29
+ .card { border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); }
30
+ </style>
31
+ </head>
32
+ <body>
33
+ <div class="card" style="display: flex; gap: 16px;">
34
+ <div>Column A</div>
35
+ <div>Column B</div>
36
+ </div>
37
+ </body>
38
+ </html>
39
+ `;
40
+
41
+ // 1. Analyze CSS compatibility across all clients
42
+ const warnings = analyzeEmail(html);
43
+ console.log(`Found ${warnings.length} warnings`);
44
+
45
+ // 2. Generate per-client scores (0–100)
46
+ const scores = generateCompatibilityScore(warnings);
47
+ console.log(scores["gmail-web"]); // { score: 75, errors: 0, warnings: 5, info: 0 }
48
+ console.log(scores["outlook-windows"]); // { score: 40, errors: 1, warnings: 8, info: 2 }
49
+
50
+ // 3. Transform HTML per client (strips unsupported CSS, inlines styles)
51
+ const transforms = transformForAllClients(html);
52
+ for (const t of transforms) {
53
+ console.log(`${t.clientId}: ${t.warnings.length} warnings`);
54
+ }
55
+
56
+ // 4. Simulate dark mode for a specific client
57
+ const darkMode = simulateDarkMode(html, "gmail-android");
58
+ console.log(darkMode.warnings); // Warns about transparent PNGs, missing prefers-color-scheme, etc.
59
+ ```
60
+
61
+ ## API Reference
62
+
63
+ ### `analyzeEmail(html: string, framework?: Framework): CSSWarning[]`
64
+
65
+ Analyzes an HTML email and returns CSS compatibility warnings for all 12 email clients. Detects usage of `<style>`, `<link>`, `<svg>`, `<video>`, `<form>`, inline CSS properties, `@font-face`, `@media` queries, gradients, flexbox/grid, and more.
66
+
67
+ The optional `framework` parameter (`"jsx"` | `"mjml"` | `"maizzle"`) controls which fix snippets are attached to warnings. Analysis always runs on compiled HTML — fix snippets reference source-level constructs so you know how to modify your framework source code.
68
+
69
+ ```typescript
70
+ // Plain HTML analysis
71
+ const warnings = analyzeEmail(html);
72
+
73
+ // Framework-aware: fixes reference React Email components
74
+ const warnings = analyzeEmail(html, "jsx");
75
+
76
+ // Framework-aware: fixes reference MJML elements
77
+ const warnings = analyzeEmail(html, "mjml");
78
+
79
+ // Framework-aware: fixes reference Maizzle/Tailwind classes
80
+ const warnings = analyzeEmail(html, "maizzle");
81
+ ```
82
+
83
+ ### `generateCompatibilityScore(warnings: CSSWarning[]): Record<string, ClientScore>`
84
+
85
+ Generates a 0–100 compatibility score per email client from a set of warnings.
86
+
87
+ **Scoring formula:** `score = 100 - (errors × 15) - (warnings × 5) - (info × 1)`, clamped to 0–100.
88
+
89
+ ```typescript
90
+ const scores = generateCompatibilityScore(warnings);
91
+ // {
92
+ // "gmail-web": { score: 75, errors: 0, warnings: 5, info: 0 },
93
+ // "outlook-windows": { score: 40, errors: 1, warnings: 8, info: 2 },
94
+ // "apple-mail-macos": { score: 100, errors: 0, warnings: 0, info: 0 },
95
+ // ...
96
+ // }
97
+ ```
98
+
99
+ ### `transformForClient(html: string, clientId: string, framework?: Framework): TransformResult`
100
+
101
+ Transforms HTML for a specific email client. Strips unsupported CSS, inlines `<style>` blocks (for Gmail), removes unsupported elements, and generates per-client warnings.
102
+
103
+ ```typescript
104
+ const result = transformForClient(html, "gmail-web");
105
+ console.log(result.html); // Transformed HTML
106
+ console.log(result.warnings); // Client-specific warnings
107
+ console.log(result.clientId); // "gmail-web"
108
+ ```
109
+
110
+ ### `transformForAllClients(html: string, framework?: Framework): TransformResult[]`
111
+
112
+ Transforms HTML for all 12 email clients at once.
113
+
114
+ ```typescript
115
+ const results = transformForAllClients(html);
116
+ for (const r of results) {
117
+ console.log(`${r.clientId}: ${r.warnings.length} issues`);
118
+ }
119
+ ```
120
+
121
+ ### `simulateDarkMode(html: string, clientId: string): { html: string; warnings: CSSWarning[] }`
122
+
123
+ Simulates how an email client applies dark mode. Different clients use different strategies:
124
+
125
+ - **Full inversion** (Gmail Android, Samsung Mail): swaps all light backgrounds to dark
126
+ - **Partial inversion** (Gmail Web, Apple Mail, Yahoo, Outlook.com, HEY, Superhuman): only inverts white backgrounds
127
+ - **No dark mode** (Outlook Windows, Thunderbird): no transformation
128
+
129
+ ```typescript
130
+ const { html: darkHtml, warnings } = simulateDarkMode(html, "gmail-android");
131
+ ```
132
+
133
+ ### `getCodeFix(property: string, clientId: string, framework?: Framework): CodeFix | undefined`
134
+
135
+ Returns a paste-ready code fix for a specific CSS property + client combination. Fixes are tiered:
136
+
137
+ 1. **Framework + client specific** (e.g., `border-radius` + Outlook + JSX → VML roundrect component)
138
+ 2. **Framework specific** (e.g., `@font-face` + MJML → `<mj-font>`)
139
+ 3. **Generic HTML fallback** (e.g., `display:flex` + Outlook → HTML table with MSO conditionals)
140
+
141
+ ```typescript
142
+ const fix = getCodeFix("display:flex", "outlook-windows", "jsx");
143
+ // {
144
+ // language: "jsx",
145
+ // description: "Use Row and Column from @react-email/components",
146
+ // before: "<div style={{ display: 'flex' }}>...",
147
+ // after: "<Row><Column>..."
148
+ // }
149
+ ```
150
+
151
+ ### `getSuggestion(property: string, clientId: string, framework?: Framework): { text: string; isGenericFallback?: boolean }`
152
+
153
+ Returns a human-readable suggestion for fixing a compatibility issue. Lighter than `getCodeFix` — returns a text description rather than before/after code.
154
+
155
+ ### `diffResults(before, after): DiffResult[]`
156
+
157
+ Compares two sets of analysis results (before and after a fix). Shows what improved, regressed, or stayed the same per client.
158
+
159
+ ```typescript
160
+ const before = { scores: generateCompatibilityScore(warningsBefore), warnings: warningsBefore };
161
+ const after = { scores: generateCompatibilityScore(warningsAfter), warnings: warningsAfter };
162
+ const diffs = diffResults(before, after);
163
+
164
+ for (const d of diffs) {
165
+ console.log(`${d.clientId}: ${d.scoreBefore} → ${d.scoreAfter} (${d.scoreDelta > 0 ? "+" : ""}${d.scoreDelta})`);
166
+ console.log(` Fixed: ${d.fixed.length}, Introduced: ${d.introduced.length}`);
167
+ }
168
+ ```
169
+
170
+ ### `generateFixPrompt(options: ExportPromptOptions): string`
171
+
172
+ Generates a markdown prompt suitable for passing to an AI assistant to fix compatibility issues. Includes the original HTML, compatibility scores table, all detected issues with fix suggestions, and format-specific instructions.
173
+
174
+ ```typescript
175
+ const prompt = generateFixPrompt({
176
+ originalHtml: html,
177
+ warnings,
178
+ scores,
179
+ scope: "all", // or "current" with selectedClientId
180
+ format: "jsx", // "html" | "jsx" | "mjml" | "maizzle"
181
+ });
182
+ ```
183
+
184
+ ### `EMAIL_CLIENTS: EmailClient[]`
185
+
186
+ Array of all 12 supported email client definitions.
187
+
188
+ ```typescript
189
+ import { EMAIL_CLIENTS } from "@emailens/engine";
190
+
191
+ for (const client of EMAIL_CLIENTS) {
192
+ console.log(`${client.name} (${client.category}) — ${client.engine}`);
193
+ }
194
+ // Gmail (webmail) — Gmail Web
195
+ // Outlook Windows (desktop) — Microsoft Word
196
+ // Apple Mail (desktop) — WebKit
197
+ // ...
198
+ ```
199
+
200
+ ### `getClient(id: string): EmailClient | undefined`
201
+
202
+ Look up a client by ID.
203
+
204
+ ## Supported Email Clients
205
+
206
+ | Client | ID | Category | Engine | Dark Mode |
207
+ |---|---|---|---|---|
208
+ | Gmail | `gmail-web` | Webmail | Gmail Web | Yes |
209
+ | Gmail Android | `gmail-android` | Mobile | Gmail Mobile | Yes |
210
+ | Gmail iOS | `gmail-ios` | Mobile | Gmail Mobile | Yes |
211
+ | Outlook 365 | `outlook-web` | Webmail | Outlook Web | Yes |
212
+ | Outlook Windows | `outlook-windows` | Desktop | Microsoft Word | No |
213
+ | Apple Mail | `apple-mail-macos` | Desktop | WebKit | Yes |
214
+ | Apple Mail iOS | `apple-mail-ios` | Mobile | WebKit | Yes |
215
+ | Yahoo Mail | `yahoo-mail` | Webmail | Yahoo | Yes |
216
+ | Samsung Mail | `samsung-mail` | Mobile | Samsung | Yes |
217
+ | Thunderbird | `thunderbird` | Desktop | Gecko | No |
218
+ | HEY Mail | `hey-mail` | Webmail | WebKit | Yes |
219
+ | Superhuman | `superhuman` | Desktop | Blink | Yes |
220
+
221
+ ## CSS Support Matrix
222
+
223
+ The engine includes a comprehensive CSS support matrix (`src/rules/css-support.ts`) covering 30+ CSS properties and HTML elements across all 12 clients. Data sourced from [caniemail.com](https://www.caniemail.com/) with inferred values for HEY Mail and Superhuman based on their rendering engines.
224
+
225
+ ## Types
226
+
227
+ ```typescript
228
+ type SupportLevel = "supported" | "partial" | "unsupported" | "unknown";
229
+ type Framework = "jsx" | "mjml" | "maizzle";
230
+ type InputFormat = "html" | Framework;
231
+
232
+ interface EmailClient {
233
+ id: string;
234
+ name: string;
235
+ category: "webmail" | "desktop" | "mobile";
236
+ engine: string;
237
+ darkModeSupport: boolean;
238
+ icon: string;
239
+ }
240
+
241
+ interface CSSWarning {
242
+ severity: "error" | "warning" | "info";
243
+ client: string;
244
+ property: string;
245
+ message: string;
246
+ suggestion?: string;
247
+ fix?: CodeFix;
248
+ fixIsGenericFallback?: boolean;
249
+ }
250
+
251
+ interface CodeFix {
252
+ before: string;
253
+ after: string;
254
+ language: "html" | "css" | "jsx" | "mjml" | "maizzle";
255
+ description: string;
256
+ }
257
+
258
+ interface TransformResult {
259
+ clientId: string;
260
+ html: string;
261
+ warnings: CSSWarning[];
262
+ }
263
+
264
+ interface DiffResult {
265
+ clientId: string;
266
+ scoreBefore: number;
267
+ scoreAfter: number;
268
+ scoreDelta: number;
269
+ fixed: CSSWarning[];
270
+ introduced: CSSWarning[];
271
+ unchanged: CSSWarning[];
272
+ }
273
+ ```
274
+
275
+ ## Testing
276
+
277
+ ```bash
278
+ bun test
279
+ ```
280
+
281
+ 95 tests covering analysis, transformation, dark mode simulation, framework-aware fixes, edge cases, and accuracy benchmarks against real-world email templates.
282
+
283
+ ## License
284
+
285
+ MIT
@@ -0,0 +1,156 @@
1
+ type SupportLevel = "supported" | "partial" | "unsupported" | "unknown";
2
+ type Framework = "jsx" | "mjml" | "maizzle";
3
+ type InputFormat = "html" | Framework;
4
+ interface EmailClient {
5
+ id: string;
6
+ name: string;
7
+ category: "webmail" | "desktop" | "mobile";
8
+ engine: string;
9
+ darkModeSupport: boolean;
10
+ icon: string;
11
+ }
12
+ interface CodeFix {
13
+ before: string;
14
+ after: string;
15
+ language: "html" | "css" | "jsx" | "mjml" | "maizzle";
16
+ description: string;
17
+ }
18
+ interface CSSWarning {
19
+ severity: "error" | "warning" | "info";
20
+ client: string;
21
+ property: string;
22
+ message: string;
23
+ suggestion?: string;
24
+ fix?: CodeFix;
25
+ fixIsGenericFallback?: boolean;
26
+ line?: number;
27
+ selector?: string;
28
+ }
29
+ interface TransformResult {
30
+ clientId: string;
31
+ html: string;
32
+ warnings: CSSWarning[];
33
+ }
34
+ interface PreviewResult {
35
+ id: string;
36
+ originalHtml: string;
37
+ transforms: TransformResult[];
38
+ cssReport: CSSWarning[];
39
+ createdAt: string;
40
+ }
41
+ interface DiffResult {
42
+ clientId: string;
43
+ scoreBefore: number;
44
+ scoreAfter: number;
45
+ scoreDelta: number;
46
+ fixed: CSSWarning[];
47
+ introduced: CSSWarning[];
48
+ unchanged: CSSWarning[];
49
+ }
50
+
51
+ declare const EMAIL_CLIENTS: EmailClient[];
52
+ declare function getClient(id: string): EmailClient | undefined;
53
+
54
+ declare function transformForClient(html: string, clientId: string, framework?: Framework): TransformResult;
55
+ declare function transformForAllClients(html: string, framework?: Framework): TransformResult[];
56
+
57
+ /**
58
+ * Analyze an HTML email and return CSS compatibility warnings
59
+ * for all target email clients.
60
+ *
61
+ * The `framework` parameter controls which fix snippets are attached
62
+ * to warnings — it does NOT change which warnings fire. Analysis always
63
+ * runs on compiled HTML (what email clients actually receive). Fix
64
+ * snippets reference source-level constructs so users know how to
65
+ * modify their framework source code.
66
+ */
67
+ declare function analyzeEmail(html: string, framework?: Framework): CSSWarning[];
68
+ /**
69
+ * Generate a summary of CSS compatibility for the email.
70
+ */
71
+ declare function generateCompatibilityScore(warnings: CSSWarning[]): Record<string, {
72
+ score: number;
73
+ errors: number;
74
+ warnings: number;
75
+ info: number;
76
+ }>;
77
+
78
+ /**
79
+ * Simulate dark mode rendering for an email.
80
+ * Email clients apply dark mode differently:
81
+ * - Some invert colors (Gmail Android)
82
+ * - Some use prefers-color-scheme media query (Apple Mail)
83
+ * - Some do partial inversion (Outlook.com)
84
+ */
85
+ declare function simulateDarkMode(html: string, clientId: string): {
86
+ html: string;
87
+ warnings: CSSWarning[];
88
+ };
89
+
90
+ /**
91
+ * Look up a code fix for a given property, client, and optional framework.
92
+ * Returns undefined if no fix snippet exists.
93
+ *
94
+ * Resolution order (most specific to least specific):
95
+ * 1. property::clientPrefix::framework (e.g. "display:flex::outlook::jsx")
96
+ * 2. property::framework (e.g. "display:grid::jsx")
97
+ * 3. property::clientPrefix (e.g. "border-radius::outlook")
98
+ * 4. property (generic fix)
99
+ */
100
+ declare function getCodeFix(property: string, clientId: string, framework?: Framework): CodeFix | undefined;
101
+ /**
102
+ * Look up a suggestion string for a given property, client, and optional framework.
103
+ *
104
+ * Resolution order mirrors `getCodeFix()`:
105
+ * 1. property::clientPrefix::framework
106
+ * 2. property::framework
107
+ * 3. property::clientPrefix
108
+ * 4. property (generic)
109
+ *
110
+ * `isGenericFallback` is true when a framework was specified but no
111
+ * framework-specific entry was found (resolution fell through to tiers 3–4).
112
+ */
113
+ declare function getSuggestion(property: string, clientId: string, framework?: Framework): {
114
+ text: string;
115
+ isGenericFallback: boolean;
116
+ };
117
+
118
+ /**
119
+ * Compare two sets of analysis results to show what improved,
120
+ * what regressed, and what stayed the same.
121
+ */
122
+ declare function diffResults(before: {
123
+ scores: Record<string, {
124
+ score: number;
125
+ errors: number;
126
+ warnings: number;
127
+ info: number;
128
+ }>;
129
+ warnings: CSSWarning[];
130
+ }, after: {
131
+ scores: Record<string, {
132
+ score: number;
133
+ errors: number;
134
+ warnings: number;
135
+ info: number;
136
+ }>;
137
+ warnings: CSSWarning[];
138
+ }): DiffResult[];
139
+
140
+ type ExportScope = "all" | "current";
141
+ interface ExportPromptOptions {
142
+ originalHtml: string;
143
+ warnings: CSSWarning[];
144
+ scores: Record<string, {
145
+ score: number;
146
+ errors: number;
147
+ warnings: number;
148
+ info: number;
149
+ }>;
150
+ scope: ExportScope;
151
+ selectedClientId?: string;
152
+ format?: "html" | "jsx" | "mjml" | "maizzle";
153
+ }
154
+ declare function generateFixPrompt(options: ExportPromptOptions): string;
155
+
156
+ export { type CSSWarning, type CodeFix, type DiffResult, EMAIL_CLIENTS, type EmailClient, type ExportPromptOptions, type ExportScope, type Framework, type InputFormat, type PreviewResult, type SupportLevel, type TransformResult, analyzeEmail, diffResults, generateCompatibilityScore, generateFixPrompt, getClient, getCodeFix, getSuggestion, simulateDarkMode, transformForAllClients, transformForClient };