@perkyzz/emvb 1.0.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -0
- package/THIRD_PARTY_NOTICES.md +1 -0
- package/package.json +10 -9
- package/src/admin/agents-api.ts +5 -1
- package/src/admin/bulk-api.ts +67 -0
- package/src/admin/content-api.ts +39 -6
- package/src/admin/copy-rich-text.ts +119 -0
- package/src/admin/dialog-tab-wrap.ts +78 -0
- package/src/admin/editor/A11yCopilot.tsx +2 -0
- package/src/admin/editor/Editor.tsx +24 -7
- package/src/admin/editor/ScheduleDialog.tsx +167 -0
- package/src/admin/editor/Timeline.tsx +6 -2
- package/src/admin/editor/TopBar.tsx +60 -8
- package/src/admin/editor/canvas/CanvasFrame.tsx +7 -2
- package/src/admin/editor/canvas/DeviceBar.tsx +22 -9
- package/src/admin/editor/canvas/canvas-scale.ts +3 -3
- package/src/admin/editor/canvas/use-canvas-session.ts +2 -2
- package/src/admin/editor/dnd/new-element.ts +68 -0
- package/src/admin/editor/editor-chrome.tsx +16 -5
- package/src/admin/editor/editor-commands.ts +10 -4
- package/src/admin/editor/editor-css.ts +2 -1
- package/src/admin/editor/element-chrome.ts +22 -0
- package/src/admin/editor/panels/ElementPanel.tsx +34 -17
- package/src/admin/editor/panels/SettingsPanel.tsx +2 -2
- package/src/admin/editor/panels/settings/DraftTextField.tsx +33 -1
- package/src/admin/editor/panels/settings/EffectsControls.tsx +79 -1
- package/src/admin/editor/panels/settings/FieldBindControl.tsx +6 -1
- package/src/admin/editor/panels/settings/FieldControl.tsx +10 -1
- package/src/admin/editor/panels/settings/IconControls.tsx +11 -0
- package/src/admin/editor/panels/settings/IconPicker.tsx +4 -1
- package/src/admin/editor/panels/settings/ItemList.tsx +36 -2
- package/src/admin/editor/panels/settings/LengthRow.tsx +36 -3
- package/src/admin/editor/panels/settings/LoopItemBindControl.tsx +18 -0
- package/src/admin/editor/panels/settings/StyleRow.tsx +7 -0
- package/src/admin/editor/panels/settings/advanced-rules.ts +3 -0
- package/src/admin/editor/panels/settings/length-units.ts +15 -3
- package/src/admin/editor/panels/settings/style-sections.ts +1 -0
- package/src/admin/editor/panels/style-target.ts +17 -4
- package/src/admin/editor/section-templates.ts +26 -4
- package/src/admin/editor/store.ts +15 -3
- package/src/admin/editor/useEditorCommands.ts +7 -5
- package/src/admin/editor/useEditorData.ts +4 -0
- package/src/admin/editor/useNodeActions.ts +4 -2
- package/src/admin/editor/useSave.ts +47 -3
- package/src/admin/export-api.ts +238 -0
- package/src/admin/index.tsx +6 -1
- package/src/admin/menus-api.ts +113 -0
- package/src/admin/pages/ExportDialog.tsx +395 -0
- package/src/admin/pages/ImportElementorDialog.tsx +25 -1
- package/src/admin/pages/PageList.tsx +67 -2
- package/src/admin/pages/ThemePartList.tsx +37 -1
- package/src/admin/pages/bulk.tsx +303 -0
- package/src/admin/search.ts +20 -0
- package/src/admin/theme-api.ts +4 -0
- package/src/admin/ui.ts +11 -2
- package/src/admin/widgets/CanvasField.tsx +8 -0
- package/src/admin/widgets/ConvertToRichText.tsx +136 -0
- package/src/admin/widgets/ExportEntryHtml.tsx +92 -0
- package/src/admin/widgets/convert-body.ts +73 -2
- package/src/astro/EmVBPage.astro +3 -0
- package/src/astro/render.ts +47 -22
- package/src/astro/request-data.ts +3 -0
- package/src/astro/resolve-theme.ts +88 -51
- package/src/astro/tabs/runtime.ts +16 -6
- package/src/astro/visitor.ts +23 -4
- package/src/astro/widgets/EmVBAlertsRuntime.astro +8 -0
- package/src/astro/widgets/EmVBCarouselRuntime.astro +8 -0
- package/src/astro/widgets/EmVBLightboxRuntime.astro +8 -0
- package/src/astro/widgets/EmVBWidgetRuntimes.astro +19 -0
- package/src/astro/widgets/alerts.ts +37 -0
- package/src/astro/widgets/carousel.ts +167 -0
- package/src/astro/widgets/index.ts +1 -0
- package/src/astro/widgets/lightbox.ts +123 -0
- package/src/constants.ts +1 -1
- package/src/core/a11y/audit.ts +103 -3
- package/src/core/arrange.ts +57 -1
- package/src/core/bounded-array.ts +16 -13
- package/src/core/contract.ts +26 -3
- package/src/core/css/generate.ts +83 -21
- package/src/core/css/scope.ts +10 -2
- package/src/core/css/trace.ts +33 -8
- package/src/core/design/classes.ts +25 -5
- package/src/core/design/devices.ts +78 -2
- package/src/core/design/variables.ts +14 -1
- package/src/core/elements/icon-svg.ts +135 -0
- package/src/core/elements/index.ts +33 -127
- package/src/core/elements/widgets.ts +907 -0
- package/src/core/export/runtimes.generated.ts +12 -0
- package/src/core/export/site.ts +579 -0
- package/src/core/export/zip.ts +95 -0
- package/src/core/extensions/schema.ts +2 -1
- package/src/core/geo-trust.ts +18 -0
- package/src/core/history/diff.ts +25 -0
- package/src/core/icons/social.generated.ts +29 -0
- package/src/core/image-edit/schema.ts +2 -1
- package/src/core/import/elementor/atomic.ts +22 -9
- package/src/core/import/elementor/context.ts +18 -0
- package/src/core/import/elementor/index.ts +72 -11
- package/src/core/import/elementor/style.ts +21 -1
- package/src/core/import/elementor/widgets.ts +394 -133
- package/src/core/index.ts +14 -0
- package/src/core/issue-cap.ts +19 -0
- package/src/core/json-schema.ts +8 -1
- package/src/core/limits.ts +6 -0
- package/src/core/loop-collections.ts +33 -0
- package/src/core/perf/meter.ts +37 -19
- package/src/core/portable-text-export.ts +243 -0
- package/src/core/proposals.ts +8 -2
- package/src/core/public-attributes.ts +30 -0
- package/src/core/render/index.ts +115 -13
- package/src/core/render/tabs.ts +18 -11
- package/src/core/render/vnode.ts +44 -6
- package/src/core/sanitize/css.ts +26 -0
- package/src/core/sanitize/svg-upload.ts +276 -23
- package/src/core/sanitize/svg.ts +3 -3
- package/src/core/schema/descriptors.ts +4 -0
- package/src/core/schema/design.ts +8 -7
- package/src/core/schema/layout.ts +406 -18
- package/src/core/schema/style.ts +49 -1
- package/src/core/search-text.ts +70 -0
- package/src/core/shadowing-ids.ts +41 -0
- package/src/core/theme/conditions.ts +2 -1
- package/src/core/theme/float.ts +1 -0
- package/src/core/theme/parse-doc.ts +10 -5
- package/src/core/theme/popup-rules.ts +16 -0
- package/src/core/theme/triggers.ts +3 -2
- package/src/core/validate.ts +3 -2
- package/src/index.ts +23 -2
- package/src/mcp/ports.ts +18 -7
- package/src/mcp/proposals.ts +36 -11
- package/src/mcp/read.ts +2 -3
- package/src/mcp/tools.ts +1 -1
- package/src/server/agent-settings.ts +12 -1
- package/src/server/hooks.ts +41 -6
- package/src/server/icon-routes.ts +52 -12
- package/src/server/mcp-routes.ts +11 -5
- package/src/server/plugin.ts +27 -2
- package/src/server/presence.ts +5 -1
- package/src/server/proposals.ts +16 -4
- package/src/server/requests.ts +60 -11
- package/src/server/site-origin.ts +13 -0
- package/src/server/targets.ts +26 -0
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import {
|
|
2
|
+
buildSiteExport,
|
|
3
|
+
withCopiedMedia,
|
|
4
|
+
type ExportPageInput,
|
|
5
|
+
type ExportPartInput,
|
|
6
|
+
type SiteExport,
|
|
7
|
+
} from "../core/export/site.ts";
|
|
8
|
+
import { zipFiles } from "../core/export/zip.ts";
|
|
9
|
+
import { PAGES_COLLECTION, THEME_PARTS_COLLECTION } from "../constants.ts";
|
|
10
|
+
import { listAllItems, type Fetcher } from "./api.ts";
|
|
11
|
+
import { titleOf, type ContentItem } from "./content-item.ts";
|
|
12
|
+
import { loadDesign } from "./editor/useEditorData.ts";
|
|
13
|
+
import { loadHostCollections } from "./setup/host-collections.ts";
|
|
14
|
+
import { readCollection } from "./setup/run.ts";
|
|
15
|
+
import { readFieldLayout } from "./widgets/field-fetcher.ts";
|
|
16
|
+
|
|
17
|
+
/** Only what visitors see: published entries (a saved draft on top stays out). */
|
|
18
|
+
const isLive = (item: ContentItem) => item.status === "published";
|
|
19
|
+
|
|
20
|
+
/** A canvas field holding a layout with at least one element. */
|
|
21
|
+
const hasLayout = (value: unknown) => {
|
|
22
|
+
const layout = readFieldLayout(value);
|
|
23
|
+
return !!layout && (layout.root.children ?? []).length > 0;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* W-977: one of your collections whose entries carry an EmVB layout (an `emvb:canvas` field), and
|
|
28
|
+
* where its entries are on the site.
|
|
29
|
+
*/
|
|
30
|
+
export type CanvasSource = {
|
|
31
|
+
collection: string;
|
|
32
|
+
label: string;
|
|
33
|
+
field: string;
|
|
34
|
+
urlPattern?: string | null;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/** Published entries of `source` with a layout, for picking one to export (W-977). */
|
|
38
|
+
export async function exportableEntries(
|
|
39
|
+
fetcher: Fetcher,
|
|
40
|
+
source: CanvasSource,
|
|
41
|
+
): Promise<{ id: string; title: string }[]> {
|
|
42
|
+
const items = await listAllItems<ContentItem>(
|
|
43
|
+
fetcher,
|
|
44
|
+
`/_emdash/api/content/${source.collection}`,
|
|
45
|
+
);
|
|
46
|
+
return items
|
|
47
|
+
.filter((item) => isLive(item) && hasLayout(item.data?.[source.field]))
|
|
48
|
+
.map((item) => ({ id: item.id, title: titleOf(item, "Untitled entry") }))
|
|
49
|
+
.toSorted((a, b) => a.title.localeCompare(b.title));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Collections with a canvas field, to export their entries like pages. */
|
|
53
|
+
export async function canvasSources(fetcher: Fetcher): Promise<CanvasSource[]> {
|
|
54
|
+
const { rows } = await loadHostCollections(fetcher);
|
|
55
|
+
const ready = rows.filter((row) => row.field === "ready" && row.fieldSlug);
|
|
56
|
+
const states = await Promise.all(ready.map((row) => readCollection(fetcher, row.slug)));
|
|
57
|
+
return ready.map((row, i) => ({
|
|
58
|
+
collection: row.slug,
|
|
59
|
+
label: row.label,
|
|
60
|
+
field: row.fieldSlug as string,
|
|
61
|
+
urlPattern: states[i]?.urlPattern ?? null,
|
|
62
|
+
}));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* An entry's public path from its collection's URL pattern (`/blog/{slug}`, `{id}` too). Without
|
|
67
|
+
* a pattern EmDash serves entries at `/<collection>/<slug>` (W-980).
|
|
68
|
+
*/
|
|
69
|
+
export function entryPath(
|
|
70
|
+
pattern: string | null | undefined,
|
|
71
|
+
entry: { collection: string; slug: string; id: string },
|
|
72
|
+
): string {
|
|
73
|
+
const path = (pattern || `/${entry.collection}/{slug}`)
|
|
74
|
+
.replaceAll("{slug}", encodeURIComponent(entry.slug))
|
|
75
|
+
.replaceAll("{id}", encodeURIComponent(entry.id));
|
|
76
|
+
return path.startsWith("/") ? path : `/${path}`;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Published pages (or one), published theme parts and the published Site styles. */
|
|
80
|
+
export async function loadExportData(
|
|
81
|
+
fetcher: Fetcher,
|
|
82
|
+
/** One id, or (W-978) the ticked ones. */
|
|
83
|
+
onlyPageId?: string | readonly string[],
|
|
84
|
+
/** W-977: export this collection's entries (their canvas layouts) instead of EmVB pages. */
|
|
85
|
+
source?: CanvasSource,
|
|
86
|
+
) {
|
|
87
|
+
const [pageItems, partItems, design] = await Promise.all([
|
|
88
|
+
listAllItems<ContentItem>(
|
|
89
|
+
fetcher,
|
|
90
|
+
`/_emdash/api/content/${source ? source.collection : PAGES_COLLECTION}`,
|
|
91
|
+
),
|
|
92
|
+
listAllItems<ContentItem>(fetcher, `/_emdash/api/content/${THEME_PARTS_COLLECTION}`),
|
|
93
|
+
loadDesign(fetcher),
|
|
94
|
+
]);
|
|
95
|
+
const pages: ExportPageInput[] = pageItems
|
|
96
|
+
.filter(
|
|
97
|
+
(item) =>
|
|
98
|
+
isLive(item) &&
|
|
99
|
+
(!onlyPageId ||
|
|
100
|
+
(typeof onlyPageId === "string" ? item.id === onlyPageId : onlyPageId.includes(item.id))),
|
|
101
|
+
)
|
|
102
|
+
// An entry without a layout shows the site's own template, which an export can't hold.
|
|
103
|
+
.filter((item) => !source || hasLayout(item.data?.[source.field]))
|
|
104
|
+
.map((item) => {
|
|
105
|
+
const slug = item.slug ?? "";
|
|
106
|
+
const page: ExportPageInput = {
|
|
107
|
+
id: item.id,
|
|
108
|
+
title: titleOf(item, source ? "Untitled entry" : "Untitled page"),
|
|
109
|
+
slug,
|
|
110
|
+
layout: source ? readFieldLayout(item.data?.[source.field]) : item.data?.["layout"],
|
|
111
|
+
};
|
|
112
|
+
if (source) {
|
|
113
|
+
page.collection = source.collection;
|
|
114
|
+
page.path = entryPath(source.urlPattern, {
|
|
115
|
+
collection: source.collection,
|
|
116
|
+
slug,
|
|
117
|
+
id: item.id,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
if (item.seo?.description) page.description = item.seo.description;
|
|
121
|
+
return page;
|
|
122
|
+
})
|
|
123
|
+
.toSorted((a, b) => a.title.localeCompare(b.title));
|
|
124
|
+
const parts: ExportPartInput[] = partItems.filter(isLive).map((item) => ({
|
|
125
|
+
id: item.id,
|
|
126
|
+
title: titleOf(item, ""),
|
|
127
|
+
partType: item.data?.["part_type"],
|
|
128
|
+
layout: item.data?.["layout"],
|
|
129
|
+
conditions: item.data?.["conditions"],
|
|
130
|
+
triggers: item.data?.["triggers"],
|
|
131
|
+
float: item.data?.["float"],
|
|
132
|
+
updatedAt: item.updatedAt ?? "",
|
|
133
|
+
}));
|
|
134
|
+
return { pages, parts, design: design.published ?? design.design };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const MEDIA_LIBRARY = "/_emdash/api/media/file/";
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Media this site serves itself (same origin), which the export can copy. S-06 (W-996): nothing
|
|
141
|
+
* under /_emdash/ but the media library's files, so an API answer can't end up in the zip.
|
|
142
|
+
*/
|
|
143
|
+
export function copyableMedia(urls: readonly string[], origin: string): string[] {
|
|
144
|
+
return urls.filter((url) => {
|
|
145
|
+
try {
|
|
146
|
+
const parsed = new URL(url, origin);
|
|
147
|
+
if (parsed.origin !== origin) return false;
|
|
148
|
+
return !parsed.pathname.startsWith("/_emdash/") || parsed.pathname.startsWith(MEDIA_LIBRARY);
|
|
149
|
+
} catch {
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** S-06: what a copied file may be; anything else (JSON, HTML) is left as a link. */
|
|
156
|
+
const MEDIA_TYPE = /^(?:image|video|audio|font)\/|^application\/(?:pdf|font-woff2?|x-font-woff)\b/i;
|
|
157
|
+
|
|
158
|
+
/** S-06: the media library needs the session cookie; public files are fetched without one. */
|
|
159
|
+
export const mediaRequest = (url: string, origin: string): RequestInit => ({
|
|
160
|
+
credentials: new URL(url, origin).pathname.startsWith(MEDIA_LIBRARY) ? "same-origin" : "omit",
|
|
161
|
+
redirect: "error",
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
/** Up to this many media files are copied, so an export can't fill the browser's memory. */
|
|
165
|
+
const MAX_COPIED_MEDIA = 500;
|
|
166
|
+
const MAX_COPIED_BYTES = 200 * 1024 * 1024;
|
|
167
|
+
|
|
168
|
+
/** Downloads media for the export; files that fail are left as links (and counted). */
|
|
169
|
+
async function fetchMedia(
|
|
170
|
+
urls: readonly string[],
|
|
171
|
+
origin: string,
|
|
172
|
+
get: (url: string) => Promise<Response>,
|
|
173
|
+
): Promise<{ copies: Map<string, Uint8Array>; failed: number }> {
|
|
174
|
+
const copies = new Map<string, Uint8Array>();
|
|
175
|
+
let failed = 0;
|
|
176
|
+
let total = 0;
|
|
177
|
+
for (const url of urls.slice(0, MAX_COPIED_MEDIA)) {
|
|
178
|
+
try {
|
|
179
|
+
// oxlint-disable-next-line no-await-in-loop -- one at a time keeps memory and load low
|
|
180
|
+
const response = await get(new URL(url, origin).href);
|
|
181
|
+
if (!response.ok) throw new Error(String(response.status));
|
|
182
|
+
if (!MEDIA_TYPE.test(response.headers.get("content-type") ?? "")) throw new Error("type");
|
|
183
|
+
// oxlint-disable-next-line no-await-in-loop -- same
|
|
184
|
+
const bytes = new Uint8Array(await response.arrayBuffer());
|
|
185
|
+
total += bytes.length;
|
|
186
|
+
if (total > MAX_COPIED_BYTES) {
|
|
187
|
+
failed += 1;
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
copies.set(url, bytes);
|
|
191
|
+
} catch {
|
|
192
|
+
failed += 1;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
failed += Math.max(0, urls.length - MAX_COPIED_MEDIA);
|
|
196
|
+
return { copies, failed };
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export type ExportResult = {
|
|
200
|
+
zip: Uint8Array;
|
|
201
|
+
site: SiteExport;
|
|
202
|
+
copied: number;
|
|
203
|
+
failed: number;
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
/** The whole export as a zip, built in the browser from published content. */
|
|
207
|
+
export async function exportSite(
|
|
208
|
+
fetcher: Fetcher,
|
|
209
|
+
options: {
|
|
210
|
+
onlyPageId?: string | readonly string[];
|
|
211
|
+
/** W-977: a collection with a canvas field instead of EmVB pages. */
|
|
212
|
+
source?: CanvasSource;
|
|
213
|
+
copyMedia: boolean;
|
|
214
|
+
origin: string;
|
|
215
|
+
get?: (url: string) => Promise<Response>;
|
|
216
|
+
date?: string;
|
|
217
|
+
},
|
|
218
|
+
): Promise<ExportResult> {
|
|
219
|
+
const data = await loadExportData(fetcher, options.onlyPageId, options.source);
|
|
220
|
+
let site = buildSiteExport({
|
|
221
|
+
...data,
|
|
222
|
+
...(options.date ? { date: options.date } : {}),
|
|
223
|
+
});
|
|
224
|
+
let copied = 0;
|
|
225
|
+
let failed = 0;
|
|
226
|
+
if (options.copyMedia) {
|
|
227
|
+
const get = options.get ?? ((url: string) => fetch(url, mediaRequest(url, options.origin)));
|
|
228
|
+
const result = await fetchMedia(
|
|
229
|
+
copyableMedia(site.report.media, options.origin),
|
|
230
|
+
options.origin,
|
|
231
|
+
get,
|
|
232
|
+
);
|
|
233
|
+
site = withCopiedMedia(site, result.copies);
|
|
234
|
+
copied = result.copies.size;
|
|
235
|
+
failed = result.failed;
|
|
236
|
+
}
|
|
237
|
+
return { zip: zipFiles(site.files), site, copied, failed };
|
|
238
|
+
}
|
package/src/admin/index.tsx
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { PluginAdminExports } from "emdash";
|
|
2
2
|
import { installSidebarShim } from "./access/sidebar-shim.ts";
|
|
3
3
|
import { defaultFetcher } from "./api.ts";
|
|
4
|
+
import { installDialogTabWrap } from "./dialog-tab-wrap.ts";
|
|
4
5
|
import { EditorPage } from "./pages/EditorPage.tsx";
|
|
5
6
|
import { PagesPage } from "./pages/PagesPage.tsx";
|
|
6
7
|
import { AgentsPage } from "./pages/AgentsPage.tsx";
|
|
@@ -9,7 +10,11 @@ import { ThemeBuilderPage } from "./pages/ThemeBuilderPage.tsx";
|
|
|
9
10
|
import { CanvasField } from "./widgets/CanvasField.tsx";
|
|
10
11
|
import { LayoutField } from "./widgets/LayoutField.tsx";
|
|
11
12
|
|
|
12
|
-
if (typeof document !== "undefined")
|
|
13
|
+
if (typeof document !== "undefined") {
|
|
14
|
+
void installSidebarShim(document, defaultFetcher);
|
|
15
|
+
// W-984: Tab stays inside EmVB dialogs.
|
|
16
|
+
installDialogTabWrap(document);
|
|
17
|
+
}
|
|
13
18
|
|
|
14
19
|
/** `/pages`, `/theme`, `/agents` and `/collections` are declared; `/editor` renders by URL (D-024). */
|
|
15
20
|
export const pages: PluginAdminExports["pages"] = {
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* W-1026: the site's own menus (EmDash's, which its WordPress import fills) as the links an
|
|
3
|
+
* imported Elementor Nav Menu needs. Menus need `menus:read`; a link to a page or post is
|
|
4
|
+
* resolved to its address through the content API.
|
|
5
|
+
*/
|
|
6
|
+
import type { MenuLink } from "../core/import/elementor/context.ts";
|
|
7
|
+
import { requestJson, type Fetcher } from "./api.ts";
|
|
8
|
+
|
|
9
|
+
type MenuSummary = { name: string; label?: string | null };
|
|
10
|
+
type MenuItem = {
|
|
11
|
+
id: string;
|
|
12
|
+
parentId: string | null;
|
|
13
|
+
sortOrder: number;
|
|
14
|
+
type: string;
|
|
15
|
+
referenceCollection: string | null;
|
|
16
|
+
referenceId: string | null;
|
|
17
|
+
customUrl: string | null;
|
|
18
|
+
label: string;
|
|
19
|
+
target: string | null;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/** A menu name as WordPress writes its slug: lower case, words joined by hyphens. */
|
|
23
|
+
export const menuKey = (name: string) =>
|
|
24
|
+
name
|
|
25
|
+
.toLowerCase()
|
|
26
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
27
|
+
.replace(/^-+|-+$/g, "");
|
|
28
|
+
|
|
29
|
+
/** Where a menu item goes: its own address, or the page or post it points at. */
|
|
30
|
+
async function itemHref(
|
|
31
|
+
fetcher: Fetcher,
|
|
32
|
+
item: MenuItem,
|
|
33
|
+
slugs: Map<string, Promise<string | undefined>>,
|
|
34
|
+
): Promise<string | undefined> {
|
|
35
|
+
if (item.customUrl) return item.customUrl;
|
|
36
|
+
if (!item.referenceCollection || !item.referenceId) return undefined;
|
|
37
|
+
const key = `${item.referenceCollection}/${item.referenceId}`;
|
|
38
|
+
let slug = slugs.get(key);
|
|
39
|
+
if (!slug) {
|
|
40
|
+
slug = requestJson<{ item?: { slug?: string | null } }>(
|
|
41
|
+
fetcher,
|
|
42
|
+
`/_emdash/api/content/${encodeURIComponent(item.referenceCollection)}/${encodeURIComponent(item.referenceId)}`,
|
|
43
|
+
).then(
|
|
44
|
+
(data) => data.item?.slug ?? undefined,
|
|
45
|
+
() => undefined,
|
|
46
|
+
);
|
|
47
|
+
slugs.set(key, slug);
|
|
48
|
+
}
|
|
49
|
+
const found = await slug;
|
|
50
|
+
if (found === undefined) return undefined;
|
|
51
|
+
return item.referenceCollection === "pages"
|
|
52
|
+
? `/${found}`
|
|
53
|
+
: `/${item.referenceCollection}/${found}`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** A menu's flat items as nested links, in the menu's order. */
|
|
57
|
+
async function menuLinks(fetcher: Fetcher, items: readonly MenuItem[]): Promise<MenuLink[]> {
|
|
58
|
+
const slugs = new Map<string, Promise<string | undefined>>();
|
|
59
|
+
const hrefs = new Map(
|
|
60
|
+
await Promise.all(
|
|
61
|
+
items.map(async (item) => [item.id, await itemHref(fetcher, item, slugs)] as const),
|
|
62
|
+
),
|
|
63
|
+
);
|
|
64
|
+
const under = (parent: string | null): MenuLink[] =>
|
|
65
|
+
items
|
|
66
|
+
.filter((item) => item.parentId === parent)
|
|
67
|
+
.toSorted((a, b) => a.sortOrder - b.sortOrder)
|
|
68
|
+
.map((item) => {
|
|
69
|
+
const link: { -readonly [K in keyof MenuLink]: MenuLink[K] } = { text: item.label };
|
|
70
|
+
const href = hrefs.get(item.id);
|
|
71
|
+
const children = under(item.id);
|
|
72
|
+
if (href) link.href = href;
|
|
73
|
+
if (item.target === "_blank") link.newTab = true;
|
|
74
|
+
if (children.length > 0) link.children = children;
|
|
75
|
+
return link;
|
|
76
|
+
});
|
|
77
|
+
return under(null);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The links of each wanted menu the site has, by the wanted name. A site menu matches by its
|
|
82
|
+
* name or label written as a slug. Menus the site doesn't have (or can't be read) are left out.
|
|
83
|
+
*/
|
|
84
|
+
export async function loadSiteMenus(
|
|
85
|
+
fetcher: Fetcher,
|
|
86
|
+
wanted: readonly string[],
|
|
87
|
+
): Promise<Map<string, MenuLink[]>> {
|
|
88
|
+
const found = new Map<string, MenuLink[]>();
|
|
89
|
+
if (wanted.length === 0) return found;
|
|
90
|
+
let menus: MenuSummary[];
|
|
91
|
+
try {
|
|
92
|
+
menus = await requestJson<MenuSummary[]>(fetcher, "/_emdash/api/menus");
|
|
93
|
+
} catch {
|
|
94
|
+
return found;
|
|
95
|
+
}
|
|
96
|
+
await Promise.all(
|
|
97
|
+
wanted.map(async (name) => {
|
|
98
|
+
const key = menuKey(name);
|
|
99
|
+
const menu = menus.find((m) => menuKey(m.name) === key || menuKey(m.label ?? "") === key);
|
|
100
|
+
if (!menu) return;
|
|
101
|
+
try {
|
|
102
|
+
const full = await requestJson<{ items?: MenuItem[] }>(
|
|
103
|
+
fetcher,
|
|
104
|
+
`/_emdash/api/menus/${encodeURIComponent(menu.name)}`,
|
|
105
|
+
);
|
|
106
|
+
found.set(name, await menuLinks(fetcher, full.items ?? []));
|
|
107
|
+
} catch {
|
|
108
|
+
// Unreadable: the import leaves that Menu empty and says so.
|
|
109
|
+
}
|
|
110
|
+
}),
|
|
111
|
+
);
|
|
112
|
+
return found;
|
|
113
|
+
}
|