@lit-pigeon/renderer-mjml 0.2.4 → 0.3.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/README.md +18 -2
- package/dist/index.cjs +68 -31
- package/dist/index.d.ts +10 -0
- package/dist/index.js +399 -199
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -11,7 +11,10 @@ email-client-safe, inlined-CSS, table-based HTML (with optional Outlook
|
|
|
11
11
|
npm install @lit-pigeon/renderer-mjml
|
|
12
12
|
```
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
Depends on [`mjml`](https://mjml.io) 4, which runs in Node.js. In a browser
|
|
15
|
+
bundle, alias `mjml` to `mjml-browser` (for example with
|
|
16
|
+
`"overrides": { "mjml": "npm:mjml-browser@^4.18.0" }` in `package.json`), or
|
|
17
|
+
the build fails with `Could not resolve "fs"`.
|
|
15
18
|
|
|
16
19
|
## Usage
|
|
17
20
|
|
|
@@ -25,14 +28,27 @@ const doc = createDefaultDocument();
|
|
|
25
28
|
const { html, errors } = await renderer.render(doc, {
|
|
26
29
|
outlookWorkarounds: true, // default
|
|
27
30
|
minify: false,
|
|
31
|
+
fonts: [{ name: 'Inter', family: 'Inter, Arial, sans-serif', url: 'https://fonts.googleapis.com/css2?family=Inter' }],
|
|
28
32
|
});
|
|
29
33
|
|
|
30
34
|
// `html` is ready to send; `errors` is empty on success.
|
|
31
35
|
```
|
|
32
36
|
|
|
37
|
+
`render()` never rejects: MJML problems are returned in `errors`, and an
|
|
38
|
+
unrecoverable failure returns `html: ''` with one error. Options are
|
|
39
|
+
`outlookWorkarounds` (default `true`: Outlook `mso` styles, heading margin
|
|
40
|
+
reset and dark-mode `color-scheme` meta tags), `fonts` (emitted as
|
|
41
|
+
`<mj-font>`), `minify` and `beautify`. `inlineCss` is accepted but has no
|
|
42
|
+
effect; MJML always inlines CSS.
|
|
43
|
+
|
|
33
44
|
`MjmlRenderer` implements the core `Renderer` interface, so it can be passed
|
|
34
45
|
straight to the editor's preview/export path or the framework wrappers. Need
|
|
35
|
-
just the MJML markup (no HTML compile)? Use `documentToMjml(doc,
|
|
46
|
+
just the MJML markup (no HTML compile)? Use `documentToMjml(doc, { outlookWorkarounds?, fonts? })`.
|
|
47
|
+
Merge tags such as `{{first_name}}` are passed through unchanged, and a row's
|
|
48
|
+
`condition` is written as `{{#if …}}` … `{{/if}}` around its section. Custom
|
|
49
|
+
blocks are rendered with their `BlockDefinition.renderMjml`. The individual
|
|
50
|
+
block renderers (`renderTextBlock`, `renderImageBlock`, …) and `spacingToMjml`
|
|
51
|
+
are also exported.
|
|
36
52
|
|
|
37
53
|
For a DOM-free server pipeline (parse + validate + merge tags), see
|
|
38
54
|
[`@lit-pigeon/ssr`](../ssr).
|
package/dist/index.cjs
CHANGED
|
@@ -1,45 +1,82 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
`);return`
|
|
11
|
-
${
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
${
|
|
15
|
-
</mj-
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const G=require("mjml"),j=require("@lit-pigeon/core");function g(e){return`${e.top}px ${e.right}px ${e.bottom}px ${e.left}px`}function $(e){return[e.hideOnMobile&&"pigeon-hide-mobile",e.hideOnDesktop&&"pigeon-hide-desktop"].filter(Boolean).join(" ")}function O(e,t){const n=t&&/^\s*<mj-([\w-]+)/.exec(e),r=n&&n[1]!=="raw"?e.search(/ ?\/?>/):-1;if(r<0)return e;const s=e.slice(0,r);return(s.includes(' css-class="')?s.replace(/( css-class="[^"]*)/,`$1 ${t}`):`${s} css-class="${t}"`)+e.slice(r)}const K=` <mj-style>
|
|
2
|
+
${j.generatedStyleMarker("visibility")}
|
|
3
|
+
.pigeon-hide-desktop, .pigeon-hide-desktop-outlook { display: none !important; mso-hide: all !important; }
|
|
4
|
+
@media only screen and (max-width:479px) {
|
|
5
|
+
td.pigeon-hide-desktop { display: table-cell !important; }
|
|
6
|
+
div.pigeon-hide-desktop { display: block !important; }
|
|
7
|
+
table.pigeon-hide-desktop { display: table !important; }
|
|
8
|
+
.pigeon-hide-mobile, td.pigeon-hide-mobile, div.pigeon-hide-mobile, table.pigeon-hide-mobile { display: none !important; }
|
|
9
|
+
}
|
|
10
|
+
</mj-style>`;function I(e){const{content:t,padding:n,textAlign:r,lineHeight:s,cssClass:i}=e.values,o=[`padding="${g(n)}"`,`align="${r}"`,`line-height="${s}"`];return i&&o.push(`css-class="${F(i)}"`),`<mj-text ${o.join(" ")}>${t}</mj-text>`}function F(e){return e.replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">")}function R(e){const{src:t,alt:n,width:r,href:s,padding:i,alignment:o,borderRadius:a,cssClass:l}=e.values,d=[`src="${k(t)}"`,`alt="${k(n)}"`,`padding="${g(i)}"`,`align="${o}"`];return r!=="auto"&&d.push(`width="${r}px"`),s&&d.push(`href="${k(s)}"`),a!==void 0&&a>0&&d.push(`border-radius="${a}px"`),l&&d.push(`css-class="${k(l)}"`),`<mj-image ${d.join(" ")} />`}function k(e){return e.replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">")}function V(e){return e.width>0?`${e.width}px ${e.style} ${e.color}`:""}function D(e){const{content:t,href:n,backgroundColor:r,textColor:s,borderRadius:i,border:o,padding:a,innerPadding:l,fontSize:d,fontWeight:m,alignment:f,fullWidth:u,cssClass:c}=e.values,p=[`href="${v(n)}"`,`background-color="${r}"`,`color="${s}"`,`border-radius="${i}px"`,`padding="${g(a)}"`,`inner-padding="${g(l)}"`,`font-size="${d}px"`,`font-weight="${m}"`,`align="${f}"`],b=o&&V(o);b&&p.push(`border="${v(b)}"`),u&&p.push('width="100%"'),c&&p.push(`css-class="${v(c)}"`);const U=t.replace(/^\s*<p>([\s\S]*?)<\/p>\s*$/i,"$1");return`<mj-button ${p.join(" ")}>${U}</mj-button>`}function v(e){return e.replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">")}function H(e){const{borderColor:t,borderWidth:n,borderStyle:r,padding:s,width:i}=e.values;return`<mj-divider ${[`border-color="${t}"`,`border-width="${n}px"`,`border-style="${r}"`,`padding="${g(s)}"`,`width="${i}"`].join(" ")} />`}function q(e){const{height:t}=e.values;return`<mj-spacer height="${t}px" />`}function J(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 Y(e,t){const r=[`name="${J(e)}"`,`href="${T(e.href)}"`,`icon-size="${t}px"`];e.iconUrl&&r.push(`src="${T(e.iconUrl)}"`);const s=e.label??"";return`<mj-social-element ${r.join(" ")}>${Q(s)}</mj-social-element>`}function E(e){const{icons:t,iconSize:n,spacing:r,alignment:s,padding:i}=e.values,o=[`icon-size="${n}px"`,`icon-padding="${r}px"`,`align="${s}"`,`padding="${g(i)}"`,'mode="horizontal"'],a=t.map(l=>Y(l,n)).join(`
|
|
11
|
+
`);return`<mj-social ${o.join(" ")}>
|
|
12
|
+
${a}
|
|
13
|
+
</mj-social>`}function T(e){return e.replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">")}function Q(e){return e.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")}const X=/<(\s*(?:\/\s*)?mj-raw\b[^>]*)>/gi;function Z(e){const t=e.lastIndexOf(">")+1;return e.slice(0,t).replace(X,"<$1>")+e.slice(t)}function N(e){const{content:t,padding:n}=e.values,r=g(n),s=Z(t),i=$(e.values);return`<mj-raw><div class="lp-html${i?` ${i}`:""}" style="padding: ${r};">${s}</div></mj-raw>`}function z(e){const{content:t,innerPadding:n}=e.values;return`<mj-text ${[`padding="${g(n)}"`].join(" ")}>${t}</mj-text>`}function _(e){const t=e.values,n=[`mode="${t.mode}"`,`height="${t.height}px"`,`vertical-align="${t.verticalAlign}"`,`padding="${g(t.padding)}"`];return t.backgroundUrl&&n.push(`background-width="${t.width}px"`),t.backgroundUrl&&(n.push(`background-url="${ee(t.backgroundUrl)}"`),n.push(`background-position="${t.backgroundPosition}"`)),t.backgroundColor&&n.push(`background-color="${t.backgroundColor}"`),` <mj-hero ${n.join(" ")}>
|
|
14
|
+
<mj-text padding="${g(t.innerPadding)}">${t.content}</mj-text>
|
|
15
|
+
</mj-hero>`}function ee(e){return e.replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">")}function te(e){const t=[`href="${ne(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(" ")}>${re(e.text)}</mj-navbar-link>`}function L(e){const{links:t,hamburger:n,padding:r}=e.values,s=[`hamburger="${n}"`,`padding="${g(r)}"`],i=t.map(o=>te(o)).join(`
|
|
16
|
+
`);return`<mj-navbar ${s.join(" ")}>
|
|
17
|
+
${i}
|
|
18
|
+
</mj-navbar>`}function ne(e){return e.replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">")}function re(e){return e.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")}function se(e){switch(e.type){case"text":return I(e);case"image":return R(e);case"button":return D(e);case"divider":return H(e);case"spacer":return q(e);case"social":return E(e);case"html":return N(e);case"hero":return z(e);case"navbar":return L(e);default:return oe(e)}}function oe(e){const t=e.type,n=j.getBlockDefinition(t);return n!=null&&n.renderMjml?n.renderMjml(e):`<!-- Unknown block type: ${t} -->`}function ie(e,t){const{backgroundColor:n,padding:r,borderRadius:s,verticalAlign:i,cssClass:o}=e.attributes,a=[`width="${t}"`,`padding="${g(r)}"`,`vertical-align="${i}"`];n&&a.push(`background-color="${n}"`),s!==void 0&&s>0&&a.push(`border-radius="${s}px"`);const l=[o&&h(o),$(e.attributes)].filter(Boolean).join(" ");l&&a.push(`css-class="${l}"`);const d=e.blocks.map(m=>y(` ${O(se(m),$(m.values))}`,m.values.condition," ")).join(`
|
|
19
|
+
`);return` <mj-column ${a.join(" ")}>
|
|
20
|
+
${d}
|
|
21
|
+
</mj-column>`}function ae(e){if(e.columns.length===1&&e.columns[0].blocks.length===1&&e.columns[0].blocks[0].type==="hero"){const u=e.columns[0].blocks[0],c=$({hideOnMobile:e.attributes.hideOnMobile||u.values.hideOnMobile,hideOnDesktop:e.attributes.hideOnDesktop||u.values.hideOnDesktop});return y(C(y(O(_(u),c),u.values.condition),e.attributes.repeat),e.attributes.condition)}const{backgroundColor:t,padding:n,fullWidth:r,cssClass:s,noStackOnMobile:i}=e.attributes,o=[`padding="${g(n)}"`];r&&o.push('full-width="full-width"'),t&&o.push(`background-color="${t}"`);const a=j.resolveRowBackground(e);a&&(o.push(`background-url="${h(a.url)}"`),o.push(`background-size="${h(a.size)}"`),o.push(`background-repeat="${a.repeat}"`),o.push(`background-position="${h(a.position)}"`));const l=[s&&h(s),$(e.attributes)].filter(Boolean).join(" ");l&&o.push(`css-class="${l}"`);const d=e.columnRatios.reduce((u,c)=>u+c,0),m=e.columns.map((u,c)=>{const b=((e.columnRatios[c]??1)/d*100).toFixed(2).replace(/\.?0+$/,"");return ie(u,`${b}%`)}).join(`
|
|
22
|
+
`),f=i?` <mj-group>
|
|
23
|
+
${m}
|
|
24
|
+
</mj-group>`:m;return y(C(` <mj-section ${o.join(" ")}>
|
|
25
|
+
${f}
|
|
26
|
+
</mj-section>`,e.attributes.repeat),e.attributes.condition)}function y(e,t,n=" "){const r=t==null?void 0:t.trim();return r?`${n}<mj-raw>{{#if ${r}}}</mj-raw>
|
|
16
27
|
${e}
|
|
17
|
-
|
|
28
|
+
${n}<mj-raw>{{/if}}</mj-raw>`:e}function C(e,t){const n=t==null?void 0:t.trim();return n?` <mj-raw>{{#each ${n}}}</mj-raw>
|
|
29
|
+
${e}
|
|
30
|
+
<mj-raw>{{/each}}</mj-raw>`:e}const ce=` <mj-raw>
|
|
18
31
|
<!--[if mso]>
|
|
19
32
|
<style>
|
|
20
33
|
body, table, td { font-family: Arial, Helvetica, sans-serif !important; }
|
|
21
34
|
blockquote { margin: 0 0 16px !important; }
|
|
22
35
|
</style>
|
|
23
36
|
<![endif]-->
|
|
24
|
-
</mj-raw>`,
|
|
37
|
+
</mj-raw>`,le=` <mj-raw>
|
|
25
38
|
<meta name="color-scheme" content="light dark">
|
|
26
39
|
<meta name="supported-color-schemes" content="light dark">
|
|
27
|
-
</mj-raw>`,
|
|
40
|
+
</mj-raw>`,ue=` <mj-style inline="inline">
|
|
28
41
|
h1, h2, h3, blockquote { margin: 0 0 0.5em; }
|
|
29
|
-
</mj-style>`;function
|
|
30
|
-
`)}function
|
|
31
|
-
<mj-all font-family="${
|
|
42
|
+
</mj-style>`;function de(e){const t=new Set,n=[];for(const r of e){if(!r.url||t.has(r.url))continue;t.add(r.url);const s=r.family.split(",")[0].trim().replace(/^['"]|['"]$/g,""),i=h(r.url),o=h(s);n.push(` <mj-font name="${o}" href="${i}" />`),n.push(` <mj-raw><link rel="stylesheet" href="${i}"></mj-raw>`)}return n.join(`
|
|
43
|
+
`)}function pe(e){const{language:t,direction:n}=e.body.attributes;if(!t&&!n)return"";const r=j.resolveDirection(t,n);return`${t?` lang="${h(t)}"`:""} dir="${r}"`}function me(e,t){const{fontFamily:n,linkStyle:r,css:s}=e.body.attributes,i=e.metadata.previewText,o=[];t.outlookWorkarounds&&(o.push(ce),o.push(le),o.push(ue));const a=de(t.fonts);a&&o.push(a),o.push(` <mj-attributes>
|
|
44
|
+
<mj-all font-family="${h(n)}" />
|
|
32
45
|
<mj-text font-size="14px" line-height="1.5" />
|
|
33
46
|
<mj-button font-size="14px" />
|
|
34
|
-
</mj-attributes>`),
|
|
35
|
-
${
|
|
36
|
-
</mj-style>`),o&&
|
|
37
|
-
${
|
|
47
|
+
</mj-attributes>`),e.body.rows.some(u=>u.columns.some(c=>c.blocks.some(p=>p.type==="html")))&&o.push(` <mj-style inline="inline">
|
|
48
|
+
.lp-html { font-size: 14px; line-height: 1.5; font-family: ${n.replace(/[<>{};]/g,"")}; }
|
|
49
|
+
</mj-style>`),e.body.rows.some(u=>$(u.attributes)||u.columns.some(c=>$(c.attributes)||c.blocks.some(p=>$(p.values))))&&o.push(K);const m=r&&j.linkStyleToCss(r);m&&o.push(` <mj-style>
|
|
50
|
+
${m.split(`
|
|
51
|
+
`).map(u=>` ${u}`).join(`
|
|
52
|
+
`)}
|
|
53
|
+
</mj-style>`),s&&o.push(` <mj-style>
|
|
54
|
+
${s.replace(/<\/mj-style/gi,"<\\/mj-style")}
|
|
55
|
+
</mj-style>`);const f=e.metadata.name.trim();return f&&o.push(` <mj-title>${M(f)}</mj-title>`),i&&o.push(` <mj-preview>${M(i)}</mj-preview>`),` <mj-head>
|
|
56
|
+
${o.join(`
|
|
38
57
|
`)}
|
|
39
|
-
</mj-head>`}function
|
|
40
|
-
`);return`<mjml>
|
|
41
|
-
${
|
|
42
|
-
<mj-body ${
|
|
43
|
-
${
|
|
58
|
+
</mj-head>`}function W(e,t){const n={outlookWorkarounds:(t==null?void 0:t.outlookWorkarounds)??!0,fonts:(t==null?void 0:t.fonts)??[]},{width:r,backgroundColor:s}=e.body.attributes,i=[`width="${r}px"`];s&&i.push(`background-color="${s}"`);const o=me(e,n),a=e.body.rows.map(l=>ae(l)).join(`
|
|
59
|
+
`);return`<mjml${pe(e)}>
|
|
60
|
+
${o}
|
|
61
|
+
<mj-body ${i.join(" ")}>
|
|
62
|
+
${a}
|
|
44
63
|
</mj-body>
|
|
45
|
-
</mjml>`}function
|
|
64
|
+
</mjml>`}function h(e){return e.replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">")}function M(e){return e.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")}class ge{async render(t,n){const r=W(t,{outlookWorkarounds:n==null?void 0:n.outlookWorkarounds,fonts:n==null?void 0:n.fonts}),s={keepComments:!1};n!=null&&n.minify&&(s.minify=!0),n!=null&&n.beautify&&(s.beautify=!0);try{const i=G(r,s),o=(i.errors||[]).map(a=>({message:a.message,line:a.line,tagName:a.tagName}));return{html:i.html,errors:o}}catch(i){return{html:"",errors:[{message:`MJML compilation failed: ${i instanceof Error?i.message:String(i)}`}]}}}}const fe={amp:"&",lt:"<",gt:">",quot:'"',apos:"'",nbsp:" "},he=/^(p|div|h[1-6]|blockquote|table|tr|ul|ol|section|header|footer)$/,$e=/^(style|script)$/;function P(e){return e.replace(/&(#x[0-9a-f]+|#\d+|amp|lt|gt|quot|apos|nbsp);/gi,(t,n)=>{if(n[0]!=="#")return fe[n.toLowerCase()];const r=/x/i.test(n)?parseInt(n.slice(2),16):parseInt(n.slice(1),10);return r<=1114111?String.fromCodePoint(r):t})}function S(e,t){const n=new RegExp(`(?:^|\\s)${t}\\s*=\\s*(?:"([^"]*)"|'([^']*)'|([^\\s>]+))`,"i").exec(e);return n?P(n[1]??n[2]??n[3]):""}function x(e){return!!e&&e.trim()!=="#"}function w(e,t){return!x(t)||t===e?e:e?`${e} (${t})`:t}function A(e){const t=[];let n,r=0;const s=[],i=/<!--|<(\/?)([a-z][a-z0-9]*)\b([^<>]*)>|([^<]+)|</gi,o=/--!?>/g;let a;for(;a=i.exec(e);){const[l,d,m,f,u]=a;if(l==="<!--"){o.lastIndex=i.lastIndex,i.lastIndex=o.exec(e)?o.lastIndex:e.length;continue}if(u!==void 0||l==="<"){r||s.push(P((u??l).replace(/\s+/g," ")));continue}if(!m)continue;const c=m.toLowerCase();if($e.test(c))r=Math.max(0,r+(d?-1:1));else{if(r)continue;if(c==="br")s.push(`
|
|
65
|
+
`);else if(c==="img"&&!d)s.push(S(f,"alt"));else if(c==="a"){if(n){const p=s.splice(n.start).join("");s.push(/^\s*/.exec(p)[0],w(p.trim(),n.href))}n=d?void 0:{href:S(f,"href"),start:s.length}}else if(c==="li"){const p=t[t.length-1];d||s.push(`
|
|
66
|
+
${p!=null&&p.ordered?`${++p.count}.`:"-"} `)}else c==="ul"||c==="ol"?(d?t.pop():t.push({ordered:c==="ol",count:0}),s.push(`
|
|
67
|
+
|
|
68
|
+
`)):he.test(c)&&!t.length&&s.push(`
|
|
69
|
+
|
|
70
|
+
`)}}return s.join("").split(`
|
|
71
|
+
`).map(l=>l.trim()).join(`
|
|
72
|
+
`).replace(/\n{3,}/g,`
|
|
73
|
+
|
|
74
|
+
`).trim()}function je(e){return e[0].toUpperCase()+e.slice(1)}function B(e,t,n){const r=n==null?void 0:n.trim();return r&&e?`{{#${t} ${r}}}
|
|
75
|
+
${e}
|
|
76
|
+
{{/${t}}}`:e}function be(e){return B(ke(e),"if",e.values.condition)}function ke(e){switch(e.type){case"text":case"html":case"hero":return A(e.values.content);case"button":{const t=A(e.values.content);return x(e.values.href)?`${t}: ${e.values.href}`:t}case"image":return e.values.alt?w(e.values.alt,e.values.href):"";case"divider":return"----------";case"social":return e.values.icons.filter(t=>x(t.href)).map(t=>`${t.label||je(t.type)}: ${t.href}`).join(`
|
|
77
|
+
`);case"navbar":return e.values.links.map(t=>w(t.text,t.href)).join(" | ");default:return""}}function ye(e){const t=e.columns.flatMap(n=>n.blocks.map(be)).filter(Boolean).join(`
|
|
78
|
+
|
|
79
|
+
`);return B(B(t,"each",e.attributes.repeat),"if",e.attributes.condition)}function ve(e){const t=e.body.rows.map(ye).filter(Boolean).join(`
|
|
80
|
+
|
|
81
|
+
`);return t?`${t}
|
|
82
|
+
`:""}exports.MjmlRenderer=ge;exports.documentToMjml=W;exports.documentToPlainText=ve;exports.renderButtonBlock=D;exports.renderDividerBlock=H;exports.renderHeroBlock=z;exports.renderHeroSection=_;exports.renderHtmlBlock=N;exports.renderImageBlock=R;exports.renderNavBarBlock=L;exports.renderSocialBlock=E;exports.renderSpacerBlock=q;exports.renderTextBlock=I;exports.spacingToMjml=g;
|
package/dist/index.d.ts
CHANGED
|
@@ -49,6 +49,16 @@ declare interface DocumentToMjmlOptions {
|
|
|
49
49
|
fonts?: FontDefinition[];
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
+
/**
|
|
53
|
+
* Converts a PigeonDocument into the `text/plain` alternative part of a
|
|
54
|
+
* multipart email. Blocks are separated by a blank line; images fall back to
|
|
55
|
+
* their alt text, buttons render as `label: url` and dividers as a rule.
|
|
56
|
+
* Merge tags are kept verbatim, and row conditions, block conditions and row
|
|
57
|
+
* repeats are emitted as the same `{{#if}}` / `{{#each}}` wrappers the MJML
|
|
58
|
+
* renderer writes, so the sending platform substitutes both parts alike.
|
|
59
|
+
*/
|
|
60
|
+
export declare function documentToPlainText(doc: PigeonDocument): string;
|
|
61
|
+
|
|
52
62
|
/**
|
|
53
63
|
* MJML-based renderer that implements the Pigeon Renderer interface.
|
|
54
64
|
*
|
package/dist/index.js
CHANGED
|
@@ -1,78 +1,101 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { getBlockDefinition as
|
|
3
|
-
function
|
|
1
|
+
import R from "mjml";
|
|
2
|
+
import { generatedStyleMarker as D, linkStyleToCss as E, resolveRowBackground as H, resolveDirection as z, getBlockDefinition as N } from "@lit-pigeon/core";
|
|
3
|
+
function g(e) {
|
|
4
4
|
return `${e.top}px ${e.right}px ${e.bottom}px ${e.left}px`;
|
|
5
5
|
}
|
|
6
|
-
function
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
function $(e) {
|
|
7
|
+
return [e.hideOnMobile && "pigeon-hide-mobile", e.hideOnDesktop && "pigeon-hide-desktop"].filter(Boolean).join(" ");
|
|
8
|
+
}
|
|
9
|
+
function O(e, t) {
|
|
10
|
+
const n = t && /^\s*<mj-([\w-]+)/.exec(e), r = n && n[1] !== "raw" ? e.search(/ ?\/?>/) : -1;
|
|
11
|
+
if (r < 0) return e;
|
|
12
|
+
const s = e.slice(0, r);
|
|
13
|
+
return (s.includes(' css-class="') ? s.replace(/( css-class="[^"]*)/, `$1 ${t}`) : `${s} css-class="${t}"`) + e.slice(r);
|
|
14
|
+
}
|
|
15
|
+
const _ = ` <mj-style>
|
|
16
|
+
${D("visibility")}
|
|
17
|
+
.pigeon-hide-desktop, .pigeon-hide-desktop-outlook { display: none !important; mso-hide: all !important; }
|
|
18
|
+
@media only screen and (max-width:479px) {
|
|
19
|
+
td.pigeon-hide-desktop { display: table-cell !important; }
|
|
20
|
+
div.pigeon-hide-desktop { display: block !important; }
|
|
21
|
+
table.pigeon-hide-desktop { display: table !important; }
|
|
22
|
+
.pigeon-hide-mobile, td.pigeon-hide-mobile, div.pigeon-hide-mobile, table.pigeon-hide-mobile { display: none !important; }
|
|
23
|
+
}
|
|
24
|
+
</mj-style>`;
|
|
25
|
+
function q(e) {
|
|
26
|
+
const { content: t, padding: n, textAlign: r, lineHeight: s, cssClass: i } = e.values, o = [
|
|
27
|
+
`padding="${g(n)}"`,
|
|
28
|
+
`align="${r}"`,
|
|
29
|
+
`line-height="${s}"`
|
|
11
30
|
];
|
|
12
|
-
return
|
|
31
|
+
return i && o.push(`css-class="${L(i)}"`), `<mj-text ${o.join(" ")}>${t}</mj-text>`;
|
|
13
32
|
}
|
|
14
|
-
function
|
|
33
|
+
function L(e) {
|
|
15
34
|
return e.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");
|
|
16
35
|
}
|
|
17
|
-
function
|
|
18
|
-
const { src: t, alt:
|
|
19
|
-
`src="${
|
|
20
|
-
`alt="${
|
|
21
|
-
`padding="${
|
|
22
|
-
`align="${
|
|
36
|
+
function W(e) {
|
|
37
|
+
const { src: t, alt: n, width: r, href: s, padding: i, alignment: o, borderRadius: a, cssClass: l } = e.values, d = [
|
|
38
|
+
`src="${b(t)}"`,
|
|
39
|
+
`alt="${b(n)}"`,
|
|
40
|
+
`padding="${g(i)}"`,
|
|
41
|
+
`align="${o}"`
|
|
23
42
|
];
|
|
24
|
-
return
|
|
43
|
+
return r !== "auto" && d.push(`width="${r}px"`), s && d.push(`href="${b(s)}"`), a !== void 0 && a > 0 && d.push(`border-radius="${a}px"`), l && d.push(`css-class="${b(l)}"`), `<mj-image ${d.join(" ")} />`;
|
|
25
44
|
}
|
|
26
|
-
function
|
|
45
|
+
function b(e) {
|
|
27
46
|
return e.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");
|
|
28
47
|
}
|
|
29
|
-
function
|
|
48
|
+
function U(e) {
|
|
49
|
+
return e.width > 0 ? `${e.width}px ${e.style} ${e.color}` : "";
|
|
50
|
+
}
|
|
51
|
+
function P(e) {
|
|
30
52
|
const {
|
|
31
53
|
content: t,
|
|
32
|
-
href:
|
|
33
|
-
backgroundColor:
|
|
34
|
-
textColor:
|
|
35
|
-
borderRadius:
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
`
|
|
46
|
-
`color="${
|
|
47
|
-
`
|
|
48
|
-
`
|
|
49
|
-
`
|
|
50
|
-
`
|
|
51
|
-
`font-
|
|
52
|
-
`
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
54
|
+
href: n,
|
|
55
|
+
backgroundColor: r,
|
|
56
|
+
textColor: s,
|
|
57
|
+
borderRadius: i,
|
|
58
|
+
border: o,
|
|
59
|
+
padding: a,
|
|
60
|
+
innerPadding: l,
|
|
61
|
+
fontSize: d,
|
|
62
|
+
fontWeight: m,
|
|
63
|
+
alignment: f,
|
|
64
|
+
fullWidth: u,
|
|
65
|
+
cssClass: c
|
|
66
|
+
} = e.values, p = [
|
|
67
|
+
`href="${v(n)}"`,
|
|
68
|
+
`background-color="${r}"`,
|
|
69
|
+
`color="${s}"`,
|
|
70
|
+
`border-radius="${i}px"`,
|
|
71
|
+
`padding="${g(a)}"`,
|
|
72
|
+
`inner-padding="${g(l)}"`,
|
|
73
|
+
`font-size="${d}px"`,
|
|
74
|
+
`font-weight="${m}"`,
|
|
75
|
+
`align="${f}"`
|
|
76
|
+
], j = o && U(o);
|
|
77
|
+
j && p.push(`border="${v(j)}"`), u && p.push('width="100%"'), c && p.push(`css-class="${v(c)}"`);
|
|
78
|
+
const I = t.replace(/^\s*<p>([\s\S]*?)<\/p>\s*$/i, "$1");
|
|
79
|
+
return `<mj-button ${p.join(" ")}>${I}</mj-button>`;
|
|
57
80
|
}
|
|
58
|
-
function
|
|
81
|
+
function v(e) {
|
|
59
82
|
return e.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");
|
|
60
83
|
}
|
|
61
|
-
function
|
|
62
|
-
const { borderColor: t, borderWidth:
|
|
84
|
+
function G(e) {
|
|
85
|
+
const { borderColor: t, borderWidth: n, borderStyle: r, padding: s, width: i } = e.values;
|
|
63
86
|
return `<mj-divider ${[
|
|
64
87
|
`border-color="${t}"`,
|
|
65
|
-
`border-width="${
|
|
66
|
-
`border-style="${
|
|
67
|
-
`padding="${
|
|
68
|
-
`width="${
|
|
88
|
+
`border-width="${n}px"`,
|
|
89
|
+
`border-style="${r}"`,
|
|
90
|
+
`padding="${g(s)}"`,
|
|
91
|
+
`width="${i}"`
|
|
69
92
|
].join(" ")} />`;
|
|
70
93
|
}
|
|
71
|
-
function
|
|
94
|
+
function K(e) {
|
|
72
95
|
const { height: t } = e.values;
|
|
73
96
|
return `<mj-spacer height="${t}px" />`;
|
|
74
97
|
}
|
|
75
|
-
function
|
|
98
|
+
function F(e) {
|
|
76
99
|
switch (e.type) {
|
|
77
100
|
case "facebook":
|
|
78
101
|
return "facebook";
|
|
@@ -92,217 +115,270 @@ function R(e) {
|
|
|
92
115
|
return "web";
|
|
93
116
|
}
|
|
94
117
|
}
|
|
95
|
-
function
|
|
96
|
-
const
|
|
97
|
-
`name="${
|
|
98
|
-
`href="${
|
|
118
|
+
function V(e, t) {
|
|
119
|
+
const r = [
|
|
120
|
+
`name="${F(e)}"`,
|
|
121
|
+
`href="${C(e.href)}"`,
|
|
99
122
|
`icon-size="${t}px"`
|
|
100
123
|
];
|
|
101
|
-
e.iconUrl &&
|
|
102
|
-
const
|
|
103
|
-
return `<mj-social-element ${
|
|
104
|
-
}
|
|
105
|
-
function
|
|
106
|
-
const { icons: t, iconSize:
|
|
107
|
-
`icon-size="${
|
|
108
|
-
`icon-padding="${
|
|
109
|
-
`align="${
|
|
110
|
-
`padding="${
|
|
124
|
+
e.iconUrl && r.push(`src="${C(e.iconUrl)}"`);
|
|
125
|
+
const s = e.label ?? "";
|
|
126
|
+
return `<mj-social-element ${r.join(" ")}>${Y(s)}</mj-social-element>`;
|
|
127
|
+
}
|
|
128
|
+
function J(e) {
|
|
129
|
+
const { icons: t, iconSize: n, spacing: r, alignment: s, padding: i } = e.values, o = [
|
|
130
|
+
`icon-size="${n}px"`,
|
|
131
|
+
`icon-padding="${r}px"`,
|
|
132
|
+
`align="${s}"`,
|
|
133
|
+
`padding="${g(i)}"`,
|
|
111
134
|
'mode="horizontal"'
|
|
112
|
-
],
|
|
135
|
+
], a = t.map((l) => V(l, n)).join(`
|
|
113
136
|
`);
|
|
114
|
-
return `<mj-social ${
|
|
115
|
-
${
|
|
137
|
+
return `<mj-social ${o.join(" ")}>
|
|
138
|
+
${a}
|
|
116
139
|
</mj-social>`;
|
|
117
140
|
}
|
|
118
|
-
function
|
|
141
|
+
function C(e) {
|
|
119
142
|
return e.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");
|
|
120
143
|
}
|
|
121
|
-
function
|
|
144
|
+
function Y(e) {
|
|
122
145
|
return e.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
123
146
|
}
|
|
124
|
-
const
|
|
125
|
-
function
|
|
126
|
-
|
|
147
|
+
const Q = /<(\s*(?:\/\s*)?mj-raw\b[^>]*)>/gi;
|
|
148
|
+
function X(e) {
|
|
149
|
+
const t = e.lastIndexOf(">") + 1;
|
|
150
|
+
return e.slice(0, t).replace(Q, "<$1>") + e.slice(t);
|
|
127
151
|
}
|
|
128
|
-
function
|
|
129
|
-
const { content: t, padding:
|
|
130
|
-
return `<mj-raw><div style="padding: ${
|
|
152
|
+
function Z(e) {
|
|
153
|
+
const { content: t, padding: n } = e.values, r = g(n), s = X(t), i = $(e.values);
|
|
154
|
+
return `<mj-raw><div class="lp-html${i ? ` ${i}` : ""}" style="padding: ${r};">${s}</div></mj-raw>`;
|
|
131
155
|
}
|
|
132
|
-
function
|
|
133
|
-
const { content: t, innerPadding:
|
|
156
|
+
function ee(e) {
|
|
157
|
+
const { content: t, innerPadding: n } = e.values;
|
|
134
158
|
return `<mj-text ${[
|
|
135
|
-
`padding="${
|
|
159
|
+
`padding="${g(n)}"`
|
|
136
160
|
].join(" ")}>${t}</mj-text>`;
|
|
137
161
|
}
|
|
138
|
-
function
|
|
139
|
-
const t = e.values,
|
|
162
|
+
function te(e) {
|
|
163
|
+
const t = e.values, n = [
|
|
140
164
|
`mode="${t.mode}"`,
|
|
141
165
|
`height="${t.height}px"`,
|
|
142
166
|
`vertical-align="${t.verticalAlign}"`,
|
|
143
|
-
`padding="${
|
|
167
|
+
`padding="${g(t.padding)}"`
|
|
144
168
|
];
|
|
145
|
-
return t.backgroundUrl &&
|
|
146
|
-
<mj-text padding="${
|
|
169
|
+
return t.backgroundUrl && n.push(`background-width="${t.width}px"`), t.backgroundUrl && (n.push(`background-url="${ne(t.backgroundUrl)}"`), n.push(`background-position="${t.backgroundPosition}"`)), t.backgroundColor && n.push(`background-color="${t.backgroundColor}"`), ` <mj-hero ${n.join(" ")}>
|
|
170
|
+
<mj-text padding="${g(t.innerPadding)}">${t.content}</mj-text>
|
|
147
171
|
</mj-hero>`;
|
|
148
172
|
}
|
|
149
|
-
function
|
|
173
|
+
function ne(e) {
|
|
150
174
|
return e.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");
|
|
151
175
|
}
|
|
152
|
-
function
|
|
176
|
+
function re(e) {
|
|
153
177
|
const t = [
|
|
154
|
-
`href="${
|
|
178
|
+
`href="${oe(e.href)}"`
|
|
155
179
|
];
|
|
156
|
-
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(" ")}>${
|
|
180
|
+
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(" ")}>${ie(e.text)}</mj-navbar-link>`;
|
|
157
181
|
}
|
|
158
|
-
function
|
|
159
|
-
const { links: t, hamburger:
|
|
160
|
-
`hamburger="${
|
|
161
|
-
`padding="${
|
|
162
|
-
],
|
|
182
|
+
function se(e) {
|
|
183
|
+
const { links: t, hamburger: n, padding: r } = e.values, s = [
|
|
184
|
+
`hamburger="${n}"`,
|
|
185
|
+
`padding="${g(r)}"`
|
|
186
|
+
], i = t.map((o) => re(o)).join(`
|
|
163
187
|
`);
|
|
164
|
-
return `<mj-navbar ${
|
|
165
|
-
${
|
|
188
|
+
return `<mj-navbar ${s.join(" ")}>
|
|
189
|
+
${i}
|
|
166
190
|
</mj-navbar>`;
|
|
167
191
|
}
|
|
168
|
-
function
|
|
192
|
+
function oe(e) {
|
|
169
193
|
return e.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");
|
|
170
194
|
}
|
|
171
|
-
function
|
|
195
|
+
function ie(e) {
|
|
172
196
|
return e.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
173
197
|
}
|
|
174
|
-
function
|
|
198
|
+
function ae(e) {
|
|
175
199
|
switch (e.type) {
|
|
176
200
|
case "text":
|
|
177
|
-
return
|
|
201
|
+
return q(e);
|
|
178
202
|
case "image":
|
|
179
|
-
return
|
|
203
|
+
return W(e);
|
|
180
204
|
case "button":
|
|
181
|
-
return
|
|
205
|
+
return P(e);
|
|
182
206
|
case "divider":
|
|
183
|
-
return
|
|
207
|
+
return G(e);
|
|
184
208
|
case "spacer":
|
|
185
|
-
return
|
|
209
|
+
return K(e);
|
|
186
210
|
case "social":
|
|
187
|
-
return
|
|
211
|
+
return J(e);
|
|
188
212
|
case "html":
|
|
189
|
-
return
|
|
213
|
+
return Z(e);
|
|
190
214
|
case "hero":
|
|
191
|
-
return
|
|
215
|
+
return ee(e);
|
|
192
216
|
case "navbar":
|
|
193
|
-
return
|
|
217
|
+
return se(e);
|
|
194
218
|
default:
|
|
195
|
-
return
|
|
219
|
+
return ce(e);
|
|
196
220
|
}
|
|
197
221
|
}
|
|
198
|
-
function
|
|
199
|
-
const t = e.type,
|
|
200
|
-
return
|
|
222
|
+
function ce(e) {
|
|
223
|
+
const t = e.type, n = N(t);
|
|
224
|
+
return n != null && n.renderMjml ? n.renderMjml(e) : `<!-- Unknown block type: ${t} -->`;
|
|
201
225
|
}
|
|
202
|
-
function
|
|
203
|
-
const { backgroundColor:
|
|
226
|
+
function le(e, t) {
|
|
227
|
+
const { backgroundColor: n, padding: r, borderRadius: s, verticalAlign: i, cssClass: o } = e.attributes, a = [
|
|
204
228
|
`width="${t}"`,
|
|
205
|
-
`padding="${
|
|
206
|
-
`vertical-align="${
|
|
229
|
+
`padding="${g(r)}"`,
|
|
230
|
+
`vertical-align="${i}"`
|
|
207
231
|
];
|
|
208
|
-
|
|
209
|
-
const l =
|
|
232
|
+
n && a.push(`background-color="${n}"`), s !== void 0 && s > 0 && a.push(`border-radius="${s}px"`);
|
|
233
|
+
const l = [o && h(o), $(e.attributes)].filter(Boolean).join(" ");
|
|
234
|
+
l && a.push(`css-class="${l}"`);
|
|
235
|
+
const d = e.blocks.map(
|
|
236
|
+
(m) => y(
|
|
237
|
+
` ${O(ae(m), $(m.values))}`,
|
|
238
|
+
m.values.condition,
|
|
239
|
+
" "
|
|
240
|
+
)
|
|
241
|
+
).join(`
|
|
210
242
|
`);
|
|
211
|
-
return ` <mj-column ${
|
|
212
|
-
${
|
|
243
|
+
return ` <mj-column ${a.join(" ")}>
|
|
244
|
+
${d}
|
|
213
245
|
</mj-column>`;
|
|
214
246
|
}
|
|
215
|
-
function
|
|
216
|
-
if (e.columns.length === 1 && e.columns[0].blocks.length === 1 && e.columns[0].blocks[0].type === "hero")
|
|
217
|
-
|
|
218
|
-
|
|
247
|
+
function ue(e) {
|
|
248
|
+
if (e.columns.length === 1 && e.columns[0].blocks.length === 1 && e.columns[0].blocks[0].type === "hero") {
|
|
249
|
+
const u = e.columns[0].blocks[0], c = $({
|
|
250
|
+
hideOnMobile: e.attributes.hideOnMobile || u.values.hideOnMobile,
|
|
251
|
+
hideOnDesktop: e.attributes.hideOnDesktop || u.values.hideOnDesktop
|
|
252
|
+
});
|
|
253
|
+
return y(
|
|
254
|
+
T(
|
|
255
|
+
y(
|
|
256
|
+
O(te(u), c),
|
|
257
|
+
u.values.condition
|
|
258
|
+
),
|
|
259
|
+
e.attributes.repeat
|
|
260
|
+
),
|
|
219
261
|
e.attributes.condition
|
|
220
262
|
);
|
|
221
|
-
|
|
222
|
-
|
|
263
|
+
}
|
|
264
|
+
const { backgroundColor: t, padding: n, fullWidth: r, cssClass: s, noStackOnMobile: i } = e.attributes, o = [
|
|
265
|
+
`padding="${g(n)}"`
|
|
223
266
|
];
|
|
224
|
-
|
|
225
|
-
const
|
|
226
|
-
|
|
227
|
-
|
|
267
|
+
r && o.push('full-width="full-width"'), t && o.push(`background-color="${t}"`);
|
|
268
|
+
const a = H(e);
|
|
269
|
+
a && (o.push(`background-url="${h(a.url)}"`), o.push(`background-size="${h(a.size)}"`), o.push(`background-repeat="${a.repeat}"`), o.push(`background-position="${h(a.position)}"`));
|
|
270
|
+
const l = [s && h(s), $(e.attributes)].filter(Boolean).join(" ");
|
|
271
|
+
l && o.push(`css-class="${l}"`);
|
|
272
|
+
const d = e.columnRatios.reduce((u, c) => u + c, 0), m = e.columns.map((u, c) => {
|
|
273
|
+
const j = ((e.columnRatios[c] ?? 1) / d * 100).toFixed(2).replace(/\.?0+$/, "");
|
|
274
|
+
return le(u, `${j}%`);
|
|
228
275
|
}).join(`
|
|
229
|
-
`)
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
276
|
+
`), f = i ? ` <mj-group>
|
|
277
|
+
${m}
|
|
278
|
+
</mj-group>` : m;
|
|
279
|
+
return y(
|
|
280
|
+
T(
|
|
281
|
+
` <mj-section ${o.join(" ")}>
|
|
282
|
+
${f}
|
|
233
283
|
</mj-section>`,
|
|
284
|
+
e.attributes.repeat
|
|
285
|
+
),
|
|
234
286
|
e.attributes.condition
|
|
235
287
|
);
|
|
236
288
|
}
|
|
237
|
-
function
|
|
289
|
+
function y(e, t, n = " ") {
|
|
238
290
|
const r = t == null ? void 0 : t.trim();
|
|
239
|
-
return r ?
|
|
291
|
+
return r ? `${n}<mj-raw>{{#if ${r}}}</mj-raw>
|
|
292
|
+
${e}
|
|
293
|
+
${n}<mj-raw>{{/if}}</mj-raw>` : e;
|
|
294
|
+
}
|
|
295
|
+
function T(e, t) {
|
|
296
|
+
const n = t == null ? void 0 : t.trim();
|
|
297
|
+
return n ? ` <mj-raw>{{#each ${n}}}</mj-raw>
|
|
240
298
|
${e}
|
|
241
|
-
<mj-raw>{{/
|
|
299
|
+
<mj-raw>{{/each}}</mj-raw>` : e;
|
|
242
300
|
}
|
|
243
|
-
const
|
|
301
|
+
const de = ` <mj-raw>
|
|
244
302
|
<!--[if mso]>
|
|
245
303
|
<style>
|
|
246
304
|
body, table, td { font-family: Arial, Helvetica, sans-serif !important; }
|
|
247
305
|
blockquote { margin: 0 0 16px !important; }
|
|
248
306
|
</style>
|
|
249
307
|
<![endif]-->
|
|
250
|
-
</mj-raw>`,
|
|
308
|
+
</mj-raw>`, pe = ` <mj-raw>
|
|
251
309
|
<meta name="color-scheme" content="light dark">
|
|
252
310
|
<meta name="supported-color-schemes" content="light dark">
|
|
253
|
-
</mj-raw>`,
|
|
311
|
+
</mj-raw>`, me = ` <mj-style inline="inline">
|
|
254
312
|
h1, h2, h3, blockquote { margin: 0 0 0.5em; }
|
|
255
313
|
</mj-style>`;
|
|
256
|
-
function
|
|
257
|
-
const t = /* @__PURE__ */ new Set(),
|
|
258
|
-
for (const
|
|
259
|
-
if (!
|
|
260
|
-
t.add(
|
|
261
|
-
const
|
|
262
|
-
|
|
314
|
+
function ge(e) {
|
|
315
|
+
const t = /* @__PURE__ */ new Set(), n = [];
|
|
316
|
+
for (const r of e) {
|
|
317
|
+
if (!r.url || t.has(r.url)) continue;
|
|
318
|
+
t.add(r.url);
|
|
319
|
+
const s = r.family.split(",")[0].trim().replace(/^['"]|['"]$/g, ""), i = h(r.url), o = h(s);
|
|
320
|
+
n.push(` <mj-font name="${o}" href="${i}" />`), n.push(` <mj-raw><link rel="stylesheet" href="${i}"></mj-raw>`);
|
|
263
321
|
}
|
|
264
|
-
return
|
|
322
|
+
return n.join(`
|
|
265
323
|
`);
|
|
266
324
|
}
|
|
267
|
-
function
|
|
268
|
-
const {
|
|
269
|
-
t
|
|
270
|
-
const
|
|
271
|
-
return
|
|
272
|
-
|
|
325
|
+
function fe(e) {
|
|
326
|
+
const { language: t, direction: n } = e.body.attributes;
|
|
327
|
+
if (!t && !n) return "";
|
|
328
|
+
const r = z(t, n);
|
|
329
|
+
return `${t ? ` lang="${h(t)}"` : ""} dir="${r}"`;
|
|
330
|
+
}
|
|
331
|
+
function he(e, t) {
|
|
332
|
+
const { fontFamily: n, linkStyle: r, css: s } = e.body.attributes, i = e.metadata.previewText, o = [];
|
|
333
|
+
t.outlookWorkarounds && (o.push(de), o.push(pe), o.push(me));
|
|
334
|
+
const a = ge(t.fonts);
|
|
335
|
+
a && o.push(a), o.push(` <mj-attributes>
|
|
336
|
+
<mj-all font-family="${h(n)}" />
|
|
273
337
|
<mj-text font-size="14px" line-height="1.5" />
|
|
274
338
|
<mj-button font-size="14px" />
|
|
275
|
-
</mj-attributes>`),
|
|
276
|
-
${
|
|
277
|
-
</mj-style>`),
|
|
278
|
-
$
|
|
339
|
+
</mj-attributes>`), e.body.rows.some((u) => u.columns.some((c) => c.blocks.some((p) => p.type === "html"))) && o.push(` <mj-style inline="inline">
|
|
340
|
+
.lp-html { font-size: 14px; line-height: 1.5; font-family: ${n.replace(/[<>{};]/g, "")}; }
|
|
341
|
+
</mj-style>`), e.body.rows.some(
|
|
342
|
+
(u) => $(u.attributes) || u.columns.some((c) => $(c.attributes) || c.blocks.some((p) => $(p.values)))
|
|
343
|
+
) && o.push(_);
|
|
344
|
+
const m = r && E(r);
|
|
345
|
+
m && o.push(` <mj-style>
|
|
346
|
+
${m.split(`
|
|
347
|
+
`).map((u) => ` ${u}`).join(`
|
|
348
|
+
`)}
|
|
349
|
+
</mj-style>`), s && o.push(` <mj-style>
|
|
350
|
+
${s.replace(/<\/mj-style/gi, "<\\/mj-style")}
|
|
351
|
+
</mj-style>`);
|
|
352
|
+
const f = e.metadata.name.trim();
|
|
353
|
+
return f && o.push(` <mj-title>${B(f)}</mj-title>`), i && o.push(` <mj-preview>${B(i)}</mj-preview>`), ` <mj-head>
|
|
354
|
+
${o.join(`
|
|
279
355
|
`)}
|
|
280
356
|
</mj-head>`;
|
|
281
357
|
}
|
|
282
|
-
function
|
|
283
|
-
const
|
|
358
|
+
function $e(e, t) {
|
|
359
|
+
const n = {
|
|
284
360
|
outlookWorkarounds: (t == null ? void 0 : t.outlookWorkarounds) ?? !0,
|
|
285
361
|
fonts: (t == null ? void 0 : t.fonts) ?? []
|
|
286
|
-
}, { width:
|
|
287
|
-
`width="${
|
|
362
|
+
}, { width: r, backgroundColor: s } = e.body.attributes, i = [
|
|
363
|
+
`width="${r}px"`
|
|
288
364
|
];
|
|
289
|
-
|
|
290
|
-
const
|
|
365
|
+
s && i.push(`background-color="${s}"`);
|
|
366
|
+
const o = he(e, n), a = e.body.rows.map((l) => ue(l)).join(`
|
|
291
367
|
`);
|
|
292
|
-
return `<mjml>
|
|
293
|
-
${
|
|
294
|
-
<mj-body ${
|
|
295
|
-
${
|
|
368
|
+
return `<mjml${fe(e)}>
|
|
369
|
+
${o}
|
|
370
|
+
<mj-body ${i.join(" ")}>
|
|
371
|
+
${a}
|
|
296
372
|
</mj-body>
|
|
297
373
|
</mjml>`;
|
|
298
374
|
}
|
|
299
|
-
function
|
|
375
|
+
function h(e) {
|
|
300
376
|
return e.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");
|
|
301
377
|
}
|
|
302
|
-
function
|
|
378
|
+
function B(e) {
|
|
303
379
|
return e.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
304
380
|
}
|
|
305
|
-
class
|
|
381
|
+
class Be {
|
|
306
382
|
/**
|
|
307
383
|
* Renders a PigeonDocument to HTML using MJML as the compilation backend.
|
|
308
384
|
*
|
|
@@ -310,45 +386,169 @@ class re {
|
|
|
310
386
|
* @param options - Optional rendering configuration (minify, beautify, inlineCss)
|
|
311
387
|
* @returns A promise resolving to the rendered HTML and any errors
|
|
312
388
|
*/
|
|
313
|
-
async render(t,
|
|
314
|
-
const
|
|
315
|
-
outlookWorkarounds:
|
|
316
|
-
fonts:
|
|
317
|
-
}),
|
|
389
|
+
async render(t, n) {
|
|
390
|
+
const r = $e(t, {
|
|
391
|
+
outlookWorkarounds: n == null ? void 0 : n.outlookWorkarounds,
|
|
392
|
+
fonts: n == null ? void 0 : n.fonts
|
|
393
|
+
}), s = {
|
|
318
394
|
// By default MJML inlines CSS; respect the option if provided
|
|
319
395
|
keepComments: !1
|
|
320
396
|
};
|
|
321
|
-
|
|
397
|
+
n != null && n.minify && (s.minify = !0), n != null && n.beautify && (s.beautify = !0);
|
|
322
398
|
try {
|
|
323
|
-
const
|
|
324
|
-
message:
|
|
325
|
-
line:
|
|
326
|
-
tagName:
|
|
399
|
+
const i = R(r, s), o = (i.errors || []).map((a) => ({
|
|
400
|
+
message: a.message,
|
|
401
|
+
line: a.line,
|
|
402
|
+
tagName: a.tagName
|
|
327
403
|
}));
|
|
328
404
|
return {
|
|
329
|
-
html:
|
|
330
|
-
errors:
|
|
405
|
+
html: i.html,
|
|
406
|
+
errors: o
|
|
331
407
|
};
|
|
332
|
-
} catch (
|
|
408
|
+
} catch (i) {
|
|
333
409
|
return {
|
|
334
410
|
html: "",
|
|
335
|
-
errors: [{ message: `MJML compilation failed: ${
|
|
411
|
+
errors: [{ message: `MJML compilation failed: ${i instanceof Error ? i.message : String(i)}` }]
|
|
336
412
|
};
|
|
337
413
|
}
|
|
338
414
|
}
|
|
339
415
|
}
|
|
416
|
+
const je = {
|
|
417
|
+
amp: "&",
|
|
418
|
+
lt: "<",
|
|
419
|
+
gt: ">",
|
|
420
|
+
quot: '"',
|
|
421
|
+
apos: "'",
|
|
422
|
+
nbsp: " "
|
|
423
|
+
}, be = /^(p|div|h[1-6]|blockquote|table|tr|ul|ol|section|header|footer)$/, ye = /^(style|script)$/;
|
|
424
|
+
function S(e) {
|
|
425
|
+
return e.replace(/&(#x[0-9a-f]+|#\d+|amp|lt|gt|quot|apos|nbsp);/gi, (t, n) => {
|
|
426
|
+
if (n[0] !== "#") return je[n.toLowerCase()];
|
|
427
|
+
const r = /x/i.test(n) ? parseInt(n.slice(2), 16) : parseInt(n.slice(1), 10);
|
|
428
|
+
return r <= 1114111 ? String.fromCodePoint(r) : t;
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
function M(e, t) {
|
|
432
|
+
const n = new RegExp(`(?:^|\\s)${t}\\s*=\\s*(?:"([^"]*)"|'([^']*)'|([^\\s>]+))`, "i").exec(e);
|
|
433
|
+
return n ? S(n[1] ?? n[2] ?? n[3]) : "";
|
|
434
|
+
}
|
|
435
|
+
function k(e) {
|
|
436
|
+
return !!e && e.trim() !== "#";
|
|
437
|
+
}
|
|
438
|
+
function x(e, t) {
|
|
439
|
+
return !k(t) || t === e ? e : e ? `${e} (${t})` : t;
|
|
440
|
+
}
|
|
441
|
+
function A(e) {
|
|
442
|
+
const t = [];
|
|
443
|
+
let n, r = 0;
|
|
444
|
+
const s = [], i = /<!--|<(\/?)([a-z][a-z0-9]*)\b([^<>]*)>|([^<]+)|</gi, o = /--!?>/g;
|
|
445
|
+
let a;
|
|
446
|
+
for (; a = i.exec(e); ) {
|
|
447
|
+
const [l, d, m, f, u] = a;
|
|
448
|
+
if (l === "<!--") {
|
|
449
|
+
o.lastIndex = i.lastIndex, i.lastIndex = o.exec(e) ? o.lastIndex : e.length;
|
|
450
|
+
continue;
|
|
451
|
+
}
|
|
452
|
+
if (u !== void 0 || l === "<") {
|
|
453
|
+
r || s.push(S((u ?? l).replace(/\s+/g, " ")));
|
|
454
|
+
continue;
|
|
455
|
+
}
|
|
456
|
+
if (!m) continue;
|
|
457
|
+
const c = m.toLowerCase();
|
|
458
|
+
if (ye.test(c))
|
|
459
|
+
r = Math.max(0, r + (d ? -1 : 1));
|
|
460
|
+
else {
|
|
461
|
+
if (r)
|
|
462
|
+
continue;
|
|
463
|
+
if (c === "br")
|
|
464
|
+
s.push(`
|
|
465
|
+
`);
|
|
466
|
+
else if (c === "img" && !d)
|
|
467
|
+
s.push(M(f, "alt"));
|
|
468
|
+
else if (c === "a") {
|
|
469
|
+
if (n) {
|
|
470
|
+
const p = s.splice(n.start).join("");
|
|
471
|
+
s.push(/^\s*/.exec(p)[0], x(p.trim(), n.href));
|
|
472
|
+
}
|
|
473
|
+
n = d ? void 0 : { href: M(f, "href"), start: s.length };
|
|
474
|
+
} else if (c === "li") {
|
|
475
|
+
const p = t[t.length - 1];
|
|
476
|
+
d || s.push(`
|
|
477
|
+
${p != null && p.ordered ? `${++p.count}.` : "-"} `);
|
|
478
|
+
} else c === "ul" || c === "ol" ? (d ? t.pop() : t.push({ ordered: c === "ol", count: 0 }), s.push(`
|
|
479
|
+
|
|
480
|
+
`)) : be.test(c) && !t.length && s.push(`
|
|
481
|
+
|
|
482
|
+
`);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
return s.join("").split(`
|
|
486
|
+
`).map((l) => l.trim()).join(`
|
|
487
|
+
`).replace(/\n{3,}/g, `
|
|
488
|
+
|
|
489
|
+
`).trim();
|
|
490
|
+
}
|
|
491
|
+
function ve(e) {
|
|
492
|
+
return e[0].toUpperCase() + e.slice(1);
|
|
493
|
+
}
|
|
494
|
+
function w(e, t, n) {
|
|
495
|
+
const r = n == null ? void 0 : n.trim();
|
|
496
|
+
return r && e ? `{{#${t} ${r}}}
|
|
497
|
+
${e}
|
|
498
|
+
{{/${t}}}` : e;
|
|
499
|
+
}
|
|
500
|
+
function ke(e) {
|
|
501
|
+
return w(xe(e), "if", e.values.condition);
|
|
502
|
+
}
|
|
503
|
+
function xe(e) {
|
|
504
|
+
switch (e.type) {
|
|
505
|
+
case "text":
|
|
506
|
+
case "html":
|
|
507
|
+
case "hero":
|
|
508
|
+
return A(e.values.content);
|
|
509
|
+
case "button": {
|
|
510
|
+
const t = A(e.values.content);
|
|
511
|
+
return k(e.values.href) ? `${t}: ${e.values.href}` : t;
|
|
512
|
+
}
|
|
513
|
+
case "image":
|
|
514
|
+
return e.values.alt ? x(e.values.alt, e.values.href) : "";
|
|
515
|
+
case "divider":
|
|
516
|
+
return "----------";
|
|
517
|
+
case "social":
|
|
518
|
+
return e.values.icons.filter((t) => k(t.href)).map((t) => `${t.label || ve(t.type)}: ${t.href}`).join(`
|
|
519
|
+
`);
|
|
520
|
+
case "navbar":
|
|
521
|
+
return e.values.links.map((t) => x(t.text, t.href)).join(" | ");
|
|
522
|
+
default:
|
|
523
|
+
return "";
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
function we(e) {
|
|
527
|
+
const t = e.columns.flatMap((n) => n.blocks.map(ke)).filter(Boolean).join(`
|
|
528
|
+
|
|
529
|
+
`);
|
|
530
|
+
return w(w(t, "each", e.attributes.repeat), "if", e.attributes.condition);
|
|
531
|
+
}
|
|
532
|
+
function Me(e) {
|
|
533
|
+
const t = e.body.rows.map(we).filter(Boolean).join(`
|
|
534
|
+
|
|
535
|
+
`);
|
|
536
|
+
return t ? `${t}
|
|
537
|
+
` : "";
|
|
538
|
+
}
|
|
340
539
|
export {
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
540
|
+
Be as MjmlRenderer,
|
|
541
|
+
$e as documentToMjml,
|
|
542
|
+
Me as documentToPlainText,
|
|
543
|
+
P as renderButtonBlock,
|
|
544
|
+
G as renderDividerBlock,
|
|
545
|
+
ee as renderHeroBlock,
|
|
546
|
+
te as renderHeroSection,
|
|
547
|
+
Z as renderHtmlBlock,
|
|
548
|
+
W as renderImageBlock,
|
|
549
|
+
se as renderNavBarBlock,
|
|
550
|
+
J as renderSocialBlock,
|
|
551
|
+
K as renderSpacerBlock,
|
|
552
|
+
q as renderTextBlock,
|
|
553
|
+
g as spacingToMjml
|
|
354
554
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lit-pigeon/renderer-mjml",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "MJML-based HTML renderer for Pigeon email editor",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -20,14 +20,14 @@
|
|
|
20
20
|
],
|
|
21
21
|
"dependencies": {
|
|
22
22
|
"mjml": "^4.15.0",
|
|
23
|
-
"@lit-pigeon/core": "^0.
|
|
23
|
+
"@lit-pigeon/core": "^0.4.0"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
26
|
"@types/mjml": "^4.7.0",
|
|
27
27
|
"typescript": "^5.5.0",
|
|
28
28
|
"vite": "^6.0.0",
|
|
29
29
|
"vite-plugin-dts": "^4.0.0",
|
|
30
|
-
"vitest": "^
|
|
30
|
+
"vitest": "^4.1.12"
|
|
31
31
|
},
|
|
32
32
|
"publishConfig": {
|
|
33
33
|
"access": "public"
|