@perkyzz/emvb 0.8.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/package.json +4 -4
- package/src/admin/agents-api.ts +45 -0
- package/src/admin/editor/A11yCopilot.tsx +1 -1
- package/src/admin/editor/Editor.tsx +325 -239
- package/src/admin/editor/FieldPreview.tsx +164 -0
- package/src/admin/editor/Timeline.tsx +2 -1
- package/src/admin/editor/TopBar.tsx +36 -3
- package/src/admin/editor/canvas/CanvasFrame.tsx +2 -2
- package/src/admin/editor/canvas/DeviceBar.tsx +17 -9
- package/src/admin/editor/canvas/canvas-scale.ts +2 -2
- package/src/admin/editor/canvas/use-canvas-session.ts +2 -2
- package/src/admin/editor/co-design.ts +190 -0
- package/src/admin/editor/dnd/new-element.ts +26 -4
- package/src/admin/editor/editor-chrome.tsx +3 -3
- package/src/admin/editor/editor-commands.ts +8 -3
- package/src/admin/editor/editor-css.ts +3 -0
- package/src/admin/editor/element-chrome.ts +3 -1
- package/src/admin/editor/extension-manifests.ts +20 -0
- package/src/admin/editor/extensions/ExtensionPanels.tsx +26 -0
- package/src/admin/editor/extensions-context.tsx +27 -0
- package/src/admin/editor/feature-css.ts +17 -0
- package/src/admin/editor/host.ts +6 -0
- package/src/admin/editor/palette-items.ts +2 -2
- package/src/admin/editor/panels/AddPanel.tsx +31 -1
- package/src/admin/editor/panels/AgentRequestBox.tsx +116 -0
- package/src/admin/editor/panels/ClassesSection.tsx +9 -6
- package/src/admin/editor/panels/CoDesignPanel.tsx +170 -0
- package/src/admin/editor/panels/ColorControl.tsx +2 -2
- package/src/admin/editor/panels/ElementPanel.tsx +53 -15
- package/src/admin/editor/panels/LanguagesSection.tsx +152 -0
- package/src/admin/editor/panels/LayersPanel.tsx +10 -6
- package/src/admin/editor/panels/LeftPanel.tsx +33 -5
- package/src/admin/editor/panels/PageSettings.tsx +4 -0
- package/src/admin/editor/panels/SettingsPanel.tsx +21 -2
- package/src/admin/editor/panels/SiteStylesDrawer.tsx +7 -7
- package/src/admin/editor/panels/settings/BackgroundControls.tsx +76 -20
- package/src/admin/editor/panels/settings/BackgroundFill.tsx +8 -6
- package/src/admin/editor/panels/settings/BoxControl.tsx +5 -5
- package/src/admin/editor/panels/settings/EffectsControls.tsx +104 -9
- package/src/admin/editor/panels/settings/FieldControl.tsx +9 -3
- package/src/admin/editor/panels/settings/IconControls.tsx +14 -11
- package/src/admin/editor/panels/settings/LengthRow.tsx +27 -16
- package/src/admin/editor/panels/settings/LoopControl.tsx +269 -0
- package/src/admin/editor/panels/settings/LoopItemBindControl.tsx +1 -1
- package/src/admin/editor/panels/settings/MediaPicker.tsx +5 -5
- package/src/admin/editor/panels/settings/MotionControl.tsx +12 -11
- package/src/admin/editor/panels/settings/QueryStyleRows.tsx +7 -4
- package/src/admin/editor/panels/settings/StyleRow.tsx +113 -13
- package/src/admin/editor/panels/settings/TextMarksControl.tsx +247 -0
- package/src/admin/editor/panels/settings/VariableBinding.tsx +2 -2
- package/src/admin/editor/panels/settings/background-mode.ts +14 -11
- package/src/admin/editor/panels/settings/box-link-name.ts +1 -1
- package/src/admin/editor/panels/settings/box-sides.ts +22 -14
- package/src/admin/editor/panels/settings/heading-warning.ts +5 -1
- package/src/admin/editor/panels/settings/length-units.ts +55 -2
- package/src/admin/editor/panels/settings/query-style.ts +3 -3
- package/src/admin/editor/panels/settings/style-sections.ts +51 -10
- package/src/admin/editor/panels/style-target.ts +9 -7
- package/src/admin/editor/presence.ts +60 -0
- package/src/admin/editor/store.ts +6 -2
- package/src/admin/editor/useCoDesign.ts +177 -0
- package/src/admin/editor/useEditorCommands.ts +4 -4
- package/src/admin/editor/useExtensions.ts +45 -0
- package/src/admin/editor/useNodeActions.ts +15 -6
- package/src/admin/editor/useProposals.ts +81 -0
- package/src/admin/index.tsx +5 -1
- package/src/admin/languages-api.ts +61 -0
- package/src/admin/media-api.ts +27 -0
- package/src/admin/pages/AgentSetup.tsx +148 -0
- package/src/admin/pages/AgentsPage.tsx +11 -28
- package/src/admin/pages/CollectionsPage.tsx +215 -0
- package/src/admin/pages/CreateDialog.tsx +15 -0
- package/src/admin/pages/ImportButton.tsx +18 -0
- package/src/admin/pages/ImportElementorDialog.tsx +669 -0
- package/src/admin/pages/ImportPageLinks.tsx +86 -0
- package/src/admin/pages/PageList.tsx +15 -1
- package/src/admin/pages/ThemePartList.tsx +15 -1
- package/src/admin/pages/section-head.tsx +27 -0
- package/src/admin/proposals-api.ts +91 -0
- package/src/admin/setup/host-collections.ts +100 -0
- package/src/admin/ui.ts +39 -2
- package/src/admin/widgets/CanvasField.tsx +202 -0
- package/src/admin/widgets/convert-body.ts +54 -0
- package/src/admin/widgets/field-fetcher.ts +79 -0
- package/src/astro/content-port.ts +14 -1
- package/src/astro/index.ts +2 -0
- package/src/astro/page-count.ts +77 -0
- package/src/astro/resolve-theme.ts +58 -3
- package/src/astro/resolve.ts +128 -5
- package/src/astro/responsive-images.ts +99 -0
- package/src/constants.ts +1 -1
- package/src/core/a11y/audit.ts +44 -14
- package/src/core/arrange.ts +5 -1
- package/src/core/bounded-array.ts +31 -0
- package/src/core/canvas-collections.ts +20 -0
- package/src/core/clipboard.ts +9 -0
- package/src/core/contract.ts +285 -0
- package/src/core/css/generate.ts +89 -20
- package/src/core/css/trace.ts +18 -9
- package/src/core/css-keywords.ts +51 -1
- package/src/core/design/cascade.ts +12 -9
- package/src/core/design/classes.ts +4 -5
- package/src/core/design/devices.ts +13 -13
- package/src/core/design/states.ts +5 -3
- package/src/core/design/transfer.ts +5 -0
- package/src/core/design/variables.ts +17 -4
- package/src/core/elements/dynamic-elements.ts +19 -4
- package/src/core/elements/index.ts +73 -9
- package/src/core/extensions/define.ts +13 -0
- package/src/core/extensions/index.ts +41 -0
- package/src/core/extensions/namespace.ts +82 -0
- package/src/core/extensions/normalize.ts +141 -0
- package/src/core/extensions/placeholder.ts +41 -0
- package/src/core/extensions/registry.ts +94 -0
- package/src/core/extensions/render-ext.ts +118 -0
- package/src/core/extensions/safe-nodes.ts +93 -0
- package/src/core/extensions/schema.ts +112 -0
- package/src/core/extensions/types.ts +159 -0
- package/src/core/forms/binding.ts +1 -1
- package/src/core/history/diff.ts +1 -1
- package/src/core/import/elementor/atomic.ts +1595 -0
- package/src/core/import/elementor/classes.ts +127 -0
- package/src/core/import/elementor/context.ts +429 -0
- package/src/core/import/elementor/counter-steps.ts +72 -0
- package/src/core/import/elementor/css.ts +445 -0
- package/src/core/import/elementor/html.ts +500 -0
- package/src/core/import/elementor/index.ts +949 -0
- package/src/core/import/elementor/inline-marks.ts +81 -0
- package/src/core/import/elementor/snapshots.ts +48 -0
- package/src/core/import/elementor/style.ts +409 -0
- package/src/core/import/elementor/values.ts +241 -0
- package/src/core/import/elementor/widgets.ts +1897 -0
- package/src/core/index.ts +84 -0
- package/src/core/json-schema.ts +107 -0
- package/src/core/limits.ts +19 -0
- package/src/core/migrate/index.ts +6 -1
- package/src/core/migrate/v18.ts +222 -0
- package/src/core/perf/meter.ts +5 -5
- package/src/core/portable-text.ts +127 -0
- package/src/core/proposals-safety.ts +102 -0
- package/src/core/proposals.ts +357 -0
- package/src/core/recipes/index.ts +2 -2
- package/src/core/render/extras.ts +3 -3
- package/src/core/render/index.ts +140 -20
- package/src/core/render/loop.ts +1 -1
- package/src/core/render/pagination.ts +20 -1
- package/src/core/render/section.ts +4 -4
- package/src/core/render/vnode.ts +1 -1
- package/src/core/sanitize/css.ts +125 -10
- package/src/core/sanitize/motion.ts +116 -0
- package/src/core/saved-sections.ts +6 -0
- package/src/core/schema/descriptors.ts +3 -1
- package/src/core/schema/design.ts +2 -1
- package/src/core/schema/layout.ts +87 -14
- package/src/core/schema/style.ts +251 -114
- package/src/core/text-marks.ts +227 -0
- package/src/core/theme/dynamic.ts +9 -4
- package/src/core/theme/float.ts +1 -0
- package/src/core/theme/pagination.ts +9 -0
- package/src/core/theme/popup-rules.ts +17 -1
- package/src/core/translatable.ts +34 -0
- package/src/core/validate.ts +13 -1
- package/src/index.ts +35 -1
- package/src/mcp/errors.ts +3 -1
- package/src/mcp/ports.ts +67 -0
- package/src/mcp/prompts.ts +1 -11
- package/src/mcp/proposals.ts +281 -0
- package/src/mcp/read.ts +10 -9
- package/src/mcp/tools.ts +71 -3
- package/src/mcp/write.ts +66 -11
- package/src/server/agent-settings.ts +21 -2
- package/src/server/hooks.ts +81 -0
- package/src/server/mcp-routes.ts +15 -0
- package/src/server/plugin.ts +53 -5
- package/src/server/presence.ts +78 -0
- package/src/server/proposals.ts +261 -0
- package/src/server/requests.ts +196 -0
- package/src/server/tools-served.ts +45 -0
|
@@ -0,0 +1,500 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Elementor's HTML (Text Editor, HTML widget, accordion and tab content) → EmVB blocks (W-945).
|
|
3
|
+
* A small tolerant tokenizer, not a DOM: it only ever produces plain text, so no markup from
|
|
4
|
+
* the input reaches the page. Scripts, styles and comments are dropped; inline formatting is
|
|
5
|
+
* flattened (and noted); links, images and embeds keep their URLs after sanitizing.
|
|
6
|
+
*/
|
|
7
|
+
import { MAX_TEXT_LENGTH } from "../../limits.ts";
|
|
8
|
+
import type { TextMark } from "../../schema/layout.ts";
|
|
9
|
+
import { combine, type Fmt, marksFromChars } from "./inline-marks.ts";
|
|
10
|
+
import { color as cssColorOf } from "./values.ts";
|
|
11
|
+
|
|
12
|
+
export type Block =
|
|
13
|
+
| { kind: "heading"; level: number; text: string; href?: string; marks?: TextMark[] }
|
|
14
|
+
| { kind: "text"; text: string; marks?: TextMark[] }
|
|
15
|
+
| { kind: "link"; text: string; href: string }
|
|
16
|
+
| { kind: "list"; ordered: boolean; start?: number; items: string[] }
|
|
17
|
+
| { kind: "image"; src: string; alt: string }
|
|
18
|
+
| { kind: "divider" }
|
|
19
|
+
| { kind: "video"; url: string };
|
|
20
|
+
|
|
21
|
+
export type HtmlResult = {
|
|
22
|
+
blocks: Block[];
|
|
23
|
+
/** Inline formatting (bold, colour spans, inline links) was flattened to plain text. */
|
|
24
|
+
flattened: boolean;
|
|
25
|
+
/** Tags or embeds that were dropped (script, iframe from an unknown host, form, table, ...). */
|
|
26
|
+
dropped: string[];
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/** Input HTML longer than this is cut (a Text Editor holds far less in practice). */
|
|
30
|
+
const MAX_HTML_LENGTH = 200_000;
|
|
31
|
+
const MAX_BLOCKS = 400;
|
|
32
|
+
|
|
33
|
+
const NAMED: Record<string, string> = {
|
|
34
|
+
amp: "&",
|
|
35
|
+
lt: "<",
|
|
36
|
+
gt: ">",
|
|
37
|
+
quot: '"',
|
|
38
|
+
apos: "'",
|
|
39
|
+
nbsp: "\u00a0",
|
|
40
|
+
hellip: "…",
|
|
41
|
+
mdash: "—",
|
|
42
|
+
ndash: "–",
|
|
43
|
+
lsquo: "‘",
|
|
44
|
+
rsquo: "’",
|
|
45
|
+
ldquo: "“",
|
|
46
|
+
rdquo: "”",
|
|
47
|
+
laquo: "«",
|
|
48
|
+
raquo: "»",
|
|
49
|
+
copy: "©",
|
|
50
|
+
reg: "®",
|
|
51
|
+
trade: "™",
|
|
52
|
+
times: "×",
|
|
53
|
+
middot: "·",
|
|
54
|
+
bull: "•",
|
|
55
|
+
euro: "€",
|
|
56
|
+
pound: "£",
|
|
57
|
+
deg: "°",
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export function decodeEntities(text: string): string {
|
|
61
|
+
return text.replace(/&(#x[0-9a-f]{1,6}|#\d{1,7}|[a-z]{2,8});/gi, (whole, body: string) => {
|
|
62
|
+
if (body[0] === "#") {
|
|
63
|
+
const code =
|
|
64
|
+
body[1] === "x" || body[1] === "X"
|
|
65
|
+
? Number.parseInt(body.slice(2), 16)
|
|
66
|
+
: Number(body.slice(1));
|
|
67
|
+
if (
|
|
68
|
+
!Number.isFinite(code) ||
|
|
69
|
+
code <= 0 ||
|
|
70
|
+
code > 0x10ffff ||
|
|
71
|
+
(code >= 0xd800 && code <= 0xdfff)
|
|
72
|
+
)
|
|
73
|
+
return "";
|
|
74
|
+
return String.fromCodePoint(code);
|
|
75
|
+
}
|
|
76
|
+
return NAMED[body.toLowerCase()] ?? whole;
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Collapses whitespace and drops control characters. A line feed is a `<br>` the tokenizer kept
|
|
82
|
+
* (source line breaks become spaces first); Text elements show it (`white-space: pre-line`).
|
|
83
|
+
*/
|
|
84
|
+
// oxlint-disable-next-line no-control-regex
|
|
85
|
+
const CONTROL = /[\u0000-\u0008\u000b-\u001f\u007f]/g;
|
|
86
|
+
|
|
87
|
+
/** Drops control characters (keeps tab and line feed). */
|
|
88
|
+
export const stripControl = (text: string) => text.replace(CONTROL, "");
|
|
89
|
+
|
|
90
|
+
export function cleanText(text: string, keepBreaks = false): string {
|
|
91
|
+
const flat = stripControl(text.replace(/\t/g, " "))
|
|
92
|
+
.replace(/[ \u00a0]+/g, " ")
|
|
93
|
+
.replace(/ ?\n ?/g, "\n")
|
|
94
|
+
.replace(/\n{3,}/g, "\n\n")
|
|
95
|
+
.trim();
|
|
96
|
+
return keepBreaks ? flat : flat.replace(/\n+/g, " ");
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Splits text longer than an element allows at word boundaries, keeping all of it. */
|
|
100
|
+
export function splitLong(text: string, max = MAX_TEXT_LENGTH): string[] {
|
|
101
|
+
if (text.length <= max) return [text];
|
|
102
|
+
const out: string[] = [];
|
|
103
|
+
let rest = text;
|
|
104
|
+
while (rest.length > max) {
|
|
105
|
+
let cut = rest.lastIndexOf(" ", max);
|
|
106
|
+
if (cut < max / 2) cut = max;
|
|
107
|
+
out.push(rest.slice(0, cut).trim());
|
|
108
|
+
rest = rest.slice(cut).trim();
|
|
109
|
+
}
|
|
110
|
+
if (rest) out.push(rest);
|
|
111
|
+
return out;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const RAW_TEXT = new Set([
|
|
115
|
+
"script",
|
|
116
|
+
"style",
|
|
117
|
+
"textarea",
|
|
118
|
+
"title",
|
|
119
|
+
"noscript",
|
|
120
|
+
"template",
|
|
121
|
+
"svg",
|
|
122
|
+
"math",
|
|
123
|
+
]);
|
|
124
|
+
const BLOCK = new Set([
|
|
125
|
+
"p",
|
|
126
|
+
"div",
|
|
127
|
+
"section",
|
|
128
|
+
"article",
|
|
129
|
+
"header",
|
|
130
|
+
"footer",
|
|
131
|
+
"blockquote",
|
|
132
|
+
"figure",
|
|
133
|
+
"figcaption",
|
|
134
|
+
"pre",
|
|
135
|
+
"address",
|
|
136
|
+
"center",
|
|
137
|
+
"main",
|
|
138
|
+
"aside",
|
|
139
|
+
"nav",
|
|
140
|
+
"dl",
|
|
141
|
+
"dt",
|
|
142
|
+
"dd",
|
|
143
|
+
"tr",
|
|
144
|
+
]);
|
|
145
|
+
const INLINE_FORMAT = new Set([
|
|
146
|
+
"strong",
|
|
147
|
+
"b",
|
|
148
|
+
"em",
|
|
149
|
+
"i",
|
|
150
|
+
"u",
|
|
151
|
+
"span",
|
|
152
|
+
"font",
|
|
153
|
+
"mark",
|
|
154
|
+
"small",
|
|
155
|
+
"sub",
|
|
156
|
+
"sup",
|
|
157
|
+
"code",
|
|
158
|
+
"s",
|
|
159
|
+
"del",
|
|
160
|
+
"ins",
|
|
161
|
+
"abbr",
|
|
162
|
+
"cite",
|
|
163
|
+
"q",
|
|
164
|
+
"kbd",
|
|
165
|
+
"var",
|
|
166
|
+
"time",
|
|
167
|
+
]);
|
|
168
|
+
/** W-950: the formatting an inline tag gives that EmVB keeps: bold, italic, a colour. */
|
|
169
|
+
function formatOf(name: string, attributes: Record<string, string>): Fmt | undefined {
|
|
170
|
+
if (name === "strong" || name === "b") return { bold: true };
|
|
171
|
+
if (name === "em" || name === "i") return { italic: true };
|
|
172
|
+
if (name === "span" || name === "font") {
|
|
173
|
+
const declared =
|
|
174
|
+
/(?:^|;)\s*color\s*:\s*([^;]+)/i.exec(attributes["style"] ?? "")?.[1] ??
|
|
175
|
+
(name === "font" ? attributes["color"] : undefined);
|
|
176
|
+
const hex = declared ? cssColorOf(declared.replace(/!important/i, "").trim()) : undefined;
|
|
177
|
+
return hex && hex.length <= 7 ? { color: hex } : undefined;
|
|
178
|
+
}
|
|
179
|
+
return undefined;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const DROP_WITH_NOTE = new Set([
|
|
183
|
+
"form",
|
|
184
|
+
"input",
|
|
185
|
+
"button",
|
|
186
|
+
"select",
|
|
187
|
+
"object",
|
|
188
|
+
"embed",
|
|
189
|
+
"canvas",
|
|
190
|
+
"audio",
|
|
191
|
+
"video",
|
|
192
|
+
]);
|
|
193
|
+
|
|
194
|
+
function attrs(source: string): Record<string, string> {
|
|
195
|
+
const out: Record<string, string> = {};
|
|
196
|
+
for (const m of source.matchAll(
|
|
197
|
+
/([^\s"'>/=]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/g,
|
|
198
|
+
)) {
|
|
199
|
+
const name = (m[1] ?? "").toLowerCase();
|
|
200
|
+
if (!name) continue;
|
|
201
|
+
if (!(name in out)) out[name] = decodeEntities(m[2] ?? m[3] ?? m[4] ?? "");
|
|
202
|
+
}
|
|
203
|
+
return out;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** YouTube / Vimeo iframe src → a watch URL EmVB's Video element accepts. */
|
|
207
|
+
export function embedVideoUrl(src: string): string | undefined {
|
|
208
|
+
const yt =
|
|
209
|
+
/^(?:https?:)?\/\/(?:www\.)?(?:youtube(?:-nocookie)?\.com\/embed\/|youtu\.be\/)([A-Za-z0-9_-]{6,20})/i.exec(
|
|
210
|
+
src,
|
|
211
|
+
);
|
|
212
|
+
if (yt) return `https://www.youtube.com/watch?v=${yt[1]}`;
|
|
213
|
+
const vimeo = /^(?:https?:)?\/\/player\.vimeo\.com\/video\/(\d{1,15})/i.exec(src);
|
|
214
|
+
if (vimeo) return `https://vimeo.com/${vimeo[1]}`;
|
|
215
|
+
return undefined;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** HTML → blocks. `safeHref`/`safeSrc` decide which URLs are kept (callers pass EmVB's sanitizers). */
|
|
219
|
+
export function htmlToBlocks(
|
|
220
|
+
input: string,
|
|
221
|
+
safeHref: (url: string) => string | undefined,
|
|
222
|
+
safeSrc: (url: string) => string | undefined,
|
|
223
|
+
): HtmlResult {
|
|
224
|
+
const html = input.length > MAX_HTML_LENGTH ? input.slice(0, MAX_HTML_LENGTH) : input;
|
|
225
|
+
const blocks: Block[] = [];
|
|
226
|
+
const dropped = new Set<string>();
|
|
227
|
+
let flattened = false;
|
|
228
|
+
if (input.length > MAX_HTML_LENGTH) dropped.add("text past 200,000 characters");
|
|
229
|
+
|
|
230
|
+
let buffer = "";
|
|
231
|
+
// W-950: the formatting of each character of `buffer`, and the inline tags open around it.
|
|
232
|
+
let fmts: (Fmt | undefined)[] = [];
|
|
233
|
+
const open: { name: string; f: Fmt }[] = [];
|
|
234
|
+
const hrefs: (string | undefined)[] = [];
|
|
235
|
+
const add = (text: string) => {
|
|
236
|
+
buffer += text;
|
|
237
|
+
const f = combine(
|
|
238
|
+
open.map((entry) => entry.f),
|
|
239
|
+
hrefs.at(-1),
|
|
240
|
+
);
|
|
241
|
+
for (let i = 0; i < text.length; i++) fmts.push(f);
|
|
242
|
+
};
|
|
243
|
+
let heading: number | undefined;
|
|
244
|
+
/** Links seen in the current paragraph, and whether all its text was inside one link. */
|
|
245
|
+
let links: { href: string; text: string }[] = [];
|
|
246
|
+
let linkDepth = 0;
|
|
247
|
+
let textOutsideLink = false;
|
|
248
|
+
let list: { ordered: boolean; start?: number; items: string[] } | undefined;
|
|
249
|
+
let listDepth = 0;
|
|
250
|
+
let item: string | undefined;
|
|
251
|
+
|
|
252
|
+
const push = (block: Block) => {
|
|
253
|
+
if (blocks.length < MAX_BLOCKS) blocks.push(block);
|
|
254
|
+
else dropped.add(`content past ${MAX_BLOCKS} blocks`);
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
const flush = () => {
|
|
258
|
+
const text = cleanText(buffer, heading === undefined);
|
|
259
|
+
const single = splitLong(text).length === 1;
|
|
260
|
+
const formatted = fmts.some(Boolean);
|
|
261
|
+
const marks =
|
|
262
|
+
single && formatted ? marksFromChars(buffer, fmts, heading === undefined, text) : [];
|
|
263
|
+
if (formatted && marks.length === 0) flattened = true;
|
|
264
|
+
buffer = "";
|
|
265
|
+
fmts = [];
|
|
266
|
+
const onlyLink = links.length === 1 && !textOutsideLink ? links[0] : undefined;
|
|
267
|
+
if (links.length > 0 && !onlyLink && marks.length === 0) flattened = true;
|
|
268
|
+
// A heading or link that is one link keeps it on the element, not on its words.
|
|
269
|
+
const ownMarks = onlyLink
|
|
270
|
+
? marks.flatMap(({ href: _href, ...rest }) =>
|
|
271
|
+
rest.bold || rest.italic || rest.color ? [rest] : [],
|
|
272
|
+
)
|
|
273
|
+
: marks;
|
|
274
|
+
links = [];
|
|
275
|
+
textOutsideLink = false;
|
|
276
|
+
if (!text) {
|
|
277
|
+
heading = undefined;
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
if (heading !== undefined) {
|
|
281
|
+
for (const part of splitLong(text)) {
|
|
282
|
+
push({
|
|
283
|
+
kind: "heading",
|
|
284
|
+
level: heading,
|
|
285
|
+
text: part,
|
|
286
|
+
...(onlyLink ? { href: onlyLink.href } : {}),
|
|
287
|
+
...(ownMarks.length > 0 ? { marks: ownMarks } : {}),
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
} else if (onlyLink && text.length <= MAX_TEXT_LENGTH) {
|
|
291
|
+
push({ kind: "link", text, href: onlyLink.href });
|
|
292
|
+
} else {
|
|
293
|
+
for (const part of splitLong(text))
|
|
294
|
+
push({ kind: "text", text: part, ...(marks.length > 0 ? { marks } : {}) });
|
|
295
|
+
}
|
|
296
|
+
heading = undefined;
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
const flushItem = () => {
|
|
300
|
+
if (item === undefined || !list) return;
|
|
301
|
+
const text = cleanText(item);
|
|
302
|
+
if (text)
|
|
303
|
+
for (const part of splitLong(text)) if (list.items.length < 200) list.items.push(part);
|
|
304
|
+
item = undefined;
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
const addText = (raw: string) => {
|
|
308
|
+
const text = decodeEntities(raw.replace(/[\r\n\t\f]+/g, " "));
|
|
309
|
+
if (item !== undefined) {
|
|
310
|
+
item += text;
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
if (list && listDepth > 0) {
|
|
314
|
+
if (text.trim()) item = text;
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
add(text);
|
|
318
|
+
if (linkDepth > 0) {
|
|
319
|
+
const last = links[links.length - 1];
|
|
320
|
+
if (last) last.text += text;
|
|
321
|
+
} else if (text.trim()) {
|
|
322
|
+
textOutsideLink = true;
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
const token =
|
|
327
|
+
/<!--[\s\S]*?(?:-->|$)|<!\[CDATA\[[\s\S]*?(?:\]\]>|$)|<![^>]*>|<\/?([a-zA-Z][a-zA-Z0-9-]*)((?:[^>"']|"[^"]*"|'[^']*')*)>|[^<]+|</g;
|
|
328
|
+
let match: RegExpExecArray | null;
|
|
329
|
+
while ((match = token.exec(html)) !== null) {
|
|
330
|
+
const whole = match[0];
|
|
331
|
+
const name = match[1]?.toLowerCase();
|
|
332
|
+
if (!name) {
|
|
333
|
+
if (whole.startsWith("<!") || whole === "<") continue;
|
|
334
|
+
addText(whole);
|
|
335
|
+
continue;
|
|
336
|
+
}
|
|
337
|
+
const closing = whole[1] === "/";
|
|
338
|
+
if (!closing && RAW_TEXT.has(name)) {
|
|
339
|
+
const end = html.toLowerCase().indexOf(`</${name}`, token.lastIndex);
|
|
340
|
+
token.lastIndex = end === -1 ? html.length : end;
|
|
341
|
+
if (name === "script" || name === "style") dropped.add(name);
|
|
342
|
+
else if (name === "svg") dropped.add("inline SVG");
|
|
343
|
+
continue;
|
|
344
|
+
}
|
|
345
|
+
if (RAW_TEXT.has(name)) continue;
|
|
346
|
+
const attributes = closing ? {} : attrs(match[2] ?? "");
|
|
347
|
+
|
|
348
|
+
if (/^h[1-6]$/.test(name)) {
|
|
349
|
+
flush();
|
|
350
|
+
if (!closing) heading = Number(name[1]);
|
|
351
|
+
continue;
|
|
352
|
+
}
|
|
353
|
+
if (name === "ul" || name === "ol") {
|
|
354
|
+
if (!closing) {
|
|
355
|
+
if (listDepth === 0) {
|
|
356
|
+
flush();
|
|
357
|
+
const start = name === "ol" ? Number(attributes["start"]) : Number.NaN;
|
|
358
|
+
list = {
|
|
359
|
+
ordered: name === "ol",
|
|
360
|
+
...(Number.isInteger(start) && start > 1 && start <= 9999 ? { start } : {}),
|
|
361
|
+
items: [],
|
|
362
|
+
};
|
|
363
|
+
} else {
|
|
364
|
+
flushItem();
|
|
365
|
+
flattened = true;
|
|
366
|
+
}
|
|
367
|
+
listDepth++;
|
|
368
|
+
} else if (listDepth > 0) {
|
|
369
|
+
listDepth--;
|
|
370
|
+
flushItem();
|
|
371
|
+
if (listDepth === 0 && list) {
|
|
372
|
+
if (list.items.length > 0) push({ kind: "list", ...list });
|
|
373
|
+
list = undefined;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
continue;
|
|
377
|
+
}
|
|
378
|
+
if (name === "li") {
|
|
379
|
+
if (list && listDepth > 0) {
|
|
380
|
+
flushItem();
|
|
381
|
+
if (!closing) item = "";
|
|
382
|
+
} else if (!closing) {
|
|
383
|
+
flush();
|
|
384
|
+
}
|
|
385
|
+
continue;
|
|
386
|
+
}
|
|
387
|
+
if (name === "br") {
|
|
388
|
+
if (item !== undefined) item += " ";
|
|
389
|
+
else add("\n");
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
if (name === "hr") {
|
|
393
|
+
flush();
|
|
394
|
+
push({ kind: "divider" });
|
|
395
|
+
continue;
|
|
396
|
+
}
|
|
397
|
+
if (name === "img") {
|
|
398
|
+
const src = safeSrc(attributes["src"] ?? "");
|
|
399
|
+
if (!src) {
|
|
400
|
+
if (attributes["src"]) dropped.add("image with an unsupported address");
|
|
401
|
+
continue;
|
|
402
|
+
}
|
|
403
|
+
flush();
|
|
404
|
+
push({ kind: "image", src, alt: cleanText(attributes["alt"] ?? "").slice(0, 500) });
|
|
405
|
+
continue;
|
|
406
|
+
}
|
|
407
|
+
if (name === "iframe") {
|
|
408
|
+
if (closing) continue;
|
|
409
|
+
const url = embedVideoUrl(attributes["src"] ?? "");
|
|
410
|
+
if (url) {
|
|
411
|
+
flush();
|
|
412
|
+
push({ kind: "video", url });
|
|
413
|
+
} else {
|
|
414
|
+
dropped.add("embedded frame (iframe)");
|
|
415
|
+
}
|
|
416
|
+
continue;
|
|
417
|
+
}
|
|
418
|
+
if (name === "a") {
|
|
419
|
+
if (!closing) {
|
|
420
|
+
const href = safeHref(attributes["href"] ?? "");
|
|
421
|
+
linkDepth++;
|
|
422
|
+
hrefs.push(href);
|
|
423
|
+
if (href && linkDepth === 1) {
|
|
424
|
+
links.push({ href, text: "" });
|
|
425
|
+
} else if (attributes["href"] && !href) {
|
|
426
|
+
dropped.add("link with an unsafe address");
|
|
427
|
+
}
|
|
428
|
+
} else if (linkDepth > 0) {
|
|
429
|
+
linkDepth--;
|
|
430
|
+
hrefs.pop();
|
|
431
|
+
}
|
|
432
|
+
continue;
|
|
433
|
+
}
|
|
434
|
+
if (name === "table" || name === "thead" || name === "tbody") {
|
|
435
|
+
if (!closing && name === "table") {
|
|
436
|
+
flush();
|
|
437
|
+
dropped.add("table (its cells are kept as paragraphs)");
|
|
438
|
+
}
|
|
439
|
+
continue;
|
|
440
|
+
}
|
|
441
|
+
if (name === "td" || name === "th") {
|
|
442
|
+
add(" ");
|
|
443
|
+
continue;
|
|
444
|
+
}
|
|
445
|
+
if (BLOCK.has(name)) {
|
|
446
|
+
if (list && listDepth > 0) continue;
|
|
447
|
+
flush();
|
|
448
|
+
continue;
|
|
449
|
+
}
|
|
450
|
+
if (INLINE_FORMAT.has(name)) {
|
|
451
|
+
if (closing) {
|
|
452
|
+
const at = open.findLastIndex((entry) => entry.name === name);
|
|
453
|
+
if (at >= 0) open.splice(at, 1);
|
|
454
|
+
continue;
|
|
455
|
+
}
|
|
456
|
+
// W-950: bold, italic and colour are kept (as marks); other formatting is flattened.
|
|
457
|
+
const f = formatOf(name, attributes);
|
|
458
|
+
if (f) open.push({ name, f });
|
|
459
|
+
if (
|
|
460
|
+
(!f || item !== undefined) &&
|
|
461
|
+
(name !== "span" || attributes["style"] || attributes["class"])
|
|
462
|
+
)
|
|
463
|
+
flattened = true;
|
|
464
|
+
continue;
|
|
465
|
+
}
|
|
466
|
+
if (DROP_WITH_NOTE.has(name)) {
|
|
467
|
+
if (!closing) dropped.add(name);
|
|
468
|
+
continue;
|
|
469
|
+
}
|
|
470
|
+
// Any other tag: keep its text, drop the tag.
|
|
471
|
+
}
|
|
472
|
+
flush();
|
|
473
|
+
if (list && list.items.length > 0) {
|
|
474
|
+
flushItem();
|
|
475
|
+
push({ kind: "list", ...list });
|
|
476
|
+
}
|
|
477
|
+
return { blocks, flattened, dropped: [...dropped] };
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
const flat = (text: string) => text.replaceAll("\n", " ");
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* W-950: the bold, italic, coloured and linked words of a heading's or paragraph's HTML, as marks
|
|
484
|
+
* over `expected` (the plain text the importer made of it); none when they wouldn't line up.
|
|
485
|
+
*/
|
|
486
|
+
export function inlineMarks(
|
|
487
|
+
html: string,
|
|
488
|
+
safeHref: (url: string) => string | undefined,
|
|
489
|
+
expected: string,
|
|
490
|
+
): TextMark[] {
|
|
491
|
+
if (!/<[a-z]/i.test(html) || splitLong(expected).length > 1) return [];
|
|
492
|
+
const { blocks } = htmlToBlocks(html, safeHref, () => undefined);
|
|
493
|
+
const only = blocks.length === 1 ? blocks[0] : undefined;
|
|
494
|
+
if ((only?.kind === "text" || only?.kind === "heading") && only.marks) {
|
|
495
|
+
return flat(only.text) === flat(expected) ? only.marks : [];
|
|
496
|
+
}
|
|
497
|
+
if (only?.kind === "link" && flat(only.text) === flat(expected))
|
|
498
|
+
return [{ start: 0, end: expected.length, href: only.href }];
|
|
499
|
+
return [];
|
|
500
|
+
}
|