@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,231 @@
|
|
|
1
|
+
const { spawn } = require('child_process');
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
|
|
5
|
+
const { PACKAGE_NAME } = require('./build/package-names');
|
|
6
|
+
|
|
7
|
+
const log = (msg) => console.log(`[canopy] ${msg}`);
|
|
8
|
+
const warn = (msg) => console.warn(`[canopy][warn] ${msg}`);
|
|
9
|
+
const err = (msg) => console.error(`[canopy][error] ${msg}`);
|
|
10
|
+
|
|
11
|
+
let uiWatcherChild = null;
|
|
12
|
+
|
|
13
|
+
const workspacePackageJsonPath = path.resolve(process.cwd(), 'packages/app/package.json');
|
|
14
|
+
const hasAppWorkspace = fs.existsSync(workspacePackageJsonPath);
|
|
15
|
+
const workspaceUiDistDir = path.resolve(process.cwd(), 'packages/app/ui/dist');
|
|
16
|
+
const requiredUiArtifacts = [
|
|
17
|
+
path.join(workspaceUiDistDir, 'server.mjs'),
|
|
18
|
+
path.join(workspaceUiDistDir, 'index.mjs'),
|
|
19
|
+
];
|
|
20
|
+
|
|
21
|
+
function prettyPath(p) {
|
|
22
|
+
try {
|
|
23
|
+
const rel = path.relative(process.cwd(), p);
|
|
24
|
+
return rel ? rel.split(path.sep).join('/') : p;
|
|
25
|
+
} catch (_) {
|
|
26
|
+
return p;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function ensureUiDistReady(context = 'build') {
|
|
31
|
+
if (!hasAppWorkspace) return;
|
|
32
|
+
const missing = [];
|
|
33
|
+
for (const file of requiredUiArtifacts) {
|
|
34
|
+
try {
|
|
35
|
+
if (!fs.existsSync(file)) missing.push(file);
|
|
36
|
+
} catch (_) {
|
|
37
|
+
missing.push(file);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (missing.length) {
|
|
41
|
+
const list = missing.map((file) => prettyPath(file)).join(', ');
|
|
42
|
+
const label = context ? `${context} ` : '';
|
|
43
|
+
throw new Error(
|
|
44
|
+
`${PACKAGE_NAME}/ui ${label}did not produce required bundle(s): ${list}. ` +
|
|
45
|
+
`Run "npm -w ${PACKAGE_NAME} run ui:build" and ensure it completes successfully.`
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function getMode(argv = process.argv.slice(2), env = process.env) {
|
|
51
|
+
const cli = new Set(argv);
|
|
52
|
+
if (cli.has('--dev')) return 'dev';
|
|
53
|
+
if (cli.has('--build')) return 'build';
|
|
54
|
+
|
|
55
|
+
if (env.CANOPY_MODE === 'dev') return 'dev';
|
|
56
|
+
if (env.CANOPY_MODE === 'build') return 'build';
|
|
57
|
+
|
|
58
|
+
const npmScript = env.npm_lifecycle_event;
|
|
59
|
+
if (npmScript === 'dev') return 'dev';
|
|
60
|
+
if (npmScript === 'build') return 'build';
|
|
61
|
+
|
|
62
|
+
return 'build';
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function runOnce(cmd, args, opts = {}) {
|
|
66
|
+
return new Promise((resolve, reject) => {
|
|
67
|
+
const child = spawn(cmd, args, { stdio: 'inherit', shell: false, ...opts });
|
|
68
|
+
child.on('error', reject);
|
|
69
|
+
child.on('exit', (code) => {
|
|
70
|
+
if (code === 0) {
|
|
71
|
+
resolve();
|
|
72
|
+
} else {
|
|
73
|
+
reject(new Error(`${cmd} ${args.join(' ')} exited with code ${code}`));
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function start(cmd, args, opts = {}) {
|
|
80
|
+
const child = spawn(cmd, args, { stdio: 'inherit', shell: false, ...opts });
|
|
81
|
+
child.on('error', (error) => {
|
|
82
|
+
const message = error && error.message ? error.message : String(error);
|
|
83
|
+
warn(`Subprocess error (${cmd}): ${message}`);
|
|
84
|
+
});
|
|
85
|
+
return child;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async function buildUiOnce(label, env = process.env) {
|
|
89
|
+
if (!hasAppWorkspace) return;
|
|
90
|
+
const prefix = label || 'Building';
|
|
91
|
+
log(`${prefix} UI assets (${PACKAGE_NAME}/ui)`);
|
|
92
|
+
try {
|
|
93
|
+
await runOnce('npm', ['-w', PACKAGE_NAME, 'run', 'ui:build'], { env });
|
|
94
|
+
} catch (error) {
|
|
95
|
+
const message = (error && error.message) || String(error);
|
|
96
|
+
throw new Error(`[canopy] Failed to ${prefix.toLowerCase()} UI assets: ${message}`);
|
|
97
|
+
}
|
|
98
|
+
ensureUiDistReady(prefix.toLowerCase());
|
|
99
|
+
log('UI assets ready');
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async function prepareUi(mode, env = process.env) {
|
|
103
|
+
if (!hasAppWorkspace) {
|
|
104
|
+
log(`Using bundled UI assets from ${PACKAGE_NAME} (workspace not detected)`);
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (mode === 'build') {
|
|
109
|
+
await buildUiOnce('Building', env);
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
await buildUiOnce('Prebuilding', env);
|
|
114
|
+
|
|
115
|
+
log(`Starting UI watcher (${PACKAGE_NAME}/ui)`);
|
|
116
|
+
try {
|
|
117
|
+
uiWatcherChild = start('npm', ['-w', PACKAGE_NAME, 'run', 'ui:watch'], { env });
|
|
118
|
+
} catch (error) {
|
|
119
|
+
warn(`UI watch skipped: ${(error && error.message) || String(error)}`);
|
|
120
|
+
uiWatcherChild = null;
|
|
121
|
+
}
|
|
122
|
+
return uiWatcherChild;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function loadLibraryApi() {
|
|
126
|
+
let lib;
|
|
127
|
+
try {
|
|
128
|
+
lib = require('./index.js');
|
|
129
|
+
} catch (e) {
|
|
130
|
+
const hint = [
|
|
131
|
+
`Unable to load ${PACKAGE_NAME}.`,
|
|
132
|
+
'Ensure dependencies are installed (npm install)',
|
|
133
|
+
"and that peer deps like 'react' are present.",
|
|
134
|
+
].join(' ');
|
|
135
|
+
const detail = e && e.message ? `\nCaused by: ${e.message}` : '';
|
|
136
|
+
throw new Error(`${hint}${detail}`);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const api = lib && (typeof lib.build === 'function' || typeof lib.dev === 'function')
|
|
140
|
+
? lib
|
|
141
|
+
: lib && lib.default
|
|
142
|
+
? lib.default
|
|
143
|
+
: lib;
|
|
144
|
+
|
|
145
|
+
if (!api || (typeof api.build !== 'function' && typeof api.dev !== 'function')) {
|
|
146
|
+
throw new TypeError(`Invalid ${PACKAGE_NAME} export: expected functions build() and/or dev().`);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return api;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function attachSignalHandlers() {
|
|
153
|
+
const clean = () => {
|
|
154
|
+
if (uiWatcherChild && !uiWatcherChild.killed) {
|
|
155
|
+
try { uiWatcherChild.kill(); } catch (_) {}
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
process.on('SIGINT', () => {
|
|
160
|
+
clean();
|
|
161
|
+
process.exit(130);
|
|
162
|
+
});
|
|
163
|
+
process.on('SIGTERM', () => {
|
|
164
|
+
clean();
|
|
165
|
+
process.exit(143);
|
|
166
|
+
});
|
|
167
|
+
process.on('exit', clean);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
async function orchestrate(options = {}) {
|
|
171
|
+
const argv = options.argv || process.argv.slice(2);
|
|
172
|
+
const env = options.env || process.env;
|
|
173
|
+
if (
|
|
174
|
+
argv.includes('--debug-iiif') ||
|
|
175
|
+
argv.includes('--iiif-debug') ||
|
|
176
|
+
argv.includes('--debug')
|
|
177
|
+
) {
|
|
178
|
+
if (!env.CANOPY_IIIF_DEBUG) env.CANOPY_IIIF_DEBUG = '1';
|
|
179
|
+
if (!process.env.CANOPY_IIIF_DEBUG) process.env.CANOPY_IIIF_DEBUG = '1';
|
|
180
|
+
log('IIIF debug logging enabled');
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
process.title = 'canopy-app';
|
|
184
|
+
const mode = getMode(argv, env);
|
|
185
|
+
log(`Mode: ${mode}`);
|
|
186
|
+
|
|
187
|
+
await prepareUi(mode, env);
|
|
188
|
+
|
|
189
|
+
const api = loadLibraryApi();
|
|
190
|
+
let exitAfterBuild = false;
|
|
191
|
+
try {
|
|
192
|
+
if (mode === 'dev') {
|
|
193
|
+
attachSignalHandlers();
|
|
194
|
+
log('Starting dev server...');
|
|
195
|
+
await (typeof api.dev === 'function' ? api.dev() : Promise.resolve());
|
|
196
|
+
} else {
|
|
197
|
+
log('Building site...');
|
|
198
|
+
if (typeof api.build === 'function') {
|
|
199
|
+
await api.build();
|
|
200
|
+
}
|
|
201
|
+
log('Build complete');
|
|
202
|
+
exitAfterBuild = true;
|
|
203
|
+
}
|
|
204
|
+
} finally {
|
|
205
|
+
if (uiWatcherChild && !uiWatcherChild.killed) {
|
|
206
|
+
try { uiWatcherChild.kill(); } catch (_) {}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (exitAfterBuild) {
|
|
211
|
+
try {
|
|
212
|
+
if (!process.exitCode || process.exitCode === 0) process.exit(0);
|
|
213
|
+
} catch (_) {
|
|
214
|
+
process.exit(0);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
module.exports = {
|
|
220
|
+
orchestrate,
|
|
221
|
+
_internals: {
|
|
222
|
+
getMode,
|
|
223
|
+
prepareUi,
|
|
224
|
+
loadLibraryApi,
|
|
225
|
+
runOnce,
|
|
226
|
+
start,
|
|
227
|
+
},
|
|
228
|
+
log,
|
|
229
|
+
warn,
|
|
230
|
+
err,
|
|
231
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const React = require('react');
|
|
2
|
+
|
|
3
|
+
const CONTEXT_KEY =
|
|
4
|
+
typeof Symbol === 'function' ? Symbol.for('__CANOPY_PAGE_CONTEXT__') : '__CANOPY_PAGE_CONTEXT__';
|
|
5
|
+
|
|
6
|
+
function getGlobalRoot() {
|
|
7
|
+
if (typeof globalThis !== 'undefined') return globalThis;
|
|
8
|
+
if (typeof global !== 'undefined') return global;
|
|
9
|
+
if (typeof window !== 'undefined') return window;
|
|
10
|
+
return {};
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function getPageContext() {
|
|
14
|
+
const root = getGlobalRoot();
|
|
15
|
+
if (root[CONTEXT_KEY]) return root[CONTEXT_KEY];
|
|
16
|
+
const ctx = React.createContext({ navigation: null, page: null, site: null });
|
|
17
|
+
root[CONTEXT_KEY] = ctx;
|
|
18
|
+
return ctx;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
module.exports = {
|
|
22
|
+
getPageContext,
|
|
23
|
+
CONTEXT_KEY,
|
|
24
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// How the search runtimes are bundled: FlexSearch's compact build in place of the
|
|
2
|
+
// package's default, its full bundle. The compact build has the Index, the Encoder, and
|
|
3
|
+
// export and import for the IndexedDB cache, at 10.7 KB gzipped against 17.6; the light
|
|
4
|
+
// build (4.5 KB) has no export. FlexSearch's `exports` map does not name its other
|
|
5
|
+
// builds, so this resolves the file beside its main one, which `~0.8.212` keeps in place.
|
|
6
|
+
|
|
7
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
const path = require('path');
|
|
10
|
+
|
|
11
|
+
function resolveFlexSearchCompact() {
|
|
12
|
+
const main = require.resolve('flexsearch');
|
|
13
|
+
return path.join(path.dirname(main), 'flexsearch.compact.module.min.js');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function flexSearchPlugin() {
|
|
17
|
+
return {
|
|
18
|
+
name: 'canopy-flexsearch-compact',
|
|
19
|
+
setup(build) {
|
|
20
|
+
build.onResolve({ filter: /^flexsearch$/ }, () => ({ path: resolveFlexSearchCompact() }));
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
module.exports = { flexSearchPlugin, resolveFlexSearchCompact };
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
// What both search runtimes read from a site's `api/` folder, fetched and joined into
|
|
2
|
+
// one record per page or work. In the browser only. `search.js` writes the files:
|
|
3
|
+
// index.json the index's version and the search settings
|
|
4
|
+
// search-index.json what each record is found by (title, metadata, summary)
|
|
5
|
+
// search-records.json what each shows (href, thumbnail, routes by locale)
|
|
6
|
+
// search-index-annotations.json annotation text, when `search.index.annotations` is on
|
|
7
|
+
// The three are joined by each record's `id`.
|
|
8
|
+
|
|
9
|
+
'use strict';
|
|
10
|
+
|
|
11
|
+
function readBasePath() {
|
|
12
|
+
const normalize = (value) => {
|
|
13
|
+
const raw = typeof value === 'string' ? value.trim() : '';
|
|
14
|
+
if (!raw) return '';
|
|
15
|
+
const withLead = raw.startsWith('/') ? raw : `/${raw}`;
|
|
16
|
+
return withLead.replace(/\/+$/, '');
|
|
17
|
+
};
|
|
18
|
+
try {
|
|
19
|
+
if (typeof window !== 'undefined' && window.CANOPY_BASE_PATH != null) {
|
|
20
|
+
const fromWindow = normalize(window.CANOPY_BASE_PATH);
|
|
21
|
+
if (fromWindow) return fromWindow;
|
|
22
|
+
}
|
|
23
|
+
} catch (_) {}
|
|
24
|
+
try {
|
|
25
|
+
if (typeof globalThis !== 'undefined' && globalThis.CANOPY_BASE_PATH != null) {
|
|
26
|
+
const fromGlobal = normalize(globalThis.CANOPY_BASE_PATH);
|
|
27
|
+
if (fromGlobal) return fromGlobal;
|
|
28
|
+
}
|
|
29
|
+
} catch (_) {}
|
|
30
|
+
return '';
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// A root-relative href under the site's base path; any other href as it is.
|
|
34
|
+
function withBasePath(href) {
|
|
35
|
+
try {
|
|
36
|
+
const raw = typeof href === 'string' ? href.trim() : '';
|
|
37
|
+
if (!raw) return href;
|
|
38
|
+
if (/^(?:[a-z][a-z0-9+.-]*:|\/\/|#)/i.test(raw)) return raw;
|
|
39
|
+
if (!raw.startsWith('/')) return raw;
|
|
40
|
+
const base = readBasePath();
|
|
41
|
+
if (!base || base === '/') return raw;
|
|
42
|
+
if (raw === base || raw.startsWith(`${base}/`)) return raw;
|
|
43
|
+
return `${base}${raw}`;
|
|
44
|
+
} catch (_) {
|
|
45
|
+
return href;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function apiUrl(pathname) {
|
|
50
|
+
const cleaned = typeof pathname === 'string' ? pathname.replace(/^\/+/, '') : '';
|
|
51
|
+
return `${withBasePath('/api')}/${cleaned}`;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function readDocumentLocale() {
|
|
55
|
+
try {
|
|
56
|
+
const lang = document.documentElement && document.documentElement.lang;
|
|
57
|
+
if (lang) return String(lang).trim();
|
|
58
|
+
} catch (_) {}
|
|
59
|
+
return '';
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// A page written in several languages has a route for each; take the page's own.
|
|
63
|
+
function resolveRecordHrefForLocale(record, fallbackHref) {
|
|
64
|
+
const routes = record && record.routes && typeof record.routes === 'object' ? record.routes : null;
|
|
65
|
+
if (!routes) return fallbackHref;
|
|
66
|
+
const docLocale = readDocumentLocale();
|
|
67
|
+
const candidates = [];
|
|
68
|
+
if (docLocale) candidates.push(docLocale);
|
|
69
|
+
if (docLocale && docLocale.includes('-')) {
|
|
70
|
+
const base = docLocale.split('-')[0];
|
|
71
|
+
if (base && base !== docLocale) candidates.push(base);
|
|
72
|
+
}
|
|
73
|
+
if (record && typeof record.locale === 'string') {
|
|
74
|
+
const localeCandidate = record.locale.trim();
|
|
75
|
+
if (localeCandidate && !candidates.includes(localeCandidate)) candidates.push(localeCandidate);
|
|
76
|
+
}
|
|
77
|
+
for (const candidate of candidates) {
|
|
78
|
+
if (!candidate) continue;
|
|
79
|
+
if (routes[candidate]) return routes[candidate];
|
|
80
|
+
const lower = candidate.toLowerCase();
|
|
81
|
+
if (lower !== candidate && routes[lower]) return routes[lower];
|
|
82
|
+
}
|
|
83
|
+
const first = Object.values(routes).find((value) => value);
|
|
84
|
+
return first || fallbackHref;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async function sha256Hex(str) {
|
|
88
|
+
try {
|
|
89
|
+
if (typeof crypto !== 'undefined' && crypto.subtle) {
|
|
90
|
+
const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(str));
|
|
91
|
+
return Array.from(new Uint8Array(digest))
|
|
92
|
+
.map((byte) => byte.toString(16).padStart(2, '0'))
|
|
93
|
+
.join('');
|
|
94
|
+
}
|
|
95
|
+
} catch (_) {}
|
|
96
|
+
let hash = 5381;
|
|
97
|
+
for (let i = 0; i < str.length; i += 1) hash = ((hash << 5) + hash) ^ str.charCodeAt(i);
|
|
98
|
+
return (hash >>> 0).toString(16);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function asRecords(json) {
|
|
102
|
+
if (Array.isArray(json)) return json;
|
|
103
|
+
if (json && Array.isArray(json.records)) return json.records;
|
|
104
|
+
return [];
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async function fetchJson(url) {
|
|
108
|
+
try {
|
|
109
|
+
const response = await fetch(url);
|
|
110
|
+
if (!response || !response.ok) return null;
|
|
111
|
+
return await response.json();
|
|
112
|
+
} catch (_) {
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async function fetchText(url) {
|
|
118
|
+
try {
|
|
119
|
+
const response = await fetch(url);
|
|
120
|
+
if (!response || !response.ok) return '';
|
|
121
|
+
return await response.text();
|
|
122
|
+
} catch (_) {
|
|
123
|
+
return '';
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Resolves to { meta, searchMeta, version, records, annotationCount }. Each record keeps
|
|
128
|
+
// its position as `__docIndex`, which the facets' document lists refer to.
|
|
129
|
+
async function loadSearchData() {
|
|
130
|
+
const meta = await fetchJson(apiUrl('index.json'));
|
|
131
|
+
let version = meta && typeof meta.version === 'string' ? meta.version : '';
|
|
132
|
+
const searchMeta = (meta && meta.search) || {};
|
|
133
|
+
const annotationsAsset = searchMeta.assets && searchMeta.assets.annotations;
|
|
134
|
+
const annotationsPath =
|
|
135
|
+
annotationsAsset && annotationsAsset.path ? String(annotationsAsset.path).replace(/^\/+/, '') : '';
|
|
136
|
+
const suffix = version ? `?v=${encodeURIComponent(version)}` : '';
|
|
137
|
+
const [indexText, displayJson, annotationsJson] = await Promise.all([
|
|
138
|
+
fetchText(`${apiUrl('search-index.json')}${suffix}`),
|
|
139
|
+
fetchJson(`${apiUrl('search-records.json')}${suffix}`),
|
|
140
|
+
annotationsPath ? fetchJson(`${apiUrl(annotationsPath)}${suffix}`) : Promise.resolve(null),
|
|
141
|
+
]);
|
|
142
|
+
let parsed = null;
|
|
143
|
+
try {
|
|
144
|
+
parsed = indexText ? JSON.parse(indexText) : null;
|
|
145
|
+
} catch (_) {
|
|
146
|
+
parsed = null;
|
|
147
|
+
}
|
|
148
|
+
const displayRecords = asRecords(displayJson);
|
|
149
|
+
let indexRecords = asRecords(parsed);
|
|
150
|
+
// Without a search index, the display records are all there is to find things by.
|
|
151
|
+
if (!indexRecords.length && displayRecords.length) indexRecords = displayRecords;
|
|
152
|
+
|
|
153
|
+
const displayMap = new Map();
|
|
154
|
+
displayRecords.forEach((record) => {
|
|
155
|
+
if (!record || typeof record !== 'object') return;
|
|
156
|
+
const key = record.id ? String(record.id) : record.href ? String(record.href) : '';
|
|
157
|
+
if (key) displayMap.set(key, record);
|
|
158
|
+
});
|
|
159
|
+
const annotationsMap = new Map();
|
|
160
|
+
asRecords(annotationsJson).forEach((record, position) => {
|
|
161
|
+
if (!record || typeof record !== 'object') return;
|
|
162
|
+
const key = record.id ? String(record.id) : String(position);
|
|
163
|
+
const text = record.annotation
|
|
164
|
+
? String(record.annotation)
|
|
165
|
+
: record.text
|
|
166
|
+
? String(record.text)
|
|
167
|
+
: '';
|
|
168
|
+
if (key && text) annotationsMap.set(key, text);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
const records = indexRecords.map((record, position) => {
|
|
172
|
+
const key = record && record.id ? String(record.id) : String(position);
|
|
173
|
+
const display = displayMap.get(key) || null;
|
|
174
|
+
const merged = { ...(display || {}), ...(record || {}), __docIndex: position };
|
|
175
|
+
if (!merged.id) merged.id = key;
|
|
176
|
+
const fallbackHref = merged.href || (display && display.href) || '';
|
|
177
|
+
const localeAwareHref = resolveRecordHrefForLocale(display || merged, fallbackHref);
|
|
178
|
+
if (localeAwareHref) merged.href = withBasePath(localeAwareHref);
|
|
179
|
+
else if (fallbackHref) merged.href = withBasePath(fallbackHref);
|
|
180
|
+
if (!merged.title) merged.title = merged.href || '';
|
|
181
|
+
// A page's image is a path from the site's root; a work's, a URL.
|
|
182
|
+
if (typeof merged.thumbnail === 'string' && merged.thumbnail.startsWith('/')) {
|
|
183
|
+
merged.thumbnail = withBasePath(merged.thumbnail);
|
|
184
|
+
}
|
|
185
|
+
if (!Array.isArray(merged.metadata)) {
|
|
186
|
+
merged.metadata = Array.isArray(record && record.metadata) ? record.metadata : [];
|
|
187
|
+
}
|
|
188
|
+
if (annotationsMap.has(key) && !merged.annotation) merged.annotation = annotationsMap.get(key);
|
|
189
|
+
return merged;
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
if (!version) version = (parsed && parsed.version) || (await sha256Hex(indexText || ''));
|
|
193
|
+
return { meta, searchMeta, version, records, annotationCount: annotationsMap.size };
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
module.exports = {
|
|
197
|
+
apiUrl,
|
|
198
|
+
loadSearchData,
|
|
199
|
+
readBasePath,
|
|
200
|
+
resolveRecordHrefForLocale,
|
|
201
|
+
withBasePath,
|
|
202
|
+
};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// The search engine both of Canopy's search runtimes use: the search page's
|
|
2
|
+
// (`search-app.jsx`) and the header's suggestions (`search-engine-runtime.js`, loaded by
|
|
3
|
+
// `search-form-runtime.js`). It is the one place that knows FlexSearch, so a change of
|
|
4
|
+
// engine, or of how words are matched, is a change to this file, and the relevance
|
|
5
|
+
// tests (`tests/unit/search/engine.test.js`) show what it changes.
|
|
6
|
+
//
|
|
7
|
+
// FlexSearch is passed in rather than required here: the browser bundles hand it the
|
|
8
|
+
// compact build (`bundle.js`), and Node and the tests the package's own.
|
|
9
|
+
|
|
10
|
+
'use strict';
|
|
11
|
+
|
|
12
|
+
// The cached index in a visitor's IndexedDB is keyed by this and the index's version
|
|
13
|
+
// (`index-cache.js`), so an index another engine, or these settings before a change,
|
|
14
|
+
// wrote is never read back. Change it whenever an option below changes.
|
|
15
|
+
const ENGINE_ID = 'flexsearch-0.8:forward:normalize';
|
|
16
|
+
|
|
17
|
+
// What a record is found by: its title first, as FlexSearch scores a match higher the
|
|
18
|
+
// earlier in the text it falls, then its metadata, summary and annotation.
|
|
19
|
+
function searchText(record) {
|
|
20
|
+
if (!record || typeof record !== 'object') return '';
|
|
21
|
+
const parts = [];
|
|
22
|
+
if (record.title) parts.push(String(record.title));
|
|
23
|
+
const metadata = Array.isArray(record.metadata) ? record.metadata : [];
|
|
24
|
+
for (const value of metadata) {
|
|
25
|
+
if (value) parts.push(String(value));
|
|
26
|
+
}
|
|
27
|
+
if (record.summary) parts.push(String(record.summary));
|
|
28
|
+
if (record.annotation) parts.push(String(record.annotation));
|
|
29
|
+
return parts.join(' ').trim();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function createEngine(FlexSearch) {
|
|
33
|
+
const flex = FlexSearch && FlexSearch.Index ? FlexSearch : FlexSearch && FlexSearch.default;
|
|
34
|
+
if (!flex || typeof flex.Index !== 'function' || typeof flex.Encoder !== 'function') {
|
|
35
|
+
throw new Error('Canopy search: FlexSearch 0.8 did not load.');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// A word is found by its beginning ("manu" finds "manuscript"), in any case, with or
|
|
39
|
+
// without its accents ("Haridwar" finds "Haridwār", and the other way). Every word of a
|
|
40
|
+
// query must be found. FlexSearch's default encoder would also collapse repeated
|
|
41
|
+
// letters and split numbers in threes, so that "iiif" found "if" and "1800" found
|
|
42
|
+
// "1805": both are off.
|
|
43
|
+
function createIndex() {
|
|
44
|
+
return new flex.Index({
|
|
45
|
+
tokenize: 'forward',
|
|
46
|
+
encoder: new flex.Encoder({ normalize: true, dedupe: false, numeric: false }),
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Each record is added by its position in `records`, which is what `search` returns.
|
|
51
|
+
function buildIndex(records) {
|
|
52
|
+
const index = createIndex();
|
|
53
|
+
(Array.isArray(records) ? records : []).forEach((record, position) => {
|
|
54
|
+
const text = searchText(record);
|
|
55
|
+
if (text) index.add(position, text);
|
|
56
|
+
});
|
|
57
|
+
return index;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function search(index, query, limit = 200) {
|
|
61
|
+
const text = typeof query === 'string' ? query.trim() : '';
|
|
62
|
+
if (!index || !text) return [];
|
|
63
|
+
const hits = index.search(text, { limit });
|
|
64
|
+
return Array.isArray(hits) ? hits : [];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async function exportIndex(index) {
|
|
68
|
+
const data = {};
|
|
69
|
+
await index.export((key, value) => {
|
|
70
|
+
data[key] = value;
|
|
71
|
+
});
|
|
72
|
+
return data;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async function importIndex(data) {
|
|
76
|
+
const index = createIndex();
|
|
77
|
+
for (const key of Object.keys(data || {})) {
|
|
78
|
+
await index.import(key, data[key]);
|
|
79
|
+
}
|
|
80
|
+
return index;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return { id: ENGINE_ID, createIndex, buildIndex, search, exportIndex, importIndex };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
module.exports = { ENGINE_ID, searchText, createEngine };
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// A visitor's built search index, kept in their browser's IndexedDB so the next page
|
|
2
|
+
// that searches imports it rather than building it again: for 4,000 records, about 20 ms
|
|
3
|
+
// against 450 (measured in Node, 2026-11-16). One index is kept, the current one. Any
|
|
4
|
+
// failure (no IndexedDB, a private window, a full disk) only means building it again.
|
|
5
|
+
|
|
6
|
+
'use strict';
|
|
7
|
+
|
|
8
|
+
const DB_NAME = 'canopy-search';
|
|
9
|
+
const STORE = 'indexes';
|
|
10
|
+
|
|
11
|
+
function hasIndexedDb() {
|
|
12
|
+
try {
|
|
13
|
+
return typeof indexedDB !== 'undefined';
|
|
14
|
+
} catch (_) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function openDb() {
|
|
20
|
+
return new Promise((resolve) => {
|
|
21
|
+
if (!hasIndexedDb()) return resolve(null);
|
|
22
|
+
try {
|
|
23
|
+
const request = indexedDB.open(DB_NAME, 1);
|
|
24
|
+
request.onupgradeneeded = () => {
|
|
25
|
+
const db = request.result;
|
|
26
|
+
if (!db.objectStoreNames.contains(STORE)) db.createObjectStore(STORE, { keyPath: 'version' });
|
|
27
|
+
};
|
|
28
|
+
request.onsuccess = () => resolve(request.result);
|
|
29
|
+
request.onerror = () => resolve(null);
|
|
30
|
+
} catch (_) {
|
|
31
|
+
resolve(null);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function readEntry(key) {
|
|
37
|
+
const db = await openDb();
|
|
38
|
+
if (!db) return null;
|
|
39
|
+
return new Promise((resolve) => {
|
|
40
|
+
try {
|
|
41
|
+
const request = db.transaction(STORE, 'readonly').objectStore(STORE).get(key);
|
|
42
|
+
request.onsuccess = () => resolve(request.result || null);
|
|
43
|
+
request.onerror = () => resolve(null);
|
|
44
|
+
} catch (_) {
|
|
45
|
+
resolve(null);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Writes the entry and drops every other, 1.x's FlexSearch 0.7 indexes among them.
|
|
51
|
+
async function writeEntry(entry) {
|
|
52
|
+
const db = await openDb();
|
|
53
|
+
if (!db) return false;
|
|
54
|
+
return new Promise((resolve) => {
|
|
55
|
+
try {
|
|
56
|
+
const transaction = db.transaction(STORE, 'readwrite');
|
|
57
|
+
const store = transaction.objectStore(STORE);
|
|
58
|
+
store.put(entry);
|
|
59
|
+
const keys = store.getAllKeys();
|
|
60
|
+
keys.onsuccess = () => {
|
|
61
|
+
(keys.result || []).forEach((key) => {
|
|
62
|
+
if (key !== entry.version) store.delete(key);
|
|
63
|
+
});
|
|
64
|
+
};
|
|
65
|
+
transaction.oncomplete = () => resolve(true);
|
|
66
|
+
transaction.onerror = () => resolve(false);
|
|
67
|
+
} catch (_) {
|
|
68
|
+
resolve(false);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Resolves to { index, cached }: the index for these records, imported from the cache
|
|
74
|
+
// when this engine stored one for this version, or else built and stored.
|
|
75
|
+
async function openIndex({ engine, records, version }) {
|
|
76
|
+
const key = `${engine.id}:${version}`;
|
|
77
|
+
if (version) {
|
|
78
|
+
const entry = await readEntry(key);
|
|
79
|
+
if (entry && entry.exportData) {
|
|
80
|
+
try {
|
|
81
|
+
return { index: await engine.importIndex(entry.exportData), cached: true };
|
|
82
|
+
} catch (_) {}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const index = engine.buildIndex(records);
|
|
86
|
+
if (version) {
|
|
87
|
+
try {
|
|
88
|
+
const exportData = await engine.exportIndex(index);
|
|
89
|
+
await writeEntry({ version: key, exportData, ts: Date.now() });
|
|
90
|
+
} catch (_) {}
|
|
91
|
+
}
|
|
92
|
+
return { index, cached: false };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
module.exports = { openIndex };
|