fix(email): validate MJML before exposing exports
This commit is contained in:
1 parent
2a93e141aa
commit
88f2e456ef
3 files changed
+20
-2
No files matched your search
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@wrnexus/email-renderer",
|
"name": "@wrnexus/email-renderer",
|
||||||
"version": "0.1.26",
|
"version": "0.1.27",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"description": "WRNexus block email renderers with HTML, text, AMP Email, and MJML interoperability.",
|
"description": "WRNexus block email renderers with HTML, text, AMP Email, and MJML interoperability.",
|
||||||
"exports": {
|
"exports": {
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import {
|
|||||||
type Result,
|
type Result,
|
||||||
} from "@wrnexus/blocks";
|
} from "@wrnexus/blocks";
|
||||||
import { emailCoreBlocks, ampLayoutCss } from "./blocks.ts";
|
import { emailCoreBlocks, ampLayoutCss } from "./blocks.ts";
|
||||||
|
import { validateMjml } from "./mjml.ts";
|
||||||
|
|
||||||
interface AmpValidationError {
|
interface AmpValidationError {
|
||||||
severity: string;
|
severity: string;
|
||||||
@@ -168,7 +169,13 @@ export async function renderEmail(
|
|||||||
warnings,
|
warnings,
|
||||||
assets: [],
|
assets: [],
|
||||||
};
|
};
|
||||||
if (mjmlResult?.ok) bundle.exports = { mjml: mjmlResult.value.content };
|
if (mjmlResult?.ok) {
|
||||||
|
const diagnostics = await validateMjml(mjmlResult.value.content);
|
||||||
|
if (!diagnostics.length) bundle.exports = { mjml: mjmlResult.value.content };
|
||||||
|
else warnings.push(...diagnostics.map(diagnostic => ({ ...diagnostic, severity: "warning" as const })));
|
||||||
|
} else if (mjmlResult) {
|
||||||
|
warnings.push(...mjmlResult.errors.map(diagnostic => ({ ...diagnostic, severity: "warning" as const })));
|
||||||
|
}
|
||||||
if (options.amp) {
|
if (options.amp) {
|
||||||
const amp = await renderTree(source, emailKind, registry, { ...base, target: "ampEmail", data: options.variables });
|
const amp = await renderTree(source, emailKind, registry, { ...base, target: "ampEmail", data: options.variables });
|
||||||
if (amp.ok) {
|
if (amp.ok) {
|
||||||
|
|||||||
@@ -5,6 +5,17 @@ import { renderEmail, validateMjml } from "../src/index.ts";
|
|||||||
const text = (id: string): BlockNode => ({ id, type: "content.text", version: 1, props: { text: id }, styles: {}, slots: {} });
|
const text = (id: string): BlockNode => ({ id, type: "content.text", version: 1, props: { text: id }, styles: {}, slots: {} });
|
||||||
const columns = (id: string): BlockNode => ({ id, type: "layout.columns", version: 1, props: {}, styles: {}, slots: { columns: [0, 1].map(index => ({ id: `${id}-${index}`, type: "layout.column", version: 1, props: { width: "50%" }, styles: {}, slots: { content: [text(`${id}-text-${index}`)] } })) } });
|
const columns = (id: string): BlockNode => ({ id, type: "layout.columns", version: 1, props: {}, styles: {}, slots: { columns: [0, 1].map(index => ({ id: `${id}-${index}`, type: "layout.column", version: 1, props: { width: "50%" }, styles: {}, slots: { content: [text(`${id}-text-${index}`)] } })) } });
|
||||||
|
|
||||||
|
test("invalid nested MJML is diagnosed and omitted while HTML and text remain usable", async () => {
|
||||||
|
const document: BlockDocument = { schemaVersion: 1, kind: "email", meta: { subject: "Nested", lang: "en" }, theme: { tokens: {} }, root: { id: "root", type: "layout.email-document", version: 1, props: {}, styles: {}, slots: { content: [{ id: "outer", type: "layout.section", version: 1, props: {}, styles: {}, slots: { content: [{ id: "inner", type: "layout.section", version: 1, props: {}, styles: {}, slots: { content: [text("Retained nested content")] } }] } }] } } };
|
||||||
|
const rendered = await renderEmail(document, { mjml: true });
|
||||||
|
expect(rendered.ok).toBe(true);
|
||||||
|
if (!rendered.ok) return;
|
||||||
|
expect(rendered.value.exports?.mjml).toBeUndefined();
|
||||||
|
expect(rendered.value.warnings.some(warning => warning.code === "mjml.validation")).toBe(true);
|
||||||
|
expect(rendered.value.parts.html.content).toContain("Retained nested content");
|
||||||
|
expect(rendered.value.parts.text.content).toContain("Retained nested content");
|
||||||
|
});
|
||||||
|
|
||||||
test("root content and separate column rows produce valid ordered MJML without changing the document", async () => {
|
test("root content and separate column rows produce valid ordered MJML without changing the document", async () => {
|
||||||
const document: BlockDocument = { schemaVersion: 1, kind: "email", meta: { subject: "Root blocks", lang: "en" }, theme: { tokens: {} }, root: { id: "root", type: "layout.email-document", version: 1, props: {}, styles: {}, slots: { content: [text("intro"), columns("first"), columns("second"), { id: "cta", type: "content.button", version: 1, props: { label: "Act", href: "https://example.com" }, styles: {}, slots: {} }, text("end")] } } };
|
const document: BlockDocument = { schemaVersion: 1, kind: "email", meta: { subject: "Root blocks", lang: "en" }, theme: { tokens: {} }, root: { id: "root", type: "layout.email-document", version: 1, props: {}, styles: {}, slots: { content: [text("intro"), columns("first"), columns("second"), { id: "cta", type: "content.button", version: 1, props: { label: "Act", href: "https://example.com" }, styles: {}, slots: {} }, text("end")] } } };
|
||||||
const before = structuredClone(document);
|
const before = structuredClone(document);
|
||||||
|
|||||||
Reference in new issue
Block a user