@lit-pigeon/renderer-mjml 0.2.5 → 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 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
- Bundles [`mjml`](https://mjml.io) as a dependency.
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, options)`.
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,47 +1,82 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const R=require("mjml"),W=require("@lit-pigeon/core");function u(e){return`${e.top}px ${e.right}px ${e.bottom}px ${e.left}px`}function k(e){const{content:t,padding:r,textAlign:a,lineHeight:o,cssClass:n}=e.values,s=[`padding="${u(r)}"`,`align="${a}"`,`line-height="${o}"`];return n&&s.push(`css-class="${q(n)}"`),`<mj-text ${s.join(" ")}>${t}</mj-text>`}function q(e){return e.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function y(e){const{src:t,alt:r,width:a,href:o,padding:n,alignment:s,borderRadius:c,cssClass:l}=e.values,i=[`src="${g(t)}"`,`alt="${g(r)}"`,`padding="${u(n)}"`,`align="${s}"`];return a!=="auto"&&i.push(`width="${a}px"`),o&&i.push(`href="${g(o)}"`),c!==void 0&&c>0&&i.push(`border-radius="${c}px"`),l&&i.push(`css-class="${g(l)}"`),`<mj-image ${i.join(" ")} />`}function g(e){return e.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function x(e){const{content:t,href:r,backgroundColor:a,textColor:o,borderRadius:n,padding:s,innerPadding:c,fontSize:l,fontWeight:i,alignment:d,fullWidth:$,cssClass:p}=e.values,f=[`href="${h(r)}"`,`background-color="${a}"`,`color="${o}"`,`border-radius="${n}px"`,`padding="${u(s)}"`,`inner-padding="${u(c)}"`,`font-size="${l}px"`,`font-weight="${i}"`,`align="${d}"`];$&&f.push('width="100%"'),p&&f.push(`css-class="${h(p)}"`);const H=t.replace(/^\s*<p>([\s\S]*?)<\/p>\s*$/i,"$1");return`<mj-button ${f.join(" ")}>${H}</mj-button>`}function h(e){return e.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function v(e){const{borderColor:t,borderWidth:r,borderStyle:a,padding:o,width:n}=e.values;return`<mj-divider ${[`border-color="${t}"`,`border-width="${r}px"`,`border-style="${a}"`,`padding="${u(o)}"`,`width="${n}"`].join(" ")} />`}function w(e){const{height:t}=e.values;return`<mj-spacer height="${t}px" />`}function N(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 z(e,t){const a=[`name="${N(e)}"`,`href="${j(e.href)}"`,`icon-size="${t}px"`];e.iconUrl&&a.push(`src="${j(e.iconUrl)}"`);const o=e.label??"";return`<mj-social-element ${a.join(" ")}>${_(o)}</mj-social-element>`}function B(e){const{icons:t,iconSize:r,spacing:a,alignment:o,padding:n}=e.values,s=[`icon-size="${r}px"`,`icon-padding="${a}px"`,`align="${o}"`,`padding="${u(n)}"`,'mode="horizontal"'],c=t.map(l=>z(l,r)).join(`
2
- `);return`<mj-social ${s.join(" ")}>
3
- ${c}
4
- </mj-social>`}function j(e){return e.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function _(e){return e.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}const D=/<(\s*\/?\s*mj-raw\b[^>]*)>/gi;function O(e){return e.replace(D,"&lt;$1&gt;")}function C(e){const{content:t,padding:r}=e.values,a=u(r),o=O(t);return`<mj-raw><div class="lp-html" style="padding: ${a};">${o}</div></mj-raw>`}function A(e){const{content:t,innerPadding:r}=e.values;return`<mj-text ${[`padding="${u(r)}"`].join(" ")}>${t}</mj-text>`}function M(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="${I(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 I(e){return e.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function E(e){const t=[`href="${U(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(" ")}>${L(e.text)}</mj-navbar-link>`}function S(e){const{links:t,hamburger:r,padding:a}=e.values,o=[`hamburger="${r}"`,`padding="${u(a)}"`],n=t.map(s=>E(s)).join(`
7
- `);return`<mj-navbar ${o.join(" ")}>
8
- ${n}
9
- </mj-navbar>`}function U(e){return e.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function L(e){return e.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function P(e){switch(e.type){case"text":return k(e);case"image":return y(e);case"button":return x(e);case"divider":return v(e);case"spacer":return w(e);case"social":return B(e);case"html":return C(e);case"hero":return A(e);case"navbar":return S(e);default:return K(e)}}function K(e){const t=e.type,r=W.getBlockDefinition(t);return r!=null&&r.renderMjml?r.renderMjml(e):`<!-- Unknown block type: ${t} -->`}function F(e,t){const{backgroundColor:r,padding:a,borderRadius:o,verticalAlign:n,cssClass:s}=e.attributes,c=[`width="${t}"`,`padding="${u(a)}"`,`vertical-align="${n}"`];r&&c.push(`background-color="${r}"`),o!==void 0&&o>0&&c.push(`border-radius="${o}px"`),s&&c.push(`css-class="${m(s)}"`);const l=e.blocks.map(i=>` ${P(i)}`).join(`
10
- `);return` <mj-column ${c.join(" ")}>
11
- ${l}
12
- </mj-column>`}function G(e){if(e.columns.length===1&&e.columns[0].blocks.length===1&&e.columns[0].blocks[0].type==="hero")return b(M(e.columns[0].blocks[0]),e.attributes.condition);const{backgroundColor:t,backgroundImage:r,padding:a,fullWidth:o,cssClass:n}=e.attributes,s=[`padding="${u(a)}"`];o&&s.push('full-width="full-width"'),t&&s.push(`background-color="${t}"`),r&&(s.push(`background-url="${m(r)}"`),s.push('background-size="cover"'),s.push('background-repeat="no-repeat"')),n&&s.push(`css-class="${m(n)}"`);const c=e.columnRatios.reduce((i,d)=>i+d,0),l=e.columns.map((i,d)=>{const p=((e.columnRatios[d]??1)/c*100).toFixed(2).replace(/\.?0+$/,"");return F(i,`${p}%`)}).join(`
13
- `);return b(` <mj-section ${s.join(" ")}>
14
- ${l}
15
- </mj-section>`,e.attributes.condition)}function b(e,t){const r=t==null?void 0:t.trim();return r?` <mj-raw>{{#if ${r}}}</mj-raw>
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,"&amp;").replace(/"/g,"&quot;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}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,"&amp;").replace(/"/g,"&quot;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}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,"&amp;").replace(/"/g,"&quot;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}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,"&amp;").replace(/"/g,"&quot;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function Q(e){return e.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}const X=/<(\s*(?:\/\s*)?mj-raw\b[^>]*)>/gi;function Z(e){const t=e.lastIndexOf(">")+1;return e.slice(0,t).replace(X,"&lt;$1&gt;")+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,"&amp;").replace(/"/g,"&quot;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}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,"&amp;").replace(/"/g,"&quot;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function re(e){return e.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}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
- <mj-raw>{{/if}}</mj-raw>`:e}const J=` <mj-raw>
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>`,Q=` <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>`,V=` <mj-style inline="inline">
40
+ </mj-raw>`,ue=` <mj-style inline="inline">
28
41
  h1, h2, h3, blockquote { margin: 0 0 0.5em; }
29
- </mj-style>`;function X(e){const t=new Set,r=[];for(const a of e){if(!a.url||t.has(a.url))continue;t.add(a.url);const o=a.family.split(",")[0].trim().replace(/^['"]|['"]$/g,""),n=m(a.url),s=m(o);r.push(` <mj-font name="${s}" href="${n}" />`),r.push(` <mj-raw><link rel="stylesheet" href="${n}"></mj-raw>`)}return r.join(`
30
- `)}function Y(e,t){const{fontFamily:r,css:a}=e.body.attributes,o=e.metadata.previewText,n=[];t.outlookWorkarounds&&(n.push(J),n.push(Q),n.push(V));const s=X(t.fonts);return s&&n.push(s),n.push(` <mj-attributes>
31
- <mj-all font-family="${m(r)}" />
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>`),e.body.rows.some(l=>l.columns.some(i=>i.blocks.some(d=>d.type==="html")))&&n.push(` <mj-style inline="inline">
35
- .lp-html { font-size: 14px; line-height: 1.5; font-family: ${r.replace(/[<>{};]/g,"")}; }
36
- </mj-style>`),a&&n.push(` <mj-style>
37
- ${a.replace(/<\/mj-style/gi,"<\\/mj-style")}
38
- </mj-style>`),o&&n.push(` <mj-preview>${Z(o)}</mj-preview>`),` <mj-head>
39
- ${n.join(`
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(`
40
57
  `)}
41
- </mj-head>`}function T(e,t){const r={outlookWorkarounds:(t==null?void 0:t.outlookWorkarounds)??!0,fonts:(t==null?void 0:t.fonts)??[]},{width:a,backgroundColor:o}=e.body.attributes,n=[`width="${a}px"`];o&&n.push(`background-color="${o}"`);const s=Y(e,r),c=e.body.rows.map(l=>G(l)).join(`
42
- `);return`<mjml>
43
- ${s}
44
- <mj-body ${n.join(" ")}>
45
- ${c}
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}
46
63
  </mj-body>
47
- </mjml>`}function m(e){return e.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function Z(e){return e.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}class ee{async render(t,r){const a=T(t,{outlookWorkarounds:r==null?void 0:r.outlookWorkarounds,fonts:r==null?void 0:r.fonts}),o={keepComments:!1};r!=null&&r.minify&&(o.minify=!0),r!=null&&r.beautify&&(o.beautify=!0);try{const n=R(a,o),s=(n.errors||[]).map(c=>({message:c.message,line:c.line,tagName:c.tagName}));return{html:n.html,errors:s}}catch(n){return{html:"",errors:[{message:`MJML compilation failed: ${n instanceof Error?n.message:String(n)}`}]}}}}exports.MjmlRenderer=ee;exports.documentToMjml=T;exports.renderButtonBlock=x;exports.renderDividerBlock=v;exports.renderHeroBlock=A;exports.renderHeroSection=M;exports.renderHtmlBlock=C;exports.renderImageBlock=y;exports.renderNavBarBlock=S;exports.renderSocialBlock=B;exports.renderSpacerBlock=w;exports.renderTextBlock=k;exports.spacingToMjml=u;
64
+ </mjml>`}function h(e){return e.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function M(e){return e.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}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 y from "mjml";
2
- import { getBlockDefinition as x } from "@lit-pigeon/core";
3
- function u(e) {
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 w(e) {
7
- const { content: t, padding: r, textAlign: a, lineHeight: o, cssClass: n } = e.values, s = [
8
- `padding="${u(r)}"`,
9
- `align="${a}"`,
10
- `line-height="${o}"`
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 n && s.push(`css-class="${v(n)}"`), `<mj-text ${s.join(" ")}>${t}</mj-text>`;
31
+ return i && o.push(`css-class="${L(i)}"`), `<mj-text ${o.join(" ")}>${t}</mj-text>`;
13
32
  }
14
- function v(e) {
33
+ function L(e) {
15
34
  return e.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
16
35
  }
17
- function C(e) {
18
- const { src: t, alt: r, width: a, href: o, padding: n, alignment: s, borderRadius: c, cssClass: l } = e.values, i = [
19
- `src="${g(t)}"`,
20
- `alt="${g(r)}"`,
21
- `padding="${u(n)}"`,
22
- `align="${s}"`
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 a !== "auto" && i.push(`width="${a}px"`), o && i.push(`href="${g(o)}"`), c !== void 0 && c > 0 && i.push(`border-radius="${c}px"`), l && i.push(`css-class="${g(l)}"`), `<mj-image ${i.join(" ")} />`;
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 g(e) {
45
+ function b(e) {
27
46
  return e.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
28
47
  }
29
- function A(e) {
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: r,
33
- backgroundColor: a,
34
- textColor: o,
35
- borderRadius: n,
36
- padding: s,
37
- innerPadding: c,
38
- fontSize: l,
39
- fontWeight: i,
40
- alignment: d,
41
- fullWidth: $,
42
- cssClass: p
43
- } = e.values, f = [
44
- `href="${h(r)}"`,
45
- `background-color="${a}"`,
46
- `color="${o}"`,
47
- `border-radius="${n}px"`,
48
- `padding="${u(s)}"`,
49
- `inner-padding="${u(c)}"`,
50
- `font-size="${l}px"`,
51
- `font-weight="${i}"`,
52
- `align="${d}"`
53
- ];
54
- $ && f.push('width="100%"'), p && f.push(`css-class="${h(p)}"`);
55
- const k = t.replace(/^\s*<p>([\s\S]*?)<\/p>\s*$/i, "$1");
56
- return `<mj-button ${f.join(" ")}>${k}</mj-button>`;
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 h(e) {
81
+ function v(e) {
59
82
  return e.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
60
83
  }
61
- function B(e) {
62
- const { borderColor: t, borderWidth: r, borderStyle: a, padding: o, width: n } = e.values;
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="${r}px"`,
66
- `border-style="${a}"`,
67
- `padding="${u(o)}"`,
68
- `width="${n}"`
88
+ `border-width="${n}px"`,
89
+ `border-style="${r}"`,
90
+ `padding="${g(s)}"`,
91
+ `width="${i}"`
69
92
  ].join(" ")} />`;
70
93
  }
71
- function M(e) {
94
+ function K(e) {
72
95
  const { height: t } = e.values;
73
96
  return `<mj-spacer height="${t}px" />`;
74
97
  }
75
- function R(e) {
98
+ function F(e) {
76
99
  switch (e.type) {
77
100
  case "facebook":
78
101
  return "facebook";
@@ -92,219 +115,270 @@ function R(e) {
92
115
  return "web";
93
116
  }
94
117
  }
95
- function S(e, t) {
96
- const a = [
97
- `name="${R(e)}"`,
98
- `href="${j(e.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 && a.push(`src="${j(e.iconUrl)}"`);
102
- const o = e.label ?? "";
103
- return `<mj-social-element ${a.join(" ")}>${T(o)}</mj-social-element>`;
104
- }
105
- function H(e) {
106
- const { icons: t, iconSize: r, spacing: a, alignment: o, padding: n } = e.values, s = [
107
- `icon-size="${r}px"`,
108
- `icon-padding="${a}px"`,
109
- `align="${o}"`,
110
- `padding="${u(n)}"`,
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
- ], c = t.map((l) => S(l, r)).join(`
135
+ ], a = t.map((l) => V(l, n)).join(`
113
136
  `);
114
- return `<mj-social ${s.join(" ")}>
115
- ${c}
137
+ return `<mj-social ${o.join(" ")}>
138
+ ${a}
116
139
  </mj-social>`;
117
140
  }
118
- function j(e) {
141
+ function C(e) {
119
142
  return e.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
120
143
  }
121
- function T(e) {
144
+ function Y(e) {
122
145
  return e.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
123
146
  }
124
- const W = /<(\s*\/?\s*mj-raw\b[^>]*)>/gi;
125
- function z(e) {
126
- return e.replace(W, "&lt;$1&gt;");
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, "&lt;$1&gt;") + e.slice(t);
127
151
  }
128
- function N(e) {
129
- const { content: t, padding: r } = e.values, a = u(r), o = z(t);
130
- return `<mj-raw><div class="lp-html" style="padding: ${a};">${o}</div></mj-raw>`;
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 _(e) {
133
- const { content: t, innerPadding: r } = e.values;
156
+ function ee(e) {
157
+ const { content: t, innerPadding: n } = e.values;
134
158
  return `<mj-text ${[
135
- `padding="${u(r)}"`
159
+ `padding="${g(n)}"`
136
160
  ].join(" ")}>${t}</mj-text>`;
137
161
  }
138
- function q(e) {
139
- const t = e.values, r = [
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="${u(t.padding)}"`
167
+ `padding="${g(t.padding)}"`
144
168
  ];
145
- 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(" ")}>
146
- <mj-text padding="${u(t.innerPadding)}">${t.content}</mj-text>
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 D(e) {
173
+ function ne(e) {
150
174
  return e.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
151
175
  }
152
- function O(e) {
176
+ function re(e) {
153
177
  const t = [
154
- `href="${I(e.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(" ")}>${U(e.text)}</mj-navbar-link>`;
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 E(e) {
159
- const { links: t, hamburger: r, padding: a } = e.values, o = [
160
- `hamburger="${r}"`,
161
- `padding="${u(a)}"`
162
- ], n = t.map((s) => O(s)).join(`
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 ${o.join(" ")}>
165
- ${n}
188
+ return `<mj-navbar ${s.join(" ")}>
189
+ ${i}
166
190
  </mj-navbar>`;
167
191
  }
168
- function I(e) {
192
+ function oe(e) {
169
193
  return e.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
170
194
  }
171
- function U(e) {
195
+ function ie(e) {
172
196
  return e.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
173
197
  }
174
- function L(e) {
198
+ function ae(e) {
175
199
  switch (e.type) {
176
200
  case "text":
177
- return w(e);
201
+ return q(e);
178
202
  case "image":
179
- return C(e);
203
+ return W(e);
180
204
  case "button":
181
- return A(e);
205
+ return P(e);
182
206
  case "divider":
183
- return B(e);
207
+ return G(e);
184
208
  case "spacer":
185
- return M(e);
209
+ return K(e);
186
210
  case "social":
187
- return H(e);
211
+ return J(e);
188
212
  case "html":
189
- return N(e);
213
+ return Z(e);
190
214
  case "hero":
191
- return _(e);
215
+ return ee(e);
192
216
  case "navbar":
193
- return E(e);
217
+ return se(e);
194
218
  default:
195
- return P(e);
219
+ return ce(e);
196
220
  }
197
221
  }
198
- function P(e) {
199
- const t = e.type, r = x(t);
200
- return r != null && r.renderMjml ? r.renderMjml(e) : `<!-- Unknown block type: ${t} -->`;
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 K(e, t) {
203
- const { backgroundColor: r, padding: a, borderRadius: o, verticalAlign: n, cssClass: s } = e.attributes, c = [
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="${u(a)}"`,
206
- `vertical-align="${n}"`
229
+ `padding="${g(r)}"`,
230
+ `vertical-align="${i}"`
207
231
  ];
208
- r && c.push(`background-color="${r}"`), o !== void 0 && o > 0 && c.push(`border-radius="${o}px"`), s && c.push(`css-class="${m(s)}"`);
209
- const l = e.blocks.map((i) => ` ${L(i)}`).join(`
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 ${c.join(" ")}>
212
- ${l}
243
+ return ` <mj-column ${a.join(" ")}>
244
+ ${d}
213
245
  </mj-column>`;
214
246
  }
215
- function F(e) {
216
- if (e.columns.length === 1 && e.columns[0].blocks.length === 1 && e.columns[0].blocks[0].type === "hero")
217
- return b(
218
- q(e.columns[0].blocks[0]),
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
- const { backgroundColor: t, backgroundImage: r, padding: a, fullWidth: o, cssClass: n } = e.attributes, s = [
222
- `padding="${u(a)}"`
263
+ }
264
+ const { backgroundColor: t, padding: n, fullWidth: r, cssClass: s, noStackOnMobile: i } = e.attributes, o = [
265
+ `padding="${g(n)}"`
223
266
  ];
224
- o && s.push('full-width="full-width"'), t && s.push(`background-color="${t}"`), r && (s.push(`background-url="${m(r)}"`), s.push('background-size="cover"'), s.push('background-repeat="no-repeat"')), n && s.push(`css-class="${m(n)}"`);
225
- const c = e.columnRatios.reduce((i, d) => i + d, 0), l = e.columns.map((i, d) => {
226
- const p = ((e.columnRatios[d] ?? 1) / c * 100).toFixed(2).replace(/\.?0+$/, "");
227
- return K(i, `${p}%`);
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
- return b(
231
- ` <mj-section ${s.join(" ")}>
232
- ${l}
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 b(e, t) {
289
+ function y(e, t, n = " ") {
238
290
  const r = t == null ? void 0 : t.trim();
239
- return r ? ` <mj-raw>{{#if ${r}}}</mj-raw>
291
+ return r ? `${n}<mj-raw>{{#if ${r}}}</mj-raw>
240
292
  ${e}
241
- <mj-raw>{{/if}}</mj-raw>` : e;
293
+ ${n}<mj-raw>{{/if}}</mj-raw>` : e;
242
294
  }
243
- const G = ` <mj-raw>
295
+ function T(e, t) {
296
+ const n = t == null ? void 0 : t.trim();
297
+ return n ? ` <mj-raw>{{#each ${n}}}</mj-raw>
298
+ ${e}
299
+ <mj-raw>{{/each}}</mj-raw>` : e;
300
+ }
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>`, J = ` <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>`, Q = ` <mj-style inline="inline">
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 V(e) {
257
- const t = /* @__PURE__ */ new Set(), r = [];
258
- for (const a of e) {
259
- if (!a.url || t.has(a.url)) continue;
260
- t.add(a.url);
261
- const o = a.family.split(",")[0].trim().replace(/^['"]|['"]$/g, ""), n = m(a.url), s = m(o);
262
- r.push(` <mj-font name="${s}" href="${n}" />`), r.push(` <mj-raw><link rel="stylesheet" href="${n}"></mj-raw>`);
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 r.join(`
322
+ return n.join(`
265
323
  `);
266
324
  }
267
- function X(e, t) {
268
- const { fontFamily: r, css: a } = e.body.attributes, o = e.metadata.previewText, n = [];
269
- t.outlookWorkarounds && (n.push(G), n.push(J), n.push(Q));
270
- const s = V(t.fonts);
271
- return s && n.push(s), n.push(` <mj-attributes>
272
- <mj-all font-family="${m(r)}" />
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>`), e.body.rows.some((l) => l.columns.some((i) => i.blocks.some((d) => d.type === "html"))) && n.push(` <mj-style inline="inline">
276
- .lp-html { font-size: 14px; line-height: 1.5; font-family: ${r.replace(/[<>{};]/g, "")}; }
277
- </mj-style>`), a && n.push(` <mj-style>
278
- ${a.replace(/<\/mj-style/gi, "<\\/mj-style")}
279
- </mj-style>`), o && n.push(` <mj-preview>${Z(o)}</mj-preview>`), ` <mj-head>
280
- ${n.join(`
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(`
281
355
  `)}
282
356
  </mj-head>`;
283
357
  }
284
- function Y(e, t) {
285
- const r = {
358
+ function $e(e, t) {
359
+ const n = {
286
360
  outlookWorkarounds: (t == null ? void 0 : t.outlookWorkarounds) ?? !0,
287
361
  fonts: (t == null ? void 0 : t.fonts) ?? []
288
- }, { width: a, backgroundColor: o } = e.body.attributes, n = [
289
- `width="${a}px"`
362
+ }, { width: r, backgroundColor: s } = e.body.attributes, i = [
363
+ `width="${r}px"`
290
364
  ];
291
- o && n.push(`background-color="${o}"`);
292
- const s = X(e, r), c = e.body.rows.map((l) => F(l)).join(`
365
+ s && i.push(`background-color="${s}"`);
366
+ const o = he(e, n), a = e.body.rows.map((l) => ue(l)).join(`
293
367
  `);
294
- return `<mjml>
295
- ${s}
296
- <mj-body ${n.join(" ")}>
297
- ${c}
368
+ return `<mjml${fe(e)}>
369
+ ${o}
370
+ <mj-body ${i.join(" ")}>
371
+ ${a}
298
372
  </mj-body>
299
373
  </mjml>`;
300
374
  }
301
- function m(e) {
375
+ function h(e) {
302
376
  return e.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
303
377
  }
304
- function Z(e) {
378
+ function B(e) {
305
379
  return e.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
306
380
  }
307
- class re {
381
+ class Be {
308
382
  /**
309
383
  * Renders a PigeonDocument to HTML using MJML as the compilation backend.
310
384
  *
@@ -312,45 +386,169 @@ class re {
312
386
  * @param options - Optional rendering configuration (minify, beautify, inlineCss)
313
387
  * @returns A promise resolving to the rendered HTML and any errors
314
388
  */
315
- async render(t, r) {
316
- const a = Y(t, {
317
- outlookWorkarounds: r == null ? void 0 : r.outlookWorkarounds,
318
- fonts: r == null ? void 0 : r.fonts
319
- }), o = {
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 = {
320
394
  // By default MJML inlines CSS; respect the option if provided
321
395
  keepComments: !1
322
396
  };
323
- r != null && r.minify && (o.minify = !0), r != null && r.beautify && (o.beautify = !0);
397
+ n != null && n.minify && (s.minify = !0), n != null && n.beautify && (s.beautify = !0);
324
398
  try {
325
- const n = y(a, o), s = (n.errors || []).map((c) => ({
326
- message: c.message,
327
- line: c.line,
328
- tagName: c.tagName
399
+ const i = R(r, s), o = (i.errors || []).map((a) => ({
400
+ message: a.message,
401
+ line: a.line,
402
+ tagName: a.tagName
329
403
  }));
330
404
  return {
331
- html: n.html,
332
- errors: s
405
+ html: i.html,
406
+ errors: o
333
407
  };
334
- } catch (n) {
408
+ } catch (i) {
335
409
  return {
336
410
  html: "",
337
- errors: [{ message: `MJML compilation failed: ${n instanceof Error ? n.message : String(n)}` }]
411
+ errors: [{ message: `MJML compilation failed: ${i instanceof Error ? i.message : String(i)}` }]
338
412
  };
339
413
  }
340
414
  }
341
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
+ }
342
539
  export {
343
- re as MjmlRenderer,
344
- Y as documentToMjml,
345
- A as renderButtonBlock,
346
- B as renderDividerBlock,
347
- _ as renderHeroBlock,
348
- q as renderHeroSection,
349
- N as renderHtmlBlock,
350
- C as renderImageBlock,
351
- E as renderNavBarBlock,
352
- H as renderSocialBlock,
353
- M as renderSpacerBlock,
354
- w as renderTextBlock,
355
- u as spacingToMjml
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
356
554
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lit-pigeon/renderer-mjml",
3
- "version": "0.2.5",
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.3.3"
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": "^3.0.0"
30
+ "vitest": "^4.1.12"
31
31
  },
32
32
  "publishConfig": {
33
33
  "access": "public"