@lit-pigeon/renderer-mjml 0.1.1
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 +21 -0
- package/dist/index.cjs +42 -0
- package/dist/index.d.ts +123 -0
- package/dist/index.js +333 -0
- package/package.json +46 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Lit Pigeon Contributors
|
|
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/dist/index.cjs
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const R=require("mjml"),T=require("@lit-pigeon/core");function u(e){return`${e.top}px ${e.right}px ${e.bottom}px ${e.left}px`}function h(e){const{content:t,padding:r,textAlign:o,lineHeight:n}=e.values;return`<mj-text ${[`padding="${u(r)}"`,`align="${o}"`,`line-height="${n}"`].join(" ")}>${t}</mj-text>`}function j(e){const{src:t,alt:r,width:o,href:n,padding:a,alignment:c,borderRadius:i}=e.values,s=[`src="${m(t)}"`,`alt="${m(r)}"`,`padding="${u(a)}"`,`align="${c}"`];return o!=="auto"&&s.push(`width="${o}px"`),n&&s.push(`href="${m(n)}"`),i!==void 0&&i>0&&s.push(`border-radius="${i}px"`),`<mj-image ${s.join(" ")} />`}function m(e){return e.replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">")}function b(e){const{content:t,href:r,backgroundColor:o,textColor:n,borderRadius:a,padding:c,innerPadding:i,fontSize:s,fontWeight:l,alignment:g,fullWidth:d}=e.values,p=[`href="${H(r)}"`,`background-color="${o}"`,`color="${n}"`,`border-radius="${a}px"`,`padding="${u(c)}"`,`inner-padding="${u(i)}"`,`font-size="${s}px"`,`font-weight="${l}"`,`align="${g}"`];d&&p.push('width="100%"');const S=t.replace(/^\s*<p>([\s\S]*?)<\/p>\s*$/i,"$1");return`<mj-button ${p.join(" ")}>${S}</mj-button>`}function H(e){return e.replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">")}function k(e){const{borderColor:t,borderWidth:r,borderStyle:o,padding:n,width:a}=e.values;return`<mj-divider ${[`border-color="${t}"`,`border-width="${r}px"`,`border-style="${o}"`,`padding="${u(n)}"`,`width="${a}"`].join(" ")} />`}function x(e){const{height:t}=e.values;return`<mj-spacer height="${t}px" />`}function W(e){switch(e.type){case"facebook":return"facebook";case"twitter":return"twitter";case"instagram":return"instagram";case"linkedin":return"linkedin";case"youtube":return"youtube";case"tiktok":return"web";case"custom":return"web";default:return"web"}}function q(e,t){const o=[`name="${W(e)}"`,`href="${$(e.href)}"`,`icon-size="${t}px"`];e.iconUrl&&o.push(`src="${$(e.iconUrl)}"`);const n=e.label??"";return`<mj-social-element ${o.join(" ")}>${N(n)}</mj-social-element>`}function v(e){const{icons:t,iconSize:r,spacing:o,alignment:n,padding:a}=e.values,c=[`icon-size="${r}px"`,`icon-padding="${o}px"`,`align="${n}"`,`padding="${u(a)}"`,'mode="horizontal"'],i=t.map(s=>q(s,r)).join(`
|
|
2
|
+
`);return`<mj-social ${c.join(" ")}>
|
|
3
|
+
${i}
|
|
4
|
+
</mj-social>`}function $(e){return e.replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">")}function N(e){return e.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")}const _=/<(\s*\/?\s*mj-raw\b[^>]*)>/gi;function z(e){return e.replace(_,"<$1>")}function w(e){const{content:t,padding:r}=e.values,o=u(r),n=z(t);return`<mj-raw><div style="padding: ${o};">${n}</div></mj-raw>`}function y(e){const{content:t,innerPadding:r}=e.values;return`<mj-text ${[`padding="${u(r)}"`].join(" ")}>${t}</mj-text>`}function B(e){const t=e.values,r=[`mode="${t.mode}"`,`height="${t.height}px"`,`vertical-align="${t.verticalAlign}"`,`padding="${u(t.padding)}"`];return t.backgroundUrl&&r.push(`background-width="${t.width}px"`),t.backgroundUrl&&(r.push(`background-url="${D(t.backgroundUrl)}"`),r.push(`background-position="${t.backgroundPosition}"`)),t.backgroundColor&&r.push(`background-color="${t.backgroundColor}"`),` <mj-hero ${r.join(" ")}>
|
|
5
|
+
<mj-text padding="${u(t.innerPadding)}">${t.content}</mj-text>
|
|
6
|
+
</mj-hero>`}function D(e){return e.replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">")}function O(e){const t=[`href="${I(e.href)}"`];return e.color&&t.push(`color="${e.color}"`),e.fontWeight&&t.push(`font-weight="${e.fontWeight}"`),e.textDecoration&&t.push(`text-decoration="${e.textDecoration}"`),e.padding&&t.push(`padding="${e.padding}"`),`<mj-navbar-link ${t.join(" ")}>${E(e.text)}</mj-navbar-link>`}function M(e){const{links:t,hamburger:r,padding:o}=e.values,n=[`hamburger="${r}"`,`padding="${u(o)}"`],a=t.map(c=>O(c)).join(`
|
|
7
|
+
`);return`<mj-navbar ${n.join(" ")}>
|
|
8
|
+
${a}
|
|
9
|
+
</mj-navbar>`}function I(e){return e.replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">")}function E(e){return e.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")}function L(e){switch(e.type){case"text":return h(e);case"image":return j(e);case"button":return b(e);case"divider":return k(e);case"spacer":return x(e);case"social":return v(e);case"html":return w(e);case"hero":return y(e);case"navbar":return M(e);default:return P(e)}}function P(e){const t=e.type,r=T.getBlockDefinition(t);return r!=null&&r.renderMjml?r.renderMjml(e):`<!-- Unknown block type: ${t} -->`}function U(e,t){const{backgroundColor:r,padding:o,borderRadius:n,verticalAlign:a}=e.attributes,c=[`width="${t}"`,`padding="${u(o)}"`,`vertical-align="${a}"`];r&&c.push(`background-color="${r}"`),n!==void 0&&n>0&&c.push(`border-radius="${n}px"`);const i=e.blocks.map(s=>` ${L(s)}`).join(`
|
|
10
|
+
`);return` <mj-column ${c.join(" ")}>
|
|
11
|
+
${i}
|
|
12
|
+
</mj-column>`}function K(e){if(e.columns.length===1&&e.columns[0].blocks.length===1&&e.columns[0].blocks[0].type==="hero")return f(B(e.columns[0].blocks[0]),e.attributes.condition);const{backgroundColor:t,backgroundImage:r,padding:o,fullWidth:n}=e.attributes,a=[`padding="${u(o)}"`];n&&a.push('full-width="full-width"'),t&&a.push(`background-color="${t}"`),r&&(a.push(`background-url="${C(r)}"`),a.push('background-size="cover"'),a.push('background-repeat="no-repeat"'));const c=e.columnRatios.reduce((s,l)=>s+l,0),i=e.columns.map((s,l)=>{const d=((e.columnRatios[l]??1)/c*100).toFixed(2).replace(/\.?0+$/,"");return U(s,`${d}%`)}).join(`
|
|
13
|
+
`);return f(` <mj-section ${a.join(" ")}>
|
|
14
|
+
${i}
|
|
15
|
+
</mj-section>`,e.attributes.condition)}function f(e,t){const r=t==null?void 0:t.trim();return r?` <mj-raw>{{#if ${r}}}</mj-raw>
|
|
16
|
+
${e}
|
|
17
|
+
<mj-raw>{{/if}}</mj-raw>`:e}const G=` <mj-raw>
|
|
18
|
+
<!--[if mso]>
|
|
19
|
+
<style>
|
|
20
|
+
body, table, td { font-family: Arial, Helvetica, sans-serif !important; }
|
|
21
|
+
blockquote { margin: 0 0 16px !important; }
|
|
22
|
+
</style>
|
|
23
|
+
<![endif]-->
|
|
24
|
+
</mj-raw>`,F=` <mj-raw>
|
|
25
|
+
<meta name="color-scheme" content="light dark">
|
|
26
|
+
<meta name="supported-color-schemes" content="light dark">
|
|
27
|
+
</mj-raw>`,J=` <mj-style inline="inline">
|
|
28
|
+
h1, h2, h3, blockquote { margin: 0 0 0.5em; }
|
|
29
|
+
</mj-style>`;function Q(e,t){const{fontFamily:r}=e.body.attributes,o=e.metadata.previewText,n=[];return t.outlookWorkarounds&&(n.push(G),n.push(F),n.push(J)),n.push(` <mj-attributes>
|
|
30
|
+
<mj-all font-family="${C(r)}" />
|
|
31
|
+
<mj-text font-size="14px" line-height="1.5" />
|
|
32
|
+
<mj-button font-size="14px" />
|
|
33
|
+
</mj-attributes>`),o&&n.push(` <mj-preview>${V(o)}</mj-preview>`),` <mj-head>
|
|
34
|
+
${n.join(`
|
|
35
|
+
`)}
|
|
36
|
+
</mj-head>`}function A(e,t){const r={outlookWorkarounds:(t==null?void 0:t.outlookWorkarounds)??!0},{width:o,backgroundColor:n}=e.body.attributes,a=[`width="${o}px"`];n&&a.push(`background-color="${n}"`);const c=Q(e,r),i=e.body.rows.map(s=>K(s)).join(`
|
|
37
|
+
`);return`<mjml>
|
|
38
|
+
${c}
|
|
39
|
+
<mj-body ${a.join(" ")}>
|
|
40
|
+
${i}
|
|
41
|
+
</mj-body>
|
|
42
|
+
</mjml>`}function C(e){return e.replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">")}function V(e){return e.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")}class X{async render(t,r){const o=A(t,{outlookWorkarounds:r==null?void 0:r.outlookWorkarounds}),n={keepComments:!1};r!=null&&r.minify&&(n.minify=!0),r!=null&&r.beautify&&(n.beautify=!0);try{const a=R(o,n),c=(a.errors||[]).map(i=>({message:i.message,line:i.line,tagName:i.tagName}));return{html:a.html,errors:c}}catch(a){return{html:"",errors:[{message:`MJML compilation failed: ${a instanceof Error?a.message:String(a)}`}]}}}}exports.MjmlRenderer=X;exports.documentToMjml=A;exports.renderButtonBlock=b;exports.renderDividerBlock=k;exports.renderHeroBlock=y;exports.renderHeroSection=B;exports.renderHtmlBlock=w;exports.renderImageBlock=j;exports.renderNavBarBlock=M;exports.renderSocialBlock=v;exports.renderSpacerBlock=x;exports.renderTextBlock=h;exports.spacingToMjml=u;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { ButtonBlock } from '@lit-pigeon/core';
|
|
2
|
+
import { DividerBlock } from '@lit-pigeon/core';
|
|
3
|
+
import { HeroBlock } from '@lit-pigeon/core';
|
|
4
|
+
import { HtmlBlock } from '@lit-pigeon/core';
|
|
5
|
+
import { ImageBlock } from '@lit-pigeon/core';
|
|
6
|
+
import { NavBarBlock } from '@lit-pigeon/core';
|
|
7
|
+
import { PigeonDocument } from '@lit-pigeon/core';
|
|
8
|
+
import { Renderer } from '@lit-pigeon/core';
|
|
9
|
+
import { RenderOptions } from '@lit-pigeon/core';
|
|
10
|
+
import { RenderResult } from '@lit-pigeon/core';
|
|
11
|
+
import { SocialBlock } from '@lit-pigeon/core';
|
|
12
|
+
import { SpacerBlock } from '@lit-pigeon/core';
|
|
13
|
+
import { Spacing } from '@lit-pigeon/core';
|
|
14
|
+
import { TextBlock } from '@lit-pigeon/core';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Converts a PigeonDocument into a complete MJML markup string.
|
|
18
|
+
*
|
|
19
|
+
* This function maps the document's body attributes, rows, columns,
|
|
20
|
+
* and content blocks to their MJML equivalents. The resulting string
|
|
21
|
+
* can then be passed to mjml2html() to produce the final HTML output.
|
|
22
|
+
*
|
|
23
|
+
* @param doc - The PigeonDocument to render.
|
|
24
|
+
* @param options - Optional MJML-shaping options. `outlookWorkarounds`
|
|
25
|
+
* defaults to `true` and injects Outlook (mso) + dark-mode workarounds.
|
|
26
|
+
*/
|
|
27
|
+
export declare function documentToMjml(doc: PigeonDocument, options?: DocumentToMjmlOptions): string;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Options that influence the MJML document the renderer emits.
|
|
31
|
+
*
|
|
32
|
+
* Kept as a local subset of `@lit-pigeon/core`'s `RenderOptions` — only the
|
|
33
|
+
* fields that affect MJML markup generation are relevant here (other fields
|
|
34
|
+
* like `minify`/`beautify` apply to the mjml2html compilation step).
|
|
35
|
+
*/
|
|
36
|
+
declare interface DocumentToMjmlOptions {
|
|
37
|
+
/**
|
|
38
|
+
* Inject Outlook (mso) and dark-mode rendering workarounds into the
|
|
39
|
+
* document. Defaults to `true`. Set to `false` to emit a bare MJML
|
|
40
|
+
* document without the heading-margin reset, dark-mode meta tags, or
|
|
41
|
+
* `[if mso]` conditional block.
|
|
42
|
+
*/
|
|
43
|
+
outlookWorkarounds?: boolean;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* MJML-based renderer that implements the Pigeon Renderer interface.
|
|
48
|
+
*
|
|
49
|
+
* Converts a PigeonDocument into a full MJML markup string, then uses
|
|
50
|
+
* the mjml library to compile it into responsive HTML email output.
|
|
51
|
+
*/
|
|
52
|
+
export declare class MjmlRenderer implements Renderer {
|
|
53
|
+
/**
|
|
54
|
+
* Renders a PigeonDocument to HTML using MJML as the compilation backend.
|
|
55
|
+
*
|
|
56
|
+
* @param doc - The Pigeon email document to render
|
|
57
|
+
* @param options - Optional rendering configuration (minify, beautify, inlineCss)
|
|
58
|
+
* @returns A promise resolving to the rendered HTML and any errors
|
|
59
|
+
*/
|
|
60
|
+
render(doc: PigeonDocument, options?: RenderOptions): Promise<RenderResult>;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Renders a ButtonBlock to an MJML <mj-button> element.
|
|
65
|
+
*/
|
|
66
|
+
export declare function renderButtonBlock(block: ButtonBlock): string;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Renders a DividerBlock to an MJML <mj-divider> element.
|
|
70
|
+
*/
|
|
71
|
+
export declare function renderDividerBlock(block: DividerBlock): string;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Renders a HeroBlock to MJML markup.
|
|
75
|
+
* Note: mj-hero is a top-level section element in MJML.
|
|
76
|
+
* This renders the inner content; the wrapping <mj-hero> is handled by renderRow().
|
|
77
|
+
*/
|
|
78
|
+
export declare function renderHeroBlock(block: HeroBlock): string;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Renders the full mj-hero wrapper for a hero block.
|
|
82
|
+
* Called from renderRow when it detects a hero block.
|
|
83
|
+
*/
|
|
84
|
+
export declare function renderHeroSection(block: HeroBlock): string;
|
|
85
|
+
|
|
86
|
+
export declare function renderHtmlBlock(block: HtmlBlock): string;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Renders an ImageBlock to an MJML <mj-image> element.
|
|
90
|
+
*/
|
|
91
|
+
export declare function renderImageBlock(block: ImageBlock): string;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Renders a NavBarBlock to an MJML <mj-navbar> element.
|
|
95
|
+
*/
|
|
96
|
+
export declare function renderNavBarBlock(block: NavBarBlock): string;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Renders a SocialBlock to an MJML <mj-social> element.
|
|
100
|
+
*/
|
|
101
|
+
export declare function renderSocialBlock(block: SocialBlock): string;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Renders a SpacerBlock to an MJML <mj-spacer> element.
|
|
105
|
+
*/
|
|
106
|
+
export declare function renderSpacerBlock(block: SpacerBlock): string;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Renders a TextBlock to an MJML <mj-text> element.
|
|
110
|
+
*/
|
|
111
|
+
export declare function renderTextBlock(block: TextBlock): string;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Converts a Spacing object to an MJML-compatible padding string.
|
|
115
|
+
* Returns a CSS shorthand string in the order: top right bottom left.
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* spacingToMjml({ top: 10, right: 20, bottom: 10, left: 20 })
|
|
119
|
+
* // => "10px 20px 10px 20px"
|
|
120
|
+
*/
|
|
121
|
+
export declare function spacingToMjml(spacing: Spacing): string;
|
|
122
|
+
|
|
123
|
+
export { }
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
import b from "mjml";
|
|
2
|
+
import { getBlockDefinition as k } from "@lit-pigeon/core";
|
|
3
|
+
function u(e) {
|
|
4
|
+
return `${e.top}px ${e.right}px ${e.bottom}px ${e.left}px`;
|
|
5
|
+
}
|
|
6
|
+
function x(e) {
|
|
7
|
+
const { content: t, padding: r, textAlign: a, lineHeight: n } = e.values;
|
|
8
|
+
return `<mj-text ${[
|
|
9
|
+
`padding="${u(r)}"`,
|
|
10
|
+
`align="${a}"`,
|
|
11
|
+
`line-height="${n}"`
|
|
12
|
+
].join(" ")}>${t}</mj-text>`;
|
|
13
|
+
}
|
|
14
|
+
function v(e) {
|
|
15
|
+
const { src: t, alt: r, width: a, href: n, padding: o, alignment: i, borderRadius: c } = e.values, s = [
|
|
16
|
+
`src="${m(t)}"`,
|
|
17
|
+
`alt="${m(r)}"`,
|
|
18
|
+
`padding="${u(o)}"`,
|
|
19
|
+
`align="${i}"`
|
|
20
|
+
];
|
|
21
|
+
return a !== "auto" && s.push(`width="${a}px"`), n && s.push(`href="${m(n)}"`), c !== void 0 && c > 0 && s.push(`border-radius="${c}px"`), `<mj-image ${s.join(" ")} />`;
|
|
22
|
+
}
|
|
23
|
+
function m(e) {
|
|
24
|
+
return e.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");
|
|
25
|
+
}
|
|
26
|
+
function w(e) {
|
|
27
|
+
const {
|
|
28
|
+
content: t,
|
|
29
|
+
href: r,
|
|
30
|
+
backgroundColor: a,
|
|
31
|
+
textColor: n,
|
|
32
|
+
borderRadius: o,
|
|
33
|
+
padding: i,
|
|
34
|
+
innerPadding: c,
|
|
35
|
+
fontSize: s,
|
|
36
|
+
fontWeight: l,
|
|
37
|
+
alignment: p,
|
|
38
|
+
fullWidth: d
|
|
39
|
+
} = e.values, g = [
|
|
40
|
+
`href="${y(r)}"`,
|
|
41
|
+
`background-color="${a}"`,
|
|
42
|
+
`color="${n}"`,
|
|
43
|
+
`border-radius="${o}px"`,
|
|
44
|
+
`padding="${u(i)}"`,
|
|
45
|
+
`inner-padding="${u(c)}"`,
|
|
46
|
+
`font-size="${s}px"`,
|
|
47
|
+
`font-weight="${l}"`,
|
|
48
|
+
`align="${p}"`
|
|
49
|
+
];
|
|
50
|
+
d && g.push('width="100%"');
|
|
51
|
+
const j = t.replace(/^\s*<p>([\s\S]*?)<\/p>\s*$/i, "$1");
|
|
52
|
+
return `<mj-button ${g.join(" ")}>${j}</mj-button>`;
|
|
53
|
+
}
|
|
54
|
+
function y(e) {
|
|
55
|
+
return e.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");
|
|
56
|
+
}
|
|
57
|
+
function A(e) {
|
|
58
|
+
const { borderColor: t, borderWidth: r, borderStyle: a, padding: n, width: o } = e.values;
|
|
59
|
+
return `<mj-divider ${[
|
|
60
|
+
`border-color="${t}"`,
|
|
61
|
+
`border-width="${r}px"`,
|
|
62
|
+
`border-style="${a}"`,
|
|
63
|
+
`padding="${u(n)}"`,
|
|
64
|
+
`width="${o}"`
|
|
65
|
+
].join(" ")} />`;
|
|
66
|
+
}
|
|
67
|
+
function B(e) {
|
|
68
|
+
const { height: t } = e.values;
|
|
69
|
+
return `<mj-spacer height="${t}px" />`;
|
|
70
|
+
}
|
|
71
|
+
function C(e) {
|
|
72
|
+
switch (e.type) {
|
|
73
|
+
case "facebook":
|
|
74
|
+
return "facebook";
|
|
75
|
+
case "twitter":
|
|
76
|
+
return "twitter";
|
|
77
|
+
case "instagram":
|
|
78
|
+
return "instagram";
|
|
79
|
+
case "linkedin":
|
|
80
|
+
return "linkedin";
|
|
81
|
+
case "youtube":
|
|
82
|
+
return "youtube";
|
|
83
|
+
case "tiktok":
|
|
84
|
+
return "web";
|
|
85
|
+
case "custom":
|
|
86
|
+
return "web";
|
|
87
|
+
default:
|
|
88
|
+
return "web";
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
function M(e, t) {
|
|
92
|
+
const a = [
|
|
93
|
+
`name="${C(e)}"`,
|
|
94
|
+
`href="${$(e.href)}"`,
|
|
95
|
+
`icon-size="${t}px"`
|
|
96
|
+
];
|
|
97
|
+
e.iconUrl && a.push(`src="${$(e.iconUrl)}"`);
|
|
98
|
+
const n = e.label ?? "";
|
|
99
|
+
return `<mj-social-element ${a.join(" ")}>${S(n)}</mj-social-element>`;
|
|
100
|
+
}
|
|
101
|
+
function R(e) {
|
|
102
|
+
const { icons: t, iconSize: r, spacing: a, alignment: n, padding: o } = e.values, i = [
|
|
103
|
+
`icon-size="${r}px"`,
|
|
104
|
+
`icon-padding="${a}px"`,
|
|
105
|
+
`align="${n}"`,
|
|
106
|
+
`padding="${u(o)}"`,
|
|
107
|
+
'mode="horizontal"'
|
|
108
|
+
], c = t.map((s) => M(s, r)).join(`
|
|
109
|
+
`);
|
|
110
|
+
return `<mj-social ${i.join(" ")}>
|
|
111
|
+
${c}
|
|
112
|
+
</mj-social>`;
|
|
113
|
+
}
|
|
114
|
+
function $(e) {
|
|
115
|
+
return e.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");
|
|
116
|
+
}
|
|
117
|
+
function S(e) {
|
|
118
|
+
return e.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
119
|
+
}
|
|
120
|
+
const W = /<(\s*\/?\s*mj-raw\b[^>]*)>/gi;
|
|
121
|
+
function H(e) {
|
|
122
|
+
return e.replace(W, "<$1>");
|
|
123
|
+
}
|
|
124
|
+
function T(e) {
|
|
125
|
+
const { content: t, padding: r } = e.values, a = u(r), n = H(t);
|
|
126
|
+
return `<mj-raw><div style="padding: ${a};">${n}</div></mj-raw>`;
|
|
127
|
+
}
|
|
128
|
+
function _(e) {
|
|
129
|
+
const { content: t, innerPadding: r } = e.values;
|
|
130
|
+
return `<mj-text ${[
|
|
131
|
+
`padding="${u(r)}"`
|
|
132
|
+
].join(" ")}>${t}</mj-text>`;
|
|
133
|
+
}
|
|
134
|
+
function z(e) {
|
|
135
|
+
const t = e.values, r = [
|
|
136
|
+
`mode="${t.mode}"`,
|
|
137
|
+
`height="${t.height}px"`,
|
|
138
|
+
`vertical-align="${t.verticalAlign}"`,
|
|
139
|
+
`padding="${u(t.padding)}"`
|
|
140
|
+
];
|
|
141
|
+
return t.backgroundUrl && r.push(`background-width="${t.width}px"`), t.backgroundUrl && (r.push(`background-url="${N(t.backgroundUrl)}"`), r.push(`background-position="${t.backgroundPosition}"`)), t.backgroundColor && r.push(`background-color="${t.backgroundColor}"`), ` <mj-hero ${r.join(" ")}>
|
|
142
|
+
<mj-text padding="${u(t.innerPadding)}">${t.content}</mj-text>
|
|
143
|
+
</mj-hero>`;
|
|
144
|
+
}
|
|
145
|
+
function N(e) {
|
|
146
|
+
return e.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");
|
|
147
|
+
}
|
|
148
|
+
function q(e) {
|
|
149
|
+
const t = [
|
|
150
|
+
`href="${O(e.href)}"`
|
|
151
|
+
];
|
|
152
|
+
return e.color && t.push(`color="${e.color}"`), e.fontWeight && t.push(`font-weight="${e.fontWeight}"`), e.textDecoration && t.push(`text-decoration="${e.textDecoration}"`), e.padding && t.push(`padding="${e.padding}"`), `<mj-navbar-link ${t.join(" ")}>${E(e.text)}</mj-navbar-link>`;
|
|
153
|
+
}
|
|
154
|
+
function D(e) {
|
|
155
|
+
const { links: t, hamburger: r, padding: a } = e.values, n = [
|
|
156
|
+
`hamburger="${r}"`,
|
|
157
|
+
`padding="${u(a)}"`
|
|
158
|
+
], o = t.map((i) => q(i)).join(`
|
|
159
|
+
`);
|
|
160
|
+
return `<mj-navbar ${n.join(" ")}>
|
|
161
|
+
${o}
|
|
162
|
+
</mj-navbar>`;
|
|
163
|
+
}
|
|
164
|
+
function O(e) {
|
|
165
|
+
return e.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");
|
|
166
|
+
}
|
|
167
|
+
function E(e) {
|
|
168
|
+
return e.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
169
|
+
}
|
|
170
|
+
function I(e) {
|
|
171
|
+
switch (e.type) {
|
|
172
|
+
case "text":
|
|
173
|
+
return x(e);
|
|
174
|
+
case "image":
|
|
175
|
+
return v(e);
|
|
176
|
+
case "button":
|
|
177
|
+
return w(e);
|
|
178
|
+
case "divider":
|
|
179
|
+
return A(e);
|
|
180
|
+
case "spacer":
|
|
181
|
+
return B(e);
|
|
182
|
+
case "social":
|
|
183
|
+
return R(e);
|
|
184
|
+
case "html":
|
|
185
|
+
return T(e);
|
|
186
|
+
case "hero":
|
|
187
|
+
return _(e);
|
|
188
|
+
case "navbar":
|
|
189
|
+
return D(e);
|
|
190
|
+
default:
|
|
191
|
+
return L(e);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
function L(e) {
|
|
195
|
+
const t = e.type, r = k(t);
|
|
196
|
+
return r != null && r.renderMjml ? r.renderMjml(e) : `<!-- Unknown block type: ${t} -->`;
|
|
197
|
+
}
|
|
198
|
+
function U(e, t) {
|
|
199
|
+
const { backgroundColor: r, padding: a, borderRadius: n, verticalAlign: o } = e.attributes, i = [
|
|
200
|
+
`width="${t}"`,
|
|
201
|
+
`padding="${u(a)}"`,
|
|
202
|
+
`vertical-align="${o}"`
|
|
203
|
+
];
|
|
204
|
+
r && i.push(`background-color="${r}"`), n !== void 0 && n > 0 && i.push(`border-radius="${n}px"`);
|
|
205
|
+
const c = e.blocks.map((s) => ` ${I(s)}`).join(`
|
|
206
|
+
`);
|
|
207
|
+
return ` <mj-column ${i.join(" ")}>
|
|
208
|
+
${c}
|
|
209
|
+
</mj-column>`;
|
|
210
|
+
}
|
|
211
|
+
function P(e) {
|
|
212
|
+
if (e.columns.length === 1 && e.columns[0].blocks.length === 1 && e.columns[0].blocks[0].type === "hero")
|
|
213
|
+
return f(
|
|
214
|
+
z(e.columns[0].blocks[0]),
|
|
215
|
+
e.attributes.condition
|
|
216
|
+
);
|
|
217
|
+
const { backgroundColor: t, backgroundImage: r, padding: a, fullWidth: n } = e.attributes, o = [
|
|
218
|
+
`padding="${u(a)}"`
|
|
219
|
+
];
|
|
220
|
+
n && o.push('full-width="full-width"'), t && o.push(`background-color="${t}"`), r && (o.push(`background-url="${h(r)}"`), o.push('background-size="cover"'), o.push('background-repeat="no-repeat"'));
|
|
221
|
+
const i = e.columnRatios.reduce((s, l) => s + l, 0), c = e.columns.map((s, l) => {
|
|
222
|
+
const d = ((e.columnRatios[l] ?? 1) / i * 100).toFixed(2).replace(/\.?0+$/, "");
|
|
223
|
+
return U(s, `${d}%`);
|
|
224
|
+
}).join(`
|
|
225
|
+
`);
|
|
226
|
+
return f(
|
|
227
|
+
` <mj-section ${o.join(" ")}>
|
|
228
|
+
${c}
|
|
229
|
+
</mj-section>`,
|
|
230
|
+
e.attributes.condition
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
function f(e, t) {
|
|
234
|
+
const r = t == null ? void 0 : t.trim();
|
|
235
|
+
return r ? ` <mj-raw>{{#if ${r}}}</mj-raw>
|
|
236
|
+
${e}
|
|
237
|
+
<mj-raw>{{/if}}</mj-raw>` : e;
|
|
238
|
+
}
|
|
239
|
+
const K = ` <mj-raw>
|
|
240
|
+
<!--[if mso]>
|
|
241
|
+
<style>
|
|
242
|
+
body, table, td { font-family: Arial, Helvetica, sans-serif !important; }
|
|
243
|
+
blockquote { margin: 0 0 16px !important; }
|
|
244
|
+
</style>
|
|
245
|
+
<![endif]-->
|
|
246
|
+
</mj-raw>`, G = ` <mj-raw>
|
|
247
|
+
<meta name="color-scheme" content="light dark">
|
|
248
|
+
<meta name="supported-color-schemes" content="light dark">
|
|
249
|
+
</mj-raw>`, F = ` <mj-style inline="inline">
|
|
250
|
+
h1, h2, h3, blockquote { margin: 0 0 0.5em; }
|
|
251
|
+
</mj-style>`;
|
|
252
|
+
function J(e, t) {
|
|
253
|
+
const { fontFamily: r } = e.body.attributes, a = e.metadata.previewText, n = [];
|
|
254
|
+
return t.outlookWorkarounds && (n.push(K), n.push(G), n.push(F)), n.push(` <mj-attributes>
|
|
255
|
+
<mj-all font-family="${h(r)}" />
|
|
256
|
+
<mj-text font-size="14px" line-height="1.5" />
|
|
257
|
+
<mj-button font-size="14px" />
|
|
258
|
+
</mj-attributes>`), a && n.push(` <mj-preview>${V(a)}</mj-preview>`), ` <mj-head>
|
|
259
|
+
${n.join(`
|
|
260
|
+
`)}
|
|
261
|
+
</mj-head>`;
|
|
262
|
+
}
|
|
263
|
+
function Q(e, t) {
|
|
264
|
+
const r = {
|
|
265
|
+
outlookWorkarounds: (t == null ? void 0 : t.outlookWorkarounds) ?? !0
|
|
266
|
+
}, { width: a, backgroundColor: n } = e.body.attributes, o = [
|
|
267
|
+
`width="${a}px"`
|
|
268
|
+
];
|
|
269
|
+
n && o.push(`background-color="${n}"`);
|
|
270
|
+
const i = J(e, r), c = e.body.rows.map((s) => P(s)).join(`
|
|
271
|
+
`);
|
|
272
|
+
return `<mjml>
|
|
273
|
+
${i}
|
|
274
|
+
<mj-body ${o.join(" ")}>
|
|
275
|
+
${c}
|
|
276
|
+
</mj-body>
|
|
277
|
+
</mjml>`;
|
|
278
|
+
}
|
|
279
|
+
function h(e) {
|
|
280
|
+
return e.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");
|
|
281
|
+
}
|
|
282
|
+
function V(e) {
|
|
283
|
+
return e.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
284
|
+
}
|
|
285
|
+
class Z {
|
|
286
|
+
/**
|
|
287
|
+
* Renders a PigeonDocument to HTML using MJML as the compilation backend.
|
|
288
|
+
*
|
|
289
|
+
* @param doc - The Pigeon email document to render
|
|
290
|
+
* @param options - Optional rendering configuration (minify, beautify, inlineCss)
|
|
291
|
+
* @returns A promise resolving to the rendered HTML and any errors
|
|
292
|
+
*/
|
|
293
|
+
async render(t, r) {
|
|
294
|
+
const a = Q(t, {
|
|
295
|
+
outlookWorkarounds: r == null ? void 0 : r.outlookWorkarounds
|
|
296
|
+
}), n = {
|
|
297
|
+
// By default MJML inlines CSS; respect the option if provided
|
|
298
|
+
keepComments: !1
|
|
299
|
+
};
|
|
300
|
+
r != null && r.minify && (n.minify = !0), r != null && r.beautify && (n.beautify = !0);
|
|
301
|
+
try {
|
|
302
|
+
const o = b(a, n), i = (o.errors || []).map((c) => ({
|
|
303
|
+
message: c.message,
|
|
304
|
+
line: c.line,
|
|
305
|
+
tagName: c.tagName
|
|
306
|
+
}));
|
|
307
|
+
return {
|
|
308
|
+
html: o.html,
|
|
309
|
+
errors: i
|
|
310
|
+
};
|
|
311
|
+
} catch (o) {
|
|
312
|
+
return {
|
|
313
|
+
html: "",
|
|
314
|
+
errors: [{ message: `MJML compilation failed: ${o instanceof Error ? o.message : String(o)}` }]
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
export {
|
|
320
|
+
Z as MjmlRenderer,
|
|
321
|
+
Q as documentToMjml,
|
|
322
|
+
w as renderButtonBlock,
|
|
323
|
+
A as renderDividerBlock,
|
|
324
|
+
_ as renderHeroBlock,
|
|
325
|
+
z as renderHeroSection,
|
|
326
|
+
T as renderHtmlBlock,
|
|
327
|
+
v as renderImageBlock,
|
|
328
|
+
D as renderNavBarBlock,
|
|
329
|
+
R as renderSocialBlock,
|
|
330
|
+
B as renderSpacerBlock,
|
|
331
|
+
x as renderTextBlock,
|
|
332
|
+
u as spacingToMjml
|
|
333
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lit-pigeon/renderer-mjml",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "MJML-based HTML renderer for Pigeon email editor",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"require": "./dist/index.cjs",
|
|
13
|
+
"types": "./dist/index.d.ts"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"mjml": "^4.15.0",
|
|
21
|
+
"@lit-pigeon/core": "^0.1.1"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@types/mjml": "^4.7.0",
|
|
25
|
+
"typescript": "^5.5.0",
|
|
26
|
+
"vite": "^5.4.0",
|
|
27
|
+
"vite-plugin-dts": "^4.0.0",
|
|
28
|
+
"vitest": "^2.0.0"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "https://github.com/snxstudio/lit-pigeon.git",
|
|
36
|
+
"directory": "packages/renderer-mjml"
|
|
37
|
+
},
|
|
38
|
+
"license": "MIT",
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "vite build",
|
|
41
|
+
"dev": "vite build --watch",
|
|
42
|
+
"test": "vitest run",
|
|
43
|
+
"test:watch": "vitest",
|
|
44
|
+
"clean": "rm -rf dist"
|
|
45
|
+
}
|
|
46
|
+
}
|