@nulib/canopy 0.0.0 → 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 +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,381 @@
|
|
|
1
|
+
const {
|
|
2
|
+
fs,
|
|
3
|
+
path,
|
|
4
|
+
CONTENT_DIR,
|
|
5
|
+
rootRelativeHref,
|
|
6
|
+
canonicalizeLocaleCode,
|
|
7
|
+
} = require("../common");
|
|
8
|
+
const mdx = require("../build/mdx.js");
|
|
9
|
+
const {getPageContext} = require("../page-context");
|
|
10
|
+
|
|
11
|
+
const EXCLUDED_ROOTS = new Set(["works", "search"]);
|
|
12
|
+
|
|
13
|
+
let NAV_CACHE = null;
|
|
14
|
+
|
|
15
|
+
function normalizeRelativePath(rel) {
|
|
16
|
+
if (!rel) return "";
|
|
17
|
+
let normalized = String(rel).replace(/\\+/g, "/");
|
|
18
|
+
while (normalized.startsWith("./")) normalized = normalized.slice(2);
|
|
19
|
+
while (normalized.startsWith("../")) normalized = normalized.slice(3);
|
|
20
|
+
if (normalized.startsWith("/")) {
|
|
21
|
+
normalized = normalized.replace(/^\/+/, "");
|
|
22
|
+
}
|
|
23
|
+
return normalized;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function humanizeSegment(seg) {
|
|
27
|
+
if (!seg) return "";
|
|
28
|
+
const cleaned = String(seg).replace(/[-_]+/g, " ");
|
|
29
|
+
return cleaned.replace(/(^|\s)([a-z])/g, (match) => match.toUpperCase());
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function slugFromRelative(relativePath) {
|
|
33
|
+
const normalized = normalizeRelativePath(relativePath);
|
|
34
|
+
if (!normalized) {
|
|
35
|
+
return {slug: "", segments: [], isIndex: false};
|
|
36
|
+
}
|
|
37
|
+
const parts = normalized.split("/");
|
|
38
|
+
const fileName = parts.pop() || "";
|
|
39
|
+
const baseName = fileName.replace(/\.mdx$/i, "");
|
|
40
|
+
const isIndex = baseName.toLowerCase() === "index";
|
|
41
|
+
const dirSegments = parts.filter(Boolean);
|
|
42
|
+
const segments = isIndex ? dirSegments : dirSegments.concat(baseName);
|
|
43
|
+
const slug = segments.join("/");
|
|
44
|
+
return {slug, segments, isIndex};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Returns true when the segment is a configured (non-default) locale code that
|
|
48
|
+
// prefixes content routes, e.g. "fr" for content/fr/...
|
|
49
|
+
function isLocaleSegment(segment) {
|
|
50
|
+
if (!segment) return false;
|
|
51
|
+
try {
|
|
52
|
+
return !!canonicalizeLocaleCode(segment);
|
|
53
|
+
} catch (_) {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Depth (number of leading segments) that identifies a navigation section root
|
|
59
|
+
// for a page. Locale-prefixed routes root at <locale>/<section>; everything else
|
|
60
|
+
// roots at <section>.
|
|
61
|
+
function rootDepthForSegments(segments) {
|
|
62
|
+
if (!Array.isArray(segments) || !segments.length) return 0;
|
|
63
|
+
if (segments.length > 1 && isLocaleSegment(segments[0])) return 2;
|
|
64
|
+
return 1;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Slug of the section root for a page's segments (locale-aware).
|
|
68
|
+
function rootSegmentForSegments(segments) {
|
|
69
|
+
const depth = rootDepthForSegments(segments);
|
|
70
|
+
if (!depth) return "";
|
|
71
|
+
return segments.slice(0, depth).join("/");
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function pageSortKey(relativePath) {
|
|
75
|
+
const normalized = normalizeRelativePath(relativePath).toLowerCase();
|
|
76
|
+
if (!normalized) return "";
|
|
77
|
+
const withoutExtension = normalized.replace(/\.mdx$/i, "");
|
|
78
|
+
return withoutExtension.replace(/(^|\/)index$/i, "$1-index");
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function extractTitleSafe(raw) {
|
|
82
|
+
try {
|
|
83
|
+
return mdx.extractTitle(raw);
|
|
84
|
+
} catch (_) {
|
|
85
|
+
return "Untitled";
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function collectPagesSync() {
|
|
90
|
+
const pages = [];
|
|
91
|
+
|
|
92
|
+
function walk(dir) {
|
|
93
|
+
let entries = [];
|
|
94
|
+
try {
|
|
95
|
+
entries = fs.readdirSync(dir, {withFileTypes: true});
|
|
96
|
+
} catch (_) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
for (const entry of entries) {
|
|
100
|
+
if (!entry) continue;
|
|
101
|
+
const name = entry.name || "";
|
|
102
|
+
if (!name) continue;
|
|
103
|
+
if (name.startsWith(".")) continue;
|
|
104
|
+
const absPath = path.join(dir, name);
|
|
105
|
+
const relPath = path.relative(CONTENT_DIR, absPath);
|
|
106
|
+
const normalizedRel = normalizeRelativePath(relPath);
|
|
107
|
+
if (!normalizedRel) continue;
|
|
108
|
+
const segments = normalizedRel.split("/");
|
|
109
|
+
const firstRaw = segments[0] || "";
|
|
110
|
+
const firstSegment = firstRaw.replace(/\.mdx$/i, "");
|
|
111
|
+
if (EXCLUDED_ROOTS.has(firstSegment)) continue;
|
|
112
|
+
if (segments.some((segment) => segment.startsWith("_"))) continue;
|
|
113
|
+
if (entry.isDirectory()) {
|
|
114
|
+
walk(absPath);
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
if (!entry.isFile()) continue;
|
|
118
|
+
if (!/\.mdx$/i.test(name)) continue;
|
|
119
|
+
let raw = "";
|
|
120
|
+
try {
|
|
121
|
+
raw = fs.readFileSync(absPath, "utf8");
|
|
122
|
+
} catch (_) {
|
|
123
|
+
raw = "";
|
|
124
|
+
}
|
|
125
|
+
let frontmatterData = null;
|
|
126
|
+
if (raw && typeof mdx.parseFrontmatter === "function") {
|
|
127
|
+
try {
|
|
128
|
+
const parsed = mdx.parseFrontmatter(raw);
|
|
129
|
+
if (parsed && parsed.data && typeof parsed.data === "object") {
|
|
130
|
+
frontmatterData = parsed.data;
|
|
131
|
+
}
|
|
132
|
+
} catch (_) {
|
|
133
|
+
frontmatterData = null;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
const {
|
|
137
|
+
slug,
|
|
138
|
+
segments: slugSegments,
|
|
139
|
+
isIndex,
|
|
140
|
+
} = slugFromRelative(normalizedRel);
|
|
141
|
+
const titleRaw = extractTitleSafe(raw);
|
|
142
|
+
const fallbackTitle = humanizeSegment(
|
|
143
|
+
slugSegments.slice(-1)[0] || firstSegment || ""
|
|
144
|
+
);
|
|
145
|
+
const title =
|
|
146
|
+
titleRaw && titleRaw !== "Untitled"
|
|
147
|
+
? titleRaw
|
|
148
|
+
: fallbackTitle || titleRaw;
|
|
149
|
+
const htmlRel = normalizedRel.replace(/\.mdx$/i, ".html");
|
|
150
|
+
const href = rootRelativeHref(htmlRel);
|
|
151
|
+
const page = {
|
|
152
|
+
filePath: absPath,
|
|
153
|
+
relativePath: normalizedRel,
|
|
154
|
+
slug,
|
|
155
|
+
segments: slugSegments,
|
|
156
|
+
isIndex,
|
|
157
|
+
href,
|
|
158
|
+
title,
|
|
159
|
+
fallbackTitle,
|
|
160
|
+
sortKey: pageSortKey(normalizedRel),
|
|
161
|
+
topSegment: slugSegments[0] || firstSegment || "",
|
|
162
|
+
};
|
|
163
|
+
pages.push(page);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
walk(CONTENT_DIR);
|
|
168
|
+
pages.sort((a, b) => a.sortKey.localeCompare(b.sortKey));
|
|
169
|
+
return pages;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function createNode(slug) {
|
|
173
|
+
const segments = slug ? slug.split("/") : [];
|
|
174
|
+
const name = segments.slice(-1)[0] || "";
|
|
175
|
+
return {
|
|
176
|
+
slug,
|
|
177
|
+
segments,
|
|
178
|
+
name,
|
|
179
|
+
title: humanizeSegment(name),
|
|
180
|
+
href: null,
|
|
181
|
+
hasContent: false,
|
|
182
|
+
relativePath: null,
|
|
183
|
+
sortKey: slug || name,
|
|
184
|
+
sourcePage: null,
|
|
185
|
+
children: [],
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function getNavigationCache() {
|
|
190
|
+
if (NAV_CACHE) return NAV_CACHE;
|
|
191
|
+
const pages = collectPagesSync();
|
|
192
|
+
const pagesByRelative = new Map();
|
|
193
|
+
const nodes = new Map();
|
|
194
|
+
|
|
195
|
+
for (const page of pages) {
|
|
196
|
+
const {slug, segments} = page;
|
|
197
|
+
pagesByRelative.set(page.relativePath, page);
|
|
198
|
+
if (!segments.length) continue;
|
|
199
|
+
for (let i = 0; i < segments.length; i += 1) {
|
|
200
|
+
const key = segments.slice(0, i + 1).join("/");
|
|
201
|
+
if (key && !nodes.has(key)) {
|
|
202
|
+
nodes.set(key, createNode(key));
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
for (const page of pages) {
|
|
208
|
+
if (!page.slug) continue;
|
|
209
|
+
const node = nodes.get(page.slug);
|
|
210
|
+
if (!node) continue;
|
|
211
|
+
if (
|
|
212
|
+
!node.sourcePage ||
|
|
213
|
+
(node.sourcePage && node.sourcePage.isIndex && !page.isIndex)
|
|
214
|
+
) {
|
|
215
|
+
node.sourcePage = page;
|
|
216
|
+
node.title = page.title || node.title;
|
|
217
|
+
node.href = page.href || node.href;
|
|
218
|
+
node.relativePath = page.relativePath;
|
|
219
|
+
node.sortKey = page.sortKey || node.sortKey;
|
|
220
|
+
node.hasContent = true;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
for (const node of nodes.values()) {
|
|
225
|
+
const {segments} = node;
|
|
226
|
+
if (!segments.length) continue;
|
|
227
|
+
const parentSlug = segments.slice(0, -1).join("/");
|
|
228
|
+
if (!parentSlug) continue;
|
|
229
|
+
const parent = nodes.get(parentSlug);
|
|
230
|
+
if (!parent) continue;
|
|
231
|
+
if (!parent.children.some((child) => child.slug === node.slug)) {
|
|
232
|
+
parent.children.push(node);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const sortChildren = (node) => {
|
|
237
|
+
node.children.sort((a, b) => a.sortKey.localeCompare(b.sortKey));
|
|
238
|
+
for (const child of node.children) sortChildren(child);
|
|
239
|
+
};
|
|
240
|
+
for (const node of nodes.values()) {
|
|
241
|
+
sortChildren(node);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const roots = new Map();
|
|
245
|
+
for (const node of nodes.values()) {
|
|
246
|
+
// A section root is either a top-level node (<section>) or, for
|
|
247
|
+
// locale-prefixed content, a <locale>/<section> node. Rooting a
|
|
248
|
+
// locale-prefixed sidebar at the locale segment alone would list every
|
|
249
|
+
// section under that locale instead of just the current one.
|
|
250
|
+
if (rootDepthForSegments(node.segments) === node.segments.length) {
|
|
251
|
+
roots.set(node.slug, node);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
NAV_CACHE = {
|
|
256
|
+
pages,
|
|
257
|
+
pagesByRelative,
|
|
258
|
+
nodes,
|
|
259
|
+
roots,
|
|
260
|
+
};
|
|
261
|
+
return NAV_CACHE;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function cloneNode(node, currentSlug) {
|
|
265
|
+
if (!node) return null;
|
|
266
|
+
const slug = node.slug;
|
|
267
|
+
const isActive = currentSlug && slug === currentSlug;
|
|
268
|
+
const isAncestor = !!(
|
|
269
|
+
currentSlug &&
|
|
270
|
+
slug &&
|
|
271
|
+
slug.length < currentSlug.length &&
|
|
272
|
+
currentSlug.startsWith(slug + "/")
|
|
273
|
+
);
|
|
274
|
+
const children = node.children
|
|
275
|
+
.map((child) => cloneNode(child, currentSlug))
|
|
276
|
+
.filter(Boolean);
|
|
277
|
+
if (!node.hasContent && !children.length) {
|
|
278
|
+
return null;
|
|
279
|
+
}
|
|
280
|
+
return {
|
|
281
|
+
slug,
|
|
282
|
+
title: node.title,
|
|
283
|
+
href: node.href,
|
|
284
|
+
segments: node.segments.slice(),
|
|
285
|
+
depth: Math.max(0, node.segments.length - 1),
|
|
286
|
+
isActive: !!isActive,
|
|
287
|
+
isAncestor,
|
|
288
|
+
isExpanded: !!(isActive || isAncestor),
|
|
289
|
+
hasContent: node.hasContent,
|
|
290
|
+
relativePath: node.relativePath,
|
|
291
|
+
children,
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function getPageInfo(relativePath) {
|
|
296
|
+
const cache = getNavigationCache();
|
|
297
|
+
const normalized = normalizeRelativePath(relativePath);
|
|
298
|
+
const page = cache.pagesByRelative.get(normalized);
|
|
299
|
+
if (page) {
|
|
300
|
+
return {
|
|
301
|
+
title: page.title,
|
|
302
|
+
href: page.href,
|
|
303
|
+
slug: page.slug,
|
|
304
|
+
segments: page.segments.slice(),
|
|
305
|
+
relativePath: page.relativePath,
|
|
306
|
+
rootSegment: rootSegmentForSegments(page.segments),
|
|
307
|
+
isIndex: page.isIndex,
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
const {slug, segments} = slugFromRelative(normalized);
|
|
311
|
+
const htmlRel = normalized.replace(/\.mdx$/i, ".html");
|
|
312
|
+
return {
|
|
313
|
+
title: humanizeSegment(segments.slice(-1)[0] || slug || ""),
|
|
314
|
+
href: rootRelativeHref(htmlRel),
|
|
315
|
+
slug,
|
|
316
|
+
segments,
|
|
317
|
+
relativePath: normalized,
|
|
318
|
+
rootSegment: rootSegmentForSegments(segments),
|
|
319
|
+
isIndex: false,
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
function buildNavigationForFile(relativePath) {
|
|
324
|
+
const cache = getNavigationCache();
|
|
325
|
+
const normalized = normalizeRelativePath(relativePath);
|
|
326
|
+
const page = cache.pagesByRelative.get(normalized);
|
|
327
|
+
const fallback = slugFromRelative(normalized);
|
|
328
|
+
const slug = page ? page.slug : fallback.slug;
|
|
329
|
+
const segments = page ? page.segments : fallback.segments;
|
|
330
|
+
const rootSegment = rootSegmentForSegments(segments);
|
|
331
|
+
// The section name is the last segment of the root (the segment after an
|
|
332
|
+
// optional locale prefix), e.g. "works" for both "works" and "fr/works".
|
|
333
|
+
const sectionName = rootSegment.split("/").pop() || "";
|
|
334
|
+
if (!slug || !rootSegment || EXCLUDED_ROOTS.has(sectionName)) {
|
|
335
|
+
return null;
|
|
336
|
+
}
|
|
337
|
+
const rootNode = cache.roots.get(rootSegment);
|
|
338
|
+
if (!rootNode) return null;
|
|
339
|
+
const cloned = cloneNode(rootNode, slug);
|
|
340
|
+
if (!cloned) return null;
|
|
341
|
+
return {
|
|
342
|
+
rootSegment,
|
|
343
|
+
currentSlug: slug,
|
|
344
|
+
root: cloned,
|
|
345
|
+
title: cloned.title,
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
function buildNavigationRoots(currentSlug) {
|
|
350
|
+
const cache = getNavigationCache();
|
|
351
|
+
const result = {};
|
|
352
|
+
const normalizedSlug = typeof currentSlug === "string" ? currentSlug : "";
|
|
353
|
+
for (const [segment, rootNode] of cache.roots.entries()) {
|
|
354
|
+
const sectionName = segment.split("/").pop() || "";
|
|
355
|
+
if (!segment || EXCLUDED_ROOTS.has(sectionName)) continue;
|
|
356
|
+
const shouldExpand =
|
|
357
|
+
normalizedSlug &&
|
|
358
|
+
(normalizedSlug === segment || normalizedSlug.startsWith(segment + "/"));
|
|
359
|
+
const clone = cloneNode(rootNode, shouldExpand ? normalizedSlug : null);
|
|
360
|
+
if (!clone) continue;
|
|
361
|
+
result[segment] = {
|
|
362
|
+
rootSegment: segment,
|
|
363
|
+
root: clone,
|
|
364
|
+
title: clone.title,
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
return result;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
function resetNavigationCache() {
|
|
371
|
+
NAV_CACHE = null;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
module.exports = {
|
|
375
|
+
normalizeRelativePath,
|
|
376
|
+
getPageInfo,
|
|
377
|
+
buildNavigationForFile,
|
|
378
|
+
buildNavigationRoots,
|
|
379
|
+
resetNavigationCache,
|
|
380
|
+
getPageContext,
|
|
381
|
+
};
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const {
|
|
4
|
+
CONTENT_DIR,
|
|
5
|
+
rootRelativeHref,
|
|
6
|
+
buildRouteRelativePath,
|
|
7
|
+
getLocaleRoute,
|
|
8
|
+
} = require('../common');
|
|
9
|
+
const mdx = require('../build/mdx.js');
|
|
10
|
+
const {
|
|
11
|
+
firstLabelString,
|
|
12
|
+
normalizeIiifId,
|
|
13
|
+
equalIiifId,
|
|
14
|
+
readJson,
|
|
15
|
+
findSlugByIdFromDiskSync,
|
|
16
|
+
} = require('./featured');
|
|
17
|
+
|
|
18
|
+
const IIIF_INDEX_PATH = path.resolve('.cache/iiif/index.json');
|
|
19
|
+
const IIIF_MANIFESTS_DIR = path.resolve('.cache/iiif/manifests');
|
|
20
|
+
let manifestReferenceIndex = null;
|
|
21
|
+
let referenceIndexBuilt = false;
|
|
22
|
+
|
|
23
|
+
function firstTextValue(value) {
|
|
24
|
+
if (value == null) return '';
|
|
25
|
+
if (typeof value === 'string') return value.trim();
|
|
26
|
+
if (Array.isArray(value)) {
|
|
27
|
+
for (const entry of value) {
|
|
28
|
+
const text = firstTextValue(entry);
|
|
29
|
+
if (text) return text;
|
|
30
|
+
}
|
|
31
|
+
return '';
|
|
32
|
+
}
|
|
33
|
+
if (typeof value === 'object') {
|
|
34
|
+
const keys = Object.keys(value);
|
|
35
|
+
for (const key of keys) {
|
|
36
|
+
const entry = value[key];
|
|
37
|
+
if (Array.isArray(entry)) {
|
|
38
|
+
for (const child of entry) {
|
|
39
|
+
const text = firstTextValue(child);
|
|
40
|
+
if (text) return text;
|
|
41
|
+
}
|
|
42
|
+
} else {
|
|
43
|
+
const text = firstTextValue(entry);
|
|
44
|
+
if (text) return text;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return '';
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function normalizeReferencedManifestList(raw) {
|
|
52
|
+
const values = Array.isArray(raw) ? raw : raw ? [raw] : [];
|
|
53
|
+
const seen = new Set();
|
|
54
|
+
const out = [];
|
|
55
|
+
for (const entry of values) {
|
|
56
|
+
if (entry === undefined || entry === null) continue;
|
|
57
|
+
const normalized = normalizeIiifId(entry);
|
|
58
|
+
const key = normalized || String(entry).trim();
|
|
59
|
+
if (!key || seen.has(key)) continue;
|
|
60
|
+
seen.add(key);
|
|
61
|
+
out.push(key);
|
|
62
|
+
}
|
|
63
|
+
return out;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function readManifestBySlug(slug) {
|
|
67
|
+
if (!slug) return null;
|
|
68
|
+
const filename = `${slug}.json`;
|
|
69
|
+
const filePath = path.join(IIIF_MANIFESTS_DIR, filename);
|
|
70
|
+
return readJson(filePath);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function assignThumbnailFields(target, entry, manifest) {
|
|
74
|
+
if (!target) return target;
|
|
75
|
+
const heroThumb = entry && entry.heroThumbnail ? String(entry.heroThumbnail) : '';
|
|
76
|
+
const fallbackThumb = entry && entry.thumbnail ? String(entry.thumbnail) : '';
|
|
77
|
+
if (heroThumb) {
|
|
78
|
+
target.thumbnail = heroThumb;
|
|
79
|
+
if (typeof entry.heroThumbnailWidth === 'number') target.thumbnailWidth = entry.heroThumbnailWidth;
|
|
80
|
+
if (typeof entry.heroThumbnailHeight === 'number') target.thumbnailHeight = entry.heroThumbnailHeight;
|
|
81
|
+
} else if (fallbackThumb) {
|
|
82
|
+
target.thumbnail = fallbackThumb;
|
|
83
|
+
if (typeof entry.thumbnailWidth === 'number') target.thumbnailWidth = entry.thumbnailWidth;
|
|
84
|
+
if (typeof entry.thumbnailHeight === 'number') target.thumbnailHeight = entry.thumbnailHeight;
|
|
85
|
+
}
|
|
86
|
+
if (!target.thumbnail && manifest && manifest.thumbnail) {
|
|
87
|
+
const thumb = manifest.thumbnail;
|
|
88
|
+
if (Array.isArray(thumb) && thumb.length) {
|
|
89
|
+
const first = thumb[0] || {};
|
|
90
|
+
const id = first.id || first['@id'] || first.url || '';
|
|
91
|
+
if (id) target.thumbnail = String(id);
|
|
92
|
+
if (typeof first.width === 'number') target.thumbnailWidth = first.width;
|
|
93
|
+
if (typeof first.height === 'number') target.thumbnailHeight = first.height;
|
|
94
|
+
} else if (thumb && typeof thumb === 'object') {
|
|
95
|
+
const id = thumb.id || thumb['@id'] || thumb.url || '';
|
|
96
|
+
if (id) target.thumbnail = String(id);
|
|
97
|
+
if (typeof thumb.width === 'number') target.thumbnailWidth = thumb.width;
|
|
98
|
+
if (typeof thumb.height === 'number') target.thumbnailHeight = thumb.height;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return target;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function buildReferencedItems(referencedIds, options = {}) {
|
|
105
|
+
const ids = normalizeReferencedManifestList(referencedIds);
|
|
106
|
+
if (!ids.length) return [];
|
|
107
|
+
const index = readJson(IIIF_INDEX_PATH) || {};
|
|
108
|
+
const byId = Array.isArray(index.byId) ? index.byId : [];
|
|
109
|
+
const items = [];
|
|
110
|
+
const locale = options && options.locale ? options.locale : null;
|
|
111
|
+
const routeBase = getLocaleRoute(locale, 'works');
|
|
112
|
+
for (const id of ids) {
|
|
113
|
+
const entry = byId.find(
|
|
114
|
+
(candidate) => candidate && candidate.type === 'Manifest' && equalIiifId(candidate.id, id)
|
|
115
|
+
);
|
|
116
|
+
const slug = entry && entry.slug ? entry.slug : findSlugByIdFromDiskSync(id);
|
|
117
|
+
if (!slug) continue;
|
|
118
|
+
const manifest = readManifestBySlug(slug);
|
|
119
|
+
if (!manifest) continue;
|
|
120
|
+
const relPath = buildRouteRelativePath(routeBase, `${slug}.html`);
|
|
121
|
+
const href = rootRelativeHref(relPath.split(path.sep).join('/'));
|
|
122
|
+
const title = firstLabelString(manifest.label);
|
|
123
|
+
const summary = firstTextValue(manifest.summary);
|
|
124
|
+
const item = { id, slug, href, title };
|
|
125
|
+
if (summary) item.summary = summary;
|
|
126
|
+
assignThumbnailFields(item, entry, manifest);
|
|
127
|
+
items.push(item);
|
|
128
|
+
}
|
|
129
|
+
return items;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function isReservedContentFile(p) {
|
|
133
|
+
return mdx.isReservedFile ? mdx.isReservedFile(p) : path.basename(p).startsWith('_');
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function resolveHrefFromContentPath(filePath) {
|
|
137
|
+
const rel = path.relative(CONTENT_DIR, filePath).replace(/\\/g, '/');
|
|
138
|
+
const htmlRel = rel.replace(/\.mdx$/i, '.html');
|
|
139
|
+
return rootRelativeHref(htmlRel);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function buildReferenceIndexSync() {
|
|
143
|
+
const map = new Map();
|
|
144
|
+
function record(filePath) {
|
|
145
|
+
let raw = '';
|
|
146
|
+
try {
|
|
147
|
+
raw = fs.readFileSync(filePath, 'utf8');
|
|
148
|
+
} catch (_) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
const frontmatter = mdx.parseFrontmatter ? mdx.parseFrontmatter(raw) : { data: null };
|
|
152
|
+
const data = frontmatter && frontmatter.data && typeof frontmatter.data === 'object' ? frontmatter.data : null;
|
|
153
|
+
const ids = normalizeReferencedManifestList(data && data.referencedManifests);
|
|
154
|
+
if (!ids.length) return;
|
|
155
|
+
const title = (data && typeof data.title === 'string' && data.title.trim())
|
|
156
|
+
? data.title.trim()
|
|
157
|
+
: mdx.extractTitle ? mdx.extractTitle(raw) : path.basename(filePath, path.extname(filePath));
|
|
158
|
+
const href = resolveHrefFromContentPath(filePath);
|
|
159
|
+
const entry = { title, href };
|
|
160
|
+
for (const id of ids) {
|
|
161
|
+
const normalized = normalizeIiifId(id);
|
|
162
|
+
if (!normalized) continue;
|
|
163
|
+
const list = map.get(normalized) || [];
|
|
164
|
+
if (!list.some((item) => item.href === entry.href)) list.push(entry);
|
|
165
|
+
map.set(normalized, list);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
function walk(dir) {
|
|
169
|
+
let entries = [];
|
|
170
|
+
try {
|
|
171
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
172
|
+
} catch (_) {
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
for (const entry of entries) {
|
|
176
|
+
const filePath = path.join(dir, entry.name);
|
|
177
|
+
if (entry.isDirectory()) {
|
|
178
|
+
walk(filePath);
|
|
179
|
+
} else if (entry.isFile() && /\.mdx$/i.test(entry.name) && !isReservedContentFile(filePath)) {
|
|
180
|
+
record(filePath);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
walk(CONTENT_DIR);
|
|
185
|
+
for (const [key, list] of map.entries()) {
|
|
186
|
+
list.sort((a, b) => (a.title || '').localeCompare(b.title || ''));
|
|
187
|
+
}
|
|
188
|
+
manifestReferenceIndex = map;
|
|
189
|
+
referenceIndexBuilt = true;
|
|
190
|
+
return manifestReferenceIndex;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function ensureReferenceIndex() {
|
|
194
|
+
if (referenceIndexBuilt && manifestReferenceIndex) return manifestReferenceIndex;
|
|
195
|
+
return buildReferenceIndexSync();
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function resetReferenceIndex() {
|
|
199
|
+
referenceIndexBuilt = false;
|
|
200
|
+
manifestReferenceIndex = null;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function getReferencesForManifest(manifestId) {
|
|
204
|
+
const normalized = normalizeIiifId(manifestId);
|
|
205
|
+
if (!normalized) return [];
|
|
206
|
+
const index = ensureReferenceIndex();
|
|
207
|
+
const list = index.get(normalized) || [];
|
|
208
|
+
return list.map((entry) => ({ ...entry }));
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
module.exports = {
|
|
212
|
+
normalizeReferencedManifestList,
|
|
213
|
+
buildReferencedItems,
|
|
214
|
+
ensureReferenceIndex,
|
|
215
|
+
resetReferenceIndex,
|
|
216
|
+
getReferencesForManifest,
|
|
217
|
+
};
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { createRoot } from 'react-dom/client';
|
|
3
|
+
import {
|
|
4
|
+
cloverSlider,
|
|
5
|
+
mergeSliderOptions,
|
|
6
|
+
normalizeSliderOptions,
|
|
7
|
+
} from '../../ui/dist/index.mjs';
|
|
8
|
+
|
|
9
|
+
function ready(fn) {
|
|
10
|
+
if (document.readyState === 'loading') {
|
|
11
|
+
document.addEventListener('DOMContentLoaded', fn, { once: true });
|
|
12
|
+
} else {
|
|
13
|
+
fn();
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function parseProps(el) {
|
|
18
|
+
try {
|
|
19
|
+
const script = el.querySelector('script[type="application/json"]');
|
|
20
|
+
if (script) return JSON.parse(script.textContent || '{}');
|
|
21
|
+
const raw = el.getAttribute('data-props') || '{}';
|
|
22
|
+
return JSON.parse(raw);
|
|
23
|
+
} catch (_) {
|
|
24
|
+
return {};
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function withDefaults(rawProps) {
|
|
29
|
+
const props = rawProps && typeof rawProps === 'object' ? rawProps : {};
|
|
30
|
+
const className = ['canopy-slider', props.className]
|
|
31
|
+
.filter(Boolean)
|
|
32
|
+
.join(' ');
|
|
33
|
+
const mergedOptions = mergeSliderOptions(props.options);
|
|
34
|
+
return {
|
|
35
|
+
...props,
|
|
36
|
+
className,
|
|
37
|
+
options: normalizeSliderOptions(mergedOptions),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
let cloverSliderPromise = null;
|
|
42
|
+
|
|
43
|
+
function loadCloverSlider() {
|
|
44
|
+
if (!cloverSliderPromise) {
|
|
45
|
+
// Clover's Items, from its own entry: the slider's part and nothing else up front.
|
|
46
|
+
cloverSliderPromise = import('@nulib/clover/items')
|
|
47
|
+
.then(cloverSlider)
|
|
48
|
+
.catch((error) => {
|
|
49
|
+
console.error('[canopy] Clover failed to load', error);
|
|
50
|
+
return null;
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
return cloverSliderPromise;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function mount(el) {
|
|
57
|
+
try {
|
|
58
|
+
if (!el || el.getAttribute('data-canopy-slider-mounted') === '1') return;
|
|
59
|
+
const props = withDefaults(parseProps(el));
|
|
60
|
+
const root = createRoot(el);
|
|
61
|
+
loadCloverSlider().then((Component) => {
|
|
62
|
+
try {
|
|
63
|
+
if (!Component) return;
|
|
64
|
+
root.render(React.createElement(Component, props));
|
|
65
|
+
el.setAttribute('data-canopy-slider-mounted', '1');
|
|
66
|
+
} catch (_) {}
|
|
67
|
+
});
|
|
68
|
+
} catch (_) {}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function scan() {
|
|
72
|
+
try {
|
|
73
|
+
document
|
|
74
|
+
.querySelectorAll('[data-canopy-slider]:not([data-canopy-slider-mounted="1"])')
|
|
75
|
+
.forEach(mount);
|
|
76
|
+
} catch (_) {}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function observe() {
|
|
80
|
+
try {
|
|
81
|
+
const obs = new MutationObserver((mutations) => {
|
|
82
|
+
const toMount = [];
|
|
83
|
+
mutations.forEach((mutation) => {
|
|
84
|
+
mutation.addedNodes &&
|
|
85
|
+
mutation.addedNodes.forEach((node) => {
|
|
86
|
+
if (!(node instanceof Element)) return;
|
|
87
|
+
if (node.matches && node.matches('[data-canopy-slider]')) toMount.push(node);
|
|
88
|
+
const inner = node.querySelectorAll
|
|
89
|
+
? node.querySelectorAll('[data-canopy-slider]')
|
|
90
|
+
: [];
|
|
91
|
+
inner && inner.forEach && inner.forEach((x) => toMount.push(x));
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
if (toMount.length) Promise.resolve().then(() => toMount.forEach(mount));
|
|
95
|
+
});
|
|
96
|
+
obs.observe(document.documentElement || document.body, {
|
|
97
|
+
childList: true,
|
|
98
|
+
subtree: true,
|
|
99
|
+
});
|
|
100
|
+
} catch (_) {}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
ready(function onReady() {
|
|
104
|
+
scan();
|
|
105
|
+
observe();
|
|
106
|
+
});
|