@perkyzz/emvb 1.3.0 → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (161) hide show
  1. package/README.md +2 -2
  2. package/package.json +2 -2
  3. package/src/admin/bulk-api.ts +5 -1
  4. package/src/admin/content-item.ts +3 -0
  5. package/src/admin/copy-rich-text.ts +99 -9
  6. package/src/admin/editor/A11yCopilot.tsx +3 -1
  7. package/src/admin/editor/Editor.tsx +28 -3
  8. package/src/admin/editor/Timeline.tsx +1 -8
  9. package/src/admin/editor/canvas/DeviceBar.tsx +7 -2
  10. package/src/admin/editor/canvas/EmptyCanvas.tsx +11 -2
  11. package/src/admin/editor/canvas/use-canvas-dnd.ts +1 -20
  12. package/src/admin/editor/canvas/vnode-react.tsx +2 -0
  13. package/src/admin/editor/co-design.ts +36 -1
  14. package/src/admin/editor/collection-previews.ts +7 -8
  15. package/src/admin/editor/dnd/dragged-source.ts +22 -0
  16. package/src/admin/editor/dnd/layer-drop.ts +141 -0
  17. package/src/admin/editor/dnd/new-element.ts +16 -0
  18. package/src/admin/editor/editor-css.ts +9 -1
  19. package/src/admin/editor/element-chrome.ts +6 -0
  20. package/src/admin/editor/extensions/ExtensionPanels.tsx +33 -1
  21. package/src/admin/editor/extensions-context.tsx +16 -1
  22. package/src/admin/editor/feature-css.ts +27 -0
  23. package/src/admin/editor/inert-links.ts +8 -0
  24. package/src/admin/editor/panels/ColorControl.tsx +2 -2
  25. package/src/admin/editor/panels/ConditionsEditor.tsx +18 -0
  26. package/src/admin/editor/panels/ElementPanel.tsx +2 -2
  27. package/src/admin/editor/panels/FontFaces.tsx +342 -0
  28. package/src/admin/editor/panels/LayersPanel.tsx +56 -6
  29. package/src/admin/editor/panels/LeftPanel.tsx +153 -29
  30. package/src/admin/editor/panels/SettingsPanel.tsx +1 -0
  31. package/src/admin/editor/panels/SiteStylesDrawer.tsx +5 -0
  32. package/src/admin/editor/panels/TemplatesPanel.tsx +205 -0
  33. package/src/admin/editor/panels/TokenScales.tsx +2 -0
  34. package/src/admin/editor/panels/TriggersEditor.tsx +203 -9
  35. package/src/admin/editor/panels/VariableSection.tsx +111 -7
  36. package/src/admin/editor/panels/color-space.ts +71 -6
  37. package/src/admin/editor/panels/settings/AudienceEditor.tsx +3 -0
  38. package/src/admin/editor/panels/settings/FieldControl.tsx +33 -0
  39. package/src/admin/editor/panels/settings/FormBindControl.tsx +27 -31
  40. package/src/admin/editor/panels/settings/ImageFieldControl.tsx +106 -0
  41. package/src/admin/editor/panels/settings/LoopItemBindControl.tsx +9 -3
  42. package/src/admin/editor/panels/settings/StickyControl.tsx +97 -0
  43. package/src/admin/editor/panels/settings/StyleRow.tsx +14 -0
  44. package/src/admin/editor/panels/settings/loop-context.ts +34 -3
  45. package/src/admin/editor/panels/settings/style-sections.ts +19 -2
  46. package/src/admin/editor/panels/url-rules.ts +43 -0
  47. package/src/admin/editor/panels/use-layers-drop.ts +232 -0
  48. package/src/admin/editor/store.ts +4 -2
  49. package/src/admin/editor/useExtensions.ts +3 -1
  50. package/src/admin/editor/useNodeActions.ts +74 -1
  51. package/src/admin/editor/useSave.ts +116 -85
  52. package/src/admin/export-api.ts +27 -2
  53. package/src/admin/extension-outputs.ts +236 -0
  54. package/src/admin/font-files-api.ts +38 -0
  55. package/src/admin/import-forms.ts +133 -0
  56. package/src/admin/pages/ImportButton.tsx +15 -4
  57. package/src/admin/pages/ImportElementorDialog.tsx +53 -187
  58. package/src/admin/pages/ImportForms.tsx +71 -0
  59. package/src/admin/pages/ImportHtmlDialog.tsx +209 -0
  60. package/src/admin/pages/ImportImageCopies.tsx +167 -0
  61. package/src/admin/pages/ImportReport.tsx +63 -0
  62. package/src/admin/pages/NewPageDialog.tsx +54 -32
  63. package/src/admin/pages/PageList.tsx +9 -0
  64. package/src/admin/pages/ThemePartList.tsx +9 -0
  65. package/src/admin/setup/plan.ts +2 -0
  66. package/src/admin/ui.ts +7 -0
  67. package/src/admin/widgets/ConvertToRichText.tsx +3 -2
  68. package/src/astro/EmVBPage.astro +5 -2
  69. package/src/astro/extension-styles.ts +35 -0
  70. package/src/astro/forms/EmVBFormsClient.astro +3 -0
  71. package/src/astro/forms/enhance.ts +107 -0
  72. package/src/astro/popups/runtime.ts +47 -2
  73. package/src/astro/render.ts +25 -4
  74. package/src/astro/resolve-theme.ts +11 -4
  75. package/src/astro/resolve.ts +2 -0
  76. package/src/astro/theme-posts.ts +17 -23
  77. package/src/astro/visitor.ts +14 -5
  78. package/src/astro/widgets/EmVBStickyRuntime.astro +9 -0
  79. package/src/astro/widgets/EmVBThemeRuntime.astro +9 -0
  80. package/src/astro/widgets/EmVBWidgetRuntimes.astro +12 -0
  81. package/src/astro/widgets/alerts.ts +16 -2
  82. package/src/astro/widgets/carousel.ts +65 -24
  83. package/src/astro/widgets/countdown.ts +18 -7
  84. package/src/astro/widgets/extension-runtimes.ts +27 -0
  85. package/src/astro/widgets/headline.ts +14 -5
  86. package/src/astro/widgets/sticky.ts +87 -0
  87. package/src/astro/widgets/theme.ts +52 -0
  88. package/src/constants.ts +1 -1
  89. package/src/core/a11y/audit.ts +150 -2
  90. package/src/core/arrange.ts +19 -1
  91. package/src/core/css/font-face.ts +72 -0
  92. package/src/core/css/generate.ts +41 -2
  93. package/src/core/css/trace.ts +8 -0
  94. package/src/core/data/bindings.ts +7 -4
  95. package/src/core/elements/dynamic-elements.ts +1 -1
  96. package/src/core/elements/element-nouns.ts +48 -0
  97. package/src/core/elements/form-elements.ts +119 -9
  98. package/src/core/elements/icon-svg.ts +14 -0
  99. package/src/core/elements/index.ts +24 -4
  100. package/src/core/elements/input-limits.ts +44 -0
  101. package/src/core/elements/menu.ts +109 -47
  102. package/src/core/elements/site.ts +108 -0
  103. package/src/core/elements/widgets.ts +13 -3
  104. package/src/core/export/runtimes.generated.ts +7 -5
  105. package/src/core/export/site.ts +53 -4
  106. package/src/core/extensions/classes.ts +114 -0
  107. package/src/core/extensions/index.ts +19 -2
  108. package/src/core/extensions/normalize.ts +13 -0
  109. package/src/core/extensions/placeholder.ts +7 -2
  110. package/src/core/extensions/registry.ts +134 -10
  111. package/src/core/extensions/render-ext.ts +115 -36
  112. package/src/core/extensions/safe-nodes.ts +67 -11
  113. package/src/core/extensions/types.ts +48 -1
  114. package/src/core/fonts/files.ts +74 -0
  115. package/src/core/fonts/google.ts +112 -0
  116. package/src/core/fonts/test-font.ts +9 -0
  117. package/src/core/import/elementor/context.ts +42 -4
  118. package/src/core/import/elementor/index.ts +55 -23
  119. package/src/core/import/elementor/widgets.ts +242 -20
  120. package/src/core/import/html/blocks.ts +602 -0
  121. package/src/core/import/html/index.ts +155 -0
  122. package/src/core/import/html/map.ts +958 -0
  123. package/src/core/import/html/parse.ts +430 -0
  124. package/src/core/index.ts +54 -0
  125. package/src/core/migrate/v18.ts +3 -0
  126. package/src/core/public-attributes.ts +17 -0
  127. package/src/core/render/extras.ts +31 -7
  128. package/src/core/render/form.ts +103 -10
  129. package/src/core/render/index.ts +165 -16
  130. package/src/core/render/section.ts +31 -1
  131. package/src/core/render/spacer-basis.ts +102 -0
  132. package/src/core/render/theme-boot.ts +16 -0
  133. package/src/core/render/vnode.ts +1 -1
  134. package/src/core/sanitize/css.ts +68 -3
  135. package/src/core/sanitize/motion.ts +12 -0
  136. package/src/core/schema/design.ts +40 -0
  137. package/src/core/schema/layout.ts +118 -6
  138. package/src/core/schema/style.ts +28 -0
  139. package/src/core/templates/index.ts +218 -0
  140. package/src/core/templates/pages.ts +29 -0
  141. package/src/core/templates/sections.ts +794 -0
  142. package/src/core/templates/types.ts +9 -0
  143. package/src/core/theme/dynamic.ts +9 -6
  144. package/src/core/theme/media-value.ts +88 -0
  145. package/src/core/theme/popup-markup.ts +21 -2
  146. package/src/core/theme/popup-rules.ts +36 -0
  147. package/src/core/theme/triggers.ts +64 -5
  148. package/src/core/translatable.ts +5 -2
  149. package/src/core/visitor-cookie.ts +91 -0
  150. package/src/extension-build.ts +331 -0
  151. package/src/index.ts +53 -4
  152. package/src/mcp/agent-defaults.ts +40 -0
  153. package/src/mcp/brief.ts +191 -0
  154. package/src/mcp/extensions.ts +138 -0
  155. package/src/mcp/prompts.ts +33 -0
  156. package/src/mcp/read.ts +84 -20
  157. package/src/mcp/tools.ts +42 -3
  158. package/src/mcp/write.ts +124 -9
  159. package/src/server/extension-routes.ts +110 -0
  160. package/src/server/font-routes.ts +301 -0
  161. package/src/server/plugin.ts +15 -0
@@ -0,0 +1,602 @@
1
+ /**
2
+ * W-1074: WordPress (Gutenberg) blocks → EmVB elements. Core blocks map onto their EmVB
3
+ * counterparts using the block's JSON attributes where they say more than the markup (columns'
4
+ * widths, a cover's image and overlay, a gallery's columns); blocks WordPress builds when the
5
+ * page is shown (latest posts, navigation, shortcodes…) are left out and reported, and blocks
6
+ * from plugins keep their text and images. A few block style attributes are kept (text and
7
+ * background colour, font size, padding, corner radius) because they are structured values
8
+ * EmVB can validate; theme presets aren't on this site and are reported instead.
9
+ */
10
+ import { SOCIAL_NETWORKS } from "../../schema/layout.ts";
11
+ import { sanitizeHref } from "../../sanitize/href.ts";
12
+ import { sanitizeMediaUrl } from "../../sanitize/media-url.ts";
13
+ import { cleanText, stripControl } from "../elementor/html.ts";
14
+ import { color, isRecord, num, str } from "../elementor/values.ts";
15
+ import type { N } from "../elementor/widgets.ts";
16
+ import { cssLength, GAP, MAX_BOX_DEPTH, type Mapper, withStyle } from "./map.ts";
17
+ import type { HtmlElement, HtmlNode, WpBlock } from "./parse.ts";
18
+
19
+ type Style = Record<string, unknown>;
20
+ type Attrs = Readonly<Record<string, unknown>>;
21
+
22
+ /** Blocks WordPress fills in when the page is shown: there is nothing to import. */
23
+ const DYNAMIC = new Set([
24
+ "latest-posts",
25
+ "latest-comments",
26
+ "archives",
27
+ "categories",
28
+ "calendar",
29
+ "tag-cloud",
30
+ "rss",
31
+ "query",
32
+ "query-loop",
33
+ "query-title",
34
+ "query-pagination",
35
+ "post-template",
36
+ "post-title",
37
+ "post-content",
38
+ "post-excerpt",
39
+ "post-featured-image",
40
+ "post-date",
41
+ "post-author",
42
+ "post-author-name",
43
+ "post-terms",
44
+ "post-navigation-link",
45
+ "comments",
46
+ "comments-query-loop",
47
+ "comment-template",
48
+ "post-comments-form",
49
+ "navigation",
50
+ "navigation-link",
51
+ "navigation-submenu",
52
+ "page-list",
53
+ "site-logo",
54
+ "site-title",
55
+ "site-tagline",
56
+ "template-part",
57
+ "loginout",
58
+ "legacy-widget",
59
+ "widget-group",
60
+ "avatar",
61
+ "term-description",
62
+ "footnotes",
63
+ "pattern",
64
+ ]);
65
+
66
+ /** Page breaks and "Read more" markers: nothing to show on an EmVB page. */
67
+ const MARKERS = new Set(["more", "nextpage", "read-more"]);
68
+
69
+ /** WordPress social link services → EmVB's Social icons networks. */
70
+ const SERVICES: Record<string, string> = {
71
+ twitter: "x",
72
+ mail: "email",
73
+ feed: "rss",
74
+ chain: "website",
75
+ };
76
+
77
+ const ACRONYMS = new Set(["rss", "html", "wp", "url"]);
78
+
79
+ const words = (text: string) =>
80
+ text
81
+ .split(/[-_]+/)
82
+ .filter(Boolean)
83
+ .map((w) => (ACRONYMS.has(w) ? w.toUpperCase() : w.charAt(0).toUpperCase() + w.slice(1)))
84
+ .join(" ");
85
+
86
+ /** `core/media-text` → "Media Text"; plugin blocks keep their namespace ("Jetpack: Slideshow"). */
87
+ function blockLabel(name: string): string {
88
+ const [space, rest] = name.includes("/") ? name.split("/", 2) : ["core", name];
89
+ const label =
90
+ space === "core" ? words(rest ?? "") : `${words(space ?? "")}: ${words(rest ?? "")}`;
91
+ return label.slice(0, 60) || "Block";
92
+ }
93
+
94
+ const elements = (list: readonly HtmlNode[]): HtmlElement[] =>
95
+ list.filter((c): c is HtmlElement => c.kind === "element");
96
+
97
+ /** Every element in the content (blocks are looked through), depth-first. */
98
+ function* descendants(list: readonly HtmlNode[]): Generator<HtmlElement> {
99
+ for (const item of list) {
100
+ if (item.kind === "text") continue;
101
+ if (item.kind === "element") yield item;
102
+ yield* descendants(item.children);
103
+ }
104
+ }
105
+
106
+ /** The blocks inside the content, through the HTML that holds them (`<ul>` around social links). */
107
+ function innerBlocks(list: readonly HtmlNode[], level = 0): WpBlock[] {
108
+ if (level > 4) return [];
109
+ return list.flatMap((item) =>
110
+ item.kind === "block"
111
+ ? [item]
112
+ : item.kind === "element"
113
+ ? innerBlocks(item.children, level + 1)
114
+ : [],
115
+ );
116
+ }
117
+
118
+ const find = (list: readonly HtmlNode[], test: (el: HtmlElement) => boolean) => {
119
+ for (const el of descendants(list)) if (test(el)) return el;
120
+ return undefined;
121
+ };
122
+
123
+ const hasClass = (el: HtmlElement, name: string) =>
124
+ (el.attrs["class"] ?? "").split(/\s+/).includes(name);
125
+
126
+ /** A block attribute's text (a label, a placeholder): plain, one line, at most `max` long. */
127
+ const attrText = (value: unknown, max: number) =>
128
+ cleanText(stripControl(str(value) ?? "")).slice(0, max);
129
+
130
+ const uses = (value: unknown) => typeof value === "string" && value.startsWith("var:");
131
+
132
+ /** The block's own colours, size, padding and radius (theme presets are only noted). */
133
+ function blockStyle(m: Mapper, attrs: Attrs, label: string): Style {
134
+ const style: Style = {};
135
+ const s = isRecord(attrs["style"]) ? attrs["style"] : {};
136
+ const colors = isRecord(s["color"]) ? s["color"] : {};
137
+ const text = color(colors["text"]);
138
+ const background = color(colors["background"]);
139
+ if (text) style["color"] = text;
140
+ if (background) style["backgroundColor"] = background;
141
+ const typography = isRecord(s["typography"]) ? s["typography"] : {};
142
+ const size = cssLength(typography["fontSize"], ["px", "rem", "em"]);
143
+ if (size && size.value > 0) style["fontSize"] = size;
144
+ const spacing = isRecord(s["spacing"]) ? s["spacing"] : {};
145
+ const padding = isRecord(spacing["padding"]) ? spacing["padding"] : {};
146
+ for (const side of ["top", "right", "bottom", "left"] as const) {
147
+ const value = cssLength(padding[side], ["px", "rem", "em", "%"]);
148
+ if (value) style[`padding${side.charAt(0).toUpperCase()}${side.slice(1)}`] = value;
149
+ }
150
+ const border = isRecord(s["border"]) ? s["border"] : {};
151
+ const radius = cssLength(border["radius"], ["px", "rem", "em", "%"]);
152
+ if (radius) style["borderRadius"] = radius;
153
+ const presets = [
154
+ attrs["textColor"],
155
+ attrs["backgroundColor"],
156
+ attrs["fontSize"],
157
+ attrs["gradient"],
158
+ ];
159
+ if (
160
+ presets.some((p) => typeof p === "string" && p) ||
161
+ uses(colors["text"]) ||
162
+ uses(colors["background"]) ||
163
+ uses(typography["fontSize"])
164
+ )
165
+ m.ctx.note(
166
+ "approximated",
167
+ label,
168
+ "Your WordPress theme's colour and size presets aren't on this site; those blocks take your site styles.",
169
+ );
170
+ return style;
171
+ }
172
+
173
+ /** A block's text alignment (`textAlign`, or `align` on text blocks). */
174
+ function textAlignOf(attrs: Attrs): Style {
175
+ const value = str(attrs["textAlign"]) ?? str(attrs["align"]);
176
+ return value === "left" || value === "center" || value === "right" || value === "justify"
177
+ ? { textAlign: value }
178
+ : {};
179
+ }
180
+
181
+ /** The cover's overlay, image or video behind its content. */
182
+ function coverStyle(attrs: Attrs, bg: HtmlElement | undefined): Style {
183
+ const style: Style = {
184
+ justifyContent: "center",
185
+ paddingTop: { value: 48, unit: "px" },
186
+ paddingBottom: { value: 48, unit: "px" },
187
+ paddingLeft: { value: 24, unit: "px" },
188
+ paddingRight: { value: 24, unit: "px" },
189
+ };
190
+ const url = sanitizeMediaUrl(str(attrs["url"]) ?? bg?.attrs["src"] ?? "");
191
+ const video = attrs["backgroundType"] === "video";
192
+ if (url && !/[()\\\s"'`]/.test(url)) {
193
+ style[video ? "backgroundVideo" : "backgroundImage"] = url;
194
+ if (!video) {
195
+ style["backgroundSize"] = "cover";
196
+ style["backgroundPosition"] = "center";
197
+ }
198
+ }
199
+ const minHeight = num(attrs["minHeight"]);
200
+ const unit = str(attrs["minHeightUnit"]) ?? "px";
201
+ const height = minHeight !== undefined ? cssLength(`${minHeight}${unit}`) : undefined;
202
+ style["minHeight"] = height ?? { value: 430, unit: "px" };
203
+ const overlay =
204
+ color(attrs["customOverlayColor"]) ?? (attrs["overlayColor"] ? "#000000" : undefined);
205
+ const dim = num(attrs["dimRatio"]) ?? (url ? 50 : 100);
206
+ if (dim > 0)
207
+ style["overlay"] = {
208
+ color: overlay ?? "#000000",
209
+ opacity: Math.min(1, Math.max(0, Math.round(dim) / 100)),
210
+ };
211
+ if (attrs["isDark"] !== false) style["color"] = "#ffffff";
212
+ return style;
213
+ }
214
+
215
+ const WRAPPERS = new Set(["div", "section", "header", "footer", "main", "article", "aside", "nav"]);
216
+
217
+ /**
218
+ * The content of a layout block: its wrapper element (`<div class="wp-block-group">`, older
219
+ * themes' `__inner-container`) is looked through, so it doesn't become an extra box.
220
+ */
221
+ function contentOf(list: readonly HtmlNode[]): readonly HtmlNode[] {
222
+ let current = list;
223
+ for (let level = 0; level < 3; level++) {
224
+ const meaningful = current.filter((c) => c.kind !== "text" || c.text.trim());
225
+ const only = meaningful.length === 1 ? meaningful[0] : undefined;
226
+ if (only?.kind !== "element" || !WRAPPERS.has(only.tag)) break;
227
+ current = only.children;
228
+ }
229
+ return current;
230
+ }
231
+
232
+ export function mapBlock(m: Mapper, block: WpBlock, depth: number): N[] {
233
+ const ctx = m.ctx;
234
+ const name = block.name;
235
+ const core = name.startsWith("core/") ? name.slice(5) : undefined;
236
+ const label = blockLabel(name);
237
+ const attrs = block.attrs;
238
+ ctx.elements++;
239
+ const done = (nodes: N[]) => {
240
+ if (nodes.length > 0) ctx.mapped++;
241
+ return nodes;
242
+ };
243
+ const inner = (d = depth + 1) => m.nodes(block.children, Math.min(d, MAX_BOX_DEPTH));
244
+ /** A layout block's content without its own wrapper `div` (the EmVB box replaces it). */
245
+ const unwrapped = () => m.nodes(contentOf(block.children), Math.min(depth + 1, MAX_BOX_DEPTH));
246
+ const styled = (nodes: N[], extra: Style = {}) =>
247
+ withStyle(nodes, { ...blockStyle(m, attrs, label), ...extra });
248
+
249
+ if (core && MARKERS.has(core)) {
250
+ ctx.elements--;
251
+ return [];
252
+ }
253
+ if (core && DYNAMIC.has(core)) {
254
+ ctx.note(
255
+ "skipped",
256
+ label,
257
+ "WordPress builds this when the page is shown, so there is nothing to import; rebuild it with EmVB's elements (a Loop for post lists, a Menu for navigation).",
258
+ );
259
+ return [];
260
+ }
261
+ switch (core) {
262
+ case "paragraph":
263
+ case "heading":
264
+ case "list":
265
+ case "verse":
266
+ case "preformatted":
267
+ case "code":
268
+ case "table":
269
+ case "details":
270
+ case "file":
271
+ case "freeform":
272
+ case "list-item":
273
+ return done(styled(inner(depth), textAlignOf(attrs)));
274
+ case "quote":
275
+ case "pullquote": {
276
+ const quote = find(block.children, (el) => el.tag === "blockquote");
277
+ const cite = quote ? elements(quote.children).find((el) => el.tag === "cite") : undefined;
278
+ const content = quote ? quote.children : block.children;
279
+ return done(styled(m.quote(content, depth, cite?.children)));
280
+ }
281
+ case "image":
282
+ return done(inner(depth));
283
+ case "gallery":
284
+ return done(gallery(m, block, attrs));
285
+ case "buttons": {
286
+ const kids = unwrapped();
287
+ if (kids.length === 0) return [];
288
+ const vertical = isRecord(attrs["layout"]) && attrs["layout"]["orientation"] === "vertical";
289
+ return done([
290
+ m.box(kids, {
291
+ flexDirection: vertical ? "column" : "row",
292
+ flexWrap: "wrap",
293
+ gap: { value: 8, unit: "px" },
294
+ ...(vertical ? { alignItems: "flex-start" } : {}),
295
+ }),
296
+ ]);
297
+ }
298
+ case "button": {
299
+ const link = find(block.children, (el) => el.tag === "a");
300
+ const href = link ? m.href(link.attrs["href"]) : undefined;
301
+ const content =
302
+ link?.children ?? find(block.children, (el) => el.tag === "button")?.children ?? [];
303
+ const text = m.plain(content).slice(0, 200);
304
+ if (!text) return [];
305
+ return done(
306
+ styled([
307
+ m.node("button", {
308
+ text,
309
+ ...(href ? { href } : {}),
310
+ ...(href && link?.attrs["target"] === "_blank" ? { newTab: true } : {}),
311
+ }),
312
+ ]),
313
+ );
314
+ }
315
+ case "columns": {
316
+ const kids = unwrapped();
317
+ if (kids.length === 0) return [];
318
+ const row = m.box(kids, {
319
+ flexDirection: "row",
320
+ gap: { value: 32, unit: "px" },
321
+ ...blockStyle(m, attrs, label),
322
+ });
323
+ if (attrs["isStackedOnMobile"] !== false)
324
+ row["devices"] = { mobile: { flexDirection: "column" } };
325
+ return done([row]);
326
+ }
327
+ case "column": {
328
+ const kids = unwrapped();
329
+ if (kids.length === 0) return [];
330
+ const width = cssLength(attrs["width"], ["px", "%", "rem", "em"]);
331
+ const size: Style = width
332
+ ? { flexBasis: width, flexGrow: 0, flexShrink: 1 }
333
+ : { flexBasis: { value: 0, unit: "px" }, flexGrow: 1 };
334
+ return done([
335
+ {
336
+ ...m.box(kids, {
337
+ minWidth: { value: 0, unit: "px" },
338
+ ...size,
339
+ ...blockStyle(m, attrs, label),
340
+ }),
341
+ devices: { mobile: { flexBasis: "auto" } },
342
+ },
343
+ ]);
344
+ }
345
+ case "group":
346
+ case "row":
347
+ case "stack": {
348
+ const kids = unwrapped();
349
+ if (kids.length === 0) return [];
350
+ const layout = isRecord(attrs["layout"]) ? attrs["layout"] : {};
351
+ const tagName = str(attrs["tagName"]);
352
+ const tag =
353
+ tagName &&
354
+ ["section", "header", "footer", "main", "article", "aside", "nav"].includes(tagName)
355
+ ? { tag: tagName }
356
+ : {};
357
+ const style = blockStyle(m, attrs, label);
358
+ if (layout["type"] === "grid") {
359
+ const count = num(layout["columnCount"]);
360
+ return done([
361
+ m.node(
362
+ "grid",
363
+ { columns: count ? Math.min(12, Math.max(1, Math.round(count))) : 3 },
364
+ { style: { gap: GAP, ...style }, children: kids },
365
+ ),
366
+ ]);
367
+ }
368
+ const row =
369
+ (layout["type"] === "flex" && layout["orientation"] !== "vertical") || core === "row";
370
+ return done([
371
+ m.box(
372
+ kids,
373
+ {
374
+ ...(row
375
+ ? {
376
+ flexDirection: "row",
377
+ flexWrap: layout["flexWrap"] === "nowrap" ? "nowrap" : "wrap",
378
+ alignItems: "center",
379
+ }
380
+ : {}),
381
+ ...style,
382
+ },
383
+ tag,
384
+ ),
385
+ ]);
386
+ }
387
+ case "cover": {
388
+ const bg = find(
389
+ block.children,
390
+ (el) => el.tag === "img" && hasClass(el, "wp-block-cover__image-background"),
391
+ );
392
+ const content = find(block.children, (el) => hasClass(el, "wp-block-cover__inner-container"));
393
+ const kids = content
394
+ ? m.nodes(content.children, Math.min(depth + 1, MAX_BOX_DEPTH))
395
+ : m.nodes(
396
+ block.children.filter(
397
+ (c) =>
398
+ c.kind !== "element" || (c.tag !== "img" && c.tag !== "span" && c.tag !== "video"),
399
+ ),
400
+ Math.min(depth + 1, MAX_BOX_DEPTH),
401
+ );
402
+ const style = coverStyle(attrs, bg);
403
+ if (typeof style["backgroundImage"] === "string")
404
+ m.ctx.externalImage(style["backgroundImage"]);
405
+ if (
406
+ attrs["hasParallax"] ||
407
+ attrs["isRepeated"] ||
408
+ attrs["gradient"] ||
409
+ attrs["customGradient"]
410
+ )
411
+ ctx.note(
412
+ "approximated",
413
+ label,
414
+ "Parallax, repeated images and gradients of covers were left out.",
415
+ );
416
+ return done([m.box(kids, { ...style, ...blockStyle(m, attrs, label) })]);
417
+ }
418
+ case "media-text": {
419
+ const media = find(block.children, (el) => hasClass(el, "wp-block-media-text__media"));
420
+ const content = find(block.children, (el) => hasClass(el, "wp-block-media-text__content"));
421
+ const mediaNodes = media ? m.nodes(media.children, depth + 1) : [];
422
+ const contentNodes = content ? m.nodes(content.children, depth + 2) : [];
423
+ if (mediaNodes.length + contentNodes.length === 0) return [];
424
+ const half: Style = {
425
+ flexBasis: { value: 0, unit: "px" },
426
+ flexGrow: 1,
427
+ minWidth: { value: 0, unit: "px" },
428
+ };
429
+ const sides = [
430
+ m.box(mediaNodes, half),
431
+ m.box(contentNodes, { ...half, justifyContent: "center" }),
432
+ ];
433
+ if (attrs["mediaPosition"] === "right") sides.reverse();
434
+ return done([
435
+ {
436
+ ...m.box(sides, {
437
+ flexDirection: "row",
438
+ gap: { value: 32, unit: "px" },
439
+ ...blockStyle(m, attrs, label),
440
+ }),
441
+ ...(attrs["isStackedOnMobile"] !== false
442
+ ? { devices: { mobile: { flexDirection: "column" } } }
443
+ : {}),
444
+ },
445
+ ]);
446
+ }
447
+ case "separator":
448
+ return done([m.node("divider", {})]);
449
+ case "spacer": {
450
+ const height = cssLength(attrs["height"] ?? "100px", ["px", "rem", "em", "vh"]) ?? {
451
+ value: 100,
452
+ unit: "px",
453
+ };
454
+ return done([m.node("spacer", { height })]);
455
+ }
456
+ case "embed":
457
+ case "video": {
458
+ const url = str(attrs["url"]) ?? "";
459
+ const caption = find(block.children, (el) => el.tag === "figcaption");
460
+ const title = caption ? m.plain(caption.children) : undefined;
461
+ const video = m.videoUrl(url);
462
+ if (video) return done([m.videoNode(video, title)]);
463
+ if (core === "video") return done(inner(depth));
464
+ const href = url ? sanitizeHref(url) : undefined;
465
+ const provider = str(attrs["providerNameSlug"]) ?? "";
466
+ if (!href || !/^https?:\/\//i.test(href)) {
467
+ ctx.note("skipped", label, "Embeds without a usable address were left out.");
468
+ return [];
469
+ }
470
+ ctx.note(
471
+ "approximated",
472
+ label,
473
+ "Embeds other than YouTube and Vimeo became a link to the original (EmVB doesn't load other sites' frames).",
474
+ );
475
+ const text = (title || (provider ? `View on ${blockLabel(provider)}` : href)).slice(0, 200);
476
+ return done([m.node("link", { text, href })]);
477
+ }
478
+ case "html":
479
+ ctx.note(
480
+ "approximated",
481
+ label,
482
+ "Custom HTML became plain EmVB elements; scripts, styles and markup were left out.",
483
+ );
484
+ return done(inner(depth));
485
+ case "search": {
486
+ ctx.note(
487
+ "approximated",
488
+ label,
489
+ "The search box sends visitors to this site's search page (/search).",
490
+ );
491
+ const fieldLabel = attrText(attrs["label"], 80);
492
+ const placeholder = attrText(attrs["placeholder"], 120);
493
+ const buttonText = attrText(attrs["buttonText"], 40);
494
+ return done([
495
+ m.node("search-form", {
496
+ ...(fieldLabel ? { label: fieldLabel } : {}),
497
+ ...(placeholder ? { placeholder } : {}),
498
+ ...(buttonText ? { buttonText } : {}),
499
+ }),
500
+ ]);
501
+ }
502
+ case "social-links":
503
+ return done(socialLinks(m, block));
504
+ case "audio":
505
+ ctx.note(
506
+ "skipped",
507
+ label,
508
+ "Audio players were left out (EmVB has no audio element); link to the file instead.",
509
+ );
510
+ return [];
511
+ case "shortcode": {
512
+ const code = m.plain(block.children).slice(0, 60);
513
+ ctx.note(
514
+ "skipped",
515
+ label,
516
+ code
517
+ ? `The shortcode ${code} needs WordPress and was left out.`
518
+ : "Shortcodes need WordPress and were left out.",
519
+ );
520
+ return [];
521
+ }
522
+ case "block":
523
+ ctx.note(
524
+ "skipped",
525
+ "Synced pattern",
526
+ "Synced patterns (reusable blocks) are only a reference in the post; import their content separately.",
527
+ );
528
+ return [];
529
+ default:
530
+ break;
531
+ }
532
+ const kept = inner(depth);
533
+ ctx.note(
534
+ "unmapped",
535
+ label,
536
+ kept.length > 0
537
+ ? "EmVB has no element for this block; its text, images and links were kept in plain elements."
538
+ : "EmVB has no element for this block, and it had no text or images to keep.",
539
+ );
540
+ return done(kept);
541
+ }
542
+
543
+ /** `core/gallery` → a Gallery of up to 60 pictures (lightbox when it linked to the files). */
544
+ function gallery(m: Mapper, block: WpBlock, attrs: Attrs): N[] {
545
+ const pictures: N[] = [];
546
+ let linkedToFiles = attrs["linkTo"] === "media" || attrs["linkTo"] === "file";
547
+ let count = 0;
548
+ for (const el of descendants(block.children)) {
549
+ if (el.tag !== "img") continue;
550
+ count++;
551
+ if (pictures.length >= 60 || m.ctx.overBudget) continue;
552
+ const pic = m.picture(el);
553
+ if (!pic) continue;
554
+ pictures.push(
555
+ m.node("gallery-image", {
556
+ src: pic.src,
557
+ alt: pic.alt,
558
+ ...(pic.decorative ? { decorative: true } : {}),
559
+ }),
560
+ );
561
+ }
562
+ for (const item of innerBlocks(block.children))
563
+ if (item.attrs["linkDestination"] === "media") linkedToFiles = true;
564
+ if (count > 60) m.ctx.note("skipped", "Gallery", "Galleries keep their first 60 pictures.");
565
+ if (pictures.length === 0) return [];
566
+ const columns = num(attrs["columns"]);
567
+ return [
568
+ m.node(
569
+ "gallery",
570
+ {
571
+ columns: Math.min(6, Math.max(1, Math.round(columns ?? Math.min(3, pictures.length)))),
572
+ ...(linkedToFiles ? { lightbox: true } : {}),
573
+ },
574
+ { children: pictures },
575
+ ),
576
+ ];
577
+ }
578
+
579
+ /** `core/social-links` → Social icons, for the services EmVB has. */
580
+ function socialLinks(m: Mapper, block: WpBlock): N[] {
581
+ const links: N[] = [];
582
+ for (const item of innerBlocks(block.children)) {
583
+ if (item.name !== "core/social-link") continue;
584
+ const service = str(item.attrs["service"]) ?? "";
585
+ const network = SERVICES[service] ?? service;
586
+ const href = sanitizeHref(str(item.attrs["url"]) ?? "");
587
+ if (!href || !(SOCIAL_NETWORKS as readonly string[]).includes(network)) {
588
+ m.ctx.note(
589
+ "skipped",
590
+ "Social Links",
591
+ href
592
+ ? `Social links for services EmVB has no icon for (${attrText(service, 30)}) were left out.`
593
+ : "Social links without an address were left out.",
594
+ );
595
+ continue;
596
+ }
597
+ const label = attrText(item.attrs["label"], 80);
598
+ links.push(m.node("social-link", { network, href, ...(label ? { label } : {}) }));
599
+ }
600
+ if (links.length === 0) return [];
601
+ return [m.node("social-icons", {}, { children: links })];
602
+ }