@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.
- package/README.md +2 -2
- package/package.json +2 -2
- package/src/admin/bulk-api.ts +5 -1
- package/src/admin/content-item.ts +3 -0
- package/src/admin/copy-rich-text.ts +99 -9
- package/src/admin/editor/A11yCopilot.tsx +3 -1
- package/src/admin/editor/Editor.tsx +28 -3
- package/src/admin/editor/Timeline.tsx +1 -8
- package/src/admin/editor/canvas/DeviceBar.tsx +7 -2
- package/src/admin/editor/canvas/EmptyCanvas.tsx +11 -2
- package/src/admin/editor/canvas/use-canvas-dnd.ts +1 -20
- package/src/admin/editor/canvas/vnode-react.tsx +2 -0
- package/src/admin/editor/co-design.ts +36 -1
- package/src/admin/editor/collection-previews.ts +7 -8
- package/src/admin/editor/dnd/dragged-source.ts +22 -0
- package/src/admin/editor/dnd/layer-drop.ts +141 -0
- package/src/admin/editor/dnd/new-element.ts +16 -0
- package/src/admin/editor/editor-css.ts +9 -1
- package/src/admin/editor/element-chrome.ts +6 -0
- package/src/admin/editor/extensions/ExtensionPanels.tsx +33 -1
- package/src/admin/editor/extensions-context.tsx +16 -1
- package/src/admin/editor/feature-css.ts +27 -0
- package/src/admin/editor/inert-links.ts +8 -0
- package/src/admin/editor/panels/ColorControl.tsx +2 -2
- package/src/admin/editor/panels/ConditionsEditor.tsx +18 -0
- package/src/admin/editor/panels/ElementPanel.tsx +2 -2
- package/src/admin/editor/panels/FontFaces.tsx +342 -0
- package/src/admin/editor/panels/LayersPanel.tsx +56 -6
- package/src/admin/editor/panels/LeftPanel.tsx +153 -29
- package/src/admin/editor/panels/SettingsPanel.tsx +1 -0
- package/src/admin/editor/panels/SiteStylesDrawer.tsx +5 -0
- package/src/admin/editor/panels/TemplatesPanel.tsx +205 -0
- package/src/admin/editor/panels/TokenScales.tsx +2 -0
- package/src/admin/editor/panels/TriggersEditor.tsx +203 -9
- package/src/admin/editor/panels/VariableSection.tsx +111 -7
- package/src/admin/editor/panels/color-space.ts +71 -6
- package/src/admin/editor/panels/settings/AudienceEditor.tsx +3 -0
- package/src/admin/editor/panels/settings/FieldControl.tsx +33 -0
- package/src/admin/editor/panels/settings/FormBindControl.tsx +27 -31
- package/src/admin/editor/panels/settings/ImageFieldControl.tsx +106 -0
- package/src/admin/editor/panels/settings/LoopItemBindControl.tsx +9 -3
- package/src/admin/editor/panels/settings/StickyControl.tsx +97 -0
- package/src/admin/editor/panels/settings/StyleRow.tsx +14 -0
- package/src/admin/editor/panels/settings/loop-context.ts +34 -3
- package/src/admin/editor/panels/settings/style-sections.ts +19 -2
- package/src/admin/editor/panels/url-rules.ts +43 -0
- package/src/admin/editor/panels/use-layers-drop.ts +232 -0
- package/src/admin/editor/store.ts +4 -2
- package/src/admin/editor/useExtensions.ts +3 -1
- package/src/admin/editor/useNodeActions.ts +74 -1
- package/src/admin/editor/useSave.ts +116 -85
- package/src/admin/export-api.ts +27 -2
- package/src/admin/extension-outputs.ts +236 -0
- package/src/admin/font-files-api.ts +38 -0
- package/src/admin/import-forms.ts +133 -0
- package/src/admin/pages/ImportButton.tsx +15 -4
- package/src/admin/pages/ImportElementorDialog.tsx +53 -187
- package/src/admin/pages/ImportForms.tsx +71 -0
- package/src/admin/pages/ImportHtmlDialog.tsx +209 -0
- package/src/admin/pages/ImportImageCopies.tsx +167 -0
- package/src/admin/pages/ImportReport.tsx +63 -0
- package/src/admin/pages/NewPageDialog.tsx +54 -32
- package/src/admin/pages/PageList.tsx +9 -0
- package/src/admin/pages/ThemePartList.tsx +9 -0
- package/src/admin/setup/plan.ts +2 -0
- package/src/admin/ui.ts +7 -0
- package/src/admin/widgets/ConvertToRichText.tsx +3 -2
- package/src/astro/EmVBPage.astro +5 -2
- package/src/astro/extension-styles.ts +35 -0
- package/src/astro/forms/EmVBFormsClient.astro +3 -0
- package/src/astro/forms/enhance.ts +107 -0
- package/src/astro/popups/runtime.ts +47 -2
- package/src/astro/render.ts +25 -4
- package/src/astro/resolve-theme.ts +11 -4
- package/src/astro/resolve.ts +2 -0
- package/src/astro/theme-posts.ts +17 -23
- package/src/astro/visitor.ts +14 -5
- package/src/astro/widgets/EmVBStickyRuntime.astro +9 -0
- package/src/astro/widgets/EmVBThemeRuntime.astro +9 -0
- package/src/astro/widgets/EmVBWidgetRuntimes.astro +12 -0
- package/src/astro/widgets/alerts.ts +16 -2
- package/src/astro/widgets/carousel.ts +65 -24
- package/src/astro/widgets/countdown.ts +18 -7
- package/src/astro/widgets/extension-runtimes.ts +27 -0
- package/src/astro/widgets/headline.ts +14 -5
- package/src/astro/widgets/sticky.ts +87 -0
- package/src/astro/widgets/theme.ts +52 -0
- package/src/constants.ts +1 -1
- package/src/core/a11y/audit.ts +150 -2
- package/src/core/arrange.ts +19 -1
- package/src/core/css/font-face.ts +72 -0
- package/src/core/css/generate.ts +41 -2
- package/src/core/css/trace.ts +8 -0
- package/src/core/data/bindings.ts +7 -4
- package/src/core/elements/dynamic-elements.ts +1 -1
- package/src/core/elements/element-nouns.ts +48 -0
- package/src/core/elements/form-elements.ts +119 -9
- package/src/core/elements/icon-svg.ts +14 -0
- package/src/core/elements/index.ts +24 -4
- package/src/core/elements/input-limits.ts +44 -0
- package/src/core/elements/menu.ts +109 -47
- package/src/core/elements/site.ts +108 -0
- package/src/core/elements/widgets.ts +13 -3
- package/src/core/export/runtimes.generated.ts +7 -5
- package/src/core/export/site.ts +53 -4
- package/src/core/extensions/classes.ts +114 -0
- package/src/core/extensions/index.ts +19 -2
- package/src/core/extensions/normalize.ts +13 -0
- package/src/core/extensions/placeholder.ts +7 -2
- package/src/core/extensions/registry.ts +134 -10
- package/src/core/extensions/render-ext.ts +115 -36
- package/src/core/extensions/safe-nodes.ts +67 -11
- package/src/core/extensions/types.ts +48 -1
- package/src/core/fonts/files.ts +74 -0
- package/src/core/fonts/google.ts +112 -0
- package/src/core/fonts/test-font.ts +9 -0
- package/src/core/import/elementor/context.ts +42 -4
- package/src/core/import/elementor/index.ts +55 -23
- package/src/core/import/elementor/widgets.ts +242 -20
- package/src/core/import/html/blocks.ts +602 -0
- package/src/core/import/html/index.ts +155 -0
- package/src/core/import/html/map.ts +958 -0
- package/src/core/import/html/parse.ts +430 -0
- package/src/core/index.ts +54 -0
- package/src/core/migrate/v18.ts +3 -0
- package/src/core/public-attributes.ts +17 -0
- package/src/core/render/extras.ts +31 -7
- package/src/core/render/form.ts +103 -10
- package/src/core/render/index.ts +165 -16
- package/src/core/render/section.ts +31 -1
- package/src/core/render/spacer-basis.ts +102 -0
- package/src/core/render/theme-boot.ts +16 -0
- package/src/core/render/vnode.ts +1 -1
- package/src/core/sanitize/css.ts +68 -3
- package/src/core/sanitize/motion.ts +12 -0
- package/src/core/schema/design.ts +40 -0
- package/src/core/schema/layout.ts +118 -6
- package/src/core/schema/style.ts +28 -0
- package/src/core/templates/index.ts +218 -0
- package/src/core/templates/pages.ts +29 -0
- package/src/core/templates/sections.ts +794 -0
- package/src/core/templates/types.ts +9 -0
- package/src/core/theme/dynamic.ts +9 -6
- package/src/core/theme/media-value.ts +88 -0
- package/src/core/theme/popup-markup.ts +21 -2
- package/src/core/theme/popup-rules.ts +36 -0
- package/src/core/theme/triggers.ts +64 -5
- package/src/core/translatable.ts +5 -2
- package/src/core/visitor-cookie.ts +91 -0
- package/src/extension-build.ts +331 -0
- package/src/index.ts +53 -4
- package/src/mcp/agent-defaults.ts +40 -0
- package/src/mcp/brief.ts +191 -0
- package/src/mcp/extensions.ts +138 -0
- package/src/mcp/prompts.ts +33 -0
- package/src/mcp/read.ts +84 -20
- package/src/mcp/tools.ts +42 -3
- package/src/mcp/write.ts +124 -9
- package/src/server/extension-routes.ts +110 -0
- package/src/server/font-routes.ts +301 -0
- package/src/server/plugin.ts +15 -0
|
@@ -0,0 +1,958 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* W-1074: the parsed HTML tree → EmVB elements. Headings, paragraphs, lists, images, quotes,
|
|
3
|
+
* tables, embeds and boxes map onto EmVB's own elements; everything else keeps its text or is
|
|
4
|
+
* left out and reported. Only plain text, sanitized URLs and a few validated style values ever
|
|
5
|
+
* reach a node: no markup, inline CSS, class or event handler from the input is kept.
|
|
6
|
+
*/
|
|
7
|
+
import { MAX_TEXT_LENGTH } from "../../limits.ts";
|
|
8
|
+
import { CONTAINER_TAGS } from "../../schema/layout.ts";
|
|
9
|
+
import { resolveEmbedUrl } from "../../sanitize/embed-url.ts";
|
|
10
|
+
import { sanitizeMediaUrl } from "../../sanitize/media-url.ts";
|
|
11
|
+
import type { ImportContext } from "../elementor/context.ts";
|
|
12
|
+
import { cleanText, embedVideoUrl, splitLong, stripControl } from "../elementor/html.ts";
|
|
13
|
+
import { type Fmt, marksFromChars } from "../elementor/inline-marks.ts";
|
|
14
|
+
import { altFromUrl, color } from "../elementor/values.ts";
|
|
15
|
+
import type { N } from "../elementor/widgets.ts";
|
|
16
|
+
import type { HtmlElement, HtmlNode, WpBlock } from "./parse.ts";
|
|
17
|
+
|
|
18
|
+
type Style = Record<string, unknown>;
|
|
19
|
+
export type Length = { value: number; unit: "px" | "rem" | "em" | "%" | "vw" | "vh" };
|
|
20
|
+
|
|
21
|
+
/** Text kept per import, so the page stays under EmVB's layout size. */
|
|
22
|
+
const MAX_IMPORT_TEXT = 200_000;
|
|
23
|
+
/** Boxes nested deeper than this are flattened into their parent. */
|
|
24
|
+
export const MAX_BOX_DEPTH = 10;
|
|
25
|
+
const MAX_LIST_ITEMS = 200;
|
|
26
|
+
const MAX_TABLE_ROWS = 100;
|
|
27
|
+
const MAX_TABLE_COLUMNS = 12;
|
|
28
|
+
|
|
29
|
+
export const GAP: Length = { value: 16, unit: "px" };
|
|
30
|
+
|
|
31
|
+
/** Inline elements: their text joins the paragraph around them. */
|
|
32
|
+
const INLINE = new Set([
|
|
33
|
+
"a",
|
|
34
|
+
"abbr",
|
|
35
|
+
"b",
|
|
36
|
+
"bdi",
|
|
37
|
+
"bdo",
|
|
38
|
+
"big",
|
|
39
|
+
"br",
|
|
40
|
+
"cite",
|
|
41
|
+
"code",
|
|
42
|
+
"data",
|
|
43
|
+
"del",
|
|
44
|
+
"dfn",
|
|
45
|
+
"em",
|
|
46
|
+
"font",
|
|
47
|
+
"i",
|
|
48
|
+
"ins",
|
|
49
|
+
"kbd",
|
|
50
|
+
"mark",
|
|
51
|
+
"nobr",
|
|
52
|
+
"q",
|
|
53
|
+
"s",
|
|
54
|
+
"samp",
|
|
55
|
+
"small",
|
|
56
|
+
"span",
|
|
57
|
+
"strike",
|
|
58
|
+
"strong",
|
|
59
|
+
"sub",
|
|
60
|
+
"sup",
|
|
61
|
+
"time",
|
|
62
|
+
"tt",
|
|
63
|
+
"u",
|
|
64
|
+
"var",
|
|
65
|
+
"wbr",
|
|
66
|
+
]);
|
|
67
|
+
/** Inline formatting EmVB can't show (underline, superscript, code…): kept as plain text. */
|
|
68
|
+
const FLATTENED = new Set([
|
|
69
|
+
"u",
|
|
70
|
+
"sub",
|
|
71
|
+
"sup",
|
|
72
|
+
"code",
|
|
73
|
+
"kbd",
|
|
74
|
+
"samp",
|
|
75
|
+
"s",
|
|
76
|
+
"strike",
|
|
77
|
+
"del",
|
|
78
|
+
"ins",
|
|
79
|
+
"mark",
|
|
80
|
+
"small",
|
|
81
|
+
"big",
|
|
82
|
+
"tt",
|
|
83
|
+
]);
|
|
84
|
+
/** Never content: skipped without a note. */
|
|
85
|
+
const SILENT = new Set([
|
|
86
|
+
"head",
|
|
87
|
+
"link",
|
|
88
|
+
"meta",
|
|
89
|
+
"base",
|
|
90
|
+
"col",
|
|
91
|
+
"colgroup",
|
|
92
|
+
"source",
|
|
93
|
+
"track",
|
|
94
|
+
"param",
|
|
95
|
+
"wbr",
|
|
96
|
+
"br",
|
|
97
|
+
"area",
|
|
98
|
+
"map",
|
|
99
|
+
]);
|
|
100
|
+
const FORM_TAGS = new Set([
|
|
101
|
+
"form",
|
|
102
|
+
"input",
|
|
103
|
+
"select",
|
|
104
|
+
"textarea",
|
|
105
|
+
"button",
|
|
106
|
+
"label",
|
|
107
|
+
"fieldset",
|
|
108
|
+
"legend",
|
|
109
|
+
"option",
|
|
110
|
+
"optgroup",
|
|
111
|
+
"datalist",
|
|
112
|
+
"output",
|
|
113
|
+
"progress",
|
|
114
|
+
"meter",
|
|
115
|
+
]);
|
|
116
|
+
const TRANSPARENT = new Set(["html", "body", "thead", "tbody", "tfoot", "picture"]);
|
|
117
|
+
const BUTTON_CLASS = /(?:^|\s)(?:wp-block-button__link|wp-element-button|button|btn)(?:\s|$)/i;
|
|
118
|
+
|
|
119
|
+
const isValidHtmlId = (id: string) => /^[A-Za-z][A-Za-z0-9_-]{0,63}$/.test(id);
|
|
120
|
+
|
|
121
|
+
/** A CSS length EmVB can store (`24px`, `1.5rem`, `50%`); undefined for anything else. */
|
|
122
|
+
export function cssLength(
|
|
123
|
+
raw: unknown,
|
|
124
|
+
units = ["px", "rem", "em", "%", "vw", "vh"],
|
|
125
|
+
): Length | undefined {
|
|
126
|
+
if (typeof raw === "number")
|
|
127
|
+
return Number.isFinite(raw) && raw >= 0 ? { value: raw, unit: "px" } : undefined;
|
|
128
|
+
if (typeof raw !== "string") return undefined;
|
|
129
|
+
const m = /^\s*(\d+(?:\.\d+)?)\s*(px|rem|em|%|vw|vh)?\s*$/i.exec(raw);
|
|
130
|
+
if (!m) return undefined;
|
|
131
|
+
const value = Number(m[1]);
|
|
132
|
+
const unit = (m[2] ?? "px").toLowerCase();
|
|
133
|
+
if (!Number.isFinite(value) || value > 10_000 || !units.includes(unit)) return undefined;
|
|
134
|
+
if (!m[2] && value !== 0) return undefined;
|
|
135
|
+
return { value: Math.round(value * 100) / 100, unit: unit as Length["unit"] };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** One `property: value` of an inline `style` attribute (last one wins, `!important` dropped). */
|
|
139
|
+
function declared(style: string | undefined, property: string): string | undefined {
|
|
140
|
+
if (!style) return undefined;
|
|
141
|
+
let found: string | undefined;
|
|
142
|
+
for (const part of style.split(";")) {
|
|
143
|
+
const at = part.indexOf(":");
|
|
144
|
+
if (at < 0) continue;
|
|
145
|
+
if (part.slice(0, at).trim().toLowerCase() === property)
|
|
146
|
+
found = part
|
|
147
|
+
.slice(at + 1)
|
|
148
|
+
.replace(/!important/i, "")
|
|
149
|
+
.trim();
|
|
150
|
+
}
|
|
151
|
+
return found;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const ALIGNS = new Set(["left", "center", "right", "justify"]);
|
|
155
|
+
|
|
156
|
+
/** An element's text alignment: its `style`, WordPress's `has-text-align-*` class or `align`. */
|
|
157
|
+
function alignOf(el: HtmlElement): string | undefined {
|
|
158
|
+
const fromStyle = declared(el.attrs["style"], "text-align")?.toLowerCase();
|
|
159
|
+
if (fromStyle && ALIGNS.has(fromStyle)) return fromStyle;
|
|
160
|
+
const fromClass = /(?:^|\s)has-text-align-(left|center|right|justify)(?:\s|$)/.exec(
|
|
161
|
+
el.attrs["class"] ?? "",
|
|
162
|
+
)?.[1];
|
|
163
|
+
if (fromClass) return fromClass;
|
|
164
|
+
const attr = el.attrs["align"]?.toLowerCase();
|
|
165
|
+
return attr && ALIGNS.has(attr) ? attr : undefined;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** A width or height attribute in pixels. */
|
|
169
|
+
const dimension = (value: string | undefined) => {
|
|
170
|
+
const n = Number(value);
|
|
171
|
+
return Number.isInteger(n) && n > 0 && n <= 10_000 ? n : undefined;
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
const isCite = (c: HtmlNode): c is HtmlElement =>
|
|
175
|
+
c.kind === "element" && (c.tag === "cite" || c.tag === "footer");
|
|
176
|
+
|
|
177
|
+
/** Puts style values on every node (keeping what a node already has). */
|
|
178
|
+
export function withStyle(nodes: N[], style: Style): N[] {
|
|
179
|
+
if (Object.keys(style).length === 0) return nodes;
|
|
180
|
+
for (const n of nodes) n["style"] = { ...style, ...(n["style"] as Style | undefined) };
|
|
181
|
+
return nodes;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
type Run = {
|
|
185
|
+
raw: string;
|
|
186
|
+
fmts: (Fmt | undefined)[];
|
|
187
|
+
links: { href: string; text: string }[];
|
|
188
|
+
outside: boolean;
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
export type BlockMapper = (m: Mapper, block: WpBlock, depth: number) => N[];
|
|
192
|
+
|
|
193
|
+
export class Mapper {
|
|
194
|
+
private textLeft = MAX_IMPORT_TEXT;
|
|
195
|
+
private warnedBudget = false;
|
|
196
|
+
|
|
197
|
+
constructor(
|
|
198
|
+
readonly ctx: ImportContext,
|
|
199
|
+
/** Gutenberg content: the report counts blocks, not HTML elements. */
|
|
200
|
+
readonly gutenberg: boolean,
|
|
201
|
+
private readonly blockOf: BlockMapper,
|
|
202
|
+
) {}
|
|
203
|
+
|
|
204
|
+
/** Counts one piece of content for the report (HTML mode; blocks count themselves). */
|
|
205
|
+
unit(produced: boolean): void {
|
|
206
|
+
if (this.gutenberg) return;
|
|
207
|
+
this.ctx.elements++;
|
|
208
|
+
if (produced) this.ctx.mapped++;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
node(type: string, props: Record<string, unknown>, extra: Record<string, unknown> = {}): N {
|
|
212
|
+
return { id: this.ctx.id(), type, props, ...extra };
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
box(children: N[], style: Style = {}, props: Record<string, unknown> = {}): N {
|
|
216
|
+
return this.node("container", props, {
|
|
217
|
+
style: { flexDirection: "column", gap: GAP, ...style },
|
|
218
|
+
children,
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** Spends text from the import's budget; false once it is used up (noted once). */
|
|
223
|
+
take(text: string): boolean {
|
|
224
|
+
if (this.textLeft >= text.length) {
|
|
225
|
+
this.textLeft -= text.length;
|
|
226
|
+
return true;
|
|
227
|
+
}
|
|
228
|
+
this.textLeft = 0;
|
|
229
|
+
if (!this.warnedBudget) {
|
|
230
|
+
this.warnedBudget = true;
|
|
231
|
+
this.ctx.note(
|
|
232
|
+
"skipped",
|
|
233
|
+
"Page",
|
|
234
|
+
"The page has more text than one EmVB page holds; the rest was left out. Import it in parts.",
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
return false;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** A link address, made safe (old-site pages become this site's paths); unsafe ones noted. */
|
|
241
|
+
href(raw: string | undefined): string | undefined {
|
|
242
|
+
if (!raw?.trim()) return undefined;
|
|
243
|
+
const safe = this.ctx.href(raw);
|
|
244
|
+
if (!safe)
|
|
245
|
+
this.ctx.note(
|
|
246
|
+
"skipped",
|
|
247
|
+
"Links",
|
|
248
|
+
"Links with an unsafe or unsupported address (javascript:, data: and the like) were left out; their text was kept.",
|
|
249
|
+
);
|
|
250
|
+
return safe;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** Notes what the import drops from any element: event handlers, inline styles, classes. */
|
|
254
|
+
private attributes(el: HtmlElement): void {
|
|
255
|
+
for (const name of Object.keys(el.attrs)) {
|
|
256
|
+
if (name.startsWith("on")) {
|
|
257
|
+
this.ctx.note(
|
|
258
|
+
"skipped",
|
|
259
|
+
"Scripts",
|
|
260
|
+
"Event handlers (onclick, onerror and the like) were left out.",
|
|
261
|
+
);
|
|
262
|
+
break;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
if (this.gutenberg) return;
|
|
266
|
+
const style = el.attrs["style"];
|
|
267
|
+
if (
|
|
268
|
+
style &&
|
|
269
|
+
style
|
|
270
|
+
.split(";")
|
|
271
|
+
.some((part) => part.includes(":") && !/^\s*(?:text-align|color)\s*:/i.test(part))
|
|
272
|
+
)
|
|
273
|
+
this.ctx.note(
|
|
274
|
+
"approximated",
|
|
275
|
+
"Styles",
|
|
276
|
+
"Inline styles were left out (apart from text colour and alignment); style the page in EmVB.",
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** A list of parsed nodes → EmVB nodes; loose text and inline elements become paragraphs. */
|
|
281
|
+
nodes(list: readonly HtmlNode[], depth: number): N[] {
|
|
282
|
+
const out: N[] = [];
|
|
283
|
+
let run: HtmlNode[] = [];
|
|
284
|
+
const flush = () => {
|
|
285
|
+
if (run.length === 0) return;
|
|
286
|
+
out.push(...this.paragraph(run));
|
|
287
|
+
run = [];
|
|
288
|
+
};
|
|
289
|
+
for (const item of list) {
|
|
290
|
+
if (this.ctx.overBudget) {
|
|
291
|
+
this.ctx.note(
|
|
292
|
+
"skipped",
|
|
293
|
+
"Page",
|
|
294
|
+
"The page was larger than EmVB allows; its last parts were left out. Import them separately.",
|
|
295
|
+
);
|
|
296
|
+
break;
|
|
297
|
+
}
|
|
298
|
+
if (item.kind === "text" || (item.kind === "element" && !this.blockish(item))) {
|
|
299
|
+
run.push(item);
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
flush();
|
|
303
|
+
out.push(
|
|
304
|
+
...(item.kind === "block" ? this.blockOf(this, item, depth) : this.element(item, depth)),
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
flush();
|
|
308
|
+
return out;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** Whether an element starts its own EmVB element (not part of the text around it). */
|
|
312
|
+
blockish(el: HtmlElement, level = 0): boolean {
|
|
313
|
+
if (el.tag === "a" && BUTTON_CLASS.test(el.attrs["class"] ?? "")) return true;
|
|
314
|
+
if (!INLINE.has(el.tag)) return true;
|
|
315
|
+
if (level > 16) return false;
|
|
316
|
+
return el.children.some(
|
|
317
|
+
(child) =>
|
|
318
|
+
child.kind === "block" || (child.kind === "element" && this.blockish(child, level + 1)),
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/** One HTML element → EmVB nodes. */
|
|
323
|
+
element(el: HtmlElement, depth: number): N[] {
|
|
324
|
+
this.attributes(el);
|
|
325
|
+
const tag = el.tag;
|
|
326
|
+
if (SILENT.has(tag)) return [];
|
|
327
|
+
if (TRANSPARENT.has(tag)) return this.nodes(el.children, depth);
|
|
328
|
+
if (/^h[1-6]$/.test(tag)) return this.heading(el, Number(tag[1]));
|
|
329
|
+
switch (tag) {
|
|
330
|
+
case "p":
|
|
331
|
+
case "figcaption":
|
|
332
|
+
case "summary":
|
|
333
|
+
case "caption":
|
|
334
|
+
return withStyle(this.nodes(el.children, depth), this.alignStyle(el));
|
|
335
|
+
case "ul":
|
|
336
|
+
case "ol":
|
|
337
|
+
case "menu":
|
|
338
|
+
return this.list(el);
|
|
339
|
+
case "img":
|
|
340
|
+
return this.imageNodes(el);
|
|
341
|
+
case "a":
|
|
342
|
+
return this.linkBlock(el, depth);
|
|
343
|
+
case "figure":
|
|
344
|
+
return this.figure(el, depth);
|
|
345
|
+
case "blockquote":
|
|
346
|
+
return this.quote(el.children, depth);
|
|
347
|
+
case "hr":
|
|
348
|
+
this.unit(true);
|
|
349
|
+
return [this.node("divider", {})];
|
|
350
|
+
case "table":
|
|
351
|
+
return this.table(el);
|
|
352
|
+
case "iframe":
|
|
353
|
+
case "embed":
|
|
354
|
+
return this.embed(el);
|
|
355
|
+
case "video":
|
|
356
|
+
return this.video(el);
|
|
357
|
+
case "audio":
|
|
358
|
+
this.unit(false);
|
|
359
|
+
this.ctx.note(
|
|
360
|
+
"skipped",
|
|
361
|
+
"Audio",
|
|
362
|
+
"Audio players were left out (EmVB has no audio element); link to the file instead.",
|
|
363
|
+
);
|
|
364
|
+
return [];
|
|
365
|
+
case "details":
|
|
366
|
+
return this.details(el, depth);
|
|
367
|
+
case "pre":
|
|
368
|
+
return this.pre(el);
|
|
369
|
+
case "dl":
|
|
370
|
+
return this.definitions(el, depth);
|
|
371
|
+
default:
|
|
372
|
+
break;
|
|
373
|
+
}
|
|
374
|
+
if (FORM_TAGS.has(tag)) {
|
|
375
|
+
this.unit(false);
|
|
376
|
+
this.ctx.note(
|
|
377
|
+
"skipped",
|
|
378
|
+
"Forms",
|
|
379
|
+
"Forms and their fields were left out; rebuild them with a Form element (forms plugin).",
|
|
380
|
+
);
|
|
381
|
+
return [];
|
|
382
|
+
}
|
|
383
|
+
if (tag.includes("-"))
|
|
384
|
+
this.ctx.note(
|
|
385
|
+
"unmapped",
|
|
386
|
+
`<${tag.slice(0, 40)}>`,
|
|
387
|
+
"A custom element became a plain box; only its text and images were kept.",
|
|
388
|
+
);
|
|
389
|
+
return this.container(el, depth);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
private alignStyle(el: HtmlElement): Style {
|
|
393
|
+
const align = alignOf(el);
|
|
394
|
+
return align ? { textAlign: align } : {};
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/** A box for a section, div… Single children and empty boxes don't add a level. */
|
|
398
|
+
container(el: HtmlElement, depth: number, style: Style = {}): N[] {
|
|
399
|
+
const deep = depth >= MAX_BOX_DEPTH;
|
|
400
|
+
const kids = withStyle(this.nodes(el.children, deep ? depth : depth + 1), this.alignStyle(el));
|
|
401
|
+
if (deep) {
|
|
402
|
+
if (kids.length > 0)
|
|
403
|
+
this.ctx.note(
|
|
404
|
+
"approximated",
|
|
405
|
+
"Boxes",
|
|
406
|
+
"Boxes nested deeper than EmVB allows were flattened into their parent.",
|
|
407
|
+
);
|
|
408
|
+
return kids;
|
|
409
|
+
}
|
|
410
|
+
const id = el.attrs["id"]?.trim();
|
|
411
|
+
const htmlId = id && isValidHtmlId(id) && !this.ctx.htmlIds.has(id) ? id : undefined;
|
|
412
|
+
if (kids.length === 0) return [];
|
|
413
|
+
if (kids.length === 1 && !htmlId && Object.keys(style).length === 0) return kids;
|
|
414
|
+
if (htmlId) this.ctx.htmlIds.add(htmlId);
|
|
415
|
+
const tag =
|
|
416
|
+
(CONTAINER_TAGS as readonly string[]).includes(el.tag) && el.tag !== "div"
|
|
417
|
+
? el.tag
|
|
418
|
+
: undefined;
|
|
419
|
+
const made = this.box(kids, style, tag ? { tag } : {});
|
|
420
|
+
if (htmlId) made["htmlId"] = htmlId;
|
|
421
|
+
return [made];
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/** Reads inline content into text with formatting for each character. */
|
|
425
|
+
private collect(
|
|
426
|
+
list: readonly HtmlNode[],
|
|
427
|
+
run: Run,
|
|
428
|
+
stack: Fmt[],
|
|
429
|
+
href: string | undefined,
|
|
430
|
+
linked: boolean,
|
|
431
|
+
): void {
|
|
432
|
+
for (const item of list) {
|
|
433
|
+
if (item.kind === "block") {
|
|
434
|
+
this.collect(item.children, run, stack, href, linked);
|
|
435
|
+
continue;
|
|
436
|
+
}
|
|
437
|
+
if (item.kind === "text") {
|
|
438
|
+
const text = item.text.replace(/[\r\n\t\f]+/g, " ");
|
|
439
|
+
this.addRun(run, text, stack, href);
|
|
440
|
+
if (linked) {
|
|
441
|
+
const last = run.links.at(-1);
|
|
442
|
+
if (last) last.text += text;
|
|
443
|
+
} else if (text.trim()) run.outside = true;
|
|
444
|
+
continue;
|
|
445
|
+
}
|
|
446
|
+
this.attributes(item);
|
|
447
|
+
const tag = item.tag;
|
|
448
|
+
if (tag === "br") {
|
|
449
|
+
this.addRun(run, "\n", stack, href);
|
|
450
|
+
continue;
|
|
451
|
+
}
|
|
452
|
+
if (tag === "a") {
|
|
453
|
+
const safe = this.href(item.attrs["href"]);
|
|
454
|
+
const opens = safe !== undefined && !linked;
|
|
455
|
+
if (opens) run.links.push({ href: safe, text: "" });
|
|
456
|
+
this.collect(item.children, run, stack, safe ?? href, linked || opens);
|
|
457
|
+
continue;
|
|
458
|
+
}
|
|
459
|
+
let f: Fmt | undefined;
|
|
460
|
+
if (tag === "strong" || tag === "b") f = { bold: true };
|
|
461
|
+
else if (tag === "em" || tag === "i" || tag === "cite" || tag === "dfn") f = { italic: true };
|
|
462
|
+
else if (tag === "span" || tag === "font" || tag === "mark") {
|
|
463
|
+
const raw =
|
|
464
|
+
declared(item.attrs["style"], "color") ??
|
|
465
|
+
(tag === "font" ? item.attrs["color"] : undefined);
|
|
466
|
+
const hex = raw ? color(raw) : undefined;
|
|
467
|
+
if (hex && hex.length <= 7) f = { color: hex };
|
|
468
|
+
}
|
|
469
|
+
if (!f && FLATTENED.has(tag))
|
|
470
|
+
this.ctx.note(
|
|
471
|
+
"approximated",
|
|
472
|
+
"Text",
|
|
473
|
+
"Inline formatting EmVB doesn't have (underline, superscript, code, highlights) became plain text.",
|
|
474
|
+
);
|
|
475
|
+
if (tag === "img" || tag === "iframe" || tag === "video" || tag === "audio") continue;
|
|
476
|
+
this.collect(item.children, run, f ? [...stack, f] : stack, href, linked);
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
private addRun(run: Run, text: string, stack: readonly Fmt[], href: string | undefined): void {
|
|
481
|
+
run.raw += text;
|
|
482
|
+
const out: Fmt = {};
|
|
483
|
+
for (const f of stack) {
|
|
484
|
+
if (f.bold) out.bold = true;
|
|
485
|
+
if (f.italic) out.italic = true;
|
|
486
|
+
if (f.color) out.color = f.color;
|
|
487
|
+
}
|
|
488
|
+
if (href) out.href = href;
|
|
489
|
+
const fmt = Object.keys(out).length > 0 ? out : undefined;
|
|
490
|
+
for (let i = 0; i < text.length; i++) run.fmts.push(fmt);
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/** Inline content's plain text, its marks and the one link it is (when it is one). */
|
|
494
|
+
inline(list: readonly HtmlNode[], keepBreaks: boolean) {
|
|
495
|
+
const run: Run = { raw: "", fmts: [], links: [], outside: false };
|
|
496
|
+
this.collect(list, run, [], undefined, false);
|
|
497
|
+
const text = cleanText(run.raw, keepBreaks);
|
|
498
|
+
const single = splitLong(text).length === 1;
|
|
499
|
+
const marks =
|
|
500
|
+
single && run.fmts.some(Boolean) ? marksFromChars(run.raw, run.fmts, keepBreaks, text) : [];
|
|
501
|
+
const onlyLink = run.links.length === 1 && !run.outside ? run.links[0] : undefined;
|
|
502
|
+
return { text, marks, onlyLink };
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/** Plain text of any content (alt-text style: no breaks, no marks). */
|
|
506
|
+
plain(list: readonly HtmlNode[]): string {
|
|
507
|
+
return this.inline(list, false).text;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
/** Loose text and inline elements → a Text (or a Link when it is one whole link). */
|
|
511
|
+
paragraph(list: readonly HtmlNode[]): N[] {
|
|
512
|
+
const { text, marks, onlyLink } = this.inline(list, true);
|
|
513
|
+
if (!text) return [];
|
|
514
|
+
if (!this.take(text)) return [];
|
|
515
|
+
this.unit(true);
|
|
516
|
+
if (onlyLink && text.length <= MAX_TEXT_LENGTH && !text.includes("\n"))
|
|
517
|
+
return [this.node("link", { text, href: onlyLink.href })];
|
|
518
|
+
const parts = splitLong(text);
|
|
519
|
+
return parts.map((part) =>
|
|
520
|
+
this.node("text", {
|
|
521
|
+
text: part,
|
|
522
|
+
...(parts.length === 1 && marks.length > 0 ? { marks } : {}),
|
|
523
|
+
}),
|
|
524
|
+
);
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
heading(el: HtmlElement, level: number): N[] {
|
|
528
|
+
const { text, marks, onlyLink } = this.inline(el.children, false);
|
|
529
|
+
if (!text || !this.take(text)) return [];
|
|
530
|
+
this.unit(true);
|
|
531
|
+
const own = onlyLink
|
|
532
|
+
? marks.flatMap(({ href: _href, ...rest }) =>
|
|
533
|
+
rest.bold || rest.italic || rest.color ? [rest] : [],
|
|
534
|
+
)
|
|
535
|
+
: marks;
|
|
536
|
+
const parts = splitLong(text);
|
|
537
|
+
return withStyle(
|
|
538
|
+
parts.map((part) =>
|
|
539
|
+
this.node("heading", {
|
|
540
|
+
text: part,
|
|
541
|
+
level,
|
|
542
|
+
...(onlyLink ? { href: onlyLink.href } : {}),
|
|
543
|
+
...(parts.length === 1 && own.length > 0 ? { marks: own } : {}),
|
|
544
|
+
}),
|
|
545
|
+
),
|
|
546
|
+
this.alignStyle(el),
|
|
547
|
+
);
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/** `ul`/`ol` → a List of plain items; nested lists join it one level down. */
|
|
551
|
+
list(el: HtmlElement): N[] {
|
|
552
|
+
const items: string[] = [];
|
|
553
|
+
let nested = false;
|
|
554
|
+
const visit = (list: readonly HtmlNode[], level: number) => {
|
|
555
|
+
for (const item of list) {
|
|
556
|
+
if (item.kind === "text") continue;
|
|
557
|
+
if (item.kind === "block") {
|
|
558
|
+
visit(item.children, level);
|
|
559
|
+
continue;
|
|
560
|
+
}
|
|
561
|
+
if (item.tag !== "li") {
|
|
562
|
+
if (item.tag === "ul" || item.tag === "ol") visit(item.children, level + 1);
|
|
563
|
+
continue;
|
|
564
|
+
}
|
|
565
|
+
const own = item.children.filter(
|
|
566
|
+
(child) => !(child.kind === "element" && (child.tag === "ul" || child.tag === "ol")),
|
|
567
|
+
);
|
|
568
|
+
const text = this.plain(own);
|
|
569
|
+
if (level > 0) nested = true;
|
|
570
|
+
if (text && this.take(text))
|
|
571
|
+
for (const part of splitLong(text)) if (items.length < MAX_LIST_ITEMS) items.push(part);
|
|
572
|
+
for (const child of item.children)
|
|
573
|
+
if (child.kind === "element" && (child.tag === "ul" || child.tag === "ol"))
|
|
574
|
+
visit(child.children, level + 1);
|
|
575
|
+
else if (child.kind === "block") visit(child.children, level + 1);
|
|
576
|
+
}
|
|
577
|
+
};
|
|
578
|
+
visit(el.children, 0);
|
|
579
|
+
if (nested)
|
|
580
|
+
this.ctx.note(
|
|
581
|
+
"approximated",
|
|
582
|
+
"Lists",
|
|
583
|
+
"Nested lists became one level (EmVB lists have one level).",
|
|
584
|
+
);
|
|
585
|
+
if (items.length >= MAX_LIST_ITEMS)
|
|
586
|
+
this.ctx.note("skipped", "Lists", `Lists keep their first ${MAX_LIST_ITEMS} items.`);
|
|
587
|
+
this.unit(items.length > 0);
|
|
588
|
+
if (items.length === 0) return [];
|
|
589
|
+
const ordered = el.tag === "ol";
|
|
590
|
+
const start = ordered ? Number(el.attrs["start"]) : Number.NaN;
|
|
591
|
+
return withStyle(
|
|
592
|
+
[
|
|
593
|
+
this.node("list", {
|
|
594
|
+
items,
|
|
595
|
+
...(ordered ? { ordered: true } : {}),
|
|
596
|
+
...(Number.isInteger(start) && start > 1 && start <= 9999 ? { start } : {}),
|
|
597
|
+
}),
|
|
598
|
+
],
|
|
599
|
+
this.alignStyle(el),
|
|
600
|
+
);
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
/** An `img` → its safe address, alt text and size; undefined (noted) when unusable. */
|
|
604
|
+
picture(
|
|
605
|
+
el: HtmlElement,
|
|
606
|
+
): { src: string; alt: string; decorative?: true; width?: number; height?: number } | undefined {
|
|
607
|
+
const raw =
|
|
608
|
+
el.attrs["src"] ||
|
|
609
|
+
el.attrs["data-src"] ||
|
|
610
|
+
el.attrs["data-lazy-src"] ||
|
|
611
|
+
el.attrs["data-orig-file"] ||
|
|
612
|
+
"";
|
|
613
|
+
const src = sanitizeMediaUrl(raw.trim());
|
|
614
|
+
if (!src) {
|
|
615
|
+
this.ctx.note(
|
|
616
|
+
"skipped",
|
|
617
|
+
"Images",
|
|
618
|
+
"Images with an unsupported address (data:, blob: or a script) were left out.",
|
|
619
|
+
);
|
|
620
|
+
return undefined;
|
|
621
|
+
}
|
|
622
|
+
const width = dimension(el.attrs["width"]);
|
|
623
|
+
const height = dimension(el.attrs["height"]);
|
|
624
|
+
const given = "alt" in el.attrs;
|
|
625
|
+
const alt = cleanText(stripControl(el.attrs["alt"] ?? "")).slice(0, 500);
|
|
626
|
+
if (!given)
|
|
627
|
+
this.ctx.note(
|
|
628
|
+
"approximated",
|
|
629
|
+
"Images",
|
|
630
|
+
"Images without alt text got one from their file name; check it.",
|
|
631
|
+
);
|
|
632
|
+
this.ctx.externalImage(src);
|
|
633
|
+
if (src.startsWith("/") && !this.gutenberg)
|
|
634
|
+
this.ctx.note(
|
|
635
|
+
"approximated",
|
|
636
|
+
"Images",
|
|
637
|
+
"Images with addresses on the old site (/images/… without the site's name) now load from this site; upload them to the media library and pick them in the editor.",
|
|
638
|
+
);
|
|
639
|
+
return {
|
|
640
|
+
src,
|
|
641
|
+
alt: given ? alt : altFromUrl(src),
|
|
642
|
+
...(given && !alt ? { decorative: true as const } : {}),
|
|
643
|
+
...(width ? { width } : {}),
|
|
644
|
+
...(height ? { height } : {}),
|
|
645
|
+
};
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
image(
|
|
649
|
+
el: HtmlElement,
|
|
650
|
+
extra: { href?: string; newTab?: boolean; caption?: string } = {},
|
|
651
|
+
): N | undefined {
|
|
652
|
+
const pic = this.picture(el);
|
|
653
|
+
if (!pic) return undefined;
|
|
654
|
+
return this.node(
|
|
655
|
+
"image",
|
|
656
|
+
{
|
|
657
|
+
...pic,
|
|
658
|
+
...(extra.href ? { href: extra.href } : {}),
|
|
659
|
+
...(extra.href && extra.newTab ? { newTab: true } : {}),
|
|
660
|
+
...(extra.caption ? { caption: extra.caption.slice(0, 500) } : {}),
|
|
661
|
+
},
|
|
662
|
+
{ style: { maxWidth: { value: 100, unit: "%" } } },
|
|
663
|
+
);
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
private imageNodes(
|
|
667
|
+
el: HtmlElement,
|
|
668
|
+
extra: { href?: string; newTab?: boolean; caption?: string } = {},
|
|
669
|
+
): N[] {
|
|
670
|
+
const made = this.image(el, extra);
|
|
671
|
+
this.unit(made !== undefined);
|
|
672
|
+
return made ? [made] : [];
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
/** A link that holds more than text: a picture (linked image), a button, or boxes. */
|
|
676
|
+
linkBlock(el: HtmlElement, depth: number): N[] {
|
|
677
|
+
const href = this.href(el.attrs["href"]);
|
|
678
|
+
const newTab = el.attrs["target"] === "_blank";
|
|
679
|
+
const elements = el.children.filter((c): c is HtmlElement => c.kind === "element");
|
|
680
|
+
const img = elements.length === 1 && elements[0]?.tag === "img" ? elements[0] : undefined;
|
|
681
|
+
if (img && !this.plain(el.children.filter((c) => c !== img)))
|
|
682
|
+
return this.imageNodes(img, { ...(href ? { href } : {}), newTab });
|
|
683
|
+
if (
|
|
684
|
+
BUTTON_CLASS.test(el.attrs["class"] ?? "") &&
|
|
685
|
+
!el.children.some((c) => c.kind === "element" && this.blockish(c))
|
|
686
|
+
) {
|
|
687
|
+
return this.button(el.children, href, newTab);
|
|
688
|
+
}
|
|
689
|
+
if (href)
|
|
690
|
+
this.ctx.note(
|
|
691
|
+
"approximated",
|
|
692
|
+
"Links",
|
|
693
|
+
"Links around whole boxes were left out (EmVB links text, buttons and images); their content was kept.",
|
|
694
|
+
);
|
|
695
|
+
return this.nodes(el.children, depth);
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
button(content: readonly HtmlNode[], href: string | undefined, newTab: boolean): N[] {
|
|
699
|
+
const text = this.plain(content).slice(0, 200);
|
|
700
|
+
this.unit(text.length > 0);
|
|
701
|
+
if (!text) return [];
|
|
702
|
+
return [
|
|
703
|
+
this.node("button", {
|
|
704
|
+
text,
|
|
705
|
+
...(href ? { href } : {}),
|
|
706
|
+
...(href && newTab ? { newTab: true } : {}),
|
|
707
|
+
}),
|
|
708
|
+
];
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
figure(el: HtmlElement, depth: number): N[] {
|
|
712
|
+
const caption = el.children.find(
|
|
713
|
+
(c): c is HtmlElement => c.kind === "element" && c.tag === "figcaption",
|
|
714
|
+
);
|
|
715
|
+
const rest = el.children.filter((c) => c !== caption);
|
|
716
|
+
const elements = rest.filter((c): c is HtmlElement => c.kind === "element");
|
|
717
|
+
const only =
|
|
718
|
+
elements.length === 1 && !this.plain(rest.filter((c) => c.kind === "text"))
|
|
719
|
+
? elements[0]
|
|
720
|
+
: undefined;
|
|
721
|
+
const captionText = caption ? this.plain(caption.children).slice(0, 500) : "";
|
|
722
|
+
if (only?.tag === "img")
|
|
723
|
+
return this.imageNodes(only, captionText ? { caption: captionText } : {});
|
|
724
|
+
if (only?.tag === "a") {
|
|
725
|
+
const inner = only.children.filter((c): c is HtmlElement => c.kind === "element");
|
|
726
|
+
if (inner.length === 1 && inner[0]?.tag === "img") {
|
|
727
|
+
const href = this.href(only.attrs["href"]);
|
|
728
|
+
return this.imageNodes(inner[0], {
|
|
729
|
+
...(href ? { href } : {}),
|
|
730
|
+
newTab: only.attrs["target"] === "_blank",
|
|
731
|
+
...(captionText ? { caption: captionText } : {}),
|
|
732
|
+
});
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
if (only?.tag === "picture") {
|
|
736
|
+
const img = only.children.find(
|
|
737
|
+
(c): c is HtmlElement => c.kind === "element" && c.tag === "img",
|
|
738
|
+
);
|
|
739
|
+
if (img) return this.imageNodes(img, captionText ? { caption: captionText } : {});
|
|
740
|
+
}
|
|
741
|
+
if (only?.tag === "blockquote") return this.quote(only.children, depth, caption?.children);
|
|
742
|
+
return this.container(el, depth);
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
/** A quote: its paragraphs beside a line, then who said it ("— Name"). */
|
|
746
|
+
quote(content: readonly HtmlNode[], depth: number, citeContent?: readonly HtmlNode[]): N[] {
|
|
747
|
+
const cite = citeContent ?? content.find(isCite)?.children;
|
|
748
|
+
const body = this.nodes(
|
|
749
|
+
content.filter((c) => !isCite(c)),
|
|
750
|
+
depth + 1,
|
|
751
|
+
);
|
|
752
|
+
const who = cite ? this.plain(cite) : "";
|
|
753
|
+
if (who && this.take(who))
|
|
754
|
+
body.push(this.node("text", { text: `— ${who}`.slice(0, MAX_TEXT_LENGTH) }));
|
|
755
|
+
if (body.length === 0) return [];
|
|
756
|
+
return [
|
|
757
|
+
this.box(body, {
|
|
758
|
+
gap: { value: 8, unit: "px" },
|
|
759
|
+
borderStyle: "solid",
|
|
760
|
+
borderWidth: { value: 0, unit: "px" },
|
|
761
|
+
borderLeftWidth: { value: 4, unit: "px" },
|
|
762
|
+
paddingLeft: { value: 20, unit: "px" },
|
|
763
|
+
}),
|
|
764
|
+
];
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
/** A table → a Grid of its cells (EmVB has no table element); header cells are bold. */
|
|
768
|
+
table(el: HtmlElement): N[] {
|
|
769
|
+
const rows: HtmlElement[][] = [];
|
|
770
|
+
let caption: string | undefined;
|
|
771
|
+
const visit = (list: readonly HtmlNode[]) => {
|
|
772
|
+
for (const item of list) {
|
|
773
|
+
if (item.kind === "text") continue;
|
|
774
|
+
if (item.kind === "block") {
|
|
775
|
+
visit(item.children);
|
|
776
|
+
continue;
|
|
777
|
+
}
|
|
778
|
+
if (item.tag === "caption") caption = this.plain(item.children);
|
|
779
|
+
else if (item.tag === "tr") {
|
|
780
|
+
const cells = item.children.filter(
|
|
781
|
+
(c): c is HtmlElement => c.kind === "element" && (c.tag === "td" || c.tag === "th"),
|
|
782
|
+
);
|
|
783
|
+
if (cells.length > 0 && rows.length < MAX_TABLE_ROWS) rows.push(cells);
|
|
784
|
+
} else if (TRANSPARENT.has(item.tag)) visit(item.children);
|
|
785
|
+
}
|
|
786
|
+
};
|
|
787
|
+
visit(el.children);
|
|
788
|
+
this.unit(rows.length > 0);
|
|
789
|
+
if (rows.length === 0) return [];
|
|
790
|
+
const columns = Math.min(MAX_TABLE_COLUMNS, Math.max(...rows.map((r) => r.length)));
|
|
791
|
+
this.ctx.note(
|
|
792
|
+
"approximated",
|
|
793
|
+
"Tables",
|
|
794
|
+
"Tables became a grid of text (EmVB has no table element), so screen readers no longer read them as a table.",
|
|
795
|
+
);
|
|
796
|
+
if (rows.some((r) => r.length > MAX_TABLE_COLUMNS))
|
|
797
|
+
this.ctx.note("skipped", "Tables", `Tables keep their first ${MAX_TABLE_COLUMNS} columns.`);
|
|
798
|
+
const cells: N[] = [];
|
|
799
|
+
for (const row of rows) {
|
|
800
|
+
for (let i = 0; i < columns; i++) {
|
|
801
|
+
if (this.ctx.overBudget) break;
|
|
802
|
+
const cell = row[i];
|
|
803
|
+
const text = cell ? this.plain(cell.children) : "";
|
|
804
|
+
if (!this.take(text)) break;
|
|
805
|
+
cells.push(
|
|
806
|
+
this.node(
|
|
807
|
+
"text",
|
|
808
|
+
{ text: text.slice(0, MAX_TEXT_LENGTH) },
|
|
809
|
+
cell?.tag === "th" ? { style: { fontWeight: 700 } } : {},
|
|
810
|
+
),
|
|
811
|
+
);
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
const grid = this.node(
|
|
815
|
+
"grid",
|
|
816
|
+
{ columns },
|
|
817
|
+
{
|
|
818
|
+
style: { gap: { value: 8, unit: "px" } },
|
|
819
|
+
children: cells,
|
|
820
|
+
},
|
|
821
|
+
);
|
|
822
|
+
if (!caption) return [grid];
|
|
823
|
+
return [
|
|
824
|
+
this.box(
|
|
825
|
+
[
|
|
826
|
+
this.node(
|
|
827
|
+
"text",
|
|
828
|
+
{ text: caption.slice(0, MAX_TEXT_LENGTH) },
|
|
829
|
+
{ style: { fontWeight: 700 } },
|
|
830
|
+
),
|
|
831
|
+
grid,
|
|
832
|
+
],
|
|
833
|
+
{ gap: { value: 8, unit: "px" } },
|
|
834
|
+
),
|
|
835
|
+
];
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
/** A YouTube or Vimeo frame → a Video; other frames are left out. */
|
|
839
|
+
embed(el: HtmlElement): N[] {
|
|
840
|
+
const raw = el.attrs["src"] ?? "";
|
|
841
|
+
const url = this.videoUrl(raw);
|
|
842
|
+
this.unit(url !== undefined);
|
|
843
|
+
if (url) return [this.videoNode(url, el.attrs["title"])];
|
|
844
|
+
if (el.attrs["srcdoc"] !== undefined && !raw)
|
|
845
|
+
this.ctx.note(
|
|
846
|
+
"skipped",
|
|
847
|
+
"Embeds",
|
|
848
|
+
"Embedded frames with their own HTML (srcdoc) were left out.",
|
|
849
|
+
);
|
|
850
|
+
else
|
|
851
|
+
this.ctx.note(
|
|
852
|
+
"skipped",
|
|
853
|
+
"Embeds",
|
|
854
|
+
"Embedded frames other than YouTube and Vimeo videos were left out (maps, forms, social posts…).",
|
|
855
|
+
);
|
|
856
|
+
return [];
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
/** A YouTube, Vimeo or video file address EmVB's Video element plays; undefined otherwise. */
|
|
860
|
+
videoUrl(raw: string): string | undefined {
|
|
861
|
+
const trimmed = raw.trim();
|
|
862
|
+
if (!trimmed) return undefined;
|
|
863
|
+
const watch = embedVideoUrl(trimmed);
|
|
864
|
+
const candidate = watch ?? trimmed;
|
|
865
|
+
if (candidate.startsWith("//")) return undefined;
|
|
866
|
+
const target = resolveEmbedUrl(candidate);
|
|
867
|
+
if (!target) return undefined;
|
|
868
|
+
return target.kind === "iframe" ? candidate : (sanitizeMediaUrl(candidate) ?? undefined);
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
videoNode(url: string, title: string | undefined): N {
|
|
872
|
+
const name = cleanText(stripControl(title ?? "")).slice(0, 200) || "Video";
|
|
873
|
+
return this.node("video", { url, title: name });
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
video(el: HtmlElement): N[] {
|
|
877
|
+
const source = el.children.find(
|
|
878
|
+
(c): c is HtmlElement =>
|
|
879
|
+
c.kind === "element" && c.tag === "source" && Boolean(c.attrs["src"]),
|
|
880
|
+
);
|
|
881
|
+
const url = this.videoUrl(el.attrs["src"] ?? source?.attrs["src"] ?? "");
|
|
882
|
+
this.unit(url !== undefined);
|
|
883
|
+
if (!url) {
|
|
884
|
+
this.ctx.note("skipped", "Videos", "Videos with an unsupported address were left out.");
|
|
885
|
+
return [];
|
|
886
|
+
}
|
|
887
|
+
return [this.videoNode(url, el.attrs["title"] ?? el.attrs["aria-label"])];
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
/** `details` → an Accordion with one item, open when it was. */
|
|
891
|
+
details(el: HtmlElement, depth: number): N[] {
|
|
892
|
+
const summary = el.children.find(
|
|
893
|
+
(c): c is HtmlElement => c.kind === "element" && c.tag === "summary",
|
|
894
|
+
);
|
|
895
|
+
const title = (summary ? this.plain(summary.children) : "").slice(0, 200) || "Details";
|
|
896
|
+
const body = this.nodes(
|
|
897
|
+
el.children.filter((c) => c !== summary),
|
|
898
|
+
depth + 2,
|
|
899
|
+
);
|
|
900
|
+
this.unit(true);
|
|
901
|
+
const item = this.node(
|
|
902
|
+
"accordion-item",
|
|
903
|
+
{ summary: title, ...("open" in el.attrs ? { open: true } : {}) },
|
|
904
|
+
{ children: body },
|
|
905
|
+
);
|
|
906
|
+
return [this.node("accordion", {}, { children: [item] })];
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
/** Preformatted text and code: a Text that keeps its spacing, in a monospace font. */
|
|
910
|
+
pre(el: HtmlElement): N[] {
|
|
911
|
+
// WordPress's Verse block is a poem: it keeps its spacing but not a code font.
|
|
912
|
+
const verse = /(?:^|\s)wp-block-verse(?:\s|$)/.test(el.attrs["class"] ?? "");
|
|
913
|
+
let raw = "";
|
|
914
|
+
const visit = (list: readonly HtmlNode[]) => {
|
|
915
|
+
for (const item of list) {
|
|
916
|
+
if (item.kind === "text") raw += item.text;
|
|
917
|
+
else if (item.kind === "element" && item.tag === "br") raw += "\n";
|
|
918
|
+
else visit(item.children);
|
|
919
|
+
}
|
|
920
|
+
};
|
|
921
|
+
visit(el.children);
|
|
922
|
+
const text = stripControl(raw.replace(/\r\n?/g, "\n").replace(/\t/g, " ")).replace(
|
|
923
|
+
/^\n|\n+$/g,
|
|
924
|
+
"",
|
|
925
|
+
);
|
|
926
|
+
this.unit(text.trim().length > 0);
|
|
927
|
+
if (!text.trim() || !this.take(text)) return [];
|
|
928
|
+
return splitLong(text).map((part) =>
|
|
929
|
+
this.node(
|
|
930
|
+
"text",
|
|
931
|
+
{ text: part },
|
|
932
|
+
{ style: { whiteSpace: "pre-wrap", ...(verse ? {} : { fontFamily: "monospace" }) } },
|
|
933
|
+
),
|
|
934
|
+
);
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
/** `dl` → its terms (bold) and descriptions as Text. */
|
|
938
|
+
definitions(el: HtmlElement, depth: number): N[] {
|
|
939
|
+
const out: N[] = [];
|
|
940
|
+
const visit = (list: readonly HtmlNode[]) => {
|
|
941
|
+
for (const item of list) {
|
|
942
|
+
if (item.kind !== "element") continue;
|
|
943
|
+
if (item.tag === "dt")
|
|
944
|
+
out.push(...withStyle(this.nodes(item.children, depth + 1), { fontWeight: 700 }));
|
|
945
|
+
else if (item.tag === "dd")
|
|
946
|
+
out.push(
|
|
947
|
+
...withStyle(this.nodes(item.children, depth + 1), {
|
|
948
|
+
paddingLeft: { value: 16, unit: "px" },
|
|
949
|
+
}),
|
|
950
|
+
);
|
|
951
|
+
else if (item.tag === "div") visit(item.children);
|
|
952
|
+
}
|
|
953
|
+
};
|
|
954
|
+
visit(el.children);
|
|
955
|
+
if (out.length === 0) return [];
|
|
956
|
+
return [this.box(out, { gap: { value: 4, unit: "px" } })];
|
|
957
|
+
}
|
|
958
|
+
}
|