@nulib/canopy 0.0.0-stage → 2.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/bin/canopy.js +16 -0
- package/lib/AGENTS.md +107 -0
- package/lib/base-path.js +67 -0
- package/lib/build/appearance.js +76 -0
- package/lib/build/assets.js +30 -0
- package/lib/build/build.js +350 -0
- package/lib/build/dev.js +1355 -0
- package/lib/build/devtoast.config.json +6 -0
- package/lib/build/devtoast.css +14 -0
- package/lib/build/facets.js +19 -0
- package/lib/build/fonts.js +105 -0
- package/lib/build/iiif.js +2990 -0
- package/lib/build/log.js +31 -0
- package/lib/build/mdx.js +1967 -0
- package/lib/build/package-names.js +111 -0
- package/lib/build/pages.js +420 -0
- package/lib/build/rehype-task-list-labels.js +54 -0
- package/lib/build/runtimes.js +141 -0
- package/lib/build/search-index.js +55 -0
- package/lib/build/search-text.js +160 -0
- package/lib/build/search-workflow.js +72 -0
- package/lib/build/search.js +302 -0
- package/lib/build/sitemap.js +197 -0
- package/lib/build/styles.js +140 -0
- package/lib/build/theme-tokens.js +78 -0
- package/lib/build/ui-styles.js +25 -0
- package/lib/build/upgrade-notices.js +121 -0
- package/lib/cli.js +64 -0
- package/lib/common.js +580 -0
- package/lib/components/bibliography.js +197 -0
- package/lib/components/featured.js +237 -0
- package/lib/components/hero-slider-runtime.js +215 -0
- package/lib/components/image-story-runtime-entry.js +249 -0
- package/lib/components/map-runtime.js +114 -0
- package/lib/components/metadata-index.js +32 -0
- package/lib/components/nav-place.js +227 -0
- package/lib/components/navigation.js +381 -0
- package/lib/components/referenced.js +217 -0
- package/lib/components/slider-runtime-entry.js +106 -0
- package/lib/components/timeline-runtime.js +80 -0
- package/lib/components/viewer-runtime-entry.js +88 -0
- package/lib/config-path.js +78 -0
- package/lib/default-locale.js +113 -0
- package/lib/discovery/browser-loader.js +107 -0
- package/lib/discovery/browser.js +115 -0
- package/lib/discovery/config.js +25 -0
- package/lib/discovery/envelope.js +124 -0
- package/lib/discovery/export.js +226 -0
- package/lib/discovery/ingestion.js +62 -0
- package/lib/discovery/pagination.js +66 -0
- package/lib/discovery/provenance.js +79 -0
- package/lib/discovery/search.js +81 -0
- package/lib/discovery/tools.js +155 -0
- package/lib/discovery/validation.js +121 -0
- package/lib/head.js +154 -0
- package/lib/iiif/thumbnail.js +402 -0
- package/lib/index.js +9 -0
- package/lib/locale-runtime.js +69 -0
- package/lib/locales.js +203 -0
- package/lib/orchestrator.js +231 -0
- package/lib/page-context.js +24 -0
- package/lib/search/bundle.js +25 -0
- package/lib/search/data.js +202 -0
- package/lib/search/engine.js +86 -0
- package/lib/search/index-cache.js +95 -0
- package/lib/search/search-app.jsx +835 -0
- package/lib/search/search-engine-runtime.js +44 -0
- package/lib/search/search-form-runtime.js +462 -0
- package/lib/search/search.js +676 -0
- package/package.json +111 -4
- package/types/base-path.d.ts +4 -0
- package/types/orchestrator.d.ts +17 -0
- package/ui/dist/index.mjs +7358 -0
- package/ui/dist/index.mjs.map +7 -0
- package/ui/dist/server.mjs +12731 -0
- package/ui/dist/server.mjs.map +7 -0
- package/ui/fonts/OFL.txt +98 -0
- package/ui/fonts/google-sans-flex-latin-ext.woff2 +0 -0
- package/ui/fonts/google-sans-flex-latin.woff2 +0 -0
- package/ui/fonts/source-serif-4-italic-latin-ext.woff2 +0 -0
- package/ui/fonts/source-serif-4-italic-latin.woff2 +0 -0
- package/ui/fonts/source-serif-4-latin-ext.woff2 +0 -0
- package/ui/fonts/source-serif-4-latin.woff2 +0 -0
- package/ui/styles/base/_common.scss +97 -0
- package/ui/styles/base/_heading.scss +61 -0
- package/ui/styles/base/_markdown.scss +188 -0
- package/ui/styles/base/_type.scss +51 -0
- package/ui/styles/base/_utilities.scss +7 -0
- package/ui/styles/base/index.scss +5 -0
- package/ui/styles/components/_article-card.scss +108 -0
- package/ui/styles/components/_band.scss +104 -0
- package/ui/styles/components/_bibliography.scss +88 -0
- package/ui/styles/components/_buttons.scss +143 -0
- package/ui/styles/components/_card.scss +109 -0
- package/ui/styles/components/_diagram.scss +76 -0
- package/ui/styles/components/_feature.scss +66 -0
- package/ui/styles/components/_footer.scss +136 -0
- package/ui/styles/components/_gallery.scss +476 -0
- package/ui/styles/components/_image-story.scss +56 -0
- package/ui/styles/components/_interstitial-hero.scss +625 -0
- package/ui/styles/components/_layout.scss +83 -0
- package/ui/styles/components/_map.scss +296 -0
- package/ui/styles/components/_metadata-index.scss +68 -0
- package/ui/styles/components/_nav-tree.scss +80 -0
- package/ui/styles/components/_referenced-items.scss +69 -0
- package/ui/styles/components/_sub-navigation.scss +208 -0
- package/ui/styles/components/_swoosh.scss +50 -0
- package/ui/styles/components/_timeline.scss +350 -0
- package/ui/styles/components/_work.scss +71 -0
- package/ui/styles/components/header/_header.scss +294 -0
- package/ui/styles/components/header/_logo.scss +99 -0
- package/ui/styles/components/header/_navbar.scss +143 -0
- package/ui/styles/components/header/index.scss +3 -0
- package/ui/styles/components/iiif/_image.scss +32 -0
- package/ui/styles/components/iiif/_slider.scss +5 -0
- package/ui/styles/components/iiif/index.scss +2 -0
- package/ui/styles/components/index.scss +25 -0
- package/ui/styles/components/modal/_modal.scss +130 -0
- package/ui/styles/components/modal/index.scss +1 -0
- package/ui/styles/components/search/_filters.scss +259 -0
- package/ui/styles/components/search/_form.scss +187 -0
- package/ui/styles/components/search/_panel.scss +7 -0
- package/ui/styles/components/search/_results.scss +241 -0
- package/ui/styles/components/search/_tabs.scss +60 -0
- package/ui/styles/components/search/index.scss +4 -0
- package/ui/styles/index.css +5007 -0
- package/ui/styles/index.scss +11 -0
- package/ui/styles/settings/_breakpoints.scss +7 -0
- package/ui/styles/settings/_focus.scss +15 -0
- package/ui/styles/settings/_tokens.scss +170 -0
- package/ui/styles/settings/_type.scss +126 -0
- package/ui/styles/version-parity.css +116 -0
- package/ui/tailwind-canopy-iiif-plugin.js +39 -0
- package/ui/tailwind-canopy-iiif-preset.js +32 -0
- package/ui/tailwind-config.d.ts +29 -0
- package/ui/tailwind-config.js +159 -0
- package/ui/tailwind-default.config.mjs +4 -0
- package/ui/theme-color.js +130 -0
- package/ui/theme-palette.js +337 -0
- package/ui/theme.js +153 -0
- package/README.md +0 -3
|
@@ -0,0 +1,2990 @@
|
|
|
1
|
+
const { rememberRetrieved, inheritProvenance, saveProvenance } = require('../discovery/provenance');
|
|
2
|
+
const { createIngestionReport } = require('../discovery/ingestion');
|
|
3
|
+
const React = require("react");
|
|
4
|
+
const ReactDOMServer = require("react-dom/server");
|
|
5
|
+
const crypto = require("crypto");
|
|
6
|
+
const slugify = require("slugify");
|
|
7
|
+
const yaml = require("js-yaml");
|
|
8
|
+
const {
|
|
9
|
+
fs,
|
|
10
|
+
fsp,
|
|
11
|
+
path,
|
|
12
|
+
OUT_DIR,
|
|
13
|
+
CONTENT_DIR,
|
|
14
|
+
ensureDirSync,
|
|
15
|
+
htmlShell,
|
|
16
|
+
rootRelativeHref,
|
|
17
|
+
canopyBodyClassForType,
|
|
18
|
+
readSiteMetadata,
|
|
19
|
+
readPrimaryNavigation,
|
|
20
|
+
withBase,
|
|
21
|
+
resolveLocaleFromHref,
|
|
22
|
+
getLocaleRouteEntries,
|
|
23
|
+
getDefaultRoute,
|
|
24
|
+
buildRouteRelativePath,
|
|
25
|
+
getLocaleRouteConfig,
|
|
26
|
+
getDefaultLocaleCode,
|
|
27
|
+
} = require("../common");
|
|
28
|
+
const {
|
|
29
|
+
readCanopyLocalesWithMessages,
|
|
30
|
+
readLocaleMessages,
|
|
31
|
+
buildLocaleRuntimeScript,
|
|
32
|
+
} = require("../locales");
|
|
33
|
+
const {resolveCanopyConfigPath} = require("../config-path");
|
|
34
|
+
const mdx = require("./mdx");
|
|
35
|
+
const {log, logLine, logResponse} = require("./log");
|
|
36
|
+
const {getPageContext} = require("../page-context");
|
|
37
|
+
const PageContext = getPageContext();
|
|
38
|
+
const referenced = require("../components/referenced");
|
|
39
|
+
const navPlace = require("../components/nav-place");
|
|
40
|
+
const navigation = require("../components/navigation.js");
|
|
41
|
+
const {
|
|
42
|
+
getThumbnail,
|
|
43
|
+
getRepresentativeImage,
|
|
44
|
+
buildIiifImageUrlFromService,
|
|
45
|
+
buildIiifImageUrlForDimensions,
|
|
46
|
+
findPrimaryCanvasImage,
|
|
47
|
+
buildIiifImageSrcset,
|
|
48
|
+
isLevel0Service,
|
|
49
|
+
} = require("../iiif/thumbnail");
|
|
50
|
+
|
|
51
|
+
const IIIF_CACHE_DIR = path.resolve(".cache/iiif");
|
|
52
|
+
const IIIF_CACHE_MANIFESTS_DIR = path.join(IIIF_CACHE_DIR, "manifests");
|
|
53
|
+
const IIIF_CACHE_COLLECTIONS_DIR = path.join(IIIF_CACHE_DIR, "collections");
|
|
54
|
+
const IIIF_CACHE_COLLECTION = path.join(IIIF_CACHE_DIR, "collection.json");
|
|
55
|
+
const IIIF_METADATA_INDEX_CACHE = path.join(
|
|
56
|
+
IIIF_CACHE_DIR,
|
|
57
|
+
"metadata-index.json",
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
function relativeRuntimeScript(outPath, filename, versioned = false) {
|
|
61
|
+
if (!outPath || !filename) return null;
|
|
62
|
+
const abs = path.join(OUT_DIR, "scripts", filename);
|
|
63
|
+
let rel = path.relative(path.dirname(outPath), abs).split(path.sep).join("/");
|
|
64
|
+
if (!versioned) return rel;
|
|
65
|
+
try {
|
|
66
|
+
const st = fs.statSync(abs);
|
|
67
|
+
const version = Math.floor(st.mtimeMs || Date.now());
|
|
68
|
+
if (version) rel += `?v=${version}`;
|
|
69
|
+
} catch (_) {}
|
|
70
|
+
return rel;
|
|
71
|
+
}
|
|
72
|
+
// Primary global index location
|
|
73
|
+
const IIIF_CACHE_INDEX = path.join(IIIF_CACHE_DIR, "index.json");
|
|
74
|
+
// Additional legacy locations kept for backward compatibility (read + optional write)
|
|
75
|
+
const IIIF_CACHE_INDEX_LEGACY = path.join(
|
|
76
|
+
IIIF_CACHE_DIR,
|
|
77
|
+
"manifest-index.json",
|
|
78
|
+
);
|
|
79
|
+
const IIIF_CACHE_INDEX_MANIFESTS = path.join(
|
|
80
|
+
IIIF_CACHE_MANIFESTS_DIR,
|
|
81
|
+
"manifest-index.json",
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
const DEFAULT_THUMBNAIL_SIZE = 400;
|
|
85
|
+
const DEFAULT_CHUNK_SIZE = 10;
|
|
86
|
+
const DEFAULT_FETCH_CONCURRENCY = 1;
|
|
87
|
+
const DEFAULT_FEATURED_FETCH_TIMEOUT_MS = 15000;
|
|
88
|
+
const HERO_THUMBNAIL_SIZE = 800;
|
|
89
|
+
const HERO_IMAGE_SIZES_ATTR = "(min-width: 1024px) 1280px, 100vw";
|
|
90
|
+
const OG_IMAGE_WIDTH = 1200;
|
|
91
|
+
const OG_IMAGE_HEIGHT = 630;
|
|
92
|
+
const HERO_REPRESENTATIVE_SIZE = Math.max(HERO_THUMBNAIL_SIZE, OG_IMAGE_WIDTH);
|
|
93
|
+
const MAX_ENTRY_SLUG_LENGTH = 50;
|
|
94
|
+
const DEBUG_IIIF = process.env.CANOPY_IIIF_DEBUG === "1";
|
|
95
|
+
|
|
96
|
+
function logDebug(message) {
|
|
97
|
+
if (!DEBUG_IIIF) return;
|
|
98
|
+
try {
|
|
99
|
+
logLine(`[IIIF][debug] ${message}`, "magenta", {dim: true});
|
|
100
|
+
} catch (_) {}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function resolvePositiveInteger(value, fallback, options = {}) {
|
|
104
|
+
const allowZero = Boolean(options && options.allowZero);
|
|
105
|
+
const num = Number(value);
|
|
106
|
+
if (Number.isFinite(num)) {
|
|
107
|
+
if (allowZero && num === 0) return 0;
|
|
108
|
+
if (num > 0) return Math.max(1, Math.floor(num));
|
|
109
|
+
}
|
|
110
|
+
const normalizedFallback = Number(fallback);
|
|
111
|
+
if (allowZero && normalizedFallback === 0) return 0;
|
|
112
|
+
return Math.max(1, Math.floor(normalizedFallback));
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function formatDurationMs(ms) {
|
|
116
|
+
if (!Number.isFinite(ms) || ms < 0) return "0ms";
|
|
117
|
+
if (ms >= 1000) return `${(ms / 1000).toFixed(1)}s`;
|
|
118
|
+
return `${Math.round(ms)}ms`;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function resolveBoolean(value) {
|
|
122
|
+
if (typeof value === "boolean") return value;
|
|
123
|
+
if (value === undefined || value === null) return false;
|
|
124
|
+
const normalized = String(value).trim().toLowerCase();
|
|
125
|
+
if (!normalized) return false;
|
|
126
|
+
return normalized === "1" || normalized === "true" || normalized === "yes";
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function normalizeCollectionUris(value) {
|
|
130
|
+
if (value === undefined || value === null) return [];
|
|
131
|
+
const rawValues = Array.isArray(value) ? value : [value];
|
|
132
|
+
const seen = new Set();
|
|
133
|
+
const uris = [];
|
|
134
|
+
for (const entry of rawValues) {
|
|
135
|
+
if (typeof entry !== "string") continue;
|
|
136
|
+
const trimmed = entry.trim();
|
|
137
|
+
if (!trimmed || seen.has(trimmed)) continue;
|
|
138
|
+
seen.add(trimmed);
|
|
139
|
+
uris.push(trimmed);
|
|
140
|
+
}
|
|
141
|
+
return uris;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function normalizeManifestConfig(cfg) {
|
|
145
|
+
if (!cfg || typeof cfg !== "object") return [];
|
|
146
|
+
const entries = [];
|
|
147
|
+
const push = (value) => {
|
|
148
|
+
if (value === undefined || value === null) return;
|
|
149
|
+
if (Array.isArray(value)) entries.push(...value);
|
|
150
|
+
else entries.push(value);
|
|
151
|
+
};
|
|
152
|
+
push(cfg.manifest);
|
|
153
|
+
push(cfg.manifests);
|
|
154
|
+
if (!entries.length) return [];
|
|
155
|
+
return normalizeCollectionUris(entries);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function resolveIiifSources(cfg) {
|
|
159
|
+
const safeCfg = cfg && typeof cfg === "object" ? cfg : {};
|
|
160
|
+
let collectionUris = normalizeCollectionUris(safeCfg.collection);
|
|
161
|
+
if (!collectionUris.length) {
|
|
162
|
+
collectionUris = normalizeCollectionUris(
|
|
163
|
+
process.env.CANOPY_COLLECTION_URI || "",
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
const manifestUris = normalizeManifestConfig(safeCfg);
|
|
167
|
+
return {collections: collectionUris, manifests: manifestUris};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function clampSlugLength(slug, limit = MAX_ENTRY_SLUG_LENGTH) {
|
|
171
|
+
if (!slug) return "";
|
|
172
|
+
const max = Math.max(1, limit);
|
|
173
|
+
if (slug.length <= max) return slug;
|
|
174
|
+
const slice = slug.slice(0, max);
|
|
175
|
+
const trimmed = slice.replace(/-+$/g, "");
|
|
176
|
+
return trimmed || slice || slug.slice(0, 1);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function isSlugTooLong(value) {
|
|
180
|
+
return typeof value === "string" && value.length > MAX_ENTRY_SLUG_LENGTH;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function normalizeSlugBase(value, fallback) {
|
|
184
|
+
const safeFallback = fallback || "entry";
|
|
185
|
+
const base = typeof value === "string" ? value : String(value || "");
|
|
186
|
+
const clamped = clampSlugLength(base, MAX_ENTRY_SLUG_LENGTH);
|
|
187
|
+
if (clamped) return clamped;
|
|
188
|
+
return clampSlugLength(safeFallback, MAX_ENTRY_SLUG_LENGTH) || safeFallback;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function manifestHrefFromSlug(slug, routePath) {
|
|
192
|
+
if (!slug) return "";
|
|
193
|
+
const rel = buildRouteRelativePath(routePath || getDefaultRoute("works"), `${String(slug).trim()}.html`);
|
|
194
|
+
return rootRelativeHref(rel);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function extractHomepageId(resource) {
|
|
198
|
+
if (!resource) return "";
|
|
199
|
+
const homepageRaw = resource.homepage;
|
|
200
|
+
const list = Array.isArray(homepageRaw)
|
|
201
|
+
? homepageRaw
|
|
202
|
+
: homepageRaw
|
|
203
|
+
? [homepageRaw]
|
|
204
|
+
: [];
|
|
205
|
+
for (const entry of list) {
|
|
206
|
+
if (!entry) continue;
|
|
207
|
+
if (typeof entry === "string") {
|
|
208
|
+
const trimmed = entry.trim();
|
|
209
|
+
if (trimmed) return trimmed;
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
if (typeof entry === "object") {
|
|
213
|
+
const id = entry.id || entry["@id"];
|
|
214
|
+
if (typeof id === "string" && id.trim()) return id.trim();
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return "";
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function resolveManifestCanonical(manifest, slug, routePath) {
|
|
221
|
+
const homepageId = extractHomepageId(manifest);
|
|
222
|
+
if (homepageId) return homepageId;
|
|
223
|
+
return manifestHrefFromSlug(slug, routePath);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function resolveCollectionCanonical(collection) {
|
|
227
|
+
const homepageId = extractHomepageId(collection);
|
|
228
|
+
if (homepageId) return homepageId;
|
|
229
|
+
const id = collection && (collection.id || collection["@id"]);
|
|
230
|
+
return typeof id === "string" ? id : "";
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function assignEntryCanonical(entry, canonical) {
|
|
234
|
+
if (!entry || typeof entry !== "object") return "";
|
|
235
|
+
const value = canonical ? String(canonical) : "";
|
|
236
|
+
entry.canonical = value;
|
|
237
|
+
return value;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function applyManifestEntryCanonical(entry, manifest, slug, routePath) {
|
|
241
|
+
if (!entry || entry.type !== "Manifest") return "";
|
|
242
|
+
const canonical = resolveManifestCanonical(manifest, slug, routePath);
|
|
243
|
+
return assignEntryCanonical(entry, canonical);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function applyCollectionEntryCanonical(entry, collection) {
|
|
247
|
+
if (!entry || entry.type !== "Collection") return "";
|
|
248
|
+
const canonical = resolveCollectionCanonical(collection);
|
|
249
|
+
return assignEntryCanonical(entry, canonical);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function buildSlugWithSuffix(base, fallback, counter) {
|
|
253
|
+
const suffix = `-${counter}`;
|
|
254
|
+
const baseLimit = Math.max(1, MAX_ENTRY_SLUG_LENGTH - suffix.length);
|
|
255
|
+
const trimmedBase =
|
|
256
|
+
clampSlugLength(base, baseLimit) ||
|
|
257
|
+
clampSlugLength(fallback, baseLimit) ||
|
|
258
|
+
fallback.slice(0, baseLimit);
|
|
259
|
+
return `${trimmedBase}${suffix}`;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function normalizeStringList(value) {
|
|
263
|
+
if (value === undefined || value === null) return [];
|
|
264
|
+
const rawValues = Array.isArray(value) ? value : [value];
|
|
265
|
+
return rawValues
|
|
266
|
+
.map((entry) => {
|
|
267
|
+
if (typeof entry === "string") return entry.trim();
|
|
268
|
+
if (entry === undefined || entry === null) return "";
|
|
269
|
+
return String(entry).trim();
|
|
270
|
+
})
|
|
271
|
+
.filter(Boolean);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function resolveThumbnailPreferences() {
|
|
275
|
+
return {
|
|
276
|
+
size: resolvePositiveInteger(
|
|
277
|
+
process.env.CANOPY_THUMBNAIL_SIZE,
|
|
278
|
+
DEFAULT_THUMBNAIL_SIZE,
|
|
279
|
+
),
|
|
280
|
+
unsafe: resolveBoolean(process.env.CANOPY_THUMBNAILS_UNSAFE),
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function ensureThumbnailValue(target, url, width, height) {
|
|
285
|
+
if (!target) return false;
|
|
286
|
+
const current = target.thumbnail;
|
|
287
|
+
const hasCurrent =
|
|
288
|
+
typeof current === "string" ? current.trim().length > 0 : Boolean(current);
|
|
289
|
+
if (hasCurrent) return false;
|
|
290
|
+
if (!url) return false;
|
|
291
|
+
const normalized = String(url || "").trim();
|
|
292
|
+
if (!normalized) return false;
|
|
293
|
+
target.thumbnail = normalized;
|
|
294
|
+
if (typeof width === "number" && Number.isFinite(width) && width > 0)
|
|
295
|
+
target.thumbnailWidth = width;
|
|
296
|
+
if (typeof height === "number" && Number.isFinite(height) && height > 0)
|
|
297
|
+
target.thumbnailHeight = height;
|
|
298
|
+
return true;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function extractResourceThumbnail(resource) {
|
|
302
|
+
try {
|
|
303
|
+
const rawThumb = resource && resource.thumbnail;
|
|
304
|
+
const first = Array.isArray(rawThumb) ? rawThumb[0] : rawThumb;
|
|
305
|
+
if (!first) return null;
|
|
306
|
+
if (typeof first === "string") {
|
|
307
|
+
const trimmed = first.trim();
|
|
308
|
+
return trimmed ? {url: trimmed} : null;
|
|
309
|
+
}
|
|
310
|
+
const id = first.id || first["@id"];
|
|
311
|
+
if (!id) return null;
|
|
312
|
+
const width = typeof first.width === "number" ? first.width : undefined;
|
|
313
|
+
const height = typeof first.height === "number" ? first.height : undefined;
|
|
314
|
+
return {url: String(id), width, height};
|
|
315
|
+
} catch (_) {
|
|
316
|
+
return null;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
async function resolveHeroMedia(manifest) {
|
|
321
|
+
if (!manifest) return null;
|
|
322
|
+
try {
|
|
323
|
+
const manifestThumb = extractResourceThumbnail(manifest);
|
|
324
|
+
const heroSource = (() => {
|
|
325
|
+
if (manifest && manifest.thumbnail) {
|
|
326
|
+
const clone = {...manifest};
|
|
327
|
+
try {
|
|
328
|
+
delete clone.thumbnail;
|
|
329
|
+
} catch (_) {
|
|
330
|
+
clone.thumbnail = undefined;
|
|
331
|
+
}
|
|
332
|
+
return clone;
|
|
333
|
+
}
|
|
334
|
+
return manifest;
|
|
335
|
+
})();
|
|
336
|
+
const heroRep = await getRepresentativeImage(
|
|
337
|
+
heroSource || manifest,
|
|
338
|
+
HERO_REPRESENTATIVE_SIZE,
|
|
339
|
+
true,
|
|
340
|
+
);
|
|
341
|
+
const canvasImage = findPrimaryCanvasImage(manifest);
|
|
342
|
+
const heroService =
|
|
343
|
+
(canvasImage && canvasImage.service) || (heroRep && heroRep.service);
|
|
344
|
+
const serviceIsLevel0 = isLevel0Service(heroService);
|
|
345
|
+
const heroPreferred = buildIiifImageUrlFromService(
|
|
346
|
+
serviceIsLevel0 ? null : heroService,
|
|
347
|
+
HERO_THUMBNAIL_SIZE,
|
|
348
|
+
);
|
|
349
|
+
const heroWidth = (() => {
|
|
350
|
+
if (canvasImage && typeof canvasImage.width === "number")
|
|
351
|
+
return canvasImage.width;
|
|
352
|
+
if (heroRep && typeof heroRep.width === "number") return heroRep.width;
|
|
353
|
+
return undefined;
|
|
354
|
+
})();
|
|
355
|
+
const heroHeight = (() => {
|
|
356
|
+
if (canvasImage && typeof canvasImage.height === "number")
|
|
357
|
+
return canvasImage.height;
|
|
358
|
+
if (heroRep && typeof heroRep.height === "number") return heroRep.height;
|
|
359
|
+
return undefined;
|
|
360
|
+
})();
|
|
361
|
+
const heroSrcset = serviceIsLevel0 ? "" : buildIiifImageSrcset(heroService);
|
|
362
|
+
const ogFromService =
|
|
363
|
+
!serviceIsLevel0 && heroService
|
|
364
|
+
? buildIiifImageUrlForDimensions(
|
|
365
|
+
heroService,
|
|
366
|
+
OG_IMAGE_WIDTH,
|
|
367
|
+
OG_IMAGE_HEIGHT,
|
|
368
|
+
)
|
|
369
|
+
: "";
|
|
370
|
+
const annotationImageId =
|
|
371
|
+
canvasImage && canvasImage.isImageBody && canvasImage.id
|
|
372
|
+
? String(canvasImage.id)
|
|
373
|
+
: "";
|
|
374
|
+
let heroThumbnail = heroPreferred || "";
|
|
375
|
+
let heroThumbWidth = heroWidth;
|
|
376
|
+
let heroThumbHeight = heroHeight;
|
|
377
|
+
if (!heroThumbnail && manifestThumb && manifestThumb.url) {
|
|
378
|
+
heroThumbnail = manifestThumb.url;
|
|
379
|
+
if (typeof manifestThumb.width === "number")
|
|
380
|
+
heroThumbWidth = manifestThumb.width;
|
|
381
|
+
if (typeof manifestThumb.height === "number")
|
|
382
|
+
heroThumbHeight = manifestThumb.height;
|
|
383
|
+
}
|
|
384
|
+
if (!heroThumbnail) {
|
|
385
|
+
if (annotationImageId) {
|
|
386
|
+
heroThumbnail = annotationImageId;
|
|
387
|
+
} else if (!serviceIsLevel0 && heroRep && heroRep.id) {
|
|
388
|
+
heroThumbnail = String(heroRep.id);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
let ogImage = "";
|
|
392
|
+
let ogImageWidth;
|
|
393
|
+
let ogImageHeight;
|
|
394
|
+
if (ogFromService) {
|
|
395
|
+
ogImage = ogFromService;
|
|
396
|
+
ogImageWidth = OG_IMAGE_WIDTH;
|
|
397
|
+
ogImageHeight = OG_IMAGE_HEIGHT;
|
|
398
|
+
} else if (heroThumbnail) {
|
|
399
|
+
ogImage = heroThumbnail;
|
|
400
|
+
if (typeof heroThumbWidth === "number") ogImageWidth = heroThumbWidth;
|
|
401
|
+
if (typeof heroThumbHeight === "number") ogImageHeight = heroThumbHeight;
|
|
402
|
+
}
|
|
403
|
+
return {
|
|
404
|
+
heroThumbnail: heroThumbnail || "",
|
|
405
|
+
heroThumbnailWidth: heroThumbWidth,
|
|
406
|
+
heroThumbnailHeight: heroThumbHeight,
|
|
407
|
+
heroThumbnailSrcset: heroSrcset || "",
|
|
408
|
+
heroThumbnailSizes: heroSrcset ? HERO_IMAGE_SIZES_ATTR : "",
|
|
409
|
+
ogImage: ogImage || "",
|
|
410
|
+
ogImageWidth,
|
|
411
|
+
ogImageHeight,
|
|
412
|
+
};
|
|
413
|
+
} catch (_) {
|
|
414
|
+
return null;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
function firstLabelString(label) {
|
|
419
|
+
if (!label) return "Untitled";
|
|
420
|
+
if (typeof label === "string") return label;
|
|
421
|
+
const keys = Object.keys(label || {});
|
|
422
|
+
if (!keys.length) return "Untitled";
|
|
423
|
+
const arr = label[keys[0]];
|
|
424
|
+
if (Array.isArray(arr) && arr.length) return String(arr[0]);
|
|
425
|
+
return "Untitled";
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
function flattenMetadataValue(value, out, depth) {
|
|
429
|
+
if (!value || depth > 5) return;
|
|
430
|
+
if (typeof value === "string") {
|
|
431
|
+
const trimmed = value.trim();
|
|
432
|
+
if (trimmed) out.push(trimmed);
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
435
|
+
if (Array.isArray(value)) {
|
|
436
|
+
for (const entry of value) flattenMetadataValue(entry, out, depth + 1);
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
if (typeof value === "object") {
|
|
440
|
+
for (const key of Object.keys(value))
|
|
441
|
+
flattenMetadataValue(value[key], out, depth + 1);
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
try {
|
|
445
|
+
const str = String(value).trim();
|
|
446
|
+
if (str) out.push(str);
|
|
447
|
+
} catch (_) {}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
function normalizeMetadataLabel(label) {
|
|
451
|
+
if (typeof label !== "string") return "";
|
|
452
|
+
const trimmed = label.trim().replace(/[:\s]+$/g, "");
|
|
453
|
+
return trimmed.toLowerCase();
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
function resolveParentFromPartOf(resource) {
|
|
457
|
+
try {
|
|
458
|
+
const partOf = resource && resource.partOf;
|
|
459
|
+
if (!partOf) return "";
|
|
460
|
+
const arr = Array.isArray(partOf) ? partOf : [partOf];
|
|
461
|
+
for (const entry of arr) {
|
|
462
|
+
if (!entry) continue;
|
|
463
|
+
const id = entry.id || entry["@id"];
|
|
464
|
+
if (id) return String(id);
|
|
465
|
+
}
|
|
466
|
+
} catch (_) {}
|
|
467
|
+
return "";
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
function extractSummaryValues(manifest) {
|
|
471
|
+
const values = [];
|
|
472
|
+
try {
|
|
473
|
+
flattenMetadataValue(manifest && manifest.summary, values, 0);
|
|
474
|
+
} catch (_) {}
|
|
475
|
+
const unique = Array.from(
|
|
476
|
+
new Set(values.map((val) => String(val || "").trim()).filter(Boolean)),
|
|
477
|
+
);
|
|
478
|
+
if (!unique.length) return "";
|
|
479
|
+
return unique.join(" ");
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
function normalizeSummaryText(value) {
|
|
483
|
+
if (!value) return "";
|
|
484
|
+
return String(value).replace(/\s+/g, " ").trim();
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
function truncateSummary(value, max = 240) {
|
|
488
|
+
const normalized = normalizeSummaryText(value);
|
|
489
|
+
if (!normalized) return "";
|
|
490
|
+
if (normalized.length <= max) return normalized;
|
|
491
|
+
const slice = normalized.slice(0, Math.max(0, max - 3)).trimEnd();
|
|
492
|
+
return `${slice}...`;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
function stripHtml(value) {
|
|
496
|
+
try {
|
|
497
|
+
return String(value || "")
|
|
498
|
+
.replace(/<[^>]*>/g, " ")
|
|
499
|
+
.replace(/\s+/g, " ")
|
|
500
|
+
.trim();
|
|
501
|
+
} catch (_) {
|
|
502
|
+
return "";
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
function collectTextualBody(body, out) {
|
|
507
|
+
if (!body) return;
|
|
508
|
+
if (Array.isArray(body)) {
|
|
509
|
+
for (const entry of body) collectTextualBody(entry, out);
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
512
|
+
if (typeof body === "string") {
|
|
513
|
+
const text = stripHtml(body);
|
|
514
|
+
if (text) out.push(text);
|
|
515
|
+
return;
|
|
516
|
+
}
|
|
517
|
+
if (typeof body !== "object") return;
|
|
518
|
+
const type = String(body.type || "").toLowerCase();
|
|
519
|
+
const format = String(body.format || "").toLowerCase();
|
|
520
|
+
const isTextual =
|
|
521
|
+
type === "textualbody" ||
|
|
522
|
+
format.startsWith("text/") ||
|
|
523
|
+
typeof body.value === "string" ||
|
|
524
|
+
Array.isArray(body.value);
|
|
525
|
+
if (!isTextual) return;
|
|
526
|
+
if (body.value !== undefined) collectTextualBody(body.value, out);
|
|
527
|
+
if (body.label !== undefined) collectTextualBody(body.label, out);
|
|
528
|
+
if (body.body !== undefined) collectTextualBody(body.body, out);
|
|
529
|
+
if (body.items !== undefined) collectTextualBody(body.items, out);
|
|
530
|
+
if (body.text !== undefined) collectTextualBody(body.text, out);
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
async function extractAnnotationText(manifest, options = {}) {
|
|
534
|
+
if (!manifest || typeof manifest !== "object") return "";
|
|
535
|
+
if (!options.enabled) return "";
|
|
536
|
+
const motivations =
|
|
537
|
+
options.motivations instanceof Set ? options.motivations : new Set();
|
|
538
|
+
const allowAll = motivations.size === 0;
|
|
539
|
+
const results = [];
|
|
540
|
+
const seenText = new Set();
|
|
541
|
+
const seenNodes = new Set();
|
|
542
|
+
|
|
543
|
+
function matchesMotivation(value) {
|
|
544
|
+
if (allowAll) return true;
|
|
545
|
+
if (!value) return false;
|
|
546
|
+
if (Array.isArray(value)) {
|
|
547
|
+
return value.some((entry) => matchesMotivation(entry));
|
|
548
|
+
}
|
|
549
|
+
try {
|
|
550
|
+
const norm = String(value || "")
|
|
551
|
+
.trim()
|
|
552
|
+
.toLowerCase();
|
|
553
|
+
return motivations.has(norm);
|
|
554
|
+
} catch (_) {
|
|
555
|
+
return false;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
async function handleAnnotation(annotation) {
|
|
560
|
+
if (!annotation || typeof annotation !== "object") return;
|
|
561
|
+
if (!matchesMotivation(annotation.motivation)) return;
|
|
562
|
+
const body = annotation.body;
|
|
563
|
+
const texts = [];
|
|
564
|
+
collectTextualBody(body, texts);
|
|
565
|
+
for (const text of texts) {
|
|
566
|
+
if (!text) continue;
|
|
567
|
+
if (seenText.has(text)) continue;
|
|
568
|
+
seenText.add(text);
|
|
569
|
+
results.push(text);
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
async function walk(value) {
|
|
574
|
+
if (!value) return;
|
|
575
|
+
if (Array.isArray(value)) {
|
|
576
|
+
for (const entry of value) await walk(entry);
|
|
577
|
+
return;
|
|
578
|
+
}
|
|
579
|
+
if (typeof value !== "object") return;
|
|
580
|
+
if (seenNodes.has(value)) return;
|
|
581
|
+
seenNodes.add(value);
|
|
582
|
+
if (Array.isArray(value.annotations)) {
|
|
583
|
+
for (const page of value.annotations) {
|
|
584
|
+
if (page && typeof page.id === 'string' && !page.items){
|
|
585
|
+
const fetchedPage = await readJsonFromUri(page.id, { log: true });
|
|
586
|
+
if (fetchedPage && Array.isArray(fetchedPage.items)) {
|
|
587
|
+
for (const item of fetchedPage.items){
|
|
588
|
+
handleAnnotation(item)
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
} else if (page && Array.isArray(page.items)) {
|
|
592
|
+
for (const item of page.items) handleAnnotation(item);
|
|
593
|
+
}
|
|
594
|
+
await walk(page);
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
if (Array.isArray(value.items)) {
|
|
598
|
+
for (const item of value.items) await walk(item);
|
|
599
|
+
}
|
|
600
|
+
for (const key of Object.keys(value)) {
|
|
601
|
+
if (key === "annotations" || key === "items") continue;
|
|
602
|
+
await walk(value[key]);
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
await walk(manifest);
|
|
607
|
+
if (!results.length) return "";
|
|
608
|
+
return results.join(" ");
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
function extractMetadataValues(manifest, options = {}) {
|
|
612
|
+
const meta = Array.isArray(manifest && manifest.metadata)
|
|
613
|
+
? manifest.metadata
|
|
614
|
+
: [];
|
|
615
|
+
if (!meta.length) return [];
|
|
616
|
+
const includeAll = !!options.includeAll;
|
|
617
|
+
const labelsSet = includeAll
|
|
618
|
+
? null
|
|
619
|
+
: options && options.labelsSet instanceof Set
|
|
620
|
+
? options.labelsSet
|
|
621
|
+
: new Set();
|
|
622
|
+
const seen = new Set();
|
|
623
|
+
const out = [];
|
|
624
|
+
for (const entry of meta) {
|
|
625
|
+
if (!entry) continue;
|
|
626
|
+
const label = firstLabelString(entry.label);
|
|
627
|
+
if (!label) continue;
|
|
628
|
+
if (!includeAll && labelsSet && labelsSet.size) {
|
|
629
|
+
const normLabel = normalizeMetadataLabel(label);
|
|
630
|
+
if (!labelsSet.has(normLabel)) continue;
|
|
631
|
+
}
|
|
632
|
+
const values = [];
|
|
633
|
+
flattenMetadataValue(entry.value, values, 0);
|
|
634
|
+
for (const val of values) {
|
|
635
|
+
const normalized = String(val || "").trim();
|
|
636
|
+
if (!normalized) continue;
|
|
637
|
+
if (seen.has(normalized)) continue;
|
|
638
|
+
seen.add(normalized);
|
|
639
|
+
out.push(normalized);
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
return out;
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
function extractMetadataEntries(manifest, options = {}) {
|
|
646
|
+
const meta = Array.isArray(manifest && manifest.metadata)
|
|
647
|
+
? manifest.metadata
|
|
648
|
+
: [];
|
|
649
|
+
if (!meta.length) return [];
|
|
650
|
+
const includeAll = !!options.includeAll;
|
|
651
|
+
const labelsSet = includeAll
|
|
652
|
+
? null
|
|
653
|
+
: options && options.labelsSet instanceof Set
|
|
654
|
+
? options.labelsSet
|
|
655
|
+
: new Set();
|
|
656
|
+
const map = new Map();
|
|
657
|
+
const order = [];
|
|
658
|
+
for (const entry of meta) {
|
|
659
|
+
if (!entry) continue;
|
|
660
|
+
const label = firstLabelString(entry.label);
|
|
661
|
+
if (!label) continue;
|
|
662
|
+
const normalized = normalizeMetadataLabel(label);
|
|
663
|
+
if (!normalized) continue;
|
|
664
|
+
if (!includeAll && labelsSet && labelsSet.size && !labelsSet.has(normalized)) continue;
|
|
665
|
+
const values = [];
|
|
666
|
+
flattenMetadataValue(entry.value, values, 0);
|
|
667
|
+
const cleaned = [];
|
|
668
|
+
for (const val of values) {
|
|
669
|
+
const text = String(val || "").trim();
|
|
670
|
+
if (text) cleaned.push(text);
|
|
671
|
+
}
|
|
672
|
+
if (!cleaned.length) continue;
|
|
673
|
+
let record = map.get(normalized);
|
|
674
|
+
if (!record) {
|
|
675
|
+
record = {label, normalized, values: [], seen: new Set()};
|
|
676
|
+
map.set(normalized, record);
|
|
677
|
+
order.push(normalized);
|
|
678
|
+
} else if (!record.label && label) {
|
|
679
|
+
record.label = label;
|
|
680
|
+
}
|
|
681
|
+
for (const valueText of cleaned) {
|
|
682
|
+
if (record.seen.has(valueText)) continue;
|
|
683
|
+
record.seen.add(valueText);
|
|
684
|
+
record.values.push(valueText);
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
return order
|
|
688
|
+
.map((normalized) => map.get(normalized))
|
|
689
|
+
.filter(Boolean)
|
|
690
|
+
.map((record) => ({
|
|
691
|
+
label: record.label,
|
|
692
|
+
normalized: record.normalized,
|
|
693
|
+
values: record.values.slice(),
|
|
694
|
+
}));
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
function buildMetadataIndexPayload(map, explicitOrder, fallbackOrder) {
|
|
698
|
+
if (!map || !map.size) return [];
|
|
699
|
+
const ordered = [];
|
|
700
|
+
const seen = new Set();
|
|
701
|
+
const primaryOrder = Array.isArray(explicitOrder) ? explicitOrder : [];
|
|
702
|
+
const secondaryOrder = Array.isArray(fallbackOrder) ? fallbackOrder : [];
|
|
703
|
+
for (const normalized of primaryOrder) {
|
|
704
|
+
if (!normalized) continue;
|
|
705
|
+
const record = map.get(normalized);
|
|
706
|
+
if (!record) continue;
|
|
707
|
+
ordered.push(record);
|
|
708
|
+
seen.add(normalized);
|
|
709
|
+
}
|
|
710
|
+
for (const normalized of secondaryOrder) {
|
|
711
|
+
if (!normalized || seen.has(normalized)) continue;
|
|
712
|
+
const record = map.get(normalized);
|
|
713
|
+
if (!record) continue;
|
|
714
|
+
ordered.push(record);
|
|
715
|
+
seen.add(normalized);
|
|
716
|
+
}
|
|
717
|
+
map.forEach((record, normalized) => {
|
|
718
|
+
if (seen.has(normalized)) return;
|
|
719
|
+
ordered.push(record);
|
|
720
|
+
});
|
|
721
|
+
const payload = [];
|
|
722
|
+
for (const record of ordered) {
|
|
723
|
+
if (!record || !record.values) continue;
|
|
724
|
+
const values = Array.from(record.values.values()).sort((a, b) =>
|
|
725
|
+
String(a && a.value ? a.value : "").localeCompare(
|
|
726
|
+
String(b && b.value ? b.value : ""),
|
|
727
|
+
),
|
|
728
|
+
);
|
|
729
|
+
if (!values.length) continue;
|
|
730
|
+
payload.push({
|
|
731
|
+
label: record.label || record.slug || record.normalized,
|
|
732
|
+
slug: record.slug || record.normalized,
|
|
733
|
+
values,
|
|
734
|
+
});
|
|
735
|
+
}
|
|
736
|
+
return payload;
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
async function writeMetadataIndexFile(payload) {
|
|
740
|
+
if (!payload || !payload.length) {
|
|
741
|
+
try {
|
|
742
|
+
await fsp.rm(IIIF_METADATA_INDEX_CACHE, {force: true});
|
|
743
|
+
} catch (_) {}
|
|
744
|
+
return;
|
|
745
|
+
}
|
|
746
|
+
try {
|
|
747
|
+
ensureDirSync(path.dirname(IIIF_METADATA_INDEX_CACHE));
|
|
748
|
+
await fsp.writeFile(
|
|
749
|
+
IIIF_METADATA_INDEX_CACHE,
|
|
750
|
+
JSON.stringify(payload, null, 2),
|
|
751
|
+
"utf8",
|
|
752
|
+
);
|
|
753
|
+
} catch (_) {}
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
// Presentation 2 upgraded to 3 by `@iiif/parser`'s upgrader (a dependency of
|
|
757
|
+
// @nulib/canopy); a resource it cannot read stays as it was.
|
|
758
|
+
let upgradeModulePromise = null;
|
|
759
|
+
async function loadUpgradeModule() {
|
|
760
|
+
if (!upgradeModulePromise) {
|
|
761
|
+
upgradeModulePromise = import("@iiif/parser/upgrader").catch(() => null);
|
|
762
|
+
}
|
|
763
|
+
return upgradeModulePromise;
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
async function upgradeIiifResource(resource) {
|
|
767
|
+
if (!resource) return resource;
|
|
768
|
+
try {
|
|
769
|
+
const mod = await loadUpgradeModule();
|
|
770
|
+
const upgrader = mod && (mod.upgrade || mod.default);
|
|
771
|
+
if (typeof upgrader === "function") {
|
|
772
|
+
let upgraded = upgrader(resource);
|
|
773
|
+
if (upgraded && typeof upgraded.then === "function") {
|
|
774
|
+
upgraded = await upgraded;
|
|
775
|
+
}
|
|
776
|
+
if (upgraded) return inheritProvenance(resource, upgraded);
|
|
777
|
+
}
|
|
778
|
+
} catch (_) {}
|
|
779
|
+
return resource;
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
async function ensurePresentation3Manifest(manifest) {
|
|
783
|
+
const upgraded = await upgradeIiifResource(manifest);
|
|
784
|
+
return {manifest: upgraded, changed: upgraded !== manifest};
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
async function readJson(p) {
|
|
788
|
+
const raw = await fsp.readFile(p, "utf8");
|
|
789
|
+
return JSON.parse(raw);
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
function normalizeIiifId(raw) {
|
|
793
|
+
try {
|
|
794
|
+
const s = String(raw || "");
|
|
795
|
+
if (!/^https?:\/\//i.test(s)) return s;
|
|
796
|
+
const u = new URL(s);
|
|
797
|
+
const entries = Array.from(u.searchParams.entries()).sort(
|
|
798
|
+
(a, b) => a[0].localeCompare(b[0]) || a[1].localeCompare(b[1]),
|
|
799
|
+
);
|
|
800
|
+
u.search = "";
|
|
801
|
+
for (const [k, v] of entries) u.searchParams.append(k, v);
|
|
802
|
+
return u.toString();
|
|
803
|
+
} catch (_) {
|
|
804
|
+
return String(raw || "");
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
function normalizeIiifType(value) {
|
|
809
|
+
try {
|
|
810
|
+
const raw = String(value || "").trim();
|
|
811
|
+
if (!raw) return "";
|
|
812
|
+
const lower = raw.toLowerCase();
|
|
813
|
+
const idx = lower.lastIndexOf(":");
|
|
814
|
+
if (idx >= 0 && idx < lower.length - 1) return lower.slice(idx + 1);
|
|
815
|
+
return lower;
|
|
816
|
+
} catch (_) {
|
|
817
|
+
return "";
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
function extractCollectionEntries(collection) {
|
|
822
|
+
if (!collection || typeof collection !== "object") return [];
|
|
823
|
+
const entries = [];
|
|
824
|
+
const seen = new Set();
|
|
825
|
+
const pushEntry = (raw, fallbackType) => {
|
|
826
|
+
if (!raw) return;
|
|
827
|
+
let id = "";
|
|
828
|
+
let type = "";
|
|
829
|
+
if (typeof raw === "string") {
|
|
830
|
+
id = raw;
|
|
831
|
+
type = fallbackType || "";
|
|
832
|
+
} else if (typeof raw === "object") {
|
|
833
|
+
id = raw.id || raw["@id"] || fallbackType || "";
|
|
834
|
+
type = raw.type || raw["@type"] || fallbackType || "";
|
|
835
|
+
}
|
|
836
|
+
const normalizedId = String(id || "").trim();
|
|
837
|
+
if (!normalizedId) return;
|
|
838
|
+
const normalizedType = normalizeIiifType(type || fallbackType || "");
|
|
839
|
+
const fallback = normalizeIiifType(fallbackType || "");
|
|
840
|
+
const key = `${normalizedType}::${normalizedId}`;
|
|
841
|
+
if (seen.has(key)) return;
|
|
842
|
+
seen.add(key);
|
|
843
|
+
entries.push({
|
|
844
|
+
id: normalizedId,
|
|
845
|
+
type: normalizedType,
|
|
846
|
+
fallback,
|
|
847
|
+
raw,
|
|
848
|
+
});
|
|
849
|
+
};
|
|
850
|
+
|
|
851
|
+
const sources = [
|
|
852
|
+
{list: collection.items, fallback: ""},
|
|
853
|
+
{list: collection.manifests, fallback: "manifest"},
|
|
854
|
+
{list: collection.collections, fallback: "collection"},
|
|
855
|
+
{list: collection.members, fallback: ""},
|
|
856
|
+
];
|
|
857
|
+
for (const source of sources) {
|
|
858
|
+
const arr = Array.isArray(source.list) ? source.list : [];
|
|
859
|
+
for (const entry of arr) pushEntry(entry, source.fallback);
|
|
860
|
+
}
|
|
861
|
+
return entries;
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
async function readJsonFromUri(uri, options = {log: false}) {
|
|
865
|
+
const opts = options && typeof options === "object" ? options : {};
|
|
866
|
+
const shouldLog = Boolean(opts.log);
|
|
867
|
+
const fetchSignal = opts.signal;
|
|
868
|
+
try {
|
|
869
|
+
if (/^https?:\/\//i.test(uri)) {
|
|
870
|
+
if (typeof fetch !== "function") return null;
|
|
871
|
+
let res = null;
|
|
872
|
+
try {
|
|
873
|
+
const fetchOptions = {headers: {Accept: "application/json"}};
|
|
874
|
+
if (fetchSignal) fetchOptions.signal = fetchSignal;
|
|
875
|
+
res = await fetch(uri, fetchOptions);
|
|
876
|
+
} catch (error) {
|
|
877
|
+
if (shouldLog) {
|
|
878
|
+
try {
|
|
879
|
+
const code = error && error.name === "AbortError" ? "ABORT" : "ERR";
|
|
880
|
+
logLine(`⊘ ${String(uri)} → ${code}`, "red", {bright: true});
|
|
881
|
+
} catch (_) {}
|
|
882
|
+
}
|
|
883
|
+
return null;
|
|
884
|
+
}
|
|
885
|
+
if (shouldLog) {
|
|
886
|
+
try {
|
|
887
|
+
if (res && res.ok) {
|
|
888
|
+
logLine(`↓ ${String(uri)} → ${res.status}`, "yellow", {
|
|
889
|
+
bright: true,
|
|
890
|
+
});
|
|
891
|
+
} else {
|
|
892
|
+
const code = res ? res.status : "ERR";
|
|
893
|
+
logLine(`⊘ ${String(uri)} → ${code}`, "red", {bright: true});
|
|
894
|
+
}
|
|
895
|
+
} catch (_) {}
|
|
896
|
+
}
|
|
897
|
+
if (!res || !res.ok) return null;
|
|
898
|
+
return rememberRetrieved(await res.json(), uri);
|
|
899
|
+
}
|
|
900
|
+
const p = uri.startsWith("file://") ? new URL(uri) : {pathname: uri};
|
|
901
|
+
const localPath = uri.startsWith("file://")
|
|
902
|
+
? p.pathname
|
|
903
|
+
: path.resolve(String(p.pathname));
|
|
904
|
+
return rememberRetrieved(await readJson(localPath), uri);
|
|
905
|
+
} catch (_) {
|
|
906
|
+
return null;
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
function computeHash(obj) {
|
|
911
|
+
try {
|
|
912
|
+
const json = JSON.stringify(deepSort(obj));
|
|
913
|
+
return crypto.createHash("sha256").update(json).digest("hex");
|
|
914
|
+
} catch (_) {
|
|
915
|
+
return "";
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
function deepSort(value) {
|
|
920
|
+
if (Array.isArray(value)) return value.map(deepSort);
|
|
921
|
+
if (value && typeof value === "object") {
|
|
922
|
+
const out = {};
|
|
923
|
+
for (const key of Object.keys(value).sort())
|
|
924
|
+
out[key] = deepSort(value[key]);
|
|
925
|
+
return out;
|
|
926
|
+
}
|
|
927
|
+
return value;
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
async function loadManifestIndex() {
|
|
931
|
+
try {
|
|
932
|
+
// Try primary path first
|
|
933
|
+
if (fs.existsSync(IIIF_CACHE_INDEX)) {
|
|
934
|
+
const idx = await readJson(IIIF_CACHE_INDEX);
|
|
935
|
+
if (idx && typeof idx === "object") {
|
|
936
|
+
const byId = Array.isArray(idx.byId)
|
|
937
|
+
? idx.byId
|
|
938
|
+
: idx.byId && typeof idx.byId === "object"
|
|
939
|
+
? Object.keys(idx.byId).map((k) => ({
|
|
940
|
+
id: k,
|
|
941
|
+
type: "Manifest",
|
|
942
|
+
slug: String(idx.byId[k] || ""),
|
|
943
|
+
parent: (idx.parents && idx.parents[k]) || "",
|
|
944
|
+
}))
|
|
945
|
+
: [];
|
|
946
|
+
return {byId, collection: idx.collection || null};
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
// Legacy index location retained for backward compatibility
|
|
950
|
+
if (fs.existsSync(IIIF_CACHE_INDEX_LEGACY)) {
|
|
951
|
+
const idx = await readJson(IIIF_CACHE_INDEX_LEGACY);
|
|
952
|
+
if (idx && typeof idx === "object") {
|
|
953
|
+
const byId = Array.isArray(idx.byId)
|
|
954
|
+
? idx.byId
|
|
955
|
+
: idx.byId && typeof idx.byId === "object"
|
|
956
|
+
? Object.keys(idx.byId).map((k) => ({
|
|
957
|
+
id: k,
|
|
958
|
+
type: "Manifest",
|
|
959
|
+
slug: String(idx.byId[k] || ""),
|
|
960
|
+
parent: (idx.parents && idx.parents[k]) || "",
|
|
961
|
+
}))
|
|
962
|
+
: [];
|
|
963
|
+
return {byId, collection: idx.collection || null};
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
// Legacy manifests index retained for backward compatibility
|
|
967
|
+
if (fs.existsSync(IIIF_CACHE_INDEX_MANIFESTS)) {
|
|
968
|
+
const idx = await readJson(IIIF_CACHE_INDEX_MANIFESTS);
|
|
969
|
+
if (idx && typeof idx === "object") {
|
|
970
|
+
const byId = Array.isArray(idx.byId)
|
|
971
|
+
? idx.byId
|
|
972
|
+
: idx.byId && typeof idx.byId === "object"
|
|
973
|
+
? Object.keys(idx.byId).map((k) => ({
|
|
974
|
+
id: k,
|
|
975
|
+
type: "Manifest",
|
|
976
|
+
slug: String(idx.byId[k] || ""),
|
|
977
|
+
parent: (idx.parents && idx.parents[k]) || "",
|
|
978
|
+
}))
|
|
979
|
+
: [];
|
|
980
|
+
return {byId, collection: idx.collection || null};
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
} catch (_) {}
|
|
984
|
+
return {byId: [], collection: null};
|
|
985
|
+
}
|
|
986
|
+
|
|
987
|
+
async function saveManifestIndex(index) {
|
|
988
|
+
try {
|
|
989
|
+
ensureDirSync(IIIF_CACHE_DIR);
|
|
990
|
+
const out = {
|
|
991
|
+
byId: Array.isArray(index.byId) ? index.byId : [],
|
|
992
|
+
collection: index.collection || null,
|
|
993
|
+
// Optional build/search version; consumers may ignore
|
|
994
|
+
version: index.version || undefined,
|
|
995
|
+
};
|
|
996
|
+
await fsp.writeFile(IIIF_CACHE_INDEX, JSON.stringify(out, null, 2), "utf8");
|
|
997
|
+
// Remove legacy files to avoid confusion
|
|
998
|
+
try {
|
|
999
|
+
await fsp.rm(IIIF_CACHE_INDEX_LEGACY, {force: true});
|
|
1000
|
+
} catch (_) {}
|
|
1001
|
+
try {
|
|
1002
|
+
await fsp.rm(IIIF_CACHE_INDEX_MANIFESTS, {force: true});
|
|
1003
|
+
} catch (_) {}
|
|
1004
|
+
} catch (_) {}
|
|
1005
|
+
}
|
|
1006
|
+
|
|
1007
|
+
// In-memory memo to avoid repeated FS scans when index mapping is missing
|
|
1008
|
+
const MEMO_ID_TO_SLUG = new Map();
|
|
1009
|
+
// Track slugs chosen during this run to avoid collisions when multiple
|
|
1010
|
+
// collections/manifests share the same base title but mappings aren't yet saved.
|
|
1011
|
+
const RESERVED_SLUGS = {Manifest: new Set(), Collection: new Set()};
|
|
1012
|
+
|
|
1013
|
+
function resetReservedSlugs() {
|
|
1014
|
+
try {
|
|
1015
|
+
Object.keys(RESERVED_SLUGS).forEach((key) => {
|
|
1016
|
+
const set = RESERVED_SLUGS[key];
|
|
1017
|
+
if (set && typeof set.clear === "function") set.clear();
|
|
1018
|
+
});
|
|
1019
|
+
} catch (_) {}
|
|
1020
|
+
}
|
|
1021
|
+
|
|
1022
|
+
function computeUniqueSlug(index, baseSlug, id, type) {
|
|
1023
|
+
const byId = Array.isArray(index && index.byId) ? index.byId : [];
|
|
1024
|
+
const normId = normalizeIiifId(String(id || ""));
|
|
1025
|
+
const fallbackBase = type === "Manifest" ? "untitled" : "collection";
|
|
1026
|
+
const normalizedBase = normalizeSlugBase(
|
|
1027
|
+
baseSlug || fallbackBase,
|
|
1028
|
+
fallbackBase,
|
|
1029
|
+
);
|
|
1030
|
+
const used = new Set(
|
|
1031
|
+
byId
|
|
1032
|
+
.filter((e) => e && e.slug && e.type === type)
|
|
1033
|
+
.map((e) => String(e.slug)),
|
|
1034
|
+
);
|
|
1035
|
+
const reserved = RESERVED_SLUGS[type] || new Set();
|
|
1036
|
+
let slug = normalizedBase;
|
|
1037
|
+
let i = 1;
|
|
1038
|
+
for (;;) {
|
|
1039
|
+
const existing = byId.find(
|
|
1040
|
+
(e) => e && e.type === type && String(e.slug) === String(slug),
|
|
1041
|
+
);
|
|
1042
|
+
if (existing) {
|
|
1043
|
+
// If this slug already maps to this id, reuse it and reserve.
|
|
1044
|
+
if (normalizeIiifId(existing.id) === normId) {
|
|
1045
|
+
reserved.add(slug);
|
|
1046
|
+
return slug;
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
if (!used.has(slug) && !reserved.has(slug)) {
|
|
1050
|
+
reserved.add(slug);
|
|
1051
|
+
return slug;
|
|
1052
|
+
}
|
|
1053
|
+
slug = buildSlugWithSuffix(normalizedBase, fallbackBase, i++);
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1057
|
+
function ensureBaseSlugFor(index, baseSlug, id, type) {
|
|
1058
|
+
try {
|
|
1059
|
+
const byId = Array.isArray(index && index.byId) ? index.byId : [];
|
|
1060
|
+
const normId = normalizeIiifId(String(id || ""));
|
|
1061
|
+
const fallbackBase = type === "Manifest" ? "untitled" : "collection";
|
|
1062
|
+
const normalizedBase = normalizeSlugBase(
|
|
1063
|
+
baseSlug || fallbackBase,
|
|
1064
|
+
fallbackBase,
|
|
1065
|
+
);
|
|
1066
|
+
const existingWithBase = byId.find(
|
|
1067
|
+
(e) => e && e.type === type && String(e.slug) === String(normalizedBase),
|
|
1068
|
+
);
|
|
1069
|
+
if (existingWithBase && normalizeIiifId(existingWithBase.id) !== normId) {
|
|
1070
|
+
// Reassign the existing entry to the next available suffix to free the base
|
|
1071
|
+
const newSlug = computeUniqueSlug(
|
|
1072
|
+
index,
|
|
1073
|
+
normalizedBase,
|
|
1074
|
+
existingWithBase.id,
|
|
1075
|
+
type,
|
|
1076
|
+
);
|
|
1077
|
+
if (newSlug && newSlug !== normalizedBase)
|
|
1078
|
+
existingWithBase.slug = newSlug;
|
|
1079
|
+
}
|
|
1080
|
+
} catch (_) {}
|
|
1081
|
+
return baseSlug;
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
async function findSlugByIdFromDisk(id) {
|
|
1085
|
+
try {
|
|
1086
|
+
if (!fs.existsSync(IIIF_CACHE_MANIFESTS_DIR)) return null;
|
|
1087
|
+
const files = await fsp.readdir(IIIF_CACHE_MANIFESTS_DIR);
|
|
1088
|
+
for (const name of files) {
|
|
1089
|
+
if (!name || !name.toLowerCase().endsWith(".json")) continue;
|
|
1090
|
+
const p = path.join(IIIF_CACHE_MANIFESTS_DIR, name);
|
|
1091
|
+
try {
|
|
1092
|
+
const raw = await fsp.readFile(p, "utf8");
|
|
1093
|
+
const obj = JSON.parse(raw);
|
|
1094
|
+
const mid = normalizeIiifId(
|
|
1095
|
+
String((obj && (obj.id || obj["@id"])) || ""),
|
|
1096
|
+
);
|
|
1097
|
+
if (mid && mid === normalizeIiifId(String(id))) {
|
|
1098
|
+
const slug = name.replace(/\.json$/i, "");
|
|
1099
|
+
return slug;
|
|
1100
|
+
}
|
|
1101
|
+
} catch (_) {}
|
|
1102
|
+
}
|
|
1103
|
+
} catch (_) {}
|
|
1104
|
+
return null;
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
async function loadCachedManifestById(id) {
|
|
1108
|
+
if (!id) return null;
|
|
1109
|
+
try {
|
|
1110
|
+
const index = await loadManifestIndex();
|
|
1111
|
+
let slug = null;
|
|
1112
|
+
if (Array.isArray(index.byId)) {
|
|
1113
|
+
const nid = normalizeIiifId(id);
|
|
1114
|
+
const entry = index.byId.find(
|
|
1115
|
+
(e) => e && normalizeIiifId(e.id) === nid && e.type === "Manifest",
|
|
1116
|
+
);
|
|
1117
|
+
slug = entry && entry.slug;
|
|
1118
|
+
}
|
|
1119
|
+
if (isSlugTooLong(slug)) slug = null;
|
|
1120
|
+
if (!slug) {
|
|
1121
|
+
// Try an on-disk scan to recover mapping if index is missing/out-of-sync
|
|
1122
|
+
const memo = MEMO_ID_TO_SLUG.get(String(id));
|
|
1123
|
+
if (memo) slug = memo;
|
|
1124
|
+
if (isSlugTooLong(slug)) slug = null;
|
|
1125
|
+
if (!slug) {
|
|
1126
|
+
const found = await findSlugByIdFromDisk(id);
|
|
1127
|
+
if (found && !isSlugTooLong(found)) {
|
|
1128
|
+
slug = found;
|
|
1129
|
+
MEMO_ID_TO_SLUG.set(String(id), slug);
|
|
1130
|
+
try {
|
|
1131
|
+
// Heal index mapping for future runs
|
|
1132
|
+
index.byId = Array.isArray(index.byId) ? index.byId : [];
|
|
1133
|
+
const nid = normalizeIiifId(id);
|
|
1134
|
+
const existingEntryIdx = index.byId.findIndex(
|
|
1135
|
+
(e) =>
|
|
1136
|
+
e && normalizeIiifId(e.id) === nid && e.type === "Manifest",
|
|
1137
|
+
);
|
|
1138
|
+
const entry = {
|
|
1139
|
+
id: String(nid),
|
|
1140
|
+
type: "Manifest",
|
|
1141
|
+
slug,
|
|
1142
|
+
parent: "",
|
|
1143
|
+
};
|
|
1144
|
+
applyManifestEntryCanonical(entry, null, slug, getDefaultRoute("works"));
|
|
1145
|
+
if (existingEntryIdx >= 0) index.byId[existingEntryIdx] = entry;
|
|
1146
|
+
else index.byId.push(entry);
|
|
1147
|
+
await saveManifestIndex(index);
|
|
1148
|
+
} catch (_) {}
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1151
|
+
}
|
|
1152
|
+
if (!slug) return null;
|
|
1153
|
+
const p = path.join(IIIF_CACHE_MANIFESTS_DIR, slug + ".json");
|
|
1154
|
+
if (!fs.existsSync(p)) return null;
|
|
1155
|
+
const raw = await readJson(p);
|
|
1156
|
+
const {manifest: normalized, changed} =
|
|
1157
|
+
await ensurePresentation3Manifest(raw);
|
|
1158
|
+
if (changed) {
|
|
1159
|
+
try {
|
|
1160
|
+
await fsp.writeFile(p, JSON.stringify(normalized, null, 2), "utf8");
|
|
1161
|
+
} catch (_) {}
|
|
1162
|
+
}
|
|
1163
|
+
try {
|
|
1164
|
+
index.byId = Array.isArray(index.byId) ? index.byId : [];
|
|
1165
|
+
const nid = normalizeIiifId(id);
|
|
1166
|
+
const existingEntryIdx = index.byId.findIndex(
|
|
1167
|
+
(e) => e && normalizeIiifId(e.id) === nid && e.type === "Manifest",
|
|
1168
|
+
);
|
|
1169
|
+
if (existingEntryIdx >= 0) {
|
|
1170
|
+
const entry = index.byId[existingEntryIdx];
|
|
1171
|
+
const prevCanonical =
|
|
1172
|
+
entry && entry.canonical ? String(entry.canonical) : "";
|
|
1173
|
+
const nextCanonical = applyManifestEntryCanonical(
|
|
1174
|
+
entry,
|
|
1175
|
+
normalized,
|
|
1176
|
+
slug,
|
|
1177
|
+
getDefaultRoute("works"),
|
|
1178
|
+
);
|
|
1179
|
+
if (nextCanonical !== prevCanonical) {
|
|
1180
|
+
await saveManifestIndex(index);
|
|
1181
|
+
}
|
|
1182
|
+
}
|
|
1183
|
+
} catch (_) {}
|
|
1184
|
+
return normalized;
|
|
1185
|
+
} catch (_) {
|
|
1186
|
+
return null;
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
|
|
1190
|
+
async function saveCachedManifest(manifest, id, parentId) {
|
|
1191
|
+
const {manifest: normalizedManifest} =
|
|
1192
|
+
await ensurePresentation3Manifest(manifest);
|
|
1193
|
+
try {
|
|
1194
|
+
const index = await loadManifestIndex();
|
|
1195
|
+
const title = firstLabelString(
|
|
1196
|
+
normalizedManifest && normalizedManifest.label,
|
|
1197
|
+
);
|
|
1198
|
+
const baseSlug =
|
|
1199
|
+
slugify(title || "untitled", {lower: true, strict: true, trim: true}) ||
|
|
1200
|
+
"untitled";
|
|
1201
|
+
const slug = computeUniqueSlug(index, baseSlug, id, "Manifest");
|
|
1202
|
+
ensureDirSync(IIIF_CACHE_MANIFESTS_DIR);
|
|
1203
|
+
const dest = path.join(IIIF_CACHE_MANIFESTS_DIR, slug + ".json");
|
|
1204
|
+
await fsp.writeFile(
|
|
1205
|
+
dest,
|
|
1206
|
+
JSON.stringify(normalizedManifest, null, 2),
|
|
1207
|
+
"utf8",
|
|
1208
|
+
);
|
|
1209
|
+
await saveProvenance(normalizedManifest);
|
|
1210
|
+
index.byId = Array.isArray(index.byId) ? index.byId : [];
|
|
1211
|
+
const nid = normalizeIiifId(id);
|
|
1212
|
+
const existingEntryIdx = index.byId.findIndex(
|
|
1213
|
+
(e) => e && normalizeIiifId(e.id) === nid && e.type === "Manifest",
|
|
1214
|
+
);
|
|
1215
|
+
const entry = {
|
|
1216
|
+
id: String(nid),
|
|
1217
|
+
type: "Manifest",
|
|
1218
|
+
slug,
|
|
1219
|
+
parent: parentId ? String(parentId) : "",
|
|
1220
|
+
};
|
|
1221
|
+
applyManifestEntryCanonical(entry, normalizedManifest, slug, getDefaultRoute("works"));
|
|
1222
|
+
if (existingEntryIdx >= 0) index.byId[existingEntryIdx] = entry;
|
|
1223
|
+
else index.byId.push(entry);
|
|
1224
|
+
await saveManifestIndex(index);
|
|
1225
|
+
return normalizedManifest;
|
|
1226
|
+
} catch (_) {
|
|
1227
|
+
return normalizedManifest;
|
|
1228
|
+
}
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1231
|
+
// Ensure any configured featured manifests are present in the local cache
|
|
1232
|
+
// (and have thumbnails computed) so interstitial heroes can read them.
|
|
1233
|
+
async function ensureFeaturedInCache(cfg) {
|
|
1234
|
+
try {
|
|
1235
|
+
const CONFIG = cfg || (await loadConfig());
|
|
1236
|
+
const featured = Array.isArray(CONFIG && CONFIG.featured)
|
|
1237
|
+
? CONFIG.featured
|
|
1238
|
+
: [];
|
|
1239
|
+
if (!featured.length) return;
|
|
1240
|
+
const {size: thumbSize, unsafe: unsafeThumbs} =
|
|
1241
|
+
resolveThumbnailPreferences();
|
|
1242
|
+
const fetchTimeoutMs = resolvePositiveInteger(
|
|
1243
|
+
process.env.CANOPY_FEATURED_FETCH_TIMEOUT_MS,
|
|
1244
|
+
DEFAULT_FEATURED_FETCH_TIMEOUT_MS,
|
|
1245
|
+
{allowZero: true},
|
|
1246
|
+
);
|
|
1247
|
+
const canAbortFetches =
|
|
1248
|
+
typeof AbortController === "function" && Number(fetchTimeoutMs) > 0;
|
|
1249
|
+
for (const rawId of featured) {
|
|
1250
|
+
const id = normalizeIiifId(String(rawId || ""));
|
|
1251
|
+
if (!id) continue;
|
|
1252
|
+
let manifest = await loadCachedManifestById(id);
|
|
1253
|
+
if (!manifest) {
|
|
1254
|
+
let controller = null;
|
|
1255
|
+
let timeoutId = null;
|
|
1256
|
+
let fetched = null;
|
|
1257
|
+
try {
|
|
1258
|
+
if (canAbortFetches) {
|
|
1259
|
+
controller = new AbortController();
|
|
1260
|
+
timeoutId = setTimeout(
|
|
1261
|
+
() => controller.abort(),
|
|
1262
|
+
Math.max(1, fetchTimeoutMs),
|
|
1263
|
+
);
|
|
1264
|
+
}
|
|
1265
|
+
fetched = await readJsonFromUri(id, {
|
|
1266
|
+
log: true,
|
|
1267
|
+
signal: controller ? controller.signal : undefined,
|
|
1268
|
+
});
|
|
1269
|
+
} catch (_) {
|
|
1270
|
+
fetched = null;
|
|
1271
|
+
} finally {
|
|
1272
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
1273
|
+
}
|
|
1274
|
+
if (!fetched) {
|
|
1275
|
+
if (controller && controller.signal && controller.signal.aborted) {
|
|
1276
|
+
try {
|
|
1277
|
+
logLine(
|
|
1278
|
+
`[iiif] Featured manifest timed out after ${formatDurationMs(
|
|
1279
|
+
fetchTimeoutMs,
|
|
1280
|
+
)}: ${id}`,
|
|
1281
|
+
"red",
|
|
1282
|
+
{dim: true},
|
|
1283
|
+
);
|
|
1284
|
+
} catch (_) {}
|
|
1285
|
+
}
|
|
1286
|
+
continue;
|
|
1287
|
+
}
|
|
1288
|
+
const upgraded = await upgradeIiifResource(fetched);
|
|
1289
|
+
if (!upgraded || !upgraded.id) continue;
|
|
1290
|
+
manifest = (await saveCachedManifest(upgraded, id, "")) || upgraded;
|
|
1291
|
+
manifest = (await loadCachedManifestById(id)) || manifest;
|
|
1292
|
+
}
|
|
1293
|
+
// Ensure thumbnail fields exist in index for this manifest (if computable)
|
|
1294
|
+
try {
|
|
1295
|
+
const t = await getThumbnail(manifest, thumbSize, unsafeThumbs);
|
|
1296
|
+
const idx = await loadManifestIndex();
|
|
1297
|
+
if (!Array.isArray(idx.byId)) continue;
|
|
1298
|
+
const entry = idx.byId.find(
|
|
1299
|
+
(e) =>
|
|
1300
|
+
e &&
|
|
1301
|
+
e.type === "Manifest" &&
|
|
1302
|
+
normalizeIiifId(String(e.id)) ===
|
|
1303
|
+
normalizeIiifId(String(manifest.id)),
|
|
1304
|
+
);
|
|
1305
|
+
if (!entry) continue;
|
|
1306
|
+
|
|
1307
|
+
let touched = false;
|
|
1308
|
+
if (t && t.url) {
|
|
1309
|
+
const nextUrl = String(t.url);
|
|
1310
|
+
if (entry.thumbnail !== nextUrl) {
|
|
1311
|
+
entry.thumbnail = nextUrl;
|
|
1312
|
+
touched = true;
|
|
1313
|
+
}
|
|
1314
|
+
if (typeof t.width === "number") {
|
|
1315
|
+
if (entry.thumbnailWidth !== t.width) touched = true;
|
|
1316
|
+
entry.thumbnailWidth = t.width;
|
|
1317
|
+
}
|
|
1318
|
+
if (typeof t.height === "number") {
|
|
1319
|
+
if (entry.thumbnailHeight !== t.height) touched = true;
|
|
1320
|
+
entry.thumbnailHeight = t.height;
|
|
1321
|
+
}
|
|
1322
|
+
}
|
|
1323
|
+
|
|
1324
|
+
try {
|
|
1325
|
+
const heroMedia = await resolveHeroMedia(manifest);
|
|
1326
|
+
if (heroMedia && heroMedia.heroThumbnail) {
|
|
1327
|
+
if (entry.heroThumbnail !== heroMedia.heroThumbnail) {
|
|
1328
|
+
entry.heroThumbnail = heroMedia.heroThumbnail;
|
|
1329
|
+
touched = true;
|
|
1330
|
+
}
|
|
1331
|
+
} else if (entry.heroThumbnail !== undefined) {
|
|
1332
|
+
delete entry.heroThumbnail;
|
|
1333
|
+
touched = true;
|
|
1334
|
+
}
|
|
1335
|
+
if (heroMedia && typeof heroMedia.heroThumbnailWidth === "number") {
|
|
1336
|
+
if (entry.heroThumbnailWidth !== heroMedia.heroThumbnailWidth)
|
|
1337
|
+
touched = true;
|
|
1338
|
+
entry.heroThumbnailWidth = heroMedia.heroThumbnailWidth;
|
|
1339
|
+
} else if (entry.heroThumbnailWidth !== undefined) {
|
|
1340
|
+
delete entry.heroThumbnailWidth;
|
|
1341
|
+
touched = true;
|
|
1342
|
+
}
|
|
1343
|
+
if (heroMedia && typeof heroMedia.heroThumbnailHeight === "number") {
|
|
1344
|
+
if (entry.heroThumbnailHeight !== heroMedia.heroThumbnailHeight)
|
|
1345
|
+
touched = true;
|
|
1346
|
+
entry.heroThumbnailHeight = heroMedia.heroThumbnailHeight;
|
|
1347
|
+
} else if (entry.heroThumbnailHeight !== undefined) {
|
|
1348
|
+
delete entry.heroThumbnailHeight;
|
|
1349
|
+
touched = true;
|
|
1350
|
+
}
|
|
1351
|
+
if (heroMedia && heroMedia.heroThumbnailSrcset) {
|
|
1352
|
+
if (entry.heroThumbnailSrcset !== heroMedia.heroThumbnailSrcset)
|
|
1353
|
+
touched = true;
|
|
1354
|
+
entry.heroThumbnailSrcset = heroMedia.heroThumbnailSrcset;
|
|
1355
|
+
if (entry.heroThumbnailSizes !== HERO_IMAGE_SIZES_ATTR)
|
|
1356
|
+
touched = true;
|
|
1357
|
+
entry.heroThumbnailSizes = HERO_IMAGE_SIZES_ATTR;
|
|
1358
|
+
} else {
|
|
1359
|
+
if (entry.heroThumbnailSrcset !== undefined) {
|
|
1360
|
+
delete entry.heroThumbnailSrcset;
|
|
1361
|
+
touched = true;
|
|
1362
|
+
}
|
|
1363
|
+
if (entry.heroThumbnailSizes !== undefined) {
|
|
1364
|
+
delete entry.heroThumbnailSizes;
|
|
1365
|
+
touched = true;
|
|
1366
|
+
}
|
|
1367
|
+
}
|
|
1368
|
+
if (heroMedia && heroMedia.ogImage) {
|
|
1369
|
+
if (entry.ogImage !== heroMedia.ogImage) {
|
|
1370
|
+
entry.ogImage = heroMedia.ogImage;
|
|
1371
|
+
touched = true;
|
|
1372
|
+
}
|
|
1373
|
+
if (typeof heroMedia.ogImageWidth === "number") {
|
|
1374
|
+
if (entry.ogImageWidth !== heroMedia.ogImageWidth) touched = true;
|
|
1375
|
+
entry.ogImageWidth = heroMedia.ogImageWidth;
|
|
1376
|
+
} else if (entry.ogImageWidth !== undefined) {
|
|
1377
|
+
delete entry.ogImageWidth;
|
|
1378
|
+
touched = true;
|
|
1379
|
+
}
|
|
1380
|
+
if (typeof heroMedia.ogImageHeight === "number") {
|
|
1381
|
+
if (entry.ogImageHeight !== heroMedia.ogImageHeight)
|
|
1382
|
+
touched = true;
|
|
1383
|
+
entry.ogImageHeight = heroMedia.ogImageHeight;
|
|
1384
|
+
} else if (entry.ogImageHeight !== undefined) {
|
|
1385
|
+
delete entry.ogImageHeight;
|
|
1386
|
+
touched = true;
|
|
1387
|
+
}
|
|
1388
|
+
} else if (entry.ogImage !== undefined) {
|
|
1389
|
+
delete entry.ogImage;
|
|
1390
|
+
if (entry.ogImageWidth !== undefined) delete entry.ogImageWidth;
|
|
1391
|
+
if (entry.ogImageHeight !== undefined) delete entry.ogImageHeight;
|
|
1392
|
+
touched = true;
|
|
1393
|
+
}
|
|
1394
|
+
if (
|
|
1395
|
+
ensureThumbnailValue(
|
|
1396
|
+
entry,
|
|
1397
|
+
heroMedia && heroMedia.heroThumbnail,
|
|
1398
|
+
heroMedia && heroMedia.heroThumbnailWidth,
|
|
1399
|
+
heroMedia && heroMedia.heroThumbnailHeight,
|
|
1400
|
+
)
|
|
1401
|
+
) {
|
|
1402
|
+
touched = true;
|
|
1403
|
+
}
|
|
1404
|
+
} catch (_) {}
|
|
1405
|
+
|
|
1406
|
+
if (touched) await saveManifestIndex(idx);
|
|
1407
|
+
} catch (_) {}
|
|
1408
|
+
}
|
|
1409
|
+
} catch (err) {
|
|
1410
|
+
const message = err && err.message ? err.message : err;
|
|
1411
|
+
throw new Error(`[iiif] Failed to populate featured cache: ${message}`);
|
|
1412
|
+
}
|
|
1413
|
+
}
|
|
1414
|
+
|
|
1415
|
+
async function flushManifestCache() {
|
|
1416
|
+
try {
|
|
1417
|
+
await fsp.rm(IIIF_CACHE_MANIFESTS_DIR, {recursive: true, force: true});
|
|
1418
|
+
} catch (_) {}
|
|
1419
|
+
ensureDirSync(IIIF_CACHE_MANIFESTS_DIR);
|
|
1420
|
+
ensureDirSync(IIIF_CACHE_COLLECTIONS_DIR);
|
|
1421
|
+
try {
|
|
1422
|
+
await fsp.rm(IIIF_CACHE_COLLECTIONS_DIR, {recursive: true, force: true});
|
|
1423
|
+
} catch (_) {}
|
|
1424
|
+
ensureDirSync(IIIF_CACHE_COLLECTIONS_DIR);
|
|
1425
|
+
}
|
|
1426
|
+
|
|
1427
|
+
// Collections cache helpers
|
|
1428
|
+
async function loadCachedCollectionById(id) {
|
|
1429
|
+
if (!id) return null;
|
|
1430
|
+
try {
|
|
1431
|
+
const index = await loadManifestIndex();
|
|
1432
|
+
let slug = null;
|
|
1433
|
+
if (Array.isArray(index.byId)) {
|
|
1434
|
+
const nid = normalizeIiifId(id);
|
|
1435
|
+
const entry = index.byId.find(
|
|
1436
|
+
(e) => e && normalizeIiifId(e.id) === nid && e.type === "Collection",
|
|
1437
|
+
);
|
|
1438
|
+
slug = entry && entry.slug;
|
|
1439
|
+
}
|
|
1440
|
+
if (isSlugTooLong(slug)) slug = null;
|
|
1441
|
+
if (!slug) {
|
|
1442
|
+
// Scan collections dir if mapping missing
|
|
1443
|
+
try {
|
|
1444
|
+
if (fs.existsSync(IIIF_CACHE_COLLECTIONS_DIR)) {
|
|
1445
|
+
const files = await fsp.readdir(IIIF_CACHE_COLLECTIONS_DIR);
|
|
1446
|
+
for (const name of files) {
|
|
1447
|
+
if (!name || !name.toLowerCase().endsWith(".json")) continue;
|
|
1448
|
+
const p = path.join(IIIF_CACHE_COLLECTIONS_DIR, name);
|
|
1449
|
+
try {
|
|
1450
|
+
const raw = await fsp.readFile(p, "utf8");
|
|
1451
|
+
const obj = JSON.parse(raw);
|
|
1452
|
+
const cid = normalizeIiifId(
|
|
1453
|
+
String((obj && (obj.id || obj["@id"])) || ""),
|
|
1454
|
+
);
|
|
1455
|
+
if (cid && cid === normalizeIiifId(String(id))) {
|
|
1456
|
+
const candidate = name.replace(/\.json$/i, "");
|
|
1457
|
+
if (isSlugTooLong(candidate)) {
|
|
1458
|
+
slug = null;
|
|
1459
|
+
break;
|
|
1460
|
+
}
|
|
1461
|
+
slug = candidate;
|
|
1462
|
+
// heal mapping
|
|
1463
|
+
try {
|
|
1464
|
+
index.byId = Array.isArray(index.byId) ? index.byId : [];
|
|
1465
|
+
const nid = normalizeIiifId(id);
|
|
1466
|
+
const existing = index.byId.findIndex(
|
|
1467
|
+
(e) =>
|
|
1468
|
+
e &&
|
|
1469
|
+
normalizeIiifId(e.id) === nid &&
|
|
1470
|
+
e.type === "Collection",
|
|
1471
|
+
);
|
|
1472
|
+
const entry = {
|
|
1473
|
+
id: String(nid),
|
|
1474
|
+
type: "Collection",
|
|
1475
|
+
slug,
|
|
1476
|
+
parent: "",
|
|
1477
|
+
};
|
|
1478
|
+
applyCollectionEntryCanonical(entry, null);
|
|
1479
|
+
if (existing >= 0) index.byId[existing] = entry;
|
|
1480
|
+
else index.byId.push(entry);
|
|
1481
|
+
await saveManifestIndex(index);
|
|
1482
|
+
} catch (_) {}
|
|
1483
|
+
break;
|
|
1484
|
+
}
|
|
1485
|
+
} catch (_) {}
|
|
1486
|
+
}
|
|
1487
|
+
}
|
|
1488
|
+
} catch (_) {}
|
|
1489
|
+
}
|
|
1490
|
+
if (!slug) return null;
|
|
1491
|
+
const p = path.join(IIIF_CACHE_COLLECTIONS_DIR, slug + ".json");
|
|
1492
|
+
if (!fs.existsSync(p)) return null;
|
|
1493
|
+
const data = await readJson(p);
|
|
1494
|
+
try {
|
|
1495
|
+
index.byId = Array.isArray(index.byId) ? index.byId : [];
|
|
1496
|
+
const nid = normalizeIiifId(id);
|
|
1497
|
+
const existingEntryIdx = index.byId.findIndex(
|
|
1498
|
+
(e) => e && normalizeIiifId(e.id) === nid && e.type === "Collection",
|
|
1499
|
+
);
|
|
1500
|
+
if (existingEntryIdx >= 0) {
|
|
1501
|
+
const entry = index.byId[existingEntryIdx];
|
|
1502
|
+
const prevCanonical =
|
|
1503
|
+
entry && entry.canonical ? String(entry.canonical) : "";
|
|
1504
|
+
const nextCanonical = applyCollectionEntryCanonical(entry, data);
|
|
1505
|
+
if (nextCanonical !== prevCanonical) {
|
|
1506
|
+
await saveManifestIndex(index);
|
|
1507
|
+
}
|
|
1508
|
+
}
|
|
1509
|
+
} catch (_) {}
|
|
1510
|
+
return data;
|
|
1511
|
+
} catch (_) {
|
|
1512
|
+
return null;
|
|
1513
|
+
}
|
|
1514
|
+
}
|
|
1515
|
+
|
|
1516
|
+
async function saveCachedCollection(collection, id, parentId) {
|
|
1517
|
+
try {
|
|
1518
|
+
const normalizedCollection = await upgradeIiifResource(collection);
|
|
1519
|
+
ensureDirSync(IIIF_CACHE_COLLECTIONS_DIR);
|
|
1520
|
+
const index = await loadManifestIndex();
|
|
1521
|
+
const title = firstLabelString(
|
|
1522
|
+
normalizedCollection && normalizedCollection.label,
|
|
1523
|
+
);
|
|
1524
|
+
const baseSlug =
|
|
1525
|
+
slugify(title || "collection", {
|
|
1526
|
+
lower: true,
|
|
1527
|
+
strict: true,
|
|
1528
|
+
trim: true,
|
|
1529
|
+
}) || "collection";
|
|
1530
|
+
const slug = computeUniqueSlug(index, baseSlug, id, "Collection");
|
|
1531
|
+
const dest = path.join(IIIF_CACHE_COLLECTIONS_DIR, slug + ".json");
|
|
1532
|
+
await fsp.writeFile(
|
|
1533
|
+
dest,
|
|
1534
|
+
JSON.stringify(normalizedCollection, null, 2),
|
|
1535
|
+
"utf8",
|
|
1536
|
+
);
|
|
1537
|
+
await saveProvenance(normalizedCollection);
|
|
1538
|
+
try {
|
|
1539
|
+
if (process.env.CANOPY_IIIF_DEBUG === "1") {
|
|
1540
|
+
const {logLine} = require("./log");
|
|
1541
|
+
logLine(`IIIF: saved collection → ${slug}.json`, "cyan", {dim: true});
|
|
1542
|
+
}
|
|
1543
|
+
} catch (_) {}
|
|
1544
|
+
index.byId = Array.isArray(index.byId) ? index.byId : [];
|
|
1545
|
+
const nid = normalizeIiifId(id);
|
|
1546
|
+
const existingEntryIdx = index.byId.findIndex(
|
|
1547
|
+
(e) => e && normalizeIiifId(e.id) === nid && e.type === "Collection",
|
|
1548
|
+
);
|
|
1549
|
+
const entry = {
|
|
1550
|
+
id: String(nid),
|
|
1551
|
+
type: "Collection",
|
|
1552
|
+
slug,
|
|
1553
|
+
parent: parentId ? String(parentId) : "",
|
|
1554
|
+
};
|
|
1555
|
+
applyCollectionEntryCanonical(entry, normalizedCollection);
|
|
1556
|
+
if (existingEntryIdx >= 0) index.byId[existingEntryIdx] = entry;
|
|
1557
|
+
else index.byId.push(entry);
|
|
1558
|
+
await saveManifestIndex(index);
|
|
1559
|
+
} catch (_) {}
|
|
1560
|
+
}
|
|
1561
|
+
|
|
1562
|
+
async function cleanupIiifCache(options = {}) {
|
|
1563
|
+
const allowedManifestIds = Array.isArray(options.allowedManifestIds)
|
|
1564
|
+
? options.allowedManifestIds
|
|
1565
|
+
: [];
|
|
1566
|
+
const allowedCollectionIds = Array.isArray(options.allowedCollectionIds)
|
|
1567
|
+
? options.allowedCollectionIds
|
|
1568
|
+
: [];
|
|
1569
|
+
const keepManifestIds = Array.isArray(options.keepManifestIds)
|
|
1570
|
+
? options.keepManifestIds
|
|
1571
|
+
: [];
|
|
1572
|
+
const manifestSet = new Set(
|
|
1573
|
+
allowedManifestIds
|
|
1574
|
+
.map((id) => normalizeIiifId(String(id || "")))
|
|
1575
|
+
.filter(Boolean),
|
|
1576
|
+
);
|
|
1577
|
+
for (const keepId of keepManifestIds) {
|
|
1578
|
+
const normalized = normalizeIiifId(String(keepId || ""));
|
|
1579
|
+
if (normalized) manifestSet.add(normalized);
|
|
1580
|
+
}
|
|
1581
|
+
const collectionSet = new Set(
|
|
1582
|
+
allowedCollectionIds
|
|
1583
|
+
.map((id) => normalizeIiifId(String(id || "")))
|
|
1584
|
+
.filter(Boolean),
|
|
1585
|
+
);
|
|
1586
|
+
if (!manifestSet.size && !collectionSet.size) return;
|
|
1587
|
+
|
|
1588
|
+
let removedManifestFiles = 0;
|
|
1589
|
+
if (fs.existsSync(IIIF_CACHE_MANIFESTS_DIR)) {
|
|
1590
|
+
const files = await fsp.readdir(IIIF_CACHE_MANIFESTS_DIR);
|
|
1591
|
+
for (const name of files) {
|
|
1592
|
+
if (!name || !name.toLowerCase().endsWith(".json")) continue;
|
|
1593
|
+
const fp = path.join(IIIF_CACHE_MANIFESTS_DIR, name);
|
|
1594
|
+
let manifest = null;
|
|
1595
|
+
try {
|
|
1596
|
+
manifest = await readJson(fp);
|
|
1597
|
+
} catch (_) {}
|
|
1598
|
+
const nid = normalizeIiifId(
|
|
1599
|
+
String(
|
|
1600
|
+
(manifest && (manifest.id || manifest["@id"])) ||
|
|
1601
|
+
name.replace(/\.json$/i, ""),
|
|
1602
|
+
),
|
|
1603
|
+
);
|
|
1604
|
+
if (!manifestSet.has(nid)) {
|
|
1605
|
+
try {
|
|
1606
|
+
await fsp.rm(fp, {force: true});
|
|
1607
|
+
removedManifestFiles += 1;
|
|
1608
|
+
} catch (_) {}
|
|
1609
|
+
}
|
|
1610
|
+
}
|
|
1611
|
+
}
|
|
1612
|
+
|
|
1613
|
+
let removedCollectionFiles = 0;
|
|
1614
|
+
if (fs.existsSync(IIIF_CACHE_COLLECTIONS_DIR)) {
|
|
1615
|
+
const files = await fsp.readdir(IIIF_CACHE_COLLECTIONS_DIR);
|
|
1616
|
+
for (const name of files) {
|
|
1617
|
+
if (!name || !name.toLowerCase().endsWith(".json")) continue;
|
|
1618
|
+
const fp = path.join(IIIF_CACHE_COLLECTIONS_DIR, name);
|
|
1619
|
+
let collection = null;
|
|
1620
|
+
try {
|
|
1621
|
+
collection = await readJson(fp);
|
|
1622
|
+
} catch (_) {}
|
|
1623
|
+
const nid = normalizeIiifId(
|
|
1624
|
+
String(
|
|
1625
|
+
(collection && (collection.id || collection["@id"])) ||
|
|
1626
|
+
name.replace(/\.json$/i, ""),
|
|
1627
|
+
),
|
|
1628
|
+
);
|
|
1629
|
+
if (!collectionSet.has(nid)) {
|
|
1630
|
+
try {
|
|
1631
|
+
await fsp.rm(fp, {force: true});
|
|
1632
|
+
removedCollectionFiles += 1;
|
|
1633
|
+
} catch (_) {}
|
|
1634
|
+
}
|
|
1635
|
+
}
|
|
1636
|
+
}
|
|
1637
|
+
|
|
1638
|
+
try {
|
|
1639
|
+
const index = await loadManifestIndex();
|
|
1640
|
+
if (Array.isArray(index.byId)) {
|
|
1641
|
+
index.byId = index.byId.filter((entry) => {
|
|
1642
|
+
if (!entry || !entry.id) return false;
|
|
1643
|
+
const nid = normalizeIiifId(String(entry.id));
|
|
1644
|
+
if (entry.type === "Manifest") return manifestSet.has(nid);
|
|
1645
|
+
if (entry.type === "Collection") return collectionSet.has(nid);
|
|
1646
|
+
return true;
|
|
1647
|
+
});
|
|
1648
|
+
await saveManifestIndex(index);
|
|
1649
|
+
}
|
|
1650
|
+
} catch (_) {}
|
|
1651
|
+
|
|
1652
|
+
try {
|
|
1653
|
+
logLine(
|
|
1654
|
+
`• Cleaned IIIF cache (${removedManifestFiles} Manifest file(s) removed, ${removedCollectionFiles} Collection file(s) removed)`,
|
|
1655
|
+
"blue",
|
|
1656
|
+
{dim: true},
|
|
1657
|
+
);
|
|
1658
|
+
} catch (_) {}
|
|
1659
|
+
}
|
|
1660
|
+
|
|
1661
|
+
async function loadConfig() {
|
|
1662
|
+
const cfgPath = resolveCanopyConfigPath();
|
|
1663
|
+
if (!fs.existsSync(cfgPath)) return {};
|
|
1664
|
+
const raw = await fsp.readFile(cfgPath, "utf8");
|
|
1665
|
+
let cfg = {};
|
|
1666
|
+
try {
|
|
1667
|
+
cfg = yaml.load(raw) || {};
|
|
1668
|
+
} catch (_) {
|
|
1669
|
+
cfg = {};
|
|
1670
|
+
}
|
|
1671
|
+
return cfg || {};
|
|
1672
|
+
}
|
|
1673
|
+
|
|
1674
|
+
// Traverse IIIF collection, cache manifests/collections, and render pages
|
|
1675
|
+
async function buildIiifCollectionPages(CONFIG) {
|
|
1676
|
+
const cfg = CONFIG || (await loadConfig());
|
|
1677
|
+
const discovery = createIngestionReport(cfg);
|
|
1678
|
+
|
|
1679
|
+
const {collections: collectionUris, manifests: manifestUris} =
|
|
1680
|
+
resolveIiifSources(cfg);
|
|
1681
|
+
if (!collectionUris.length && !manifestUris.length) return {iiifRecords: []};
|
|
1682
|
+
|
|
1683
|
+
const searchIndexCfg = (cfg && cfg.search && cfg.search.index) || {};
|
|
1684
|
+
const metadataCfg = (searchIndexCfg && searchIndexCfg.metadata) || {};
|
|
1685
|
+
const summaryCfg = (searchIndexCfg && searchIndexCfg.summary) || {};
|
|
1686
|
+
const annotationsCfg = (searchIndexCfg && searchIndexCfg.annotations) || {};
|
|
1687
|
+
const metadataEnabled =
|
|
1688
|
+
metadataCfg && Object.prototype.hasOwnProperty.call(metadataCfg, "enabled")
|
|
1689
|
+
? resolveBoolean(metadataCfg.enabled)
|
|
1690
|
+
: true;
|
|
1691
|
+
const summaryEnabled =
|
|
1692
|
+
summaryCfg && Object.prototype.hasOwnProperty.call(summaryCfg, "enabled")
|
|
1693
|
+
? resolveBoolean(summaryCfg.enabled)
|
|
1694
|
+
: true;
|
|
1695
|
+
const annotationsEnabled =
|
|
1696
|
+
annotationsCfg &&
|
|
1697
|
+
Object.prototype.hasOwnProperty.call(annotationsCfg, "enabled")
|
|
1698
|
+
? resolveBoolean(annotationsCfg.enabled)
|
|
1699
|
+
: false;
|
|
1700
|
+
const metadataIncludeAll = metadataEnabled && resolveBoolean(metadataCfg.all);
|
|
1701
|
+
const metadataLabelsRaw = Array.isArray(cfg && cfg.metadata)
|
|
1702
|
+
? cfg.metadata
|
|
1703
|
+
: [];
|
|
1704
|
+
const metadataLabelsNormalized = metadataLabelsRaw
|
|
1705
|
+
.map((label) => normalizeMetadataLabel(String(label || "")))
|
|
1706
|
+
.filter(Boolean);
|
|
1707
|
+
const metadataLabelSet = new Set(metadataLabelsNormalized);
|
|
1708
|
+
const metadataFacetLabels = (() => {
|
|
1709
|
+
if (!Array.isArray(metadataLabelsRaw) || !metadataLabelsRaw.length)
|
|
1710
|
+
return [];
|
|
1711
|
+
const seen = new Set();
|
|
1712
|
+
const entries = [];
|
|
1713
|
+
for (const label of metadataLabelsRaw) {
|
|
1714
|
+
const raw =
|
|
1715
|
+
typeof label === "string" ? label.trim() : String(label || "");
|
|
1716
|
+
if (!raw) continue;
|
|
1717
|
+
const normalized = normalizeMetadataLabel(raw);
|
|
1718
|
+
if (!normalized || seen.has(normalized)) continue;
|
|
1719
|
+
const slug = slugify(raw, {lower: true, strict: true, trim: true});
|
|
1720
|
+
if (!slug) continue;
|
|
1721
|
+
seen.add(normalized);
|
|
1722
|
+
entries.push({label: raw, slug, normalized});
|
|
1723
|
+
}
|
|
1724
|
+
return entries;
|
|
1725
|
+
})();
|
|
1726
|
+
const metadataLabelSlugMap = new Map();
|
|
1727
|
+
for (const entry of metadataFacetLabels) {
|
|
1728
|
+
if (!entry || !entry.normalized) continue;
|
|
1729
|
+
metadataLabelSlugMap.set(entry.normalized, entry.slug || "");
|
|
1730
|
+
}
|
|
1731
|
+
const metadataOptions = {
|
|
1732
|
+
enabled:
|
|
1733
|
+
metadataEnabled &&
|
|
1734
|
+
(metadataIncludeAll || (metadataLabelSet && metadataLabelSet.size > 0)),
|
|
1735
|
+
includeAll: metadataIncludeAll,
|
|
1736
|
+
labelsSet: metadataIncludeAll ? null : metadataLabelSet,
|
|
1737
|
+
};
|
|
1738
|
+
const metadataIndexMap = new Map();
|
|
1739
|
+
const metadataDynamicOrder = [];
|
|
1740
|
+
const metadataDynamicOrderSet = new Set();
|
|
1741
|
+
const metadataCollectAllLabels = metadataLabelsNormalized.length === 0;
|
|
1742
|
+
function recordMetadataIndexEntry(entry) {
|
|
1743
|
+
if (!entry || !entry.normalized || !Array.isArray(entry.values)) return;
|
|
1744
|
+
if (!entry.values.length) return;
|
|
1745
|
+
const normalized = entry.normalized;
|
|
1746
|
+
if (!metadataCollectAllLabels && !metadataLabelSet.has(normalized)) return;
|
|
1747
|
+
let record = metadataIndexMap.get(normalized);
|
|
1748
|
+
const labelText = entry.label || normalized;
|
|
1749
|
+
if (!record) {
|
|
1750
|
+
const labelSlug =
|
|
1751
|
+
metadataLabelSlugMap.get(normalized) ||
|
|
1752
|
+
slugify(labelText, {lower: true, strict: true, trim: true}) ||
|
|
1753
|
+
normalized;
|
|
1754
|
+
record = {
|
|
1755
|
+
normalized,
|
|
1756
|
+
label: labelText,
|
|
1757
|
+
slug: labelSlug || normalized,
|
|
1758
|
+
values: new Map(),
|
|
1759
|
+
};
|
|
1760
|
+
metadataIndexMap.set(normalized, record);
|
|
1761
|
+
if (metadataCollectAllLabels && !metadataDynamicOrderSet.has(normalized)) {
|
|
1762
|
+
metadataDynamicOrderSet.add(normalized);
|
|
1763
|
+
metadataDynamicOrder.push(normalized);
|
|
1764
|
+
}
|
|
1765
|
+
} else if (!record.label && labelText) {
|
|
1766
|
+
record.label = labelText;
|
|
1767
|
+
}
|
|
1768
|
+
const valueMap = record.values;
|
|
1769
|
+
for (const text of entry.values) {
|
|
1770
|
+
const trimmed = String(text || "").trim();
|
|
1771
|
+
if (!trimmed) continue;
|
|
1772
|
+
const valueSlug =
|
|
1773
|
+
slugify(trimmed, {lower: true, strict: true, trim: true}) ||
|
|
1774
|
+
trimmed.toLowerCase().replace(/[^a-z0-9]+/g, "-") ||
|
|
1775
|
+
trimmed;
|
|
1776
|
+
if (!valueSlug) continue;
|
|
1777
|
+
if (valueMap.has(valueSlug)) {
|
|
1778
|
+
valueMap.get(valueSlug).count += 1;
|
|
1779
|
+
} else {
|
|
1780
|
+
valueMap.set(valueSlug, {value: trimmed, slug: valueSlug, count: 1});
|
|
1781
|
+
}
|
|
1782
|
+
}
|
|
1783
|
+
}
|
|
1784
|
+
|
|
1785
|
+
const summaryOptions = {
|
|
1786
|
+
enabled: summaryEnabled,
|
|
1787
|
+
};
|
|
1788
|
+
const annotationMotivations = new Set(
|
|
1789
|
+
normalizeStringList(annotationsCfg && annotationsCfg.motivation).map((m) =>
|
|
1790
|
+
m.toLowerCase(),
|
|
1791
|
+
),
|
|
1792
|
+
);
|
|
1793
|
+
const annotationsOptions = {
|
|
1794
|
+
enabled: annotationsEnabled,
|
|
1795
|
+
motivations: annotationMotivations,
|
|
1796
|
+
};
|
|
1797
|
+
|
|
1798
|
+
// Recursively traverse Collections and gather all Manifest tasks
|
|
1799
|
+
const tasks = [];
|
|
1800
|
+
let manifestTasksFromCollections = 0;
|
|
1801
|
+
let manifestTasksFromConfig = 0;
|
|
1802
|
+
const queuedManifestIds = new Set();
|
|
1803
|
+
const visitedCollections = new Set(); // normalized ids
|
|
1804
|
+
const renderedManifestIds = new Set();
|
|
1805
|
+
const norm = (x) => {
|
|
1806
|
+
try {
|
|
1807
|
+
return normalizeIiifId(String(x || ""));
|
|
1808
|
+
} catch (_) {
|
|
1809
|
+
return String(x || "");
|
|
1810
|
+
}
|
|
1811
|
+
};
|
|
1812
|
+
async function gatherFromCollection(colLike, parentId, requestedId) {
|
|
1813
|
+
try {
|
|
1814
|
+
// Resolve the URI we were asked to fetch. Some providers (e.g. Internet Archive)
|
|
1815
|
+
// return paged collections where the JSON payload's `id` does not match the
|
|
1816
|
+
// URI that served it. We rely on the requested URI as the stable, unique key
|
|
1817
|
+
// so pagination continues even when `id` flips back to the root collection.
|
|
1818
|
+
const uri =
|
|
1819
|
+
typeof colLike === "string"
|
|
1820
|
+
? colLike
|
|
1821
|
+
: (colLike && (colLike.id || colLike["@id"])) || "";
|
|
1822
|
+
const col =
|
|
1823
|
+
typeof colLike === "object" && colLike && colLike.items
|
|
1824
|
+
? colLike
|
|
1825
|
+
: await readJsonFromUri(uri, {log: true});
|
|
1826
|
+
if (!col) {
|
|
1827
|
+
discovery.failure(uri);
|
|
1828
|
+
return;
|
|
1829
|
+
}
|
|
1830
|
+
const ncol = await upgradeIiifResource(col);
|
|
1831
|
+
const reportedId = String(
|
|
1832
|
+
(ncol && (ncol.id || ncol["@id"])) ||
|
|
1833
|
+
(typeof colLike === "object" && (colLike.id || colLike["@id"])) ||
|
|
1834
|
+
"",
|
|
1835
|
+
);
|
|
1836
|
+
const effectiveId = String(uri || reportedId || "");
|
|
1837
|
+
const collectionKey = effectiveId || reportedId || uri || "";
|
|
1838
|
+
const visitKey = norm(collectionKey) || collectionKey;
|
|
1839
|
+
const childEntries = extractCollectionEntries(ncol);
|
|
1840
|
+
// A configured root may already have been traversed as another root's child.
|
|
1841
|
+
// Record its entry point and source aliases before the traversal cycle guard.
|
|
1842
|
+
discovery.collection(
|
|
1843
|
+
ncol,
|
|
1844
|
+
childEntries.map((entry) => ({
|
|
1845
|
+
...entry.raw,
|
|
1846
|
+
id: entry.id,
|
|
1847
|
+
type: normalizeIiifType(entry.type || entry.fallback) === "collection"
|
|
1848
|
+
? "Collection" : "Manifest",
|
|
1849
|
+
})),
|
|
1850
|
+
!parentId,
|
|
1851
|
+
requestedId || uri,
|
|
1852
|
+
);
|
|
1853
|
+
if (visitedCollections.has(visitKey)) return; // avoid cycles
|
|
1854
|
+
visitedCollections.add(visitKey);
|
|
1855
|
+
try {
|
|
1856
|
+
await saveCachedCollection(ncol, collectionKey, parentId || "");
|
|
1857
|
+
} catch (_) {}
|
|
1858
|
+
for (const entry of childEntries) {
|
|
1859
|
+
const entryId = entry && entry.id;
|
|
1860
|
+
if (!entryId) continue;
|
|
1861
|
+
const entryType = normalizeIiifType(entry.type || entry.fallback || "");
|
|
1862
|
+
const dedupeKey = norm(entryId) || String(entryId || "");
|
|
1863
|
+
if (!dedupeKey) continue;
|
|
1864
|
+
if (entryType === "manifest") {
|
|
1865
|
+
if (queuedManifestIds.has(dedupeKey)) continue;
|
|
1866
|
+
queuedManifestIds.add(dedupeKey);
|
|
1867
|
+
tasks.push({id: entryId, parent: collectionKey});
|
|
1868
|
+
manifestTasksFromCollections += 1;
|
|
1869
|
+
} else if (entryType === "collection") {
|
|
1870
|
+
await gatherFromCollection(entry.raw || entryId, collectionKey);
|
|
1871
|
+
}
|
|
1872
|
+
}
|
|
1873
|
+
// Handle IIIF Presentation 2 paged collections (first/next pattern).
|
|
1874
|
+
// The root of a paged v2 collection carries a `first` link instead of
|
|
1875
|
+
// embedding items; each page carries a `next` link to the following page.
|
|
1876
|
+
const resolvePageUrl = (val) => {
|
|
1877
|
+
if (!val) return "";
|
|
1878
|
+
if (typeof val === "string") return val.trim();
|
|
1879
|
+
const id = val["@id"] || val.id;
|
|
1880
|
+
return typeof id === "string" ? id.trim() : "";
|
|
1881
|
+
};
|
|
1882
|
+
// Start from `first` if present (root collection), otherwise `next` (current is a page).
|
|
1883
|
+
let pageUrl =
|
|
1884
|
+
resolvePageUrl(col && col.first) ||
|
|
1885
|
+
resolvePageUrl(ncol && ncol.first) ||
|
|
1886
|
+
resolvePageUrl(col && col.next) ||
|
|
1887
|
+
resolvePageUrl(ncol && ncol.next);
|
|
1888
|
+
while (pageUrl) {
|
|
1889
|
+
const pageKey = norm(pageUrl) || pageUrl;
|
|
1890
|
+
if (visitedCollections.has(pageKey)) break;
|
|
1891
|
+
visitedCollections.add(pageKey);
|
|
1892
|
+
const page = await readJsonFromUri(pageUrl, {log: true});
|
|
1893
|
+
if (!page) {
|
|
1894
|
+
discovery.failure(pageUrl);
|
|
1895
|
+
break;
|
|
1896
|
+
}
|
|
1897
|
+
const npage = await upgradeIiifResource(page);
|
|
1898
|
+
const pageEntries = extractCollectionEntries(npage);
|
|
1899
|
+
discovery.collection(
|
|
1900
|
+
ncol,
|
|
1901
|
+
pageEntries.map((entry) => ({
|
|
1902
|
+
...entry.raw,
|
|
1903
|
+
id: entry.id,
|
|
1904
|
+
type: normalizeIiifType(entry.type || entry.fallback) === "collection"
|
|
1905
|
+
? "Collection" : "Manifest",
|
|
1906
|
+
})),
|
|
1907
|
+
!parentId,
|
|
1908
|
+
);
|
|
1909
|
+
for (const entry of pageEntries) {
|
|
1910
|
+
const entryId = entry && entry.id;
|
|
1911
|
+
if (!entryId) continue;
|
|
1912
|
+
const entryType = normalizeIiifType(entry.type || entry.fallback || "");
|
|
1913
|
+
const dedupeKey = norm(entryId) || String(entryId || "");
|
|
1914
|
+
if (!dedupeKey) continue;
|
|
1915
|
+
if (entryType === "manifest") {
|
|
1916
|
+
if (queuedManifestIds.has(dedupeKey)) continue;
|
|
1917
|
+
queuedManifestIds.add(dedupeKey);
|
|
1918
|
+
tasks.push({id: entryId, parent: collectionKey});
|
|
1919
|
+
manifestTasksFromCollections += 1;
|
|
1920
|
+
} else if (entryType === "collection") {
|
|
1921
|
+
await gatherFromCollection(entry.raw || entryId, collectionKey);
|
|
1922
|
+
}
|
|
1923
|
+
}
|
|
1924
|
+
// Advance to the next page.
|
|
1925
|
+
pageUrl = resolvePageUrl((page && page.next) || (npage && npage.next));
|
|
1926
|
+
}
|
|
1927
|
+
// Traverse strictly by parent/child hierarchy (Presentation 3): items → Manifest or Collection
|
|
1928
|
+
} catch (_) {
|
|
1929
|
+
discovery.failure(typeof colLike === "string" ? colLike : colLike?.id);
|
|
1930
|
+
}
|
|
1931
|
+
}
|
|
1932
|
+
// Fetch each configured collection and queue manifests from all of them
|
|
1933
|
+
logLine("• Traversing IIIF Collection(s)", "blue", {dim: true});
|
|
1934
|
+
for (const uri of collectionUris) {
|
|
1935
|
+
let root = null;
|
|
1936
|
+
try {
|
|
1937
|
+
root = await readJsonFromUri(uri, {log: true});
|
|
1938
|
+
} catch (_) {
|
|
1939
|
+
root = null;
|
|
1940
|
+
}
|
|
1941
|
+
if (!root) {
|
|
1942
|
+
discovery.failure(uri);
|
|
1943
|
+
try {
|
|
1944
|
+
logLine(`IIIF: Failed to fetch collection → ${uri}`, "red");
|
|
1945
|
+
} catch (_) {}
|
|
1946
|
+
continue;
|
|
1947
|
+
}
|
|
1948
|
+
const normalizedRoot = await upgradeIiifResource(root);
|
|
1949
|
+
try {
|
|
1950
|
+
await saveCachedCollection(normalizedRoot, normalizedRoot.id || uri, "");
|
|
1951
|
+
} catch (_) {}
|
|
1952
|
+
await gatherFromCollection(normalizedRoot, "", uri);
|
|
1953
|
+
}
|
|
1954
|
+
if (manifestUris.length) {
|
|
1955
|
+
for (const uri of manifestUris) {
|
|
1956
|
+
const dedupeKey = norm(uri) || String(uri || "");
|
|
1957
|
+
if (!dedupeKey || queuedManifestIds.has(dedupeKey)) continue;
|
|
1958
|
+
queuedManifestIds.add(dedupeKey);
|
|
1959
|
+
tasks.push({id: uri, parent: ""});
|
|
1960
|
+
manifestTasksFromConfig += 1;
|
|
1961
|
+
}
|
|
1962
|
+
}
|
|
1963
|
+
if (!tasks.length) return {
|
|
1964
|
+
iiifRecords: [],
|
|
1965
|
+
discovery: discovery.result(),
|
|
1966
|
+
collectionIds: Array.from(visitedCollections),
|
|
1967
|
+
manifestIds: [],
|
|
1968
|
+
};
|
|
1969
|
+
try {
|
|
1970
|
+
logLine(
|
|
1971
|
+
`• Processing ${tasks.length} Manifest(s) (${manifestTasksFromCollections} from collections, ${manifestTasksFromConfig} direct)`,
|
|
1972
|
+
"blue",
|
|
1973
|
+
{dim: true},
|
|
1974
|
+
);
|
|
1975
|
+
} catch (_) {}
|
|
1976
|
+
logDebug(
|
|
1977
|
+
`Queued ${tasks.length} Manifest task(s) (${manifestTasksFromCollections} from collections, ${manifestTasksFromConfig} direct)`,
|
|
1978
|
+
);
|
|
1979
|
+
|
|
1980
|
+
// Split into chunks and process with limited concurrency
|
|
1981
|
+
const chunkSize = resolvePositiveInteger(
|
|
1982
|
+
process.env.CANOPY_CHUNK_SIZE,
|
|
1983
|
+
DEFAULT_CHUNK_SIZE,
|
|
1984
|
+
);
|
|
1985
|
+
const chunks = Math.ceil(tasks.length / chunkSize);
|
|
1986
|
+
const requestedConcurrency = resolvePositiveInteger(
|
|
1987
|
+
process.env.CANOPY_FETCH_CONCURRENCY,
|
|
1988
|
+
DEFAULT_FETCH_CONCURRENCY,
|
|
1989
|
+
{allowZero: true},
|
|
1990
|
+
);
|
|
1991
|
+
// Summary before processing chunks
|
|
1992
|
+
try {
|
|
1993
|
+
logLine(
|
|
1994
|
+
`• Fetching ${tasks.length} Manifest(s) in ${chunks} chunk(s)`,
|
|
1995
|
+
"blue",
|
|
1996
|
+
{dim: true},
|
|
1997
|
+
);
|
|
1998
|
+
const concurrencySummary =
|
|
1999
|
+
requestedConcurrency === 0
|
|
2000
|
+
? "auto (no explicit cap)"
|
|
2001
|
+
: String(requestedConcurrency);
|
|
2002
|
+
logLine(`• Fetch concurrency: ${concurrencySummary}`, "blue", {dim: true});
|
|
2003
|
+
} catch (_) {}
|
|
2004
|
+
const iiifRecords = [];
|
|
2005
|
+
const navPlaceRecords = [];
|
|
2006
|
+
const {size: thumbSize, unsafe: unsafeThumbs} = resolveThumbnailPreferences();
|
|
2007
|
+
let workRouteEntries = getLocaleRouteEntries("works");
|
|
2008
|
+
if (!workRouteEntries.length) {
|
|
2009
|
+
workRouteEntries = [
|
|
2010
|
+
{
|
|
2011
|
+
locale: getDefaultLocaleCode(),
|
|
2012
|
+
route: getDefaultRoute("works"),
|
|
2013
|
+
isDefault: true,
|
|
2014
|
+
},
|
|
2015
|
+
];
|
|
2016
|
+
}
|
|
2017
|
+
|
|
2018
|
+
// Compile the works layout component once per run
|
|
2019
|
+
const worksLayoutPath = path.join(CONTENT_DIR, "works", "_layout.mdx");
|
|
2020
|
+
if (!fs.existsSync(worksLayoutPath)) {
|
|
2021
|
+
throw new Error(
|
|
2022
|
+
"IIIF build requires content/works/_layout.mdx. Create the layout instead of relying on generated output.",
|
|
2023
|
+
);
|
|
2024
|
+
}
|
|
2025
|
+
let WorksLayoutComp = null;
|
|
2026
|
+
try {
|
|
2027
|
+
WorksLayoutComp = await mdx.compileMdxToComponent(worksLayoutPath);
|
|
2028
|
+
} catch (err) {
|
|
2029
|
+
const message = err && err.message ? err.message : err;
|
|
2030
|
+
throw new Error(`Failed to compile content/works/_layout.mdx: ${message}`);
|
|
2031
|
+
}
|
|
2032
|
+
|
|
2033
|
+
referenced.ensureReferenceIndex();
|
|
2034
|
+
|
|
2035
|
+
const chunkMetrics = [];
|
|
2036
|
+
for (let ci = 0; ci < chunks; ci++) {
|
|
2037
|
+
const chunk = tasks.slice(ci * chunkSize, (ci + 1) * chunkSize);
|
|
2038
|
+
logLine(`• Chunk ${ci + 1}/${chunks}`, "blue", {dim: true});
|
|
2039
|
+
const chunkStart = Date.now();
|
|
2040
|
+
|
|
2041
|
+
const concurrency =
|
|
2042
|
+
requestedConcurrency === 0
|
|
2043
|
+
? Math.max(1, chunk.length)
|
|
2044
|
+
: requestedConcurrency;
|
|
2045
|
+
let next = 0;
|
|
2046
|
+
const logs = new Array(chunk.length);
|
|
2047
|
+
let nextPrint = 0;
|
|
2048
|
+
function tryFlush() {
|
|
2049
|
+
try {
|
|
2050
|
+
while (nextPrint < logs.length && logs[nextPrint]) {
|
|
2051
|
+
const lines = logs[nextPrint];
|
|
2052
|
+
for (const [txt, color, opts] of lines) {
|
|
2053
|
+
try {
|
|
2054
|
+
logLine(txt, color, opts);
|
|
2055
|
+
} catch (_) {}
|
|
2056
|
+
}
|
|
2057
|
+
logs[nextPrint] = null;
|
|
2058
|
+
nextPrint++;
|
|
2059
|
+
}
|
|
2060
|
+
} catch (_) {}
|
|
2061
|
+
}
|
|
2062
|
+
async function worker() {
|
|
2063
|
+
for (;;) {
|
|
2064
|
+
const it = chunk[next++];
|
|
2065
|
+
if (!it) break;
|
|
2066
|
+
const idx = next - 1;
|
|
2067
|
+
const id = it.id || it["@id"] || "";
|
|
2068
|
+
discovery.request(id);
|
|
2069
|
+
let manifest = await loadCachedManifestById(id);
|
|
2070
|
+
const lns = [];
|
|
2071
|
+
if (manifest) {
|
|
2072
|
+
lns.push([`↓ ${String(id)} → Cached`, "yellow"]);
|
|
2073
|
+
} else if (/^https?:\/\//i.test(String(id || ""))) {
|
|
2074
|
+
try {
|
|
2075
|
+
const res = await fetch(String(id), {
|
|
2076
|
+
headers: {Accept: "application/json"},
|
|
2077
|
+
}).catch(() => null);
|
|
2078
|
+
if (res && res.ok) {
|
|
2079
|
+
lns.push([`↓ ${String(id)} → ${res.status}`, "yellow"]);
|
|
2080
|
+
const remote = rememberRetrieved(await res.json(), id);
|
|
2081
|
+
manifest = await upgradeIiifResource(remote);
|
|
2082
|
+
const saved = await saveCachedManifest(
|
|
2083
|
+
manifest,
|
|
2084
|
+
String(id),
|
|
2085
|
+
String(it.parent || ""),
|
|
2086
|
+
);
|
|
2087
|
+
manifest = saved || manifest;
|
|
2088
|
+
const cached = await loadCachedManifestById(String(id));
|
|
2089
|
+
if (cached) manifest = cached;
|
|
2090
|
+
} else {
|
|
2091
|
+
lns.push([
|
|
2092
|
+
`⊘ ${String(id)} → ${res ? res.status : "ERR"}`,
|
|
2093
|
+
"red",
|
|
2094
|
+
]);
|
|
2095
|
+
continue;
|
|
2096
|
+
}
|
|
2097
|
+
} catch (e) {
|
|
2098
|
+
lns.push([`⊘ ${String(id)} → ERR`, "red"]);
|
|
2099
|
+
continue;
|
|
2100
|
+
}
|
|
2101
|
+
} else if (/^file:\/\//i.test(String(id || ""))) {
|
|
2102
|
+
try {
|
|
2103
|
+
const local = await readJsonFromUri(String(id), {log: false});
|
|
2104
|
+
if (!local) {
|
|
2105
|
+
lns.push([`⊘ ${String(id)} → ERR`, "red"]);
|
|
2106
|
+
continue;
|
|
2107
|
+
}
|
|
2108
|
+
manifest = await upgradeIiifResource(local);
|
|
2109
|
+
const saved = await saveCachedManifest(
|
|
2110
|
+
manifest,
|
|
2111
|
+
String(id),
|
|
2112
|
+
String(it.parent || ""),
|
|
2113
|
+
);
|
|
2114
|
+
manifest = saved || manifest;
|
|
2115
|
+
const cached = await loadCachedManifestById(String(id));
|
|
2116
|
+
if (cached) manifest = cached;
|
|
2117
|
+
lns.push([`↓ ${String(id)} → Cached`, "yellow"]);
|
|
2118
|
+
} catch (_) {
|
|
2119
|
+
lns.push([`⊘ ${String(id)} → ERR`, "red"]);
|
|
2120
|
+
continue;
|
|
2121
|
+
}
|
|
2122
|
+
} else if (!/^[a-zA-Z][a-zA-Z0-9+\-.]*:/.test(String(id || ""))) {
|
|
2123
|
+
// Relative or bare file path (no scheme) — resolve against cwd
|
|
2124
|
+
try {
|
|
2125
|
+
const local = await readJsonFromUri(String(id), {log: false});
|
|
2126
|
+
if (!local) {
|
|
2127
|
+
lns.push([`⊘ ${String(id)} → ERR`, "red"]);
|
|
2128
|
+
continue;
|
|
2129
|
+
}
|
|
2130
|
+
manifest = await upgradeIiifResource(local);
|
|
2131
|
+
const saved = await saveCachedManifest(
|
|
2132
|
+
manifest,
|
|
2133
|
+
String(id),
|
|
2134
|
+
String(it.parent || ""),
|
|
2135
|
+
);
|
|
2136
|
+
manifest = saved || manifest;
|
|
2137
|
+
const cached = await loadCachedManifestById(String(id));
|
|
2138
|
+
if (cached) manifest = cached;
|
|
2139
|
+
lns.push([`↓ ${String(id)} → Cached`, "yellow"]);
|
|
2140
|
+
} catch (_) {
|
|
2141
|
+
lns.push([`⊘ ${String(id)} → ERR`, "red"]);
|
|
2142
|
+
continue;
|
|
2143
|
+
}
|
|
2144
|
+
} else {
|
|
2145
|
+
lns.push([`⊘ ${String(id)} → SKIP`, "red"]);
|
|
2146
|
+
continue;
|
|
2147
|
+
}
|
|
2148
|
+
if (!manifest) continue;
|
|
2149
|
+
const ensured = await ensurePresentation3Manifest(manifest);
|
|
2150
|
+
manifest = ensured.manifest;
|
|
2151
|
+
discovery.manifest(manifest, id);
|
|
2152
|
+
const title = firstLabelString(manifest.label);
|
|
2153
|
+
const manifestLabel = title || String(manifest.id || id);
|
|
2154
|
+
logDebug(`Preparing manifest ${manifestLabel}`);
|
|
2155
|
+
let summaryRaw = "";
|
|
2156
|
+
try {
|
|
2157
|
+
summaryRaw = extractSummaryValues(manifest);
|
|
2158
|
+
} catch (_) {
|
|
2159
|
+
summaryRaw = "";
|
|
2160
|
+
}
|
|
2161
|
+
const summaryForMeta = truncateSummary(summaryRaw || title);
|
|
2162
|
+
const baseSlug =
|
|
2163
|
+
slugify(title || "untitled", {
|
|
2164
|
+
lower: true,
|
|
2165
|
+
strict: true,
|
|
2166
|
+
trim: true,
|
|
2167
|
+
}) || "untitled";
|
|
2168
|
+
const nid = normalizeIiifId(String(manifest.id || id));
|
|
2169
|
+
let idxMap = await loadManifestIndex();
|
|
2170
|
+
idxMap.byId = Array.isArray(idxMap.byId) ? idxMap.byId : [];
|
|
2171
|
+
let mEntry = idxMap.byId.find(
|
|
2172
|
+
(e) => e && e.type === "Manifest" && normalizeIiifId(e.id) === nid,
|
|
2173
|
+
);
|
|
2174
|
+
let slug = mEntry && mEntry.slug;
|
|
2175
|
+
if (isSlugTooLong(slug)) slug = null;
|
|
2176
|
+
if (!slug) {
|
|
2177
|
+
slug = computeUniqueSlug(idxMap, baseSlug, nid, "Manifest");
|
|
2178
|
+
const parentNorm = normalizeIiifId(String(it.parent || ""));
|
|
2179
|
+
const newEntry = {
|
|
2180
|
+
id: nid,
|
|
2181
|
+
type: "Manifest",
|
|
2182
|
+
slug,
|
|
2183
|
+
parent: parentNorm,
|
|
2184
|
+
};
|
|
2185
|
+
applyManifestEntryCanonical(newEntry, manifest, slug);
|
|
2186
|
+
const existingIdx = idxMap.byId.findIndex(
|
|
2187
|
+
(e) => e && e.type === "Manifest" && normalizeIiifId(e.id) === nid,
|
|
2188
|
+
);
|
|
2189
|
+
if (existingIdx >= 0) idxMap.byId[existingIdx] = newEntry;
|
|
2190
|
+
else idxMap.byId.push(newEntry);
|
|
2191
|
+
await saveManifestIndex(idxMap);
|
|
2192
|
+
mEntry = newEntry;
|
|
2193
|
+
} else if (mEntry) {
|
|
2194
|
+
const prevCanonical = mEntry.canonical || "";
|
|
2195
|
+
const nextCanonical = applyManifestEntryCanonical(
|
|
2196
|
+
mEntry,
|
|
2197
|
+
manifest,
|
|
2198
|
+
slug,
|
|
2199
|
+
);
|
|
2200
|
+
if (nextCanonical !== prevCanonical) {
|
|
2201
|
+
await saveManifestIndex(idxMap);
|
|
2202
|
+
}
|
|
2203
|
+
}
|
|
2204
|
+
const manifestId = manifest && manifest.id ? manifest.id : id;
|
|
2205
|
+
const normalizedManifestId = normalizeIiifId(String(manifestId || id));
|
|
2206
|
+
if (normalizedManifestId) renderedManifestIds.add(normalizedManifestId);
|
|
2207
|
+
logDebug(`Resolved slug ${slug} for ${manifestLabel}`);
|
|
2208
|
+
const references = referenced.getReferencesForManifest(manifestId);
|
|
2209
|
+
for (const routeEntry of workRouteEntries) {
|
|
2210
|
+
const routeBase =
|
|
2211
|
+
routeEntry && routeEntry.route
|
|
2212
|
+
? routeEntry.route
|
|
2213
|
+
: getDefaultRoute("works");
|
|
2214
|
+
const isDefaultRoute = routeEntry && routeEntry.isDefault === true;
|
|
2215
|
+
const localeFromRoute =
|
|
2216
|
+
routeEntry && routeEntry.locale
|
|
2217
|
+
? routeEntry.locale
|
|
2218
|
+
: getDefaultLocaleCode();
|
|
2219
|
+
const href = buildRouteRelativePath(routeBase, `${slug}.html`);
|
|
2220
|
+
const outPath = path.join(OUT_DIR, href);
|
|
2221
|
+
ensureDirSync(path.dirname(outPath));
|
|
2222
|
+
try {
|
|
2223
|
+
let components = {};
|
|
2224
|
+
try {
|
|
2225
|
+
components = await mdx.loadUiComponents();
|
|
2226
|
+
} catch (_) {
|
|
2227
|
+
components = {};
|
|
2228
|
+
}
|
|
2229
|
+
const Anchor = function A(props) {
|
|
2230
|
+
let {href = "", ...rest} = props || {};
|
|
2231
|
+
href = withBase(href);
|
|
2232
|
+
return React.createElement("a", {href, ...rest}, props.children);
|
|
2233
|
+
};
|
|
2234
|
+
// Map exported UI components into MDX and add anchor helper
|
|
2235
|
+
const compMap = {...components, a: Anchor};
|
|
2236
|
+
let MDXProvider = null;
|
|
2237
|
+
try {
|
|
2238
|
+
const mod = await import("@mdx-js/react");
|
|
2239
|
+
MDXProvider = mod.MDXProvider || mod.default || null;
|
|
2240
|
+
} catch (_) {
|
|
2241
|
+
MDXProvider = null;
|
|
2242
|
+
}
|
|
2243
|
+
const app = await mdx.loadAppWrapper();
|
|
2244
|
+
|
|
2245
|
+
let heroMedia = null;
|
|
2246
|
+
try {
|
|
2247
|
+
heroMedia = await resolveHeroMedia(manifest);
|
|
2248
|
+
} catch (_) {
|
|
2249
|
+
heroMedia = null;
|
|
2250
|
+
}
|
|
2251
|
+
const normalizedHref = href.split(path.sep).join("/");
|
|
2252
|
+
const pageHref = rootRelativeHref(normalizedHref);
|
|
2253
|
+
const pageLocale = localeFromRoute || resolveLocaleFromHref(pageHref);
|
|
2254
|
+
const pageDescription = summaryForMeta || title;
|
|
2255
|
+
const canonical = resolveManifestCanonical(manifest, slug, routeBase);
|
|
2256
|
+
const pageDetails = {
|
|
2257
|
+
title,
|
|
2258
|
+
href: pageHref,
|
|
2259
|
+
url: pageHref,
|
|
2260
|
+
slug,
|
|
2261
|
+
type: "work",
|
|
2262
|
+
description: pageDescription,
|
|
2263
|
+
manifestId,
|
|
2264
|
+
referencedBy: references,
|
|
2265
|
+
canonical,
|
|
2266
|
+
locale: pageLocale,
|
|
2267
|
+
meta: {
|
|
2268
|
+
title,
|
|
2269
|
+
description: pageDescription,
|
|
2270
|
+
type: "work",
|
|
2271
|
+
url: pageHref,
|
|
2272
|
+
canonical,
|
|
2273
|
+
locale: pageLocale,
|
|
2274
|
+
},
|
|
2275
|
+
};
|
|
2276
|
+
const ogImageForPage =
|
|
2277
|
+
heroMedia && heroMedia.ogImage ? heroMedia.ogImage : "";
|
|
2278
|
+
if (ogImageForPage) {
|
|
2279
|
+
pageDetails.image = ogImageForPage;
|
|
2280
|
+
pageDetails.ogImage = ogImageForPage;
|
|
2281
|
+
pageDetails.meta.image = ogImageForPage;
|
|
2282
|
+
pageDetails.meta.ogImage = ogImageForPage;
|
|
2283
|
+
}
|
|
2284
|
+
const navigationRoots = navigation.buildNavigationRoots(slug || "");
|
|
2285
|
+
const navigationContext =
|
|
2286
|
+
navigationRoots && Object.keys(navigationRoots).length
|
|
2287
|
+
? {allRoots: navigationRoots}
|
|
2288
|
+
: null;
|
|
2289
|
+
const primaryNav = readPrimaryNavigation(pageLocale);
|
|
2290
|
+
const siteMeta = readSiteMetadata ? {...readSiteMetadata()} : null;
|
|
2291
|
+
const siteLanguageToggle = (() => {
|
|
2292
|
+
try {
|
|
2293
|
+
const data = readCanopyLocalesWithMessages();
|
|
2294
|
+
if (data && Array.isArray(data.locales) && data.locales.length) {
|
|
2295
|
+
return {locales: data.locales, messages: data.messages || {}};
|
|
2296
|
+
}
|
|
2297
|
+
} catch (_) {}
|
|
2298
|
+
return null;
|
|
2299
|
+
})();
|
|
2300
|
+
const siteContext = siteMeta ? {...siteMeta} : {};
|
|
2301
|
+
if (siteLanguageToggle) {
|
|
2302
|
+
siteContext.languageToggle = siteLanguageToggle;
|
|
2303
|
+
}
|
|
2304
|
+
try {
|
|
2305
|
+
const localeMessages = readLocaleMessages(pageLocale);
|
|
2306
|
+
if (localeMessages) siteContext.localeMessages = localeMessages;
|
|
2307
|
+
} catch (_) {}
|
|
2308
|
+
try {
|
|
2309
|
+
const localeRoutes = getLocaleRouteConfig(pageLocale);
|
|
2310
|
+
if (localeRoutes) siteContext.routes = localeRoutes;
|
|
2311
|
+
} catch (_) {}
|
|
2312
|
+
try {
|
|
2313
|
+
const defaultRoutes = getLocaleRouteConfig(getDefaultLocaleCode());
|
|
2314
|
+
if (defaultRoutes) siteContext.routesDefault = defaultRoutes;
|
|
2315
|
+
} catch (_) {}
|
|
2316
|
+
const pageContextValue = {
|
|
2317
|
+
navigation: navigationContext,
|
|
2318
|
+
page: pageDetails,
|
|
2319
|
+
site: siteContext && Object.keys(siteContext).length ? siteContext : null,
|
|
2320
|
+
primaryNavigation: Array.isArray(primaryNav) ? primaryNav : [],
|
|
2321
|
+
};
|
|
2322
|
+
if (
|
|
2323
|
+
metadataFacetLabels.length &&
|
|
2324
|
+
manifest &&
|
|
2325
|
+
typeof manifest === "object"
|
|
2326
|
+
) {
|
|
2327
|
+
try {
|
|
2328
|
+
Object.defineProperty(manifest, "__canopyMetadataFacets", {
|
|
2329
|
+
configurable: true,
|
|
2330
|
+
enumerable: false,
|
|
2331
|
+
writable: true,
|
|
2332
|
+
value: metadataFacetLabels,
|
|
2333
|
+
});
|
|
2334
|
+
} catch (_) {
|
|
2335
|
+
manifest.__canopyMetadataFacets = metadataFacetLabels;
|
|
2336
|
+
}
|
|
2337
|
+
}
|
|
2338
|
+
const mdxContent = React.createElement(WorksLayoutComp, {
|
|
2339
|
+
manifest,
|
|
2340
|
+
references,
|
|
2341
|
+
manifestId,
|
|
2342
|
+
});
|
|
2343
|
+
const siteTree = mdxContent;
|
|
2344
|
+
const wrappedApp =
|
|
2345
|
+
app && app.App
|
|
2346
|
+
? React.createElement(app.App, null, siteTree)
|
|
2347
|
+
: siteTree;
|
|
2348
|
+
const withContext =
|
|
2349
|
+
PageContext && pageContextValue
|
|
2350
|
+
? React.createElement(
|
|
2351
|
+
PageContext.Provider,
|
|
2352
|
+
{value: pageContextValue},
|
|
2353
|
+
wrappedApp,
|
|
2354
|
+
)
|
|
2355
|
+
: wrappedApp;
|
|
2356
|
+
const page = MDXProvider
|
|
2357
|
+
? React.createElement(
|
|
2358
|
+
MDXProvider,
|
|
2359
|
+
{components: compMap},
|
|
2360
|
+
withContext,
|
|
2361
|
+
)
|
|
2362
|
+
: withContext;
|
|
2363
|
+
const body = ReactDOMServer.renderToStaticMarkup(page);
|
|
2364
|
+
let head = "";
|
|
2365
|
+
if (app && app.Head) {
|
|
2366
|
+
const headElement = React.createElement(app.Head, {
|
|
2367
|
+
page: pageContextValue.page,
|
|
2368
|
+
navigation: pageContextValue.navigation,
|
|
2369
|
+
});
|
|
2370
|
+
const wrappedHead = PageContext
|
|
2371
|
+
? React.createElement(
|
|
2372
|
+
PageContext.Provider,
|
|
2373
|
+
{value: pageContextValue},
|
|
2374
|
+
headElement,
|
|
2375
|
+
)
|
|
2376
|
+
: headElement;
|
|
2377
|
+
head = ReactDOMServer.renderToStaticMarkup(wrappedHead);
|
|
2378
|
+
}
|
|
2379
|
+
const needsHydrateViewer =
|
|
2380
|
+
body.includes("data-canopy-viewer") ||
|
|
2381
|
+
body.includes("data-canopy-scroll") ||
|
|
2382
|
+
body.includes("data-canopy-image");
|
|
2383
|
+
const needsRelated = body.includes("data-canopy-related-items");
|
|
2384
|
+
const needsImageStory = body.includes("data-canopy-image-story");
|
|
2385
|
+
const needsHeroSlider = body.includes("data-canopy-hero-slider");
|
|
2386
|
+
const needsTimeline = body.includes("data-canopy-timeline");
|
|
2387
|
+
const needsMap = body.includes("data-canopy-map");
|
|
2388
|
+
const needsSearchForm = body.includes("data-canopy-search-form");
|
|
2389
|
+
const needsCustomClients = body.includes(
|
|
2390
|
+
"data-canopy-client-component",
|
|
2391
|
+
);
|
|
2392
|
+
const needsHydrate =
|
|
2393
|
+
body.includes("data-canopy-hydrate") ||
|
|
2394
|
+
needsHydrateViewer ||
|
|
2395
|
+
needsRelated ||
|
|
2396
|
+
needsSearchForm;
|
|
2397
|
+
|
|
2398
|
+
const viewerRel = needsHydrateViewer
|
|
2399
|
+
? relativeRuntimeScript(outPath, "canopy-viewer.js", true)
|
|
2400
|
+
: null;
|
|
2401
|
+
const sliderRel = needsRelated
|
|
2402
|
+
? relativeRuntimeScript(outPath, "canopy-slider.js", true)
|
|
2403
|
+
: null;
|
|
2404
|
+
const imageStoryRel = needsImageStory
|
|
2405
|
+
? relativeRuntimeScript(outPath, "canopy-image-story.js", true)
|
|
2406
|
+
: null;
|
|
2407
|
+
const timelineRel = needsTimeline
|
|
2408
|
+
? relativeRuntimeScript(outPath, "canopy-timeline.js", true)
|
|
2409
|
+
: null;
|
|
2410
|
+
const mapRel = needsMap
|
|
2411
|
+
? relativeRuntimeScript(outPath, "canopy-map.js", true)
|
|
2412
|
+
: null;
|
|
2413
|
+
const mapCssRel = needsMap
|
|
2414
|
+
? path
|
|
2415
|
+
.relative(
|
|
2416
|
+
path.dirname(outPath),
|
|
2417
|
+
path.join(OUT_DIR, "scripts", "canopy-map.css"),
|
|
2418
|
+
)
|
|
2419
|
+
.split(path.sep)
|
|
2420
|
+
.join("/")
|
|
2421
|
+
: null;
|
|
2422
|
+
const heroRel = needsHeroSlider
|
|
2423
|
+
? relativeRuntimeScript(outPath, "canopy-hero-slider.js", true)
|
|
2424
|
+
: null;
|
|
2425
|
+
const relatedRel = needsRelated
|
|
2426
|
+
? relativeRuntimeScript(outPath, "canopy-related-items.js", true)
|
|
2427
|
+
: null;
|
|
2428
|
+
const searchFormRel = needsSearchForm
|
|
2429
|
+
? relativeRuntimeScript(outPath, "canopy-search-form.js", true)
|
|
2430
|
+
: null;
|
|
2431
|
+
let customClientRel = null;
|
|
2432
|
+
if (needsCustomClients) {
|
|
2433
|
+
try {
|
|
2434
|
+
await mdx.ensureCustomClientRuntime();
|
|
2435
|
+
const customAbs = path.join(
|
|
2436
|
+
OUT_DIR,
|
|
2437
|
+
"scripts",
|
|
2438
|
+
"canopy-custom-components.js",
|
|
2439
|
+
);
|
|
2440
|
+
let rel = path
|
|
2441
|
+
.relative(path.dirname(outPath), customAbs)
|
|
2442
|
+
.split(path.sep)
|
|
2443
|
+
.join("/");
|
|
2444
|
+
try {
|
|
2445
|
+
const st = fs.statSync(customAbs);
|
|
2446
|
+
rel += `?v=${Math.floor(st.mtimeMs || Date.now())}`;
|
|
2447
|
+
} catch (_) {}
|
|
2448
|
+
customClientRel = rel;
|
|
2449
|
+
} catch (e) {
|
|
2450
|
+
try {
|
|
2451
|
+
console.warn(
|
|
2452
|
+
"[canopy][mdx] failed to build custom client runtime:",
|
|
2453
|
+
e && e.message ? e.message : e,
|
|
2454
|
+
);
|
|
2455
|
+
} catch (_) {}
|
|
2456
|
+
}
|
|
2457
|
+
}
|
|
2458
|
+
|
|
2459
|
+
const moduleScriptRels = [];
|
|
2460
|
+
if (viewerRel) moduleScriptRels.push(viewerRel);
|
|
2461
|
+
if (sliderRel) moduleScriptRels.push(sliderRel);
|
|
2462
|
+
if (imageStoryRel) moduleScriptRels.push(imageStoryRel);
|
|
2463
|
+
if (customClientRel) moduleScriptRels.push(customClientRel);
|
|
2464
|
+
const primaryClassicScripts = [];
|
|
2465
|
+
if (heroRel) primaryClassicScripts.push(heroRel);
|
|
2466
|
+
if (relatedRel) primaryClassicScripts.push(relatedRel);
|
|
2467
|
+
if (timelineRel) primaryClassicScripts.push(timelineRel);
|
|
2468
|
+
if (mapRel) primaryClassicScripts.push(mapRel);
|
|
2469
|
+
const secondaryClassicScripts = [];
|
|
2470
|
+
if (searchFormRel) secondaryClassicScripts.push(searchFormRel);
|
|
2471
|
+
let jsRel = null;
|
|
2472
|
+
if (primaryClassicScripts.length) {
|
|
2473
|
+
jsRel = primaryClassicScripts.shift();
|
|
2474
|
+
}
|
|
2475
|
+
const classicScriptRels = primaryClassicScripts.concat(
|
|
2476
|
+
secondaryClassicScripts,
|
|
2477
|
+
);
|
|
2478
|
+
|
|
2479
|
+
const headSegments = [head];
|
|
2480
|
+
try {
|
|
2481
|
+
const localeScript = buildLocaleRuntimeScript(pageLocale);
|
|
2482
|
+
if (localeScript) headSegments.push(localeScript);
|
|
2483
|
+
} catch (_) {}
|
|
2484
|
+
const needsReact = !!(
|
|
2485
|
+
needsHydrateViewer ||
|
|
2486
|
+
needsRelated ||
|
|
2487
|
+
needsTimeline ||
|
|
2488
|
+
needsMap ||
|
|
2489
|
+
(customClientRel && needsCustomClients)
|
|
2490
|
+
);
|
|
2491
|
+
let vendorTag = "";
|
|
2492
|
+
if (needsReact) {
|
|
2493
|
+
try {
|
|
2494
|
+
const vendorAbs = path.join(
|
|
2495
|
+
OUT_DIR,
|
|
2496
|
+
"scripts",
|
|
2497
|
+
"react-globals.js",
|
|
2498
|
+
);
|
|
2499
|
+
let vendorRel = path
|
|
2500
|
+
.relative(path.dirname(outPath), vendorAbs)
|
|
2501
|
+
.split(path.sep)
|
|
2502
|
+
.join("/");
|
|
2503
|
+
try {
|
|
2504
|
+
const stv = fs.statSync(vendorAbs);
|
|
2505
|
+
vendorRel += `?v=${Math.floor(stv.mtimeMs || Date.now())}`;
|
|
2506
|
+
} catch (_) {}
|
|
2507
|
+
vendorTag = `<script src="${vendorRel}"></script>`;
|
|
2508
|
+
} catch (_) {}
|
|
2509
|
+
}
|
|
2510
|
+
const extraScripts = [];
|
|
2511
|
+
const pushClassicScript = (src) => {
|
|
2512
|
+
if (!src || src === jsRel) return;
|
|
2513
|
+
extraScripts.push(`<script defer src="${src}"></script>`);
|
|
2514
|
+
};
|
|
2515
|
+
const pushModuleScript = (src) => {
|
|
2516
|
+
if (!src) return;
|
|
2517
|
+
extraScripts.push(`<script type="module" src="${src}"></script>`);
|
|
2518
|
+
};
|
|
2519
|
+
classicScriptRels.forEach((src) => pushClassicScript(src));
|
|
2520
|
+
moduleScriptRels.forEach((src) => pushModuleScript(src));
|
|
2521
|
+
try {
|
|
2522
|
+
const {BASE_PATH} = require("../common");
|
|
2523
|
+
if (BASE_PATH)
|
|
2524
|
+
vendorTag =
|
|
2525
|
+
`<script>window.CANOPY_BASE_PATH=${JSON.stringify(
|
|
2526
|
+
BASE_PATH,
|
|
2527
|
+
)}</script>` + vendorTag;
|
|
2528
|
+
} catch (_) {}
|
|
2529
|
+
let pageBody = body;
|
|
2530
|
+
const extraStyles = [];
|
|
2531
|
+
if (mapCssRel) {
|
|
2532
|
+
let rel = mapCssRel;
|
|
2533
|
+
try {
|
|
2534
|
+
const mapCssAbs = path.join(OUT_DIR, "scripts", "canopy-map.css");
|
|
2535
|
+
const st = fs.statSync(mapCssAbs);
|
|
2536
|
+
rel += `?v=${Math.floor(st.mtimeMs || Date.now())}`;
|
|
2537
|
+
} catch (_) {}
|
|
2538
|
+
extraStyles.push(`<link rel="stylesheet" href="${rel}">`);
|
|
2539
|
+
}
|
|
2540
|
+
if (extraStyles.length) headSegments.push(extraStyles.join(""));
|
|
2541
|
+
if (vendorTag) headSegments.push(vendorTag);
|
|
2542
|
+
if (extraScripts.length) headSegments.push(extraScripts.join(""));
|
|
2543
|
+
const headExtra = headSegments.join("");
|
|
2544
|
+
const pageType = (pageDetails && pageDetails.type) || "work";
|
|
2545
|
+
const bodyClass = canopyBodyClassForType(pageType);
|
|
2546
|
+
let html = htmlShell({
|
|
2547
|
+
title,
|
|
2548
|
+
body: pageBody,
|
|
2549
|
+
cssHref: null,
|
|
2550
|
+
scriptHref: jsRel,
|
|
2551
|
+
headExtra,
|
|
2552
|
+
bodyClass,
|
|
2553
|
+
lang: pageLocale,
|
|
2554
|
+
});
|
|
2555
|
+
try {
|
|
2556
|
+
html = require("../common").applyBaseToHtml(html);
|
|
2557
|
+
} catch (_) {}
|
|
2558
|
+
await fsp.writeFile(outPath, html, "utf8");
|
|
2559
|
+
logDebug(
|
|
2560
|
+
`Wrote work page → ${path.relative(process.cwd(), outPath)}`,
|
|
2561
|
+
);
|
|
2562
|
+
lns.push([
|
|
2563
|
+
`✔ Created ${path.relative(process.cwd(), outPath)}`,
|
|
2564
|
+
"green",
|
|
2565
|
+
]);
|
|
2566
|
+
let thumbUrl = "";
|
|
2567
|
+
let thumbWidth = undefined;
|
|
2568
|
+
let thumbHeight = undefined;
|
|
2569
|
+
try {
|
|
2570
|
+
const t = await getThumbnail(manifest, thumbSize, unsafeThumbs);
|
|
2571
|
+
if (t && t.url) {
|
|
2572
|
+
thumbUrl = String(t.url);
|
|
2573
|
+
thumbWidth = typeof t.width === "number" ? t.width : undefined;
|
|
2574
|
+
thumbHeight = typeof t.height === "number" ? t.height : undefined;
|
|
2575
|
+
logDebug(
|
|
2576
|
+
`Thumbnail resolved for ${manifestLabel}: ${thumbUrl} (${thumbWidth || "auto"}×${thumbHeight || "auto"})`,
|
|
2577
|
+
);
|
|
2578
|
+
}
|
|
2579
|
+
} catch (_) {}
|
|
2580
|
+
try {
|
|
2581
|
+
const idx = await loadManifestIndex();
|
|
2582
|
+
if (Array.isArray(idx.byId)) {
|
|
2583
|
+
const entry = idx.byId.find(
|
|
2584
|
+
(e) =>
|
|
2585
|
+
e &&
|
|
2586
|
+
e.id === String(manifest.id || id) &&
|
|
2587
|
+
e.type === "Manifest",
|
|
2588
|
+
);
|
|
2589
|
+
if (entry) {
|
|
2590
|
+
let touched = false;
|
|
2591
|
+
if (thumbUrl) {
|
|
2592
|
+
const nextThumb = String(thumbUrl);
|
|
2593
|
+
if (entry.thumbnail !== nextThumb) {
|
|
2594
|
+
entry.thumbnail = nextThumb;
|
|
2595
|
+
touched = true;
|
|
2596
|
+
}
|
|
2597
|
+
if (
|
|
2598
|
+
typeof thumbWidth === "number" &&
|
|
2599
|
+
entry.thumbnailWidth !== thumbWidth
|
|
2600
|
+
) {
|
|
2601
|
+
entry.thumbnailWidth = thumbWidth;
|
|
2602
|
+
touched = true;
|
|
2603
|
+
}
|
|
2604
|
+
if (
|
|
2605
|
+
typeof thumbHeight === "number" &&
|
|
2606
|
+
entry.thumbnailHeight !== thumbHeight
|
|
2607
|
+
) {
|
|
2608
|
+
entry.thumbnailHeight = thumbHeight;
|
|
2609
|
+
touched = true;
|
|
2610
|
+
}
|
|
2611
|
+
}
|
|
2612
|
+
if (heroMedia && heroMedia.heroThumbnail) {
|
|
2613
|
+
logDebug(
|
|
2614
|
+
`Hero thumbnail cached for ${manifestLabel}: ${heroMedia.heroThumbnail}`,
|
|
2615
|
+
);
|
|
2616
|
+
if (entry.heroThumbnail !== heroMedia.heroThumbnail) {
|
|
2617
|
+
entry.heroThumbnail = heroMedia.heroThumbnail;
|
|
2618
|
+
touched = true;
|
|
2619
|
+
}
|
|
2620
|
+
if (
|
|
2621
|
+
typeof heroMedia.heroThumbnailWidth === "number" &&
|
|
2622
|
+
entry.heroThumbnailWidth !== heroMedia.heroThumbnailWidth
|
|
2623
|
+
) {
|
|
2624
|
+
entry.heroThumbnailWidth = heroMedia.heroThumbnailWidth;
|
|
2625
|
+
touched = true;
|
|
2626
|
+
}
|
|
2627
|
+
if (
|
|
2628
|
+
typeof heroMedia.heroThumbnailHeight === "number" &&
|
|
2629
|
+
entry.heroThumbnailHeight !== heroMedia.heroThumbnailHeight
|
|
2630
|
+
) {
|
|
2631
|
+
entry.heroThumbnailHeight = heroMedia.heroThumbnailHeight;
|
|
2632
|
+
touched = true;
|
|
2633
|
+
}
|
|
2634
|
+
if (heroMedia.heroThumbnailSrcset) {
|
|
2635
|
+
if (
|
|
2636
|
+
entry.heroThumbnailSrcset !==
|
|
2637
|
+
heroMedia.heroThumbnailSrcset
|
|
2638
|
+
) {
|
|
2639
|
+
entry.heroThumbnailSrcset = heroMedia.heroThumbnailSrcset;
|
|
2640
|
+
touched = true;
|
|
2641
|
+
}
|
|
2642
|
+
if (entry.heroThumbnailSizes !== HERO_IMAGE_SIZES_ATTR) {
|
|
2643
|
+
entry.heroThumbnailSizes = HERO_IMAGE_SIZES_ATTR;
|
|
2644
|
+
touched = true;
|
|
2645
|
+
}
|
|
2646
|
+
logDebug(
|
|
2647
|
+
`Hero srcset cached for ${manifestLabel} (${heroMedia.heroThumbnailSrcset.length} chars)`,
|
|
2648
|
+
);
|
|
2649
|
+
}
|
|
2650
|
+
} else {
|
|
2651
|
+
if (entry.heroThumbnail !== undefined) {
|
|
2652
|
+
delete entry.heroThumbnail;
|
|
2653
|
+
touched = true;
|
|
2654
|
+
}
|
|
2655
|
+
if (entry.heroThumbnailWidth !== undefined) {
|
|
2656
|
+
delete entry.heroThumbnailWidth;
|
|
2657
|
+
touched = true;
|
|
2658
|
+
}
|
|
2659
|
+
if (entry.heroThumbnailHeight !== undefined) {
|
|
2660
|
+
delete entry.heroThumbnailHeight;
|
|
2661
|
+
touched = true;
|
|
2662
|
+
}
|
|
2663
|
+
if (entry.heroThumbnailSrcset !== undefined) {
|
|
2664
|
+
delete entry.heroThumbnailSrcset;
|
|
2665
|
+
touched = true;
|
|
2666
|
+
}
|
|
2667
|
+
if (entry.heroThumbnailSizes !== undefined) {
|
|
2668
|
+
delete entry.heroThumbnailSizes;
|
|
2669
|
+
touched = true;
|
|
2670
|
+
}
|
|
2671
|
+
}
|
|
2672
|
+
if (heroMedia && heroMedia.ogImage) {
|
|
2673
|
+
logDebug(
|
|
2674
|
+
`OG image cached for ${manifestLabel}: ${heroMedia.ogImage}`,
|
|
2675
|
+
);
|
|
2676
|
+
if (entry.ogImage !== heroMedia.ogImage) {
|
|
2677
|
+
entry.ogImage = heroMedia.ogImage;
|
|
2678
|
+
touched = true;
|
|
2679
|
+
}
|
|
2680
|
+
if (typeof heroMedia.ogImageWidth === "number") {
|
|
2681
|
+
if (entry.ogImageWidth !== heroMedia.ogImageWidth)
|
|
2682
|
+
touched = true;
|
|
2683
|
+
entry.ogImageWidth = heroMedia.ogImageWidth;
|
|
2684
|
+
} else if (entry.ogImageWidth !== undefined) {
|
|
2685
|
+
delete entry.ogImageWidth;
|
|
2686
|
+
touched = true;
|
|
2687
|
+
}
|
|
2688
|
+
if (typeof heroMedia.ogImageHeight === "number") {
|
|
2689
|
+
if (entry.ogImageHeight !== heroMedia.ogImageHeight)
|
|
2690
|
+
touched = true;
|
|
2691
|
+
entry.ogImageHeight = heroMedia.ogImageHeight;
|
|
2692
|
+
} else if (entry.ogImageHeight !== undefined) {
|
|
2693
|
+
delete entry.ogImageHeight;
|
|
2694
|
+
touched = true;
|
|
2695
|
+
}
|
|
2696
|
+
} else {
|
|
2697
|
+
try {
|
|
2698
|
+
if (entry.ogImage !== undefined) {
|
|
2699
|
+
delete entry.ogImage;
|
|
2700
|
+
touched = true;
|
|
2701
|
+
}
|
|
2702
|
+
if (entry.ogImageWidth !== undefined) {
|
|
2703
|
+
delete entry.ogImageWidth;
|
|
2704
|
+
touched = true;
|
|
2705
|
+
}
|
|
2706
|
+
if (entry.ogImageHeight !== undefined) {
|
|
2707
|
+
delete entry.ogImageHeight;
|
|
2708
|
+
touched = true;
|
|
2709
|
+
}
|
|
2710
|
+
} catch (_) {}
|
|
2711
|
+
}
|
|
2712
|
+
if (
|
|
2713
|
+
ensureThumbnailValue(
|
|
2714
|
+
entry,
|
|
2715
|
+
heroMedia && heroMedia.heroThumbnail,
|
|
2716
|
+
heroMedia && heroMedia.heroThumbnailWidth,
|
|
2717
|
+
heroMedia && heroMedia.heroThumbnailHeight,
|
|
2718
|
+
)
|
|
2719
|
+
) {
|
|
2720
|
+
touched = true;
|
|
2721
|
+
}
|
|
2722
|
+
if (touched) await saveManifestIndex(idx);
|
|
2723
|
+
}
|
|
2724
|
+
}
|
|
2725
|
+
} catch (_) {}
|
|
2726
|
+
if (isDefaultRoute) {
|
|
2727
|
+
let metadataValues = [];
|
|
2728
|
+
let summaryValue = "";
|
|
2729
|
+
let annotationValue = "";
|
|
2730
|
+
try {
|
|
2731
|
+
const metadataEntries = extractMetadataEntries(manifest, {
|
|
2732
|
+
includeAll: metadataCollectAllLabels,
|
|
2733
|
+
labelsSet: metadataLabelSet,
|
|
2734
|
+
});
|
|
2735
|
+
if (metadataEntries && metadataEntries.length) {
|
|
2736
|
+
for (const entry of metadataEntries) recordMetadataIndexEntry(entry);
|
|
2737
|
+
}
|
|
2738
|
+
} catch (_) {}
|
|
2739
|
+
if (metadataOptions && metadataOptions.enabled) {
|
|
2740
|
+
try {
|
|
2741
|
+
metadataValues = extractMetadataValues(manifest, metadataOptions);
|
|
2742
|
+
} catch (_) {
|
|
2743
|
+
metadataValues = [];
|
|
2744
|
+
}
|
|
2745
|
+
}
|
|
2746
|
+
if (summaryOptions && summaryOptions.enabled) {
|
|
2747
|
+
summaryValue = summaryRaw || "";
|
|
2748
|
+
}
|
|
2749
|
+
if (annotationsOptions && annotationsOptions.enabled) {
|
|
2750
|
+
try {
|
|
2751
|
+
annotationValue = await extractAnnotationText(
|
|
2752
|
+
manifest,
|
|
2753
|
+
annotationsOptions,
|
|
2754
|
+
);
|
|
2755
|
+
} catch (_) {
|
|
2756
|
+
annotationValue = "";
|
|
2757
|
+
}
|
|
2758
|
+
}
|
|
2759
|
+
const fallbackThumbnail =
|
|
2760
|
+
(heroMedia && heroMedia.heroThumbnail) || "";
|
|
2761
|
+
const fallbackThumbWidth =
|
|
2762
|
+
heroMedia && typeof heroMedia.heroThumbnailWidth === "number"
|
|
2763
|
+
? heroMedia.heroThumbnailWidth
|
|
2764
|
+
: undefined;
|
|
2765
|
+
const fallbackThumbHeight =
|
|
2766
|
+
heroMedia && typeof heroMedia.heroThumbnailHeight === "number"
|
|
2767
|
+
? heroMedia.heroThumbnailHeight
|
|
2768
|
+
: undefined;
|
|
2769
|
+
const navThumbnail = thumbUrl || fallbackThumbnail;
|
|
2770
|
+
const navThumbWidth =
|
|
2771
|
+
typeof thumbWidth === "number" ? thumbWidth : fallbackThumbWidth;
|
|
2772
|
+
const navThumbHeight =
|
|
2773
|
+
typeof thumbHeight === "number" ? thumbHeight : fallbackThumbHeight;
|
|
2774
|
+
const navRecord = navPlace.buildManifestNavPlaceRecord({
|
|
2775
|
+
manifest,
|
|
2776
|
+
slug,
|
|
2777
|
+
href: pageHref,
|
|
2778
|
+
title,
|
|
2779
|
+
summary: summaryRaw,
|
|
2780
|
+
thumbnail: navThumbnail,
|
|
2781
|
+
thumbnailWidth: navThumbWidth,
|
|
2782
|
+
thumbnailHeight: navThumbHeight,
|
|
2783
|
+
});
|
|
2784
|
+
if (navRecord) navPlaceRecords.push(navRecord);
|
|
2785
|
+
|
|
2786
|
+
const recordThumbnail = navThumbnail;
|
|
2787
|
+
const recordThumbWidth = navThumbWidth;
|
|
2788
|
+
const recordThumbHeight = navThumbHeight;
|
|
2789
|
+
const localeHrefMap = {};
|
|
2790
|
+
for (const entry of workRouteEntries) {
|
|
2791
|
+
const targetBase =
|
|
2792
|
+
entry && entry.route ? entry.route : getDefaultRoute("works");
|
|
2793
|
+
const targetLocale =
|
|
2794
|
+
entry && entry.locale ? entry.locale : getDefaultLocaleCode();
|
|
2795
|
+
const relHref = buildRouteRelativePath(
|
|
2796
|
+
targetBase,
|
|
2797
|
+
`${slug}.html`,
|
|
2798
|
+
);
|
|
2799
|
+
localeHrefMap[targetLocale] = rootRelativeHref(
|
|
2800
|
+
relHref.split(path.sep).join("/"),
|
|
2801
|
+
);
|
|
2802
|
+
}
|
|
2803
|
+
iiifRecords.push({
|
|
2804
|
+
id: String(manifest.id || id),
|
|
2805
|
+
title,
|
|
2806
|
+
href: pageHref,
|
|
2807
|
+
type: "work",
|
|
2808
|
+
slug,
|
|
2809
|
+
locale: pageLocale,
|
|
2810
|
+
thumbnail: recordThumbnail || undefined,
|
|
2811
|
+
thumbnailWidth:
|
|
2812
|
+
typeof recordThumbWidth === "number"
|
|
2813
|
+
? recordThumbWidth
|
|
2814
|
+
: undefined,
|
|
2815
|
+
thumbnailHeight:
|
|
2816
|
+
typeof recordThumbHeight === "number"
|
|
2817
|
+
? recordThumbHeight
|
|
2818
|
+
: undefined,
|
|
2819
|
+
searchMetadataValues:
|
|
2820
|
+
metadataValues && metadataValues.length
|
|
2821
|
+
? metadataValues
|
|
2822
|
+
: undefined,
|
|
2823
|
+
searchSummary:
|
|
2824
|
+
summaryValue && summaryValue.length ? summaryValue : undefined,
|
|
2825
|
+
searchAnnotation:
|
|
2826
|
+
annotationValue && annotationValue.length
|
|
2827
|
+
? annotationValue
|
|
2828
|
+
: undefined,
|
|
2829
|
+
routes: localeHrefMap,
|
|
2830
|
+
});
|
|
2831
|
+
logDebug(
|
|
2832
|
+
`Search record queued for ${manifestLabel}: ${pageHref} (metadata values ${
|
|
2833
|
+
metadataValues ? metadataValues.length : 0
|
|
2834
|
+
})`,
|
|
2835
|
+
);
|
|
2836
|
+
}
|
|
2837
|
+
} catch (e) {
|
|
2838
|
+
lns.push([
|
|
2839
|
+
`IIIF: failed to render for ${id || "<unknown>"} — ${e.message}`,
|
|
2840
|
+
"red",
|
|
2841
|
+
]);
|
|
2842
|
+
}
|
|
2843
|
+
}
|
|
2844
|
+
logs[idx] = lns;
|
|
2845
|
+
tryFlush();
|
|
2846
|
+
}
|
|
2847
|
+
}
|
|
2848
|
+
const workers = Array.from(
|
|
2849
|
+
{length: Math.min(concurrency, chunk.length)},
|
|
2850
|
+
() => worker(),
|
|
2851
|
+
);
|
|
2852
|
+
await Promise.all(workers);
|
|
2853
|
+
tryFlush();
|
|
2854
|
+
const chunkDuration = Date.now() - chunkStart;
|
|
2855
|
+
chunkMetrics.push({
|
|
2856
|
+
index: ci + 1,
|
|
2857
|
+
count: chunk.length,
|
|
2858
|
+
durationMs: chunkDuration,
|
|
2859
|
+
});
|
|
2860
|
+
try {
|
|
2861
|
+
logLine(
|
|
2862
|
+
`⏱ Chunk ${ci + 1}/${chunks}: processed ${chunk.length} Manifest(s) in ${formatDurationMs(chunkDuration)}`,
|
|
2863
|
+
"cyan",
|
|
2864
|
+
{dim: true},
|
|
2865
|
+
);
|
|
2866
|
+
} catch (_) {}
|
|
2867
|
+
}
|
|
2868
|
+
if (chunkMetrics.length) {
|
|
2869
|
+
const totalDuration = chunkMetrics.reduce(
|
|
2870
|
+
(sum, entry) => sum + (entry.durationMs || 0),
|
|
2871
|
+
0,
|
|
2872
|
+
);
|
|
2873
|
+
const totalItems = chunkMetrics.reduce(
|
|
2874
|
+
(sum, entry) => sum + (entry.count || 0),
|
|
2875
|
+
0,
|
|
2876
|
+
);
|
|
2877
|
+
const avgDuration = chunkMetrics.length
|
|
2878
|
+
? totalDuration / chunkMetrics.length
|
|
2879
|
+
: 0;
|
|
2880
|
+
const rate = totalDuration > 0 ? totalItems / (totalDuration / 1000) : 0;
|
|
2881
|
+
try {
|
|
2882
|
+
const rateLabel = rate ? `${rate.toFixed(1)} manifest(s)/s` : "n/a";
|
|
2883
|
+
logLine(
|
|
2884
|
+
`IIIF chunk summary: ${totalItems} Manifest(s) in ${formatDurationMs(totalDuration)} (avg chunk ${formatDurationMs(avgDuration)}, ${rateLabel})`,
|
|
2885
|
+
"cyan",
|
|
2886
|
+
{dim: true},
|
|
2887
|
+
);
|
|
2888
|
+
} catch (_) {}
|
|
2889
|
+
}
|
|
2890
|
+
try {
|
|
2891
|
+
const metadataIndexPayload = buildMetadataIndexPayload(
|
|
2892
|
+
metadataIndexMap,
|
|
2893
|
+
metadataLabelsNormalized,
|
|
2894
|
+
metadataDynamicOrder,
|
|
2895
|
+
);
|
|
2896
|
+
await writeMetadataIndexFile(metadataIndexPayload);
|
|
2897
|
+
} catch (_) {}
|
|
2898
|
+
try {
|
|
2899
|
+
await navPlace.writeNavPlaceDataset(navPlaceRecords);
|
|
2900
|
+
try {
|
|
2901
|
+
logLine(
|
|
2902
|
+
`✓ Wrote navPlace dataset (${navPlaceRecords.length} record(s))`,
|
|
2903
|
+
"cyan",
|
|
2904
|
+
);
|
|
2905
|
+
} catch (_) {}
|
|
2906
|
+
} catch (error) {
|
|
2907
|
+
try {
|
|
2908
|
+
console.warn(
|
|
2909
|
+
"[canopy][navPlace] failed to write dataset:",
|
|
2910
|
+
error && error.message ? error.message : error,
|
|
2911
|
+
);
|
|
2912
|
+
} catch (_) {}
|
|
2913
|
+
}
|
|
2914
|
+
return {
|
|
2915
|
+
iiifRecords,
|
|
2916
|
+
manifestIds: Array.from(renderedManifestIds),
|
|
2917
|
+
collectionIds: Array.from(visitedCollections),
|
|
2918
|
+
discovery: discovery.result(),
|
|
2919
|
+
};
|
|
2920
|
+
}
|
|
2921
|
+
|
|
2922
|
+
module.exports = {
|
|
2923
|
+
buildIiifCollectionPages,
|
|
2924
|
+
loadConfig,
|
|
2925
|
+
loadManifestIndex,
|
|
2926
|
+
saveManifestIndex,
|
|
2927
|
+
resolveIiifSources,
|
|
2928
|
+
// Expose helpers used by build for cache warming
|
|
2929
|
+
loadCachedManifestById,
|
|
2930
|
+
saveCachedManifest,
|
|
2931
|
+
ensureFeaturedInCache,
|
|
2932
|
+
cleanupIiifCache,
|
|
2933
|
+
};
|
|
2934
|
+
|
|
2935
|
+
// Expose a stable set of pure helper utilities for unit testing.
|
|
2936
|
+
module.exports.__TESTING__ = {
|
|
2937
|
+
resolvePositiveInteger,
|
|
2938
|
+
formatDurationMs,
|
|
2939
|
+
resolveBoolean,
|
|
2940
|
+
normalizeCollectionUris,
|
|
2941
|
+
normalizeManifestConfig,
|
|
2942
|
+
clampSlugLength,
|
|
2943
|
+
isSlugTooLong,
|
|
2944
|
+
normalizeSlugBase,
|
|
2945
|
+
buildSlugWithSuffix,
|
|
2946
|
+
normalizeStringList,
|
|
2947
|
+
ensureThumbnailValue,
|
|
2948
|
+
extractSummaryValues,
|
|
2949
|
+
truncateSummary,
|
|
2950
|
+
extractMetadataValues,
|
|
2951
|
+
extractMetadataEntries,
|
|
2952
|
+
extractAnnotationText,
|
|
2953
|
+
normalizeIiifId,
|
|
2954
|
+
normalizeIiifType,
|
|
2955
|
+
resolveParentFromPartOf,
|
|
2956
|
+
computeUniqueSlug,
|
|
2957
|
+
ensureBaseSlugFor,
|
|
2958
|
+
resetReservedSlugs,
|
|
2959
|
+
resolveThumbnailPreferences,
|
|
2960
|
+
loadManifestIndex,
|
|
2961
|
+
saveManifestIndex,
|
|
2962
|
+
upgradeIiifResource,
|
|
2963
|
+
ensurePresentation3Manifest,
|
|
2964
|
+
paths: {
|
|
2965
|
+
IIIF_CACHE_INDEX,
|
|
2966
|
+
IIIF_CACHE_INDEX_LEGACY,
|
|
2967
|
+
IIIF_CACHE_INDEX_MANIFESTS,
|
|
2968
|
+
},
|
|
2969
|
+
};
|
|
2970
|
+
|
|
2971
|
+
// Debug: list collections cache after traversal
|
|
2972
|
+
try {
|
|
2973
|
+
if (process.env.CANOPY_IIIF_DEBUG === "1") {
|
|
2974
|
+
const {logLine} = require("./log");
|
|
2975
|
+
try {
|
|
2976
|
+
const files = fs.existsSync(IIIF_CACHE_COLLECTIONS_DIR)
|
|
2977
|
+
? fs
|
|
2978
|
+
.readdirSync(IIIF_CACHE_COLLECTIONS_DIR)
|
|
2979
|
+
.filter((n) => /\.json$/i.test(n))
|
|
2980
|
+
: [];
|
|
2981
|
+
const head = files.slice(0, 8).join(", ");
|
|
2982
|
+
logLine(
|
|
2983
|
+
`IIIF: cache/collections (end): ${files.length} file(s)` +
|
|
2984
|
+
(head ? ` [${head}${files.length > 8 ? ", …" : ""}]` : ""),
|
|
2985
|
+
"blue",
|
|
2986
|
+
{dim: true},
|
|
2987
|
+
);
|
|
2988
|
+
} catch (_) {}
|
|
2989
|
+
}
|
|
2990
|
+
} catch (_) {}
|