@nulib/canopy 0.0.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/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 +109 -3
- 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,402 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Note: Do not shorten naming conventions here. Keep things readable and
|
|
4
|
+
// semantic. This module deliberately uses full words like "thumbnail" instead
|
|
5
|
+
// of abbreviations like "thumb".
|
|
6
|
+
|
|
7
|
+
// Helper for resolving a representative thumbnail for an IIIF resource.
|
|
8
|
+
// Uses Clover's thumbnail helpers (`@nulib/clover/helpers`) to choose an appropriate size.
|
|
9
|
+
|
|
10
|
+
function arrayify(value) {
|
|
11
|
+
if (!value) return [];
|
|
12
|
+
return Array.isArray(value) ? value : [value];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function normalizeBodyType(value) {
|
|
16
|
+
if (!value) return '';
|
|
17
|
+
if (typeof value === 'string') {
|
|
18
|
+
const trimmed = value.trim();
|
|
19
|
+
return trimmed;
|
|
20
|
+
}
|
|
21
|
+
if (Array.isArray(value)) {
|
|
22
|
+
for (const entry of value) {
|
|
23
|
+
const normalized = normalizeBodyType(entry);
|
|
24
|
+
if (normalized) return normalized;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return '';
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function isImageBodyType(value) {
|
|
31
|
+
if (!value) return false;
|
|
32
|
+
try {
|
|
33
|
+
return /image/i.test(String(value));
|
|
34
|
+
} catch (_) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function normalizeImageServiceCandidate(candidate) {
|
|
40
|
+
if (!candidate || typeof candidate !== 'object') return null;
|
|
41
|
+
const id = candidate.id || candidate['@id'];
|
|
42
|
+
if (!id) return null;
|
|
43
|
+
const typeValue = candidate.type || candidate['@type'] || '';
|
|
44
|
+
const type = Array.isArray(typeValue) ? typeValue[0] : typeValue;
|
|
45
|
+
const profileValue = candidate.profile || candidate['@profile'];
|
|
46
|
+
const profile = Array.isArray(profileValue)
|
|
47
|
+
? profileValue.find((p) => typeof p === 'string')
|
|
48
|
+
: profileValue;
|
|
49
|
+
const preferredFormats = arrayify(candidate.preferredFormats)
|
|
50
|
+
.map((val) => String(val || '').toLowerCase())
|
|
51
|
+
.filter(Boolean);
|
|
52
|
+
const formats = arrayify(candidate.formats || candidate.supportedFormats)
|
|
53
|
+
.map((val) => String(val || '').toLowerCase())
|
|
54
|
+
.filter(Boolean);
|
|
55
|
+
const qualities = arrayify(candidate.qualities)
|
|
56
|
+
.map((val) => String(val || '').toLowerCase())
|
|
57
|
+
.filter(Boolean);
|
|
58
|
+
return {
|
|
59
|
+
id: String(id),
|
|
60
|
+
type: type ? String(type) : '',
|
|
61
|
+
profile: profile ? String(profile) : '',
|
|
62
|
+
formats,
|
|
63
|
+
preferredFormats,
|
|
64
|
+
qualities,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function isLevel0Profile(profile) {
|
|
69
|
+
if (!profile) return false;
|
|
70
|
+
try {
|
|
71
|
+
return /level0/i.test(String(profile));
|
|
72
|
+
} catch (_) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function isLevel0Service(candidate) {
|
|
78
|
+
if (!candidate || typeof candidate !== 'object') return false;
|
|
79
|
+
if (candidate.profile && isLevel0Profile(candidate.profile)) return true;
|
|
80
|
+
const normalized = normalizeImageServiceCandidate(candidate);
|
|
81
|
+
if (!normalized) return false;
|
|
82
|
+
return Boolean(normalized.profile && isLevel0Profile(normalized.profile));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function isIiifImageService(candidate) {
|
|
86
|
+
if (!candidate) return false;
|
|
87
|
+
const {type, profile} = candidate;
|
|
88
|
+
if (type && /ImageService/i.test(type)) return true;
|
|
89
|
+
if (profile && /iiif\.io\/api\/image/i.test(profile)) return true;
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function extractImageService(value, seen = new Set()) {
|
|
94
|
+
if (!value || typeof value !== 'object') return null;
|
|
95
|
+
if (seen.has(value)) return null;
|
|
96
|
+
seen.add(value);
|
|
97
|
+
|
|
98
|
+
const direct = normalizeImageServiceCandidate(value);
|
|
99
|
+
if (isIiifImageService(direct)) return direct;
|
|
100
|
+
|
|
101
|
+
const branches = [
|
|
102
|
+
value.service,
|
|
103
|
+
value.services,
|
|
104
|
+
value.thumbnail,
|
|
105
|
+
value.thumbnails,
|
|
106
|
+
value.body,
|
|
107
|
+
];
|
|
108
|
+
for (const branch of branches) {
|
|
109
|
+
const list = arrayify(branch);
|
|
110
|
+
for (const entry of list) {
|
|
111
|
+
const svc = extractImageService(entry, seen);
|
|
112
|
+
if (svc) return svc;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function normalizeImagePayload(body, canvas) {
|
|
119
|
+
if (!body || typeof body !== 'object') return null;
|
|
120
|
+
const id = body.id || body['@id'];
|
|
121
|
+
const bodyType = normalizeBodyType(body.type || body['@type']);
|
|
122
|
+
const width =
|
|
123
|
+
typeof body.width === 'number'
|
|
124
|
+
? body.width
|
|
125
|
+
: typeof (canvas && canvas.width) === 'number'
|
|
126
|
+
? canvas.width
|
|
127
|
+
: undefined;
|
|
128
|
+
const height =
|
|
129
|
+
typeof body.height === 'number'
|
|
130
|
+
? body.height
|
|
131
|
+
: typeof (canvas && canvas.height) === 'number'
|
|
132
|
+
? canvas.height
|
|
133
|
+
: undefined;
|
|
134
|
+
const service = extractImageService(body);
|
|
135
|
+
return {
|
|
136
|
+
id: id ? String(id) : '',
|
|
137
|
+
width,
|
|
138
|
+
height,
|
|
139
|
+
service: service || undefined,
|
|
140
|
+
bodyType: bodyType || undefined,
|
|
141
|
+
isImageBody: Boolean(bodyType && isImageBodyType(bodyType)),
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function inspectCanvasForImage(canvas) {
|
|
146
|
+
if (!canvas || typeof canvas !== 'object') return null;
|
|
147
|
+
const annotationPages = arrayify(canvas.items || canvas.annotations);
|
|
148
|
+
for (const page of annotationPages) {
|
|
149
|
+
if (!page || typeof page !== 'object') continue;
|
|
150
|
+
const annotations = arrayify(page.items || page.annotations);
|
|
151
|
+
for (const annotation of annotations) {
|
|
152
|
+
if (!annotation || typeof annotation !== 'object') continue;
|
|
153
|
+
const bodies = arrayify(annotation.body);
|
|
154
|
+
for (const body of bodies) {
|
|
155
|
+
const payload = normalizeImagePayload(body, canvas);
|
|
156
|
+
if (payload && (payload.id || payload.service)) return payload;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
if (canvas.placeholderCanvas) {
|
|
161
|
+
const placeholderPayload = inspectCanvasForImage(canvas.placeholderCanvas);
|
|
162
|
+
if (placeholderPayload) return placeholderPayload;
|
|
163
|
+
}
|
|
164
|
+
const thumbnails = arrayify(canvas.thumbnail);
|
|
165
|
+
for (const thumb of thumbnails) {
|
|
166
|
+
const payload = normalizeImagePayload(thumb, canvas);
|
|
167
|
+
if (payload && (payload.id || payload.service)) return payload;
|
|
168
|
+
}
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function findPrimaryCanvasImage(resource) {
|
|
173
|
+
try {
|
|
174
|
+
const canvases = arrayify(resource && resource.items);
|
|
175
|
+
if (!canvases.length) return null;
|
|
176
|
+
for (const canvas of canvases) {
|
|
177
|
+
const payload = inspectCanvasForImage(canvas);
|
|
178
|
+
if (payload) return payload;
|
|
179
|
+
}
|
|
180
|
+
} catch (_) {}
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function findCanvasImageService(resource) {
|
|
185
|
+
const payload = findPrimaryCanvasImage(resource);
|
|
186
|
+
return payload && payload.service ? payload.service : null;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function normalizeServiceBaseId(id) {
|
|
190
|
+
if (!id) return '';
|
|
191
|
+
try {
|
|
192
|
+
let cleaned = String(id).trim();
|
|
193
|
+
cleaned = cleaned.replace(/\/info\.json$/i, '');
|
|
194
|
+
cleaned = cleaned.replace(/\/$/, '');
|
|
195
|
+
return cleaned;
|
|
196
|
+
} catch (_) {
|
|
197
|
+
return String(id || '');
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function selectServiceFormat(candidate) {
|
|
202
|
+
if (!candidate) return 'jpg';
|
|
203
|
+
const formats = Array.isArray(candidate.formats)
|
|
204
|
+
? candidate.formats
|
|
205
|
+
: Array.isArray(candidate.preferredFormats)
|
|
206
|
+
? candidate.preferredFormats
|
|
207
|
+
: [];
|
|
208
|
+
const prioritized = ['jpg', 'jpeg', 'png', 'webp'];
|
|
209
|
+
const lower = formats.map((f) => String(f || '').toLowerCase());
|
|
210
|
+
for (const fmt of prioritized) {
|
|
211
|
+
if (lower.includes(fmt)) return fmt === 'jpeg' ? 'jpg' : fmt;
|
|
212
|
+
}
|
|
213
|
+
return 'jpg';
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function selectServiceQuality(candidate) {
|
|
217
|
+
if (!candidate) return 'default';
|
|
218
|
+
const {qualities = [], type = '', profile = ''} = candidate;
|
|
219
|
+
if (qualities.includes('default')) return 'default';
|
|
220
|
+
if (qualities.includes('native')) return 'native';
|
|
221
|
+
if (/ImageService3/i.test(type)) return 'default';
|
|
222
|
+
if (/ImageService2/i.test(type) || /ImageService1/i.test(type)) {
|
|
223
|
+
if (/level0/i.test(profile)) return 'default';
|
|
224
|
+
return 'default';
|
|
225
|
+
}
|
|
226
|
+
if (/iiif\.io\/api\/image/i.test(profile)) return 'default';
|
|
227
|
+
return 'default';
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function buildIiifImageUrlFromNormalizedService(service, preferredSize = 800) {
|
|
231
|
+
if (!service || !isIiifImageService(service)) return '';
|
|
232
|
+
if (isLevel0Profile(service.profile)) return '';
|
|
233
|
+
const baseId = normalizeServiceBaseId(service.id);
|
|
234
|
+
if (!baseId) return '';
|
|
235
|
+
const size = preferredSize && preferredSize > 0 ? preferredSize : 800;
|
|
236
|
+
const quality = selectServiceQuality(service);
|
|
237
|
+
const format = selectServiceFormat(service);
|
|
238
|
+
return `${baseId}/full/!${size},${size}/0/${quality}.${format}`;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function buildIiifImageUrlFromService(service, preferredSize = 800) {
|
|
242
|
+
const normalized = normalizeImageServiceCandidate(service);
|
|
243
|
+
if (!normalized) return '';
|
|
244
|
+
return buildIiifImageUrlFromNormalizedService(normalized, preferredSize);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function buildIiifImageUrlForDimensions(service, width = 1200, height = 630) {
|
|
248
|
+
const normalized = normalizeImageServiceCandidate(service);
|
|
249
|
+
if (!normalized || !isIiifImageService(normalized)) return '';
|
|
250
|
+
if (isLevel0Profile(normalized.profile)) return '';
|
|
251
|
+
const baseId = normalizeServiceBaseId(normalized.id);
|
|
252
|
+
if (!baseId) return '';
|
|
253
|
+
const safeWidth = Math.max(1, Math.floor(Number(width) || 0));
|
|
254
|
+
const safeHeight = Math.max(1, Math.floor(Number(height) || 0));
|
|
255
|
+
const quality = selectServiceQuality(normalized);
|
|
256
|
+
const format = selectServiceFormat(normalized);
|
|
257
|
+
return `${baseId}/full/!${safeWidth},${safeHeight}/0/${quality}.${format}`;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function buildIiifImageSrcset(service, steps = [360, 640, 960, 1280, 1600]) {
|
|
261
|
+
const normalized = normalizeImageServiceCandidate(service);
|
|
262
|
+
if (!normalized || !isIiifImageService(normalized)) return '';
|
|
263
|
+
if (isLevel0Profile(normalized.profile)) return '';
|
|
264
|
+
const uniqueSteps = Array.from(
|
|
265
|
+
new Set(
|
|
266
|
+
(Array.isArray(steps) ? steps : [])
|
|
267
|
+
.map((value) => Number(value) || 0)
|
|
268
|
+
.filter((value) => value > 0)
|
|
269
|
+
.sort((a, b) => a - b)
|
|
270
|
+
)
|
|
271
|
+
);
|
|
272
|
+
if (!uniqueSteps.length) return '';
|
|
273
|
+
const entries = uniqueSteps
|
|
274
|
+
.map((width) => {
|
|
275
|
+
const url = buildIiifImageUrlFromNormalizedService(normalized, width);
|
|
276
|
+
return url ? `${url} ${width}w` : '';
|
|
277
|
+
})
|
|
278
|
+
.filter(Boolean);
|
|
279
|
+
return entries.join(', ');
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
async function getRepresentativeImage(resource, preferredSize = 1200, unsafe = false) {
|
|
283
|
+
// Fast path: if resource already contains a thumbnail, return it without any helper work
|
|
284
|
+
try {
|
|
285
|
+
const t = resource && resource.thumbnail;
|
|
286
|
+
if (t) {
|
|
287
|
+
const first = Array.isArray(t) ? t[0] : t;
|
|
288
|
+
if (first && (first.id || first['@id'])) {
|
|
289
|
+
const canvasImage = findPrimaryCanvasImage(resource);
|
|
290
|
+
const service =
|
|
291
|
+
extractImageService(first) ||
|
|
292
|
+
(canvasImage && canvasImage.service) ||
|
|
293
|
+
undefined;
|
|
294
|
+
return {
|
|
295
|
+
id: String(first.id || first['@id']),
|
|
296
|
+
width:
|
|
297
|
+
typeof first.width === 'number'
|
|
298
|
+
? first.width
|
|
299
|
+
: canvasImage && typeof canvasImage.width === 'number'
|
|
300
|
+
? canvasImage.width
|
|
301
|
+
: undefined,
|
|
302
|
+
height:
|
|
303
|
+
typeof first.height === 'number'
|
|
304
|
+
? first.height
|
|
305
|
+
: canvasImage && typeof canvasImage.height === 'number'
|
|
306
|
+
? canvasImage.height
|
|
307
|
+
: undefined,
|
|
308
|
+
service: service || undefined,
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
} catch (_) {}
|
|
313
|
+
|
|
314
|
+
// Avoid potentially long-running network calls in safe mode
|
|
315
|
+
if (!unsafe) return null;
|
|
316
|
+
|
|
317
|
+
// Unsafe mode: Clover's own thumbnail helpers (`@nulib/clover/helpers`, which need no
|
|
318
|
+
// React), asking the image service for a size it offers, with a timeout so builds never
|
|
319
|
+
// hang.
|
|
320
|
+
try {
|
|
321
|
+
const clover = await import('@nulib/clover/helpers');
|
|
322
|
+
const task = (async () => {
|
|
323
|
+
const source = clover.thumbnailSource(await clover.toPresentation4(resource));
|
|
324
|
+
const url = source ? await clover.resolveThumbnail(source, preferredSize) : undefined;
|
|
325
|
+
return url ? { id: url } : null;
|
|
326
|
+
})();
|
|
327
|
+
const timeoutMs = Number(process.env.CANOPY_THUMB_TIMEOUT || 2000);
|
|
328
|
+
const timeout = new Promise((resolve) => setTimeout(() => resolve(null), timeoutMs));
|
|
329
|
+
const result = await Promise.race([task, timeout]);
|
|
330
|
+
if (!result) return null;
|
|
331
|
+
const id = String(result.id || '');
|
|
332
|
+
const width = undefined;
|
|
333
|
+
const height = undefined;
|
|
334
|
+
const canvasImage = findPrimaryCanvasImage(resource);
|
|
335
|
+
const service =
|
|
336
|
+
extractImageService(result) ||
|
|
337
|
+
extractImageService(resource) ||
|
|
338
|
+
(canvasImage && canvasImage.service);
|
|
339
|
+
return id
|
|
340
|
+
? {
|
|
341
|
+
id,
|
|
342
|
+
width:
|
|
343
|
+
width != null
|
|
344
|
+
? width
|
|
345
|
+
: canvasImage && typeof canvasImage.width === 'number'
|
|
346
|
+
? canvasImage.width
|
|
347
|
+
: undefined,
|
|
348
|
+
height:
|
|
349
|
+
height != null
|
|
350
|
+
? height
|
|
351
|
+
: canvasImage && typeof canvasImage.height === 'number'
|
|
352
|
+
? canvasImage.height
|
|
353
|
+
: undefined,
|
|
354
|
+
service: service || undefined,
|
|
355
|
+
}
|
|
356
|
+
: null;
|
|
357
|
+
} catch (_) {
|
|
358
|
+
return null;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
async function getThumbnail(resource, preferredSize = 1200, unsafe = false) {
|
|
363
|
+
// Prefer embedded thumbnail with available dimensions
|
|
364
|
+
try {
|
|
365
|
+
const t = resource && resource.thumbnail;
|
|
366
|
+
const first = Array.isArray(t) ? t[0] : t;
|
|
367
|
+
if (first && (first.id || first['@id'])) {
|
|
368
|
+
const url = String(first.id || first['@id']);
|
|
369
|
+
const width = typeof first.width === 'number' ? first.width : undefined;
|
|
370
|
+
const height = typeof first.height === 'number' ? first.height : undefined;
|
|
371
|
+
return { url, width, height };
|
|
372
|
+
}
|
|
373
|
+
} catch (_) {}
|
|
374
|
+
// Fall back to helper (unsafe mode only) with timeout
|
|
375
|
+
try {
|
|
376
|
+
const rep = await getRepresentativeImage(resource, preferredSize, unsafe);
|
|
377
|
+
if (rep && (rep.id || rep['@id'])) {
|
|
378
|
+
return {
|
|
379
|
+
url: String(rep.id || rep['@id']),
|
|
380
|
+
width: typeof rep.width === 'number' ? rep.width : undefined,
|
|
381
|
+
height: typeof rep.height === 'number' ? rep.height : undefined,
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
} catch (_) {}
|
|
385
|
+
return { url: '' };
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
async function getThumbnailUrl(resource, preferredSize = 1200, unsafe = false) {
|
|
389
|
+
const res = await getThumbnail(resource, preferredSize, unsafe);
|
|
390
|
+
return res && res.url ? String(res.url) : '';
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
module.exports = {
|
|
394
|
+
getRepresentativeImage,
|
|
395
|
+
getThumbnail,
|
|
396
|
+
getThumbnailUrl,
|
|
397
|
+
buildIiifImageUrlFromService,
|
|
398
|
+
buildIiifImageUrlForDimensions,
|
|
399
|
+
findPrimaryCanvasImage,
|
|
400
|
+
buildIiifImageSrcset,
|
|
401
|
+
isLevel0Service,
|
|
402
|
+
};
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
const DEFAULT_LOCALE_MESSAGES = require('./default-locale');
|
|
2
|
+
|
|
3
|
+
function getGlobalScope() {
|
|
4
|
+
if (typeof globalThis !== 'undefined') return globalThis;
|
|
5
|
+
if (typeof window !== 'undefined') return window;
|
|
6
|
+
if (typeof global !== 'undefined') return global;
|
|
7
|
+
return null;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function accessPath(obj, path) {
|
|
11
|
+
if (!obj || typeof obj !== 'object' || !path) return undefined;
|
|
12
|
+
const parts = Array.isArray(path) ? path : String(path).split('.');
|
|
13
|
+
let current = obj;
|
|
14
|
+
for (const part of parts) {
|
|
15
|
+
if (!current || typeof current !== 'object') return undefined;
|
|
16
|
+
const key = String(part).trim();
|
|
17
|
+
if (!key || !(key in current)) return undefined;
|
|
18
|
+
current = current[key];
|
|
19
|
+
}
|
|
20
|
+
return current;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function formatTemplate(template, replacements = {}) {
|
|
24
|
+
if (template == null) return template;
|
|
25
|
+
const str = String(template);
|
|
26
|
+
if (!replacements || typeof replacements !== 'object') return str;
|
|
27
|
+
return str.replace(/\{([^}]+)\}/g, (match, key) => {
|
|
28
|
+
const replacement = Object.prototype.hasOwnProperty.call(replacements, key)
|
|
29
|
+
? replacements[key]
|
|
30
|
+
: undefined;
|
|
31
|
+
if (replacement == null) return match;
|
|
32
|
+
return String(replacement);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function getRuntimeLocaleMessages() {
|
|
37
|
+
const scope = getGlobalScope();
|
|
38
|
+
const candidate = scope && scope.CANOPY_LOCALE_MESSAGES;
|
|
39
|
+
if (candidate && typeof candidate === 'object') return candidate;
|
|
40
|
+
return DEFAULT_LOCALE_MESSAGES || {};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function getRuntimeLocaleCode() {
|
|
44
|
+
const scope = getGlobalScope();
|
|
45
|
+
const value = scope && scope.CANOPY_LOCALE_CODE;
|
|
46
|
+
return typeof value === 'string' ? value : '';
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function getRuntimeMessage(path, fallback) {
|
|
50
|
+
const messages = getRuntimeLocaleMessages();
|
|
51
|
+
const value = accessPath(messages, path);
|
|
52
|
+
if (value == null) return fallback;
|
|
53
|
+
return value;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function formatRuntimeMessage(path, fallback, replacements) {
|
|
57
|
+
const template = getRuntimeMessage(path, fallback);
|
|
58
|
+
if (template == null) return template;
|
|
59
|
+
return formatTemplate(template, replacements);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
module.exports = {
|
|
63
|
+
getRuntimeLocaleMessages,
|
|
64
|
+
getRuntimeLocaleCode,
|
|
65
|
+
getRuntimeMessage,
|
|
66
|
+
formatRuntimeMessage,
|
|
67
|
+
formatTemplate,
|
|
68
|
+
accessPath,
|
|
69
|
+
};
|
package/lib/locales.js
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const yaml = require('js-yaml');
|
|
4
|
+
const {resolveCanopyConfigPath} = require('./config-path');
|
|
5
|
+
const DEFAULT_LOCALE_MESSAGES = require('./default-locale');
|
|
6
|
+
|
|
7
|
+
const CONTENT_DIR = path.resolve('content');
|
|
8
|
+
|
|
9
|
+
let cachedLocales = null;
|
|
10
|
+
let cachedMessages = null;
|
|
11
|
+
const DEFAULT_LOCALE_ROUTES = {
|
|
12
|
+
works: 'works',
|
|
13
|
+
search: 'search',
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
function normalizeLocale(entry, index) {
|
|
17
|
+
if (!entry || typeof entry !== 'object') return null;
|
|
18
|
+
const lang = typeof entry.lang === 'string' ? entry.lang.trim() : '';
|
|
19
|
+
if (!lang) return null;
|
|
20
|
+
const label =
|
|
21
|
+
typeof entry.label === 'string' && entry.label.trim()
|
|
22
|
+
? entry.label.trim()
|
|
23
|
+
: lang.toUpperCase();
|
|
24
|
+
return {lang, label, default: entry.default === true, index};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function ensureLocales(list) {
|
|
28
|
+
if (!Array.isArray(list) || !list.length) {
|
|
29
|
+
return [
|
|
30
|
+
{
|
|
31
|
+
lang: 'en',
|
|
32
|
+
label: 'English',
|
|
33
|
+
default: true,
|
|
34
|
+
},
|
|
35
|
+
];
|
|
36
|
+
}
|
|
37
|
+
const normalized = list
|
|
38
|
+
.map((item, index) => normalizeLocale(item, index))
|
|
39
|
+
.filter(Boolean);
|
|
40
|
+
if (!normalized.length) return ensureLocales(null);
|
|
41
|
+
const explicitDefault = normalized.find((item) => item.default);
|
|
42
|
+
if (explicitDefault) {
|
|
43
|
+
normalized.forEach((item) => {
|
|
44
|
+
item.default = item.index === explicitDefault.index;
|
|
45
|
+
});
|
|
46
|
+
} else {
|
|
47
|
+
normalized[0].default = true;
|
|
48
|
+
}
|
|
49
|
+
return normalized.map(({index, ...rest}) => rest);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function readCanopyLocales() {
|
|
53
|
+
if (cachedLocales) return cachedLocales;
|
|
54
|
+
try {
|
|
55
|
+
const cfgPath = resolveCanopyConfigPath();
|
|
56
|
+
if (!fs.existsSync(cfgPath)) {
|
|
57
|
+
cachedLocales = ensureLocales(null);
|
|
58
|
+
return cachedLocales;
|
|
59
|
+
}
|
|
60
|
+
const raw = fs.readFileSync(cfgPath, 'utf8');
|
|
61
|
+
const data = yaml.load(raw) || {};
|
|
62
|
+
cachedLocales = ensureLocales(data.locales);
|
|
63
|
+
} catch (_) {
|
|
64
|
+
cachedLocales = ensureLocales(null);
|
|
65
|
+
}
|
|
66
|
+
return cachedLocales;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function readYamlFile(filePath) {
|
|
70
|
+
try {
|
|
71
|
+
if (!fs.existsSync(filePath)) return null;
|
|
72
|
+
const raw = fs.readFileSync(filePath, 'utf8');
|
|
73
|
+
if (!raw) return null;
|
|
74
|
+
const data = yaml.load(raw);
|
|
75
|
+
return data && typeof data === 'object' ? data : null;
|
|
76
|
+
} catch (_) {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function deepMerge(base, override) {
|
|
82
|
+
if (!override) return base || null;
|
|
83
|
+
if (!base) return override || null;
|
|
84
|
+
const result = Array.isArray(base) ? base.slice() : {...base};
|
|
85
|
+
Object.keys(override).forEach((key) => {
|
|
86
|
+
const baseVal = base[key];
|
|
87
|
+
const overrideVal = override[key];
|
|
88
|
+
if (
|
|
89
|
+
baseVal &&
|
|
90
|
+
overrideVal &&
|
|
91
|
+
typeof baseVal === 'object' &&
|
|
92
|
+
typeof overrideVal === 'object' &&
|
|
93
|
+
!Array.isArray(baseVal) &&
|
|
94
|
+
!Array.isArray(overrideVal)
|
|
95
|
+
) {
|
|
96
|
+
result[key] = deepMerge(baseVal, overrideVal);
|
|
97
|
+
} else {
|
|
98
|
+
result[key] = overrideVal;
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
return result;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function readLocaleMessages(lang) {
|
|
105
|
+
const basePath = path.join(CONTENT_DIR, 'locale.yml');
|
|
106
|
+
const userMessages = readYamlFile(basePath);
|
|
107
|
+
const defaultMessages = deepMerge(DEFAULT_LOCALE_MESSAGES, userMessages) || {};
|
|
108
|
+
if (!lang) return defaultMessages;
|
|
109
|
+
const langPath = path.join(CONTENT_DIR, lang, 'locale.yml');
|
|
110
|
+
const override = readYamlFile(langPath);
|
|
111
|
+
return deepMerge(defaultMessages, override);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function serializeForScript(value) {
|
|
115
|
+
try {
|
|
116
|
+
return JSON.stringify(value || {}).replace(/</g, '\\u003c');
|
|
117
|
+
} catch (_) {
|
|
118
|
+
return '{}';
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function buildLocaleRuntimeScript(localeCode) {
|
|
123
|
+
let messages = null;
|
|
124
|
+
try {
|
|
125
|
+
messages = readLocaleMessages(localeCode);
|
|
126
|
+
} catch (_) {
|
|
127
|
+
messages = null;
|
|
128
|
+
}
|
|
129
|
+
if (!messages) {
|
|
130
|
+
try {
|
|
131
|
+
messages = readLocaleMessages(null);
|
|
132
|
+
} catch (_) {
|
|
133
|
+
messages = DEFAULT_LOCALE_MESSAGES;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
const langValue = typeof localeCode === 'string' ? localeCode : '';
|
|
137
|
+
const serializedMessages = serializeForScript(messages || {});
|
|
138
|
+
const serializedCode = JSON.stringify(langValue || '').replace(/</g, '\\u003c');
|
|
139
|
+
return `<script>window.CANOPY_LOCALE_CODE=${serializedCode};window.CANOPY_LOCALE_MESSAGES=${serializedMessages};</script>`;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function normalizeRouteValue(value) {
|
|
143
|
+
if (typeof value !== 'string') return '';
|
|
144
|
+
const trimmed = value.trim();
|
|
145
|
+
if (!trimmed) return '';
|
|
146
|
+
return trimmed.replace(/^\/+/, '').replace(/\/+$/, '');
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function readLocaleRoutes(lang) {
|
|
150
|
+
const data = readLocaleMessages(lang);
|
|
151
|
+
const rawRoutes = data && data.routes ? data.routes : null;
|
|
152
|
+
const routes = {};
|
|
153
|
+
Object.keys(DEFAULT_LOCALE_ROUTES).forEach((key) => {
|
|
154
|
+
const candidate = rawRoutes && rawRoutes[key];
|
|
155
|
+
const normalized = normalizeRouteValue(
|
|
156
|
+
typeof candidate === 'string' ? candidate : ''
|
|
157
|
+
);
|
|
158
|
+
routes[key] = normalized || DEFAULT_LOCALE_ROUTES[key];
|
|
159
|
+
});
|
|
160
|
+
return routes;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function buildLanguageToggleCopy(locales) {
|
|
164
|
+
const normalized = Array.isArray(locales) ? locales : [];
|
|
165
|
+
const copyMap = {};
|
|
166
|
+
const fallback = readLocaleMessages(null);
|
|
167
|
+
const fallbackToggle =
|
|
168
|
+
fallback && fallback.ui && fallback.ui.languageToggle
|
|
169
|
+
? {...fallback.ui.languageToggle}
|
|
170
|
+
: {};
|
|
171
|
+
copyMap.__default = fallbackToggle;
|
|
172
|
+
normalized.forEach((locale) => {
|
|
173
|
+
if (!locale || !locale.lang) return;
|
|
174
|
+
const data = readLocaleMessages(locale.lang);
|
|
175
|
+
const localeToggle =
|
|
176
|
+
data && data.ui && data.ui.languageToggle
|
|
177
|
+
? {...data.ui.languageToggle}
|
|
178
|
+
: null;
|
|
179
|
+
if (localeToggle && typeof localeToggle.label !== 'string') {
|
|
180
|
+
localeToggle.label = null;
|
|
181
|
+
}
|
|
182
|
+
copyMap[locale.lang] = localeToggle;
|
|
183
|
+
});
|
|
184
|
+
return copyMap;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function readCanopyLocalesWithMessages() {
|
|
188
|
+
if (cachedMessages) return cachedMessages;
|
|
189
|
+
const locales = readCanopyLocales();
|
|
190
|
+
const messages = buildLanguageToggleCopy(locales);
|
|
191
|
+
cachedMessages = {locales, messages};
|
|
192
|
+
return cachedMessages;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
module.exports = {
|
|
196
|
+
readCanopyLocales,
|
|
197
|
+
buildLanguageToggleCopy,
|
|
198
|
+
readCanopyLocalesWithMessages,
|
|
199
|
+
readLocaleMessages,
|
|
200
|
+
readLocaleRoutes,
|
|
201
|
+
buildLocaleRuntimeScript,
|
|
202
|
+
DEFAULT_LOCALE_ROUTES,
|
|
203
|
+
};
|