@lit-pigeon/import-unlayer 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Lit Pigeon Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,102 @@
1
+ # @lit-pigeon/import-unlayer
2
+
3
+ Import an [Unlayer](https://unlayer.com/) design JSON into a Lit Pigeon
4
+ `PigeonDocument` — migrate existing templates instead of rebuilding them.
5
+
6
+ Pairs with [`@lit-pigeon/parser-mjml`](../parser-mjml) (MJML → document) and
7
+ [`@lit-pigeon/figma-import`](../figma-import) (Figma frame → document).
8
+
9
+ ## Install
10
+
11
+ ```bash
12
+ npm install @lit-pigeon/import-unlayer
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ Feed it whatever `editor.saveDesign()` gave you — the parsed object or the raw
18
+ JSON string:
19
+
20
+ ```ts
21
+ import { unlayerToDocument } from '@lit-pigeon/import-unlayer';
22
+
23
+ const { document, warnings } = unlayerToDocument(design);
24
+
25
+ for (const warning of warnings) {
26
+ console.warn(`[${warning.code}] ${warning.message}`);
27
+ }
28
+ ```
29
+
30
+ Then render it, or load it straight into the editor:
31
+
32
+ ```ts
33
+ import { documentToMjml } from '@lit-pigeon/renderer-mjml';
34
+
35
+ const mjml = documentToMjml(document);
36
+ ```
37
+
38
+ ### Options
39
+
40
+ | Option | Default | Description |
41
+ | --- | --- | --- |
42
+ | `name` | `'Imported from Unlayer'` | Name for the imported template. |
43
+
44
+ ## What gets imported
45
+
46
+ | Unlayer | Lit Pigeon | Notes |
47
+ | --- | --- | --- |
48
+ | `text` | `text` | Block-level font size and colour are inlined (see below). |
49
+ | `heading` | `text` | Wrapped in its `<h1>`–`<h3>` tag; `h4`–`h6` clamp to `h3`. |
50
+ | `image` | `image` | Link, alt text, explicit and auto widths. |
51
+ | `button` | `button` | Colours, radius, inner/outer padding. `size.autoWidth` inverts to `fullWidth`. |
52
+ | `divider` | `divider` | Border colour, width, and style. |
53
+ | `html` | `html` | Passed through verbatim. |
54
+ | `menu` | `navbar` | Items, alignment, link colour and size. |
55
+ | `social` | `social` | Known platforms map by name; anything else becomes a `custom` icon. |
56
+ | rows / `cells` | rows / `columnRatios` | Relative widths convert to the 12-column grid (`[1, 2]` → `[4, 8]`). |
57
+ | body `values` | body attributes | Width, background, font family, alignment, preheader. |
58
+
59
+ ### Why colours end up in a `<span>`
60
+
61
+ Unlayer stores font size and colour on the *content block*. A Lit Pigeon
62
+ `TextBlock` only carries `content`, `padding`, `lineHeight` and `textAlign`, so
63
+ dropping them would lose the design — a template with `textColor: #ffffff` on a
64
+ black body would import as black-on-black.
65
+
66
+ The importer pushes that typography into the HTML instead. The editor's
67
+ rich-text sanitiser keeps `style` on `<span>` only, and only for `color`,
68
+ `font-family` and `font-size`, so the styling wraps the *inner* content of each
69
+ top-level element:
70
+
71
+ ```html
72
+ <!-- Unlayer: { text: "<p>Hi</p>", color: "#ffffff", fontSize: "17px" } -->
73
+ <p><span style="color: #ffffff; font-size: 17px">Hi</span></p>
74
+ ```
75
+
76
+ ## What does not survive
77
+
78
+ Import is best-effort and never throws. Anything that cannot be represented is
79
+ dropped and reported in `warnings`, so a migration UI can tell the user exactly
80
+ what needs a second look.
81
+
82
+ | Code | Meaning |
83
+ | --- | --- |
84
+ | `not-a-design` | Input was not valid JSON, or had no `body` key. |
85
+ | `unsupported-block` | An Unlayer block with no Pigeon equivalent (`video`, `timer`, `form`). |
86
+ | `custom-tool` | A `custom#*` tool. Re-create it as a Lit Pigeon plugin block. |
87
+ | `display-condition-dropped` | See below. |
88
+ | `heading-level-clamped` | `h4`–`h6` imported as `h3`. |
89
+ | `empty-row` | A row with no columns was skipped. |
90
+ | `no-rows` | The design had no rows at all. |
91
+
92
+ **Display conditions are dropped deliberately.** Unlayer stores them as raw
93
+ `before`/`after` template fragments, not as the boolean expression Pigeon's
94
+ `condition` wraps in `{{#if}}`. Guessing a translation would silently change
95
+ who receives the content, so the importer reports the condition by name and
96
+ leaves the row unconditional for you to re-add.
97
+
98
+ Mobile `_override` values and `hideDesktop` / `hideMobile` are not yet mapped.
99
+
100
+ ## Licence
101
+
102
+ MIT
package/dist/index.cjs ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=require("@lit-pigeon/core"),z=require("htmlparser2");function p(t,e=0){const o=r=>({top:r,right:r,bottom:r,left:r});if(typeof t!="string"||!t.trim())return o(e);const n=t.trim().split(/\s+/).map(r=>g(r,e));switch(n.length){case 1:return o(n[0]);case 2:return{top:n[0],right:n[1],bottom:n[0],left:n[1]};case 3:return{top:n[0],right:n[1],bottom:n[2],left:n[1]};case 4:return{top:n[0],right:n[1],bottom:n[2],left:n[3]};default:return o(e)}}function g(t,e=0){if(typeof t=="number")return Number.isFinite(t)?t:e;if(typeof t!="string")return e;const o=parseFloat(t);return Number.isNaN(o)?e:o}function s(t,e=""){return typeof t=="string"?t:e}function u(t){const e=s(t).trim();return e||void 0}function y(t,e="left"){return t==="left"||t==="center"||t==="right"?t:e}function c(t,...e){let o=t;for(const n of e){if(typeof o!="object"||o===null)return;o=o[n]}return o}function D(t,e){if(e<=0)return[];const o=t&&t.length===e&&t.every(a=>a>0)?t:new Array(e).fill(1),n=o.reduce((a,d)=>a+d,0),r=o.map(a=>Math.max(1,Math.round(a*12/n)));let i=12-r.reduce((a,d)=>a+d,0);for(;i!==0;){const a=i>0?r.indexOf(Math.max(...r)):r.indexOf(Math.max(...r.filter(d=>d>1)));if(a===-1)break;r[a]+=i>0?1:-1,i+=i>0?-1:1}return r}const x=new Set(["area","base","br","col","embed","hr","img","input","link","meta","source","track","wbr"]);function P(t){const e=[];return t.color&&e.push(`color: ${t.color}`),t.fontFamily&&e.push(`font-family: ${t.fontFamily}`),t.fontSize&&e.push(`font-size: ${t.fontSize}`),e.length?e.join("; "):void 0}function S(t){return t.replace(/&/g,"&amp;").replace(/"/g,"&quot;")}function k(t){return Object.entries(t).map(([e,o])=>` ${e}="${S(o)}"`).join("")}function $(t,e){const o=P(e);if(!o)return t;const n=`<span style="${S(o)}">`,r="</span>";let i="",a=0,d=!1;const m=new z.Parser({onopentag(l,h){if(x.has(l)){a===0?i+=`${n}<${l}${k(h)}>${r}`:i+=`<${l}${k(h)}>`;return}i+=`<${l}${k(h)}>`,a===0&&(i+=n,d=!0),a++},ontext(l){if(a===0){i+=l.trim()?`${n}${l}${r}`:l;return}i+=l},onclosetag(l){x.has(l)||(a--,a===0&&d&&(i+=r,d=!1),i+=`</${l}>`)}},{recognizeSelfClosing:!0,lowerCaseTags:!0,lowerCaseAttributeNames:!0});return m.write(t),m.end(),i}function T(t){const e=t.trim();return e?/^<(p|h[1-6]|ul|ol|blockquote|div|table)[\s>]/i.test(e)?e:`<p>${e}</p>`:"<p></p>"}function O(t,e,o){const n=s(t.type),r=t.values??{};switch(n){case"text":return W(r,e);case"heading":return B(r,e,o);case"image":return N(r);case"button":return U(r);case"divider":return L(r);case"html":return R(r);case"menu":return H(r,e);case"social":return j(r);default:return null}}function W(t,e){const o=$(s(t.text),{fontSize:s(t.fontSize)||void 0,color:u(t.color)??e.textColor,fontFamily:e.fontFamily});return{id:f.generateId(),type:"text",values:{content:T(o),padding:p(t.containerPadding,10),lineHeight:s(t.lineHeight,"140%"),textAlign:y(t.textAlign)}}}function B(t,e,o){const n=s(t.headingType,"h1").toLowerCase();let r=/^h[1-6]$/.test(n)?n:"h1";/^h[4-6]$/.test(r)&&(o.push({code:"heading-level-clamped",message:`Heading level <${r}> is not supported by the editor; imported as <h3>.`}),r="h3");const i=$(s(t.text),{fontSize:s(t.fontSize)||void 0,color:u(t.color)??e.textColor,fontFamily:e.fontFamily});return{id:f.generateId(),type:"text",values:{content:`<${r}>${i}</${r}>`,padding:p(t.containerPadding,10),lineHeight:s(t.lineHeight,"140%"),textAlign:y(t.textAlign)}}}function N(t){const e=s(c(t,"action","values","href")),o=c(t,"src","autoWidth")===!0;return{id:f.generateId(),type:"image",values:{src:s(c(t,"src","url")),alt:s(t.altText),width:o?"auto":g(c(t,"src","width"),0)||"auto",...e?{href:e}:{},padding:p(t.containerPadding,10),alignment:y(t.textAlign,"center")}}}function U(t){return{id:f.generateId(),type:"button",values:{content:T(s(t.text)),href:s(c(t,"href","values","href"),"#"),backgroundColor:u(c(t,"buttonColors","backgroundColor"))??"#3b82f6",textColor:u(c(t,"buttonColors","color"))??"#ffffff",borderRadius:g(t.borderRadius,4),padding:p(t.containerPadding,10),innerPadding:p(t.padding,12),fontSize:g(t.fontSize,16),fontWeight:"600",alignment:y(t.textAlign,"center"),fullWidth:c(t,"size","autoWidth")===!1}}}function L(t){const e=s(c(t,"border","borderTopStyle"),"solid");return{id:f.generateId(),type:"divider",values:{borderColor:u(c(t,"border","borderTopColor"))??"#cccccc",borderWidth:g(c(t,"border","borderTopWidth"),1),borderStyle:e==="dashed"||e==="dotted"?e:"solid",padding:p(t.containerPadding,10),width:s(t.width,"100%")}}}function R(t){return{id:f.generateId(),type:"html",values:{content:s(t.html),padding:p(t.containerPadding,0)}}}function H(t,e){const o=c(t,"menu","items"),n=Array.isArray(o)?o.map(r=>({href:s(c(r,"link","values","href"),"#"),text:s(c(r,"text"))})):[];return{id:f.generateId(),type:"navbar",values:{links:n,hamburger:"none",alignment:y(t.align,"center"),padding:p(t.containerPadding,10),linkColor:u(t.linkColor)??u(t.textColor)??e.textColor??"#000000",linkFontSize:g(t.fontSize,14),linkPadding:s(t.padding,"5px 10px")}}}const E=["facebook","twitter","instagram","linkedin","youtube","tiktok"];function M(t){const e=t.toLowerCase();return e==="x"?"twitter":E.includes(e)?e:"custom"}function j(t){const e=c(t,"icons","icons"),o=Array.isArray(e)?e.map(n=>{const r=s(c(n,"name")),i=M(r),a=s(c(n,"image","url"))||s(c(n,"src"));return{type:i,href:s(c(n,"url"),"#"),...r?{label:r}:{},...i==="custom"&&a?{iconUrl:a}:{}}}):[];return{id:f.generateId(),type:"social",values:{icons:o,iconSize:g(c(t,"icons","iconSize"),32),spacing:g(t.spacing,8),alignment:y(t.align,"center"),padding:p(t.containerPadding,10)}}}const w=600,I="#f4f4f5",F="Arial, Helvetica, sans-serif";function q(t,e={}){var A,C;const o=[],n=_(t,o),r=e.name??"Imported from Unlayer";if(!n)return{document:K(r),warnings:o};const i=((A=n.body)==null?void 0:A.values)??{},a={fontFamily:s(c(i,"fontFamily","value"))||void 0,textColor:u(i.textColor)},d=Array.isArray((C=n.body)==null?void 0:C.rows)?n.body.rows:[];d.length===0&&o.push({code:"no-rows",message:"The design contains no rows; imported an empty template."});const m=d.map(b=>J(b,a,o)).filter(b=>b!==null),l=new Date().toISOString(),h=s(i.preheaderText);return{document:{version:"1.0",metadata:{name:r,...h?{previewText:h}:{},createdAt:l,updatedAt:l},body:{attributes:{width:g(i.contentWidth,w)||w,backgroundColor:u(i.backgroundColor)??I,fontFamily:a.fontFamily??F,contentAlignment:i.contentAlign==="left"?"left":"center"},rows:m}},warnings:o}}function _(t,e){let o=t;if(typeof t=="string")try{o=JSON.parse(t)}catch{return e.push({code:"not-a-design",message:"Input is not valid JSON."}),null}return typeof o!="object"||o===null||!("body"in o)?(e.push({code:"not-a-design",message:"Input does not look like an Unlayer design (no `body` key). Export it with `editor.saveDesign()`."}),null):o}function J(t,e,o){const n=t.values??{},r=Array.isArray(t.columns)?t.columns:[];if(r.length===0)return o.push({code:"empty-row",message:"Skipped a row with no columns."}),null;if(n.displayCondition!=null){const d=s(c(n,"displayCondition","label"));o.push({code:"display-condition-dropped",message:`Display condition${d?` "${d}"`:""} was dropped — Unlayer stores raw template fragments that cannot be translated automatically. Re-add it on the row.`})}const i=r.map(d=>V(d,e,o)),a=s(c(n,"backgroundImage","url"));return{id:f.generateId(),type:"row",attributes:{...u(n.backgroundColor)??u(n.columnsBackgroundColor)?{backgroundColor:u(n.backgroundColor)??u(n.columnsBackgroundColor)}:{},...a?{backgroundImage:a}:{},padding:p(n.padding,0),fullWidth:n.fullWidth===!0||c(n,"backgroundImage","fullWidth")===!0},columns:i,columnRatios:D(t.cells,i.length),locked:n.locked===!0||n.draggable===!1&&n.deletable===!1}}function V(t,e,o){const n=t.values??{},r=Array.isArray(t.contents)?t.contents:[],i=[];for(const d of r){const m=O(d,e,o);if(m){i.push(m);continue}const l=s(d.type,"unknown");l.startsWith("custom#")?o.push({code:"custom-tool",contentType:l,message:`Custom tool "${l.slice(7)}" was dropped — re-create it as a Lit Pigeon plugin block.`}):o.push({code:"unsupported-block",contentType:l,message:`Unsupported Unlayer block "${l}" was dropped.`})}const a=u(n.backgroundColor);return{id:f.generateId(),type:"column",attributes:{...a?{backgroundColor:a}:{},padding:p(n.padding,0),verticalAlign:G(n.verticalAlign)},blocks:i}}function G(t){return t==="middle"||t==="bottom"?t:"top"}function K(t){const e=new Date().toISOString();return{version:"1.0",metadata:{name:t,createdAt:e,updatedAt:e},body:{attributes:{width:w,backgroundColor:I,fontFamily:F,contentAlignment:"center"},rows:[]}}}exports.unlayerToDocument=q;
@@ -0,0 +1,78 @@
1
+ import { PigeonDocument } from '@lit-pigeon/core';
2
+
3
+ export declare interface ImportOptions {
4
+ /** Name for the imported template. Defaults to `'Imported from Unlayer'`. */
5
+ name?: string;
6
+ }
7
+
8
+ export declare interface ImportResult {
9
+ document: PigeonDocument;
10
+ warnings: ImportWarning[];
11
+ }
12
+
13
+ /**
14
+ * A single piece of fidelity loss encountered during import.
15
+ *
16
+ * The importer never throws on unsupported content — it converts what it can
17
+ * and reports the rest, so a migration UI can show the user exactly what needs
18
+ * a second look rather than failing the whole template.
19
+ */
20
+ export declare interface ImportWarning {
21
+ code: ImportWarningCode;
22
+ message: string;
23
+ /** The Unlayer content type responsible, when the warning is block-scoped. */
24
+ contentType?: string;
25
+ }
26
+
27
+ export declare type ImportWarningCode = 'not-a-design' | 'no-rows' | 'unsupported-block' | 'custom-tool' | 'display-condition-dropped' | 'heading-level-clamped' | 'empty-row';
28
+
29
+ export declare interface UnlayerBody {
30
+ id?: string;
31
+ rows?: UnlayerRow[];
32
+ values?: Record<string, unknown>;
33
+ }
34
+
35
+ export declare interface UnlayerColumn {
36
+ id?: string;
37
+ contents?: UnlayerContent[];
38
+ values?: Record<string, unknown>;
39
+ }
40
+
41
+ export declare interface UnlayerContent {
42
+ id?: string;
43
+ /** e.g. 'text' | 'heading' | 'image' | 'button' | 'divider' | 'html' | 'menu' | 'social' | 'custom#my_tool' */
44
+ type?: string;
45
+ values?: Record<string, unknown>;
46
+ }
47
+
48
+ export declare interface UnlayerDesign {
49
+ counters?: Record<string, number>;
50
+ body?: UnlayerBody;
51
+ schemaVersion?: number;
52
+ }
53
+
54
+ export declare interface UnlayerRow {
55
+ id?: string;
56
+ /** Relative column widths, e.g. `[1, 1]` for a 50/50 split. */
57
+ cells?: number[];
58
+ columns?: UnlayerColumn[];
59
+ values?: Record<string, unknown>;
60
+ }
61
+
62
+ /**
63
+ * Converts an Unlayer design JSON export into a `PigeonDocument`.
64
+ *
65
+ * Accepts either the parsed object or the raw JSON string produced by
66
+ * `editor.saveDesign()`. Import is best-effort and never throws on malformed
67
+ * or unsupported input: anything that cannot be represented is dropped and
68
+ * reported through {@link ImportResult.warnings}.
69
+ *
70
+ * @example
71
+ * ```ts
72
+ * const { document, warnings } = unlayerToDocument(designJson);
73
+ * if (warnings.length) console.warn(warnings);
74
+ * ```
75
+ */
76
+ export declare function unlayerToDocument(design: UnlayerDesign | string, options?: ImportOptions): ImportResult;
77
+
78
+ export { }
package/dist/index.js ADDED
@@ -0,0 +1,406 @@
1
+ import { generateId as f } from "@lit-pigeon/core";
2
+ import { Parser as D } from "htmlparser2";
3
+ function p(t, e = 0) {
4
+ const o = (r) => ({ top: r, right: r, bottom: r, left: r });
5
+ if (typeof t != "string" || !t.trim()) return o(e);
6
+ const n = t.trim().split(/\s+/).map((r) => g(r, e));
7
+ switch (n.length) {
8
+ case 1:
9
+ return o(n[0]);
10
+ case 2:
11
+ return { top: n[0], right: n[1], bottom: n[0], left: n[1] };
12
+ case 3:
13
+ return { top: n[0], right: n[1], bottom: n[2], left: n[1] };
14
+ case 4:
15
+ return { top: n[0], right: n[1], bottom: n[2], left: n[3] };
16
+ default:
17
+ return o(e);
18
+ }
19
+ }
20
+ function g(t, e = 0) {
21
+ if (typeof t == "number") return Number.isFinite(t) ? t : e;
22
+ if (typeof t != "string") return e;
23
+ const o = parseFloat(t);
24
+ return Number.isNaN(o) ? e : o;
25
+ }
26
+ function a(t, e = "") {
27
+ return typeof t == "string" ? t : e;
28
+ }
29
+ function d(t) {
30
+ const e = a(t).trim();
31
+ return e || void 0;
32
+ }
33
+ function y(t, e = "left") {
34
+ return t === "left" || t === "center" || t === "right" ? t : e;
35
+ }
36
+ function c(t, ...e) {
37
+ let o = t;
38
+ for (const n of e) {
39
+ if (typeof o != "object" || o === null) return;
40
+ o = o[n];
41
+ }
42
+ return o;
43
+ }
44
+ function I(t, e) {
45
+ if (e <= 0) return [];
46
+ const o = t && t.length === e && t.every((s) => s > 0) ? t : new Array(e).fill(1), n = o.reduce((s, u) => s + u, 0), r = o.map((s) => Math.max(1, Math.round(s * 12 / n)));
47
+ let i = 12 - r.reduce((s, u) => s + u, 0);
48
+ for (; i !== 0; ) {
49
+ const s = i > 0 ? r.indexOf(Math.max(...r)) : r.indexOf(Math.max(...r.filter((u) => u > 1)));
50
+ if (s === -1) break;
51
+ r[s] += i > 0 ? 1 : -1, i += i > 0 ? -1 : 1;
52
+ }
53
+ return r;
54
+ }
55
+ const x = /* @__PURE__ */ new Set([
56
+ "area",
57
+ "base",
58
+ "br",
59
+ "col",
60
+ "embed",
61
+ "hr",
62
+ "img",
63
+ "input",
64
+ "link",
65
+ "meta",
66
+ "source",
67
+ "track",
68
+ "wbr"
69
+ ]);
70
+ function P(t) {
71
+ const e = [];
72
+ return t.color && e.push(`color: ${t.color}`), t.fontFamily && e.push(`font-family: ${t.fontFamily}`), t.fontSize && e.push(`font-size: ${t.fontSize}`), e.length ? e.join("; ") : void 0;
73
+ }
74
+ function S(t) {
75
+ return t.replace(/&/g, "&amp;").replace(/"/g, "&quot;");
76
+ }
77
+ function k(t) {
78
+ return Object.entries(t).map(([e, o]) => ` ${e}="${S(o)}"`).join("");
79
+ }
80
+ function $(t, e) {
81
+ const o = P(e);
82
+ if (!o) return t;
83
+ const n = `<span style="${S(o)}">`, r = "</span>";
84
+ let i = "", s = 0, u = !1;
85
+ const m = new D(
86
+ {
87
+ onopentag(l, h) {
88
+ if (x.has(l)) {
89
+ s === 0 ? i += `${n}<${l}${k(h)}>${r}` : i += `<${l}${k(h)}>`;
90
+ return;
91
+ }
92
+ i += `<${l}${k(h)}>`, s === 0 && (i += n, u = !0), s++;
93
+ },
94
+ ontext(l) {
95
+ if (s === 0) {
96
+ i += l.trim() ? `${n}${l}${r}` : l;
97
+ return;
98
+ }
99
+ i += l;
100
+ },
101
+ onclosetag(l) {
102
+ x.has(l) || (s--, s === 0 && u && (i += r, u = !1), i += `</${l}>`);
103
+ }
104
+ },
105
+ { recognizeSelfClosing: !0, lowerCaseTags: !0, lowerCaseAttributeNames: !0 }
106
+ );
107
+ return m.write(t), m.end(), i;
108
+ }
109
+ function T(t) {
110
+ const e = t.trim();
111
+ return e ? /^<(p|h[1-6]|ul|ol|blockquote|div|table)[\s>]/i.test(e) ? e : `<p>${e}</p>` : "<p></p>";
112
+ }
113
+ function W(t, e, o) {
114
+ const n = a(t.type), r = t.values ?? {};
115
+ switch (n) {
116
+ case "text":
117
+ return B(r, e);
118
+ case "heading":
119
+ return O(r, e, o);
120
+ case "image":
121
+ return N(r);
122
+ case "button":
123
+ return U(r);
124
+ case "divider":
125
+ return L(r);
126
+ case "html":
127
+ return R(r);
128
+ case "menu":
129
+ return H(r, e);
130
+ case "social":
131
+ return j(r);
132
+ default:
133
+ return null;
134
+ }
135
+ }
136
+ function B(t, e) {
137
+ const o = $(a(t.text), {
138
+ fontSize: a(t.fontSize) || void 0,
139
+ color: d(t.color) ?? e.textColor,
140
+ fontFamily: e.fontFamily
141
+ });
142
+ return {
143
+ id: f(),
144
+ type: "text",
145
+ values: {
146
+ content: T(o),
147
+ padding: p(t.containerPadding, 10),
148
+ lineHeight: a(t.lineHeight, "140%"),
149
+ textAlign: y(t.textAlign)
150
+ }
151
+ };
152
+ }
153
+ function O(t, e, o) {
154
+ const n = a(t.headingType, "h1").toLowerCase();
155
+ let r = /^h[1-6]$/.test(n) ? n : "h1";
156
+ /^h[4-6]$/.test(r) && (o.push({
157
+ code: "heading-level-clamped",
158
+ message: `Heading level <${r}> is not supported by the editor; imported as <h3>.`
159
+ }), r = "h3");
160
+ const i = $(a(t.text), {
161
+ fontSize: a(t.fontSize) || void 0,
162
+ color: d(t.color) ?? e.textColor,
163
+ fontFamily: e.fontFamily
164
+ });
165
+ return {
166
+ id: f(),
167
+ type: "text",
168
+ values: {
169
+ content: `<${r}>${i}</${r}>`,
170
+ padding: p(t.containerPadding, 10),
171
+ lineHeight: a(t.lineHeight, "140%"),
172
+ textAlign: y(t.textAlign)
173
+ }
174
+ };
175
+ }
176
+ function N(t) {
177
+ const e = a(c(t, "action", "values", "href")), o = c(t, "src", "autoWidth") === !0;
178
+ return {
179
+ id: f(),
180
+ type: "image",
181
+ values: {
182
+ src: a(c(t, "src", "url")),
183
+ alt: a(t.altText),
184
+ width: o ? "auto" : g(c(t, "src", "width"), 0) || "auto",
185
+ ...e ? { href: e } : {},
186
+ padding: p(t.containerPadding, 10),
187
+ alignment: y(t.textAlign, "center")
188
+ }
189
+ };
190
+ }
191
+ function U(t) {
192
+ return {
193
+ id: f(),
194
+ type: "button",
195
+ values: {
196
+ content: T(a(t.text)),
197
+ href: a(c(t, "href", "values", "href"), "#"),
198
+ backgroundColor: d(c(t, "buttonColors", "backgroundColor")) ?? "#3b82f6",
199
+ textColor: d(c(t, "buttonColors", "color")) ?? "#ffffff",
200
+ borderRadius: g(t.borderRadius, 4),
201
+ padding: p(t.containerPadding, 10),
202
+ innerPadding: p(t.padding, 12),
203
+ fontSize: g(t.fontSize, 16),
204
+ fontWeight: "600",
205
+ alignment: y(t.textAlign, "center"),
206
+ // Unlayer's `size.autoWidth` is the inverse of Pigeon's `fullWidth`.
207
+ fullWidth: c(t, "size", "autoWidth") === !1
208
+ }
209
+ };
210
+ }
211
+ function L(t) {
212
+ const e = a(c(t, "border", "borderTopStyle"), "solid");
213
+ return {
214
+ id: f(),
215
+ type: "divider",
216
+ values: {
217
+ borderColor: d(c(t, "border", "borderTopColor")) ?? "#cccccc",
218
+ borderWidth: g(c(t, "border", "borderTopWidth"), 1),
219
+ borderStyle: e === "dashed" || e === "dotted" ? e : "solid",
220
+ padding: p(t.containerPadding, 10),
221
+ width: a(t.width, "100%")
222
+ }
223
+ };
224
+ }
225
+ function R(t) {
226
+ return {
227
+ id: f(),
228
+ type: "html",
229
+ values: {
230
+ content: a(t.html),
231
+ padding: p(t.containerPadding, 0)
232
+ }
233
+ };
234
+ }
235
+ function H(t, e) {
236
+ const o = c(t, "menu", "items"), n = Array.isArray(o) ? o.map((r) => ({
237
+ href: a(c(r, "link", "values", "href"), "#"),
238
+ text: a(c(r, "text"))
239
+ })) : [];
240
+ return {
241
+ id: f(),
242
+ type: "navbar",
243
+ values: {
244
+ links: n,
245
+ hamburger: "none",
246
+ alignment: y(t.align, "center"),
247
+ padding: p(t.containerPadding, 10),
248
+ linkColor: d(t.linkColor) ?? d(t.textColor) ?? e.textColor ?? "#000000",
249
+ linkFontSize: g(t.fontSize, 14),
250
+ linkPadding: a(t.padding, "5px 10px")
251
+ }
252
+ };
253
+ }
254
+ const E = ["facebook", "twitter", "instagram", "linkedin", "youtube", "tiktok"];
255
+ function M(t) {
256
+ const e = t.toLowerCase();
257
+ return e === "x" ? "twitter" : E.includes(e) ? e : "custom";
258
+ }
259
+ function j(t) {
260
+ const e = c(t, "icons", "icons"), o = Array.isArray(e) ? e.map((n) => {
261
+ const r = a(c(n, "name")), i = M(r), s = a(c(n, "image", "url")) || a(c(n, "src"));
262
+ return {
263
+ type: i,
264
+ href: a(c(n, "url"), "#"),
265
+ ...r ? { label: r } : {},
266
+ ...i === "custom" && s ? { iconUrl: s } : {}
267
+ };
268
+ }) : [];
269
+ return {
270
+ id: f(),
271
+ type: "social",
272
+ values: {
273
+ icons: o,
274
+ iconSize: g(c(t, "icons", "iconSize"), 32),
275
+ spacing: g(t.spacing, 8),
276
+ alignment: y(t.align, "center"),
277
+ padding: p(t.containerPadding, 10)
278
+ }
279
+ };
280
+ }
281
+ const w = 600, F = "#f4f4f5", z = "Arial, Helvetica, sans-serif";
282
+ function X(t, e = {}) {
283
+ var A, C;
284
+ const o = [], n = _(t, o), r = e.name ?? "Imported from Unlayer";
285
+ if (!n) return { document: G(r), warnings: o };
286
+ const i = ((A = n.body) == null ? void 0 : A.values) ?? {}, s = {
287
+ fontFamily: a(c(i, "fontFamily", "value")) || void 0,
288
+ textColor: d(i.textColor)
289
+ }, u = Array.isArray((C = n.body) == null ? void 0 : C.rows) ? n.body.rows : [];
290
+ u.length === 0 && o.push({ code: "no-rows", message: "The design contains no rows; imported an empty template." });
291
+ const m = u.map((b) => q(b, s, o)).filter((b) => b !== null), l = (/* @__PURE__ */ new Date()).toISOString(), h = a(i.preheaderText);
292
+ return {
293
+ document: {
294
+ version: "1.0",
295
+ metadata: {
296
+ name: r,
297
+ ...h ? { previewText: h } : {},
298
+ createdAt: l,
299
+ updatedAt: l
300
+ },
301
+ body: {
302
+ attributes: {
303
+ width: g(i.contentWidth, w) || w,
304
+ backgroundColor: d(i.backgroundColor) ?? F,
305
+ fontFamily: s.fontFamily ?? z,
306
+ contentAlignment: i.contentAlign === "left" ? "left" : "center"
307
+ },
308
+ rows: m
309
+ }
310
+ },
311
+ warnings: o
312
+ };
313
+ }
314
+ function _(t, e) {
315
+ let o = t;
316
+ if (typeof t == "string")
317
+ try {
318
+ o = JSON.parse(t);
319
+ } catch {
320
+ return e.push({ code: "not-a-design", message: "Input is not valid JSON." }), null;
321
+ }
322
+ return typeof o != "object" || o === null || !("body" in o) ? (e.push({
323
+ code: "not-a-design",
324
+ message: "Input does not look like an Unlayer design (no `body` key). Export it with `editor.saveDesign()`."
325
+ }), null) : o;
326
+ }
327
+ function q(t, e, o) {
328
+ const n = t.values ?? {}, r = Array.isArray(t.columns) ? t.columns : [];
329
+ if (r.length === 0)
330
+ return o.push({ code: "empty-row", message: "Skipped a row with no columns." }), null;
331
+ if (n.displayCondition != null) {
332
+ const u = a(c(n, "displayCondition", "label"));
333
+ o.push({
334
+ code: "display-condition-dropped",
335
+ message: `Display condition${u ? ` "${u}"` : ""} was dropped — Unlayer stores raw template fragments that cannot be translated automatically. Re-add it on the row.`
336
+ });
337
+ }
338
+ const i = r.map((u) => J(u, e, o)), s = a(c(n, "backgroundImage", "url"));
339
+ return {
340
+ id: f(),
341
+ type: "row",
342
+ attributes: {
343
+ ...d(n.backgroundColor) ?? d(n.columnsBackgroundColor) ? { backgroundColor: d(n.backgroundColor) ?? d(n.columnsBackgroundColor) } : {},
344
+ ...s ? { backgroundImage: s } : {},
345
+ padding: p(n.padding, 0),
346
+ fullWidth: n.fullWidth === !0 || c(n, "backgroundImage", "fullWidth") === !0
347
+ },
348
+ columns: i,
349
+ columnRatios: I(t.cells, i.length),
350
+ // Unlayer expresses "locked" as the absence of interaction flags.
351
+ locked: n.locked === !0 || n.draggable === !1 && n.deletable === !1
352
+ };
353
+ }
354
+ function J(t, e, o) {
355
+ const n = t.values ?? {}, r = Array.isArray(t.contents) ? t.contents : [], i = [];
356
+ for (const u of r) {
357
+ const m = W(u, e, o);
358
+ if (m) {
359
+ i.push(m);
360
+ continue;
361
+ }
362
+ const l = a(u.type, "unknown");
363
+ l.startsWith("custom#") ? o.push({
364
+ code: "custom-tool",
365
+ contentType: l,
366
+ message: `Custom tool "${l.slice(7)}" was dropped — re-create it as a Lit Pigeon plugin block.`
367
+ }) : o.push({
368
+ code: "unsupported-block",
369
+ contentType: l,
370
+ message: `Unsupported Unlayer block "${l}" was dropped.`
371
+ });
372
+ }
373
+ const s = d(n.backgroundColor);
374
+ return {
375
+ id: f(),
376
+ type: "column",
377
+ attributes: {
378
+ ...s ? { backgroundColor: s } : {},
379
+ padding: p(n.padding, 0),
380
+ verticalAlign: V(n.verticalAlign)
381
+ },
382
+ blocks: i
383
+ };
384
+ }
385
+ function V(t) {
386
+ return t === "middle" || t === "bottom" ? t : "top";
387
+ }
388
+ function G(t) {
389
+ const e = (/* @__PURE__ */ new Date()).toISOString();
390
+ return {
391
+ version: "1.0",
392
+ metadata: { name: t, createdAt: e, updatedAt: e },
393
+ body: {
394
+ attributes: {
395
+ width: w,
396
+ backgroundColor: F,
397
+ fontFamily: z,
398
+ contentAlignment: "center"
399
+ },
400
+ rows: []
401
+ }
402
+ };
403
+ }
404
+ export {
405
+ X as unlayerToDocument
406
+ };
package/package.json ADDED
@@ -0,0 +1,63 @@
1
+ {
2
+ "name": "@lit-pigeon/import-unlayer",
3
+ "version": "0.2.0",
4
+ "description": "Unlayer design-JSON to PigeonDocument importer for Lit Pigeon — migrate off Unlayer without rebuilding your templates",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "require": "./dist/index.cjs"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist",
18
+ "README.md",
19
+ "LICENSE"
20
+ ],
21
+ "dependencies": {
22
+ "htmlparser2": "^9.1.0",
23
+ "@lit-pigeon/core": "^0.3.3"
24
+ },
25
+ "devDependencies": {
26
+ "typescript": "^5.5.0",
27
+ "vite": "^6.0.0",
28
+ "vite-plugin-dts": "^4.0.0",
29
+ "vitest": "^3.0.0"
30
+ },
31
+ "publishConfig": {
32
+ "access": "public"
33
+ },
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "https://github.com/snxstudio/lit-pigeon.git",
37
+ "directory": "packages/import-unlayer"
38
+ },
39
+ "license": "MIT",
40
+ "homepage": "https://github.com/snxstudio/lit-pigeon/tree/main/packages/import-unlayer#readme",
41
+ "bugs": "https://github.com/snxstudio/lit-pigeon/issues",
42
+ "author": "Lit Pigeon Contributors",
43
+ "keywords": [
44
+ "email",
45
+ "email-template",
46
+ "email-editor",
47
+ "unlayer",
48
+ "unlayer-alternative",
49
+ "migration",
50
+ "import",
51
+ "mjml",
52
+ "html-email",
53
+ "lit",
54
+ "web-components"
55
+ ],
56
+ "scripts": {
57
+ "build": "vite build",
58
+ "dev": "vite build --watch",
59
+ "test": "vitest run",
60
+ "test:watch": "vitest",
61
+ "clean": "rm -rf dist"
62
+ }
63
+ }