@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,302 @@
|
|
|
1
|
+
const { fs, fsp, path, OUT_DIR, ensureDirSync, absoluteUrl } = require('../common');
|
|
2
|
+
const slugify = require('slugify');
|
|
3
|
+
|
|
4
|
+
function normalizeMetadataLabel(label) {
|
|
5
|
+
if (typeof label !== 'string') return '';
|
|
6
|
+
return label
|
|
7
|
+
.trim()
|
|
8
|
+
.replace(/[:\s]+$/g, '')
|
|
9
|
+
.toLowerCase();
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function firstI18nString(x) {
|
|
13
|
+
if (!x) return '';
|
|
14
|
+
if (typeof x === 'string') return x;
|
|
15
|
+
try {
|
|
16
|
+
const keys = Object.keys(x || {});
|
|
17
|
+
if (!keys.length) return '';
|
|
18
|
+
const arr = x[keys[0]];
|
|
19
|
+
if (Array.isArray(arr) && arr.length) return String(arr[0]);
|
|
20
|
+
} catch (_) {}
|
|
21
|
+
return '';
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async function buildFacetsForWorks(combined, labelWhitelist) {
|
|
25
|
+
const facetsDir = path.resolve('.cache/iiif');
|
|
26
|
+
ensureDirSync(facetsDir);
|
|
27
|
+
const map = new Map(); // label -> Map(value -> Set(docIdx))
|
|
28
|
+
const normalizedLabels = new Set(
|
|
29
|
+
(Array.isArray(labelWhitelist) ? labelWhitelist : [])
|
|
30
|
+
.map((label) => normalizeMetadataLabel(String(label || '')))
|
|
31
|
+
.filter(Boolean)
|
|
32
|
+
);
|
|
33
|
+
if (!Array.isArray(combined)) combined = [];
|
|
34
|
+
for (let i = 0; i < combined.length; i++) {
|
|
35
|
+
const rec = combined[i];
|
|
36
|
+
if (!rec || String(rec.type) !== 'work') continue;
|
|
37
|
+
const href = String(rec.href || '');
|
|
38
|
+
const normalizedHref = href.replace(/^\/+/, '');
|
|
39
|
+
const m = normalizedHref.match(/^works\/(.+)\.html$/i);
|
|
40
|
+
if (!m) continue;
|
|
41
|
+
const slug = m[1];
|
|
42
|
+
const p = path.resolve('.cache/iiif/manifests', slug + '.json');
|
|
43
|
+
if (!fs.existsSync(p)) continue;
|
|
44
|
+
let manifest = null;
|
|
45
|
+
try { manifest = JSON.parse(fs.readFileSync(p, 'utf8')); } catch (_) { manifest = null; }
|
|
46
|
+
const meta = Array.isArray(manifest && manifest.metadata) ? manifest.metadata : [];
|
|
47
|
+
for (const entry of meta) {
|
|
48
|
+
if (!entry) continue;
|
|
49
|
+
const label = firstI18nString(entry.label);
|
|
50
|
+
const valueRaw = entry.value && (typeof entry.value === 'string' ? entry.value : firstI18nString(entry.value));
|
|
51
|
+
if (!label || !valueRaw) continue;
|
|
52
|
+
if (
|
|
53
|
+
normalizedLabels.size &&
|
|
54
|
+
!normalizedLabels.has(normalizeMetadataLabel(label))
|
|
55
|
+
) {
|
|
56
|
+
continue; // only configured labels
|
|
57
|
+
}
|
|
58
|
+
const values = [];
|
|
59
|
+
try {
|
|
60
|
+
if (typeof entry.value === 'string') values.push(entry.value);
|
|
61
|
+
else {
|
|
62
|
+
const obj = entry.value || {};
|
|
63
|
+
for (const k of Object.keys(obj)) {
|
|
64
|
+
const arr = Array.isArray(obj[k]) ? obj[k] : [];
|
|
65
|
+
for (const v of arr) if (v) values.push(String(v));
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
} catch (_) { values.push(valueRaw); }
|
|
69
|
+
if (!map.has(label)) map.set(label, new Map());
|
|
70
|
+
const vmap = map.get(label);
|
|
71
|
+
for (const v of values) {
|
|
72
|
+
const key = String(v);
|
|
73
|
+
if (!vmap.has(key)) vmap.set(key, new Set());
|
|
74
|
+
vmap.get(key).add(i); // doc index in combined
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const out = [];
|
|
79
|
+
for (const [label, vmap] of map.entries()) {
|
|
80
|
+
const labelSlug = slugify(label || 'label', { lower: true, strict: true, trim: true });
|
|
81
|
+
const values = [];
|
|
82
|
+
for (const [value, set] of vmap.entries()) {
|
|
83
|
+
const docs = Array.from(set.values()).sort((a, b) => a - b);
|
|
84
|
+
values.push({ value, slug: slugify(value || 'value', { lower: true, strict: true, trim: true }), doc_count: docs.length, docs });
|
|
85
|
+
}
|
|
86
|
+
values.sort((a, b) => b.doc_count - a.doc_count || String(a.value).localeCompare(String(b.value)));
|
|
87
|
+
out.push({ label, slug: labelSlug, values });
|
|
88
|
+
}
|
|
89
|
+
out.sort((a, b) => String(a.label).localeCompare(String(b.label)));
|
|
90
|
+
const dest = path.join(facetsDir, 'facets.json');
|
|
91
|
+
await fsp.writeFile(dest, JSON.stringify(out, null, 2), 'utf8');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function writeFacetCollections(labelWhitelist, combined) {
|
|
95
|
+
const facetsPath = path.resolve('.cache/iiif/facets.json');
|
|
96
|
+
if (!fs.existsSync(facetsPath)) return;
|
|
97
|
+
let facets = [];
|
|
98
|
+
try { facets = JSON.parse(fs.readFileSync(facetsPath, 'utf8')) || []; } catch (_) { facets = []; }
|
|
99
|
+
const normalizedLabels = new Set(
|
|
100
|
+
(Array.isArray(labelWhitelist) ? labelWhitelist : [])
|
|
101
|
+
.map((label) => normalizeMetadataLabel(String(label || '')))
|
|
102
|
+
.filter(Boolean)
|
|
103
|
+
);
|
|
104
|
+
const apiRoot = path.join(OUT_DIR, 'api');
|
|
105
|
+
const facetRoot = path.join(apiRoot, 'facet');
|
|
106
|
+
ensureDirSync(facetRoot);
|
|
107
|
+
const list = (Array.isArray(facets) ? facets : []).filter((f) => {
|
|
108
|
+
if (!normalizedLabels.size) return true;
|
|
109
|
+
const normalized = normalizeMetadataLabel(String((f && f.label) || ''));
|
|
110
|
+
return normalized ? normalizedLabels.has(normalized) : false;
|
|
111
|
+
});
|
|
112
|
+
const labelIndexItems = [];
|
|
113
|
+
for (const f of list) {
|
|
114
|
+
if (!f || !f.label || !Array.isArray(f.values)) continue;
|
|
115
|
+
const label = String(f.label);
|
|
116
|
+
const labelSlug = slugify(label || 'label', { lower: true, strict: true, trim: true });
|
|
117
|
+
const labelDir = path.join(facetRoot, labelSlug);
|
|
118
|
+
ensureDirSync(labelDir);
|
|
119
|
+
for (const v of f.values) {
|
|
120
|
+
if (!v || typeof v !== 'object') continue;
|
|
121
|
+
const value = String(v.value || '');
|
|
122
|
+
const valueSlug = slugify(value || 'value', { lower: true, strict: true, trim: true });
|
|
123
|
+
const dest = path.join(labelDir, valueSlug + '.json');
|
|
124
|
+
const docIdxs = Array.isArray(v.docs) ? v.docs : [];
|
|
125
|
+
const items = [];
|
|
126
|
+
for (const idx of docIdxs) {
|
|
127
|
+
const rec = combined && Array.isArray(combined) ? combined[idx] : null;
|
|
128
|
+
if (!rec || String(rec.type) !== 'work') continue;
|
|
129
|
+
const id = String(rec.id || '');
|
|
130
|
+
const title = String(rec.title || rec.href || '');
|
|
131
|
+
const thumb = String(rec.thumbnail || '');
|
|
132
|
+
const href = String(rec.href || '');
|
|
133
|
+
const homepageId = absoluteUrl('/' + href.replace(/^\/?/, ''));
|
|
134
|
+
const item = { id, type: 'Manifest', label: { none: [title] } };
|
|
135
|
+
if (thumb) item.thumbnail = [{ id: thumb, type: 'Image' }];
|
|
136
|
+
item.homepage = [{ id: homepageId, type: 'Text', label: { none: [title] } }];
|
|
137
|
+
items.push(item);
|
|
138
|
+
}
|
|
139
|
+
const selfId = absoluteUrl(`/api/facet/${labelSlug}/${valueSlug}.json`);
|
|
140
|
+
const parentId = absoluteUrl(`/api/facet/${labelSlug}.json`);
|
|
141
|
+
const homepage = absoluteUrl(`/search/index.html?${encodeURIComponent(labelSlug)}=${encodeURIComponent(valueSlug)}`);
|
|
142
|
+
const col = {
|
|
143
|
+
'@context': 'https://iiif.io/api/presentation/3/context.json',
|
|
144
|
+
id: selfId,
|
|
145
|
+
type: 'Collection',
|
|
146
|
+
label: { none: [value] },
|
|
147
|
+
items,
|
|
148
|
+
partOf: [{ id: parentId, type: 'Collection' }],
|
|
149
|
+
summary: { none: [label] },
|
|
150
|
+
homepage: [{ id: homepage, type: 'Text', label: { none: [value] } }],
|
|
151
|
+
};
|
|
152
|
+
await fsp.writeFile(dest, JSON.stringify(col, null, 2), 'utf8');
|
|
153
|
+
}
|
|
154
|
+
const labelIndexDest = path.join(facetRoot, labelSlug + '.json');
|
|
155
|
+
const labelItems = (f.values || []).map((v) => ({
|
|
156
|
+
id: absoluteUrl(`/api/facet/${labelSlug}/${slugify(String(v && v.value || ''), { lower: true, strict: true, trim: true })}.json`),
|
|
157
|
+
type: 'Collection',
|
|
158
|
+
label: { none: [String(v && v.value || '')] },
|
|
159
|
+
summary: { none: [label] },
|
|
160
|
+
}));
|
|
161
|
+
const labelIndex = {
|
|
162
|
+
'@context': 'https://iiif.io/api/presentation/3/context.json',
|
|
163
|
+
id: absoluteUrl(`/api/facet/${labelSlug}.json`),
|
|
164
|
+
type: 'Collection',
|
|
165
|
+
label: { none: [label] },
|
|
166
|
+
items: labelItems,
|
|
167
|
+
};
|
|
168
|
+
await fsp.writeFile(labelIndexDest, JSON.stringify(labelIndex, null, 2), 'utf8');
|
|
169
|
+
labelIndexItems.push({ id: absoluteUrl(`/api/facet/${labelSlug}.json`), type: 'Collection', label: { none: [label] } });
|
|
170
|
+
}
|
|
171
|
+
const facetIndex = {
|
|
172
|
+
'@context': 'https://iiif.io/api/presentation/3/context.json',
|
|
173
|
+
id: absoluteUrl('/api/facet/index.json'),
|
|
174
|
+
type: 'Collection',
|
|
175
|
+
label: { none: ['Facets'] },
|
|
176
|
+
items: labelIndexItems,
|
|
177
|
+
};
|
|
178
|
+
await fsp.writeFile(path.join(facetRoot, 'index.json'), JSON.stringify(facetIndex, null, 2), 'utf8');
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
async function writeFacetsSearchApi() {
|
|
182
|
+
const src = path.resolve('.cache/iiif/facets.json');
|
|
183
|
+
if (!fs.existsSync(src)) return;
|
|
184
|
+
let data = null;
|
|
185
|
+
try { data = JSON.parse(fs.readFileSync(src, 'utf8')); } catch (_) { data = null; }
|
|
186
|
+
if (!data) return;
|
|
187
|
+
const destDir = path.join(OUT_DIR, 'api', 'search');
|
|
188
|
+
ensureDirSync(destDir);
|
|
189
|
+
const dest = path.join(destDir, 'facets.json');
|
|
190
|
+
await fsp.writeFile(dest, JSON.stringify(data, null, 2), 'utf8');
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function frontmatterString(data, key) {
|
|
194
|
+
const raw = data && typeof data === 'object' ? data[key] : null;
|
|
195
|
+
return typeof raw === 'string' ? raw.trim() : '';
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// A search result's image: an absolute URL as it is, any other path from the site's root
|
|
199
|
+
// (`/images/a.jpg`); the search runtimes put the site's base path before it.
|
|
200
|
+
function resolvePageImage(value) {
|
|
201
|
+
const raw = String(value || '').trim();
|
|
202
|
+
if (!raw) return '';
|
|
203
|
+
if (/^(?:[a-z][a-z0-9+.-]*:|\/\/)/i.test(raw)) return raw;
|
|
204
|
+
const { rootRelativeHref } = require('../common');
|
|
205
|
+
return rootRelativeHref(raw);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// What an MDX page gives search, from its source and its front matter
|
|
209
|
+
// (content/docs/user-guides/customize-the-search-index.mdx):
|
|
210
|
+
// search: false left out
|
|
211
|
+
// searchType, or type its type, and so its tab; else its folder's layout's, else
|
|
212
|
+
// `page`. A `type` given empty leaves it out, as before.
|
|
213
|
+
// searchSummaryMarkdown its result's text, in inline Markdown
|
|
214
|
+
// searchSummary its result's text, plain
|
|
215
|
+
// description its result's text where neither is given
|
|
216
|
+
// image (or og:image) its result's image
|
|
217
|
+
// Its result shows the passage around the words searched for where none of the three is
|
|
218
|
+
// given. A summary's or description's words are searched, before the page's own.
|
|
219
|
+
function pageSearchRecord(source, { href = '', layoutType = '' } = {}) {
|
|
220
|
+
const mdx = require('./mdx');
|
|
221
|
+
const { escapeMarkdown } = require('./search-text');
|
|
222
|
+
const fm = mdx.parseFrontmatter(source);
|
|
223
|
+
const data = fm && fm.data && typeof fm.data === 'object' ? fm.data : null;
|
|
224
|
+
const title = String(mdx.extractTitle(source) || '').trim();
|
|
225
|
+
const words = mdx.extractSearchText(source);
|
|
226
|
+
|
|
227
|
+
// The search page is not a result; its folder holds only its layout and templates.
|
|
228
|
+
let include = !/^\/?search(?:\/|\.html$)/i.test(href);
|
|
229
|
+
if (data && data.search === false) include = false;
|
|
230
|
+
let type = frontmatterString(data, 'searchType');
|
|
231
|
+
if (!type && include && data && Object.prototype.hasOwnProperty.call(data, 'type')) {
|
|
232
|
+
if (data.type) type = String(data.type).trim();
|
|
233
|
+
else include = false;
|
|
234
|
+
}
|
|
235
|
+
if (!type) type = String(layoutType || '').trim() || 'page';
|
|
236
|
+
|
|
237
|
+
const summaryMarkdown = frontmatterString(data, 'searchSummaryMarkdown');
|
|
238
|
+
const summary = frontmatterString(data, 'searchSummary');
|
|
239
|
+
const description = frontmatterString(data, 'description');
|
|
240
|
+
const shownMarkdown =
|
|
241
|
+
summaryMarkdown ||
|
|
242
|
+
(summary ? escapeMarkdown(summary) : '') ||
|
|
243
|
+
(description ? escapeMarkdown(description) : '') ||
|
|
244
|
+
words.markdown;
|
|
245
|
+
const leadText = summaryMarkdown
|
|
246
|
+
? mdx.extractSearchText(summaryMarkdown).text
|
|
247
|
+
: summary || description;
|
|
248
|
+
const image = resolvePageImage(
|
|
249
|
+
frontmatterString(data, 'image') ||
|
|
250
|
+
frontmatterString(data, 'og:image') ||
|
|
251
|
+
frontmatterString(data, 'ogImage'),
|
|
252
|
+
);
|
|
253
|
+
|
|
254
|
+
const record = {
|
|
255
|
+
title,
|
|
256
|
+
href,
|
|
257
|
+
searchInclude: include && !!type,
|
|
258
|
+
searchType: type || undefined,
|
|
259
|
+
searchSummary: [leadText, words.text].filter(Boolean).join(' '),
|
|
260
|
+
searchSummaryMarkdown: shownMarkdown,
|
|
261
|
+
};
|
|
262
|
+
if (image) record.thumbnail = image;
|
|
263
|
+
return record;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
async function collectMdxPageRecords() {
|
|
267
|
+
const { fsp, path, CONTENT_DIR, rootRelativeHref } = require('../common');
|
|
268
|
+
const mdx = require('./mdx');
|
|
269
|
+
const pagesHelpers = require('./pages');
|
|
270
|
+
const pages = [];
|
|
271
|
+
async function walk(dir) {
|
|
272
|
+
const entries = await fsp.readdir(dir, { withFileTypes: true });
|
|
273
|
+
for (const e of entries) {
|
|
274
|
+
const p = path.join(dir, e.name);
|
|
275
|
+
if (e.isDirectory()) await walk(p);
|
|
276
|
+
else if (e.isFile() && /\.mdx$/i.test(p) && !mdx.isReservedFile(p)) {
|
|
277
|
+
const base = path.basename(p).toLowerCase();
|
|
278
|
+
if (base === 'sitemap.mdx') continue;
|
|
279
|
+
const src = await fsp.readFile(p, 'utf8');
|
|
280
|
+
const rel = path.relative(CONTENT_DIR, p).replace(/\.mdx$/i, '.html');
|
|
281
|
+
const href = rootRelativeHref(rel.split(path.sep).join('/'));
|
|
282
|
+
let layoutType = '';
|
|
283
|
+
try {
|
|
284
|
+
const layoutMeta = await pagesHelpers.getNearestDirLayoutMeta(p);
|
|
285
|
+
if (layoutMeta && layoutMeta.type) layoutType = String(layoutMeta.type);
|
|
286
|
+
} catch (_) {}
|
|
287
|
+
pages.push(pageSearchRecord(src, { href, layoutType }));
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
await walk(CONTENT_DIR);
|
|
292
|
+
return pages;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
module.exports = {
|
|
297
|
+
buildFacetsForWorks,
|
|
298
|
+
writeFacetCollections,
|
|
299
|
+
writeFacetsSearchApi,
|
|
300
|
+
collectMdxPageRecords,
|
|
301
|
+
pageSearchRecord,
|
|
302
|
+
};
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
const { fsp, path, OUT_DIR, absoluteUrl, rootRelativeHref } = require('../common');
|
|
2
|
+
const { logLine } = require('./log');
|
|
3
|
+
|
|
4
|
+
const DEFAULT_CHANGEFREQ = 'monthly';
|
|
5
|
+
const DEFAULT_PRIORITY = '0.5';
|
|
6
|
+
const MAX_URLS_PER_SITEMAP = 1000;
|
|
7
|
+
const SITEMAP_INDEX_BASENAME = 'sitemap.xml';
|
|
8
|
+
|
|
9
|
+
function escapeXml(value) {
|
|
10
|
+
return String(value || '')
|
|
11
|
+
.replace(/&/g, '&')
|
|
12
|
+
.replace(/</g, '<')
|
|
13
|
+
.replace(/>/g, '>')
|
|
14
|
+
.replace(/"/g, '"')
|
|
15
|
+
.replace(/'/g, ''');
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function normalizeHref(href) {
|
|
19
|
+
if (!href && href !== 0) return '';
|
|
20
|
+
const rel = rootRelativeHref(href);
|
|
21
|
+
if (!rel || rel === '#') return '';
|
|
22
|
+
if (rel.startsWith('?') || rel.startsWith('#')) return '';
|
|
23
|
+
return rel;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function collectAbsoluteUrls(iiifRecords, pageRecords) {
|
|
27
|
+
const urls = new Set();
|
|
28
|
+
const push = (href) => {
|
|
29
|
+
const rel = normalizeHref(href);
|
|
30
|
+
if (!rel) return;
|
|
31
|
+
try {
|
|
32
|
+
const abs = absoluteUrl(rel);
|
|
33
|
+
if (abs) urls.add(abs);
|
|
34
|
+
} catch (_) {}
|
|
35
|
+
};
|
|
36
|
+
(Array.isArray(pageRecords) ? pageRecords : []).forEach((page) => {
|
|
37
|
+
if (!page || !page.href) return;
|
|
38
|
+
push(page.href);
|
|
39
|
+
});
|
|
40
|
+
(Array.isArray(iiifRecords) ? iiifRecords : []).forEach((record) => {
|
|
41
|
+
if (!record || !record.href) return;
|
|
42
|
+
push(record.href);
|
|
43
|
+
});
|
|
44
|
+
// Ensure the search page is always present even though it is generated separately
|
|
45
|
+
push('/search/index.html');
|
|
46
|
+
return Array.from(urls.values()).sort((a, b) => a.localeCompare(b));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function buildUrlsetXml(urls) {
|
|
50
|
+
const rows = urls.map((loc) => {
|
|
51
|
+
const escapedLoc = escapeXml(loc);
|
|
52
|
+
return [
|
|
53
|
+
' <url>',
|
|
54
|
+
` <loc>${escapedLoc}</loc>`,
|
|
55
|
+
` <changefreq>${DEFAULT_CHANGEFREQ}</changefreq>`,
|
|
56
|
+
` <priority>${DEFAULT_PRIORITY}</priority>`,
|
|
57
|
+
' </url>',
|
|
58
|
+
].join('\n');
|
|
59
|
+
});
|
|
60
|
+
return [
|
|
61
|
+
'<?xml version="1.0" encoding="UTF-8"?>',
|
|
62
|
+
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
|
|
63
|
+
rows.join('\n'),
|
|
64
|
+
'</urlset>',
|
|
65
|
+
'',
|
|
66
|
+
].join('\n');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function buildSitemapIndexXml(entries) {
|
|
70
|
+
const rows = entries.map((entry) => {
|
|
71
|
+
const escapedLoc = escapeXml(entry.loc);
|
|
72
|
+
return [' <sitemap>', ` <loc>${escapedLoc}</loc>`, ' </sitemap>'].join('\n');
|
|
73
|
+
});
|
|
74
|
+
return [
|
|
75
|
+
'<?xml version="1.0" encoding="UTF-8"?>',
|
|
76
|
+
'<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
|
|
77
|
+
rows.join('\n'),
|
|
78
|
+
'</sitemapindex>',
|
|
79
|
+
'',
|
|
80
|
+
].join('\n');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function chunkList(list, chunkSize) {
|
|
84
|
+
const chunks = [];
|
|
85
|
+
if (!Array.isArray(list) || chunkSize <= 0) return chunks;
|
|
86
|
+
for (let i = 0; i < list.length; i += chunkSize) {
|
|
87
|
+
chunks.push(list.slice(i, i + chunkSize));
|
|
88
|
+
}
|
|
89
|
+
return chunks;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async function cleanupLegacySitemaps() {
|
|
93
|
+
let entries;
|
|
94
|
+
try {
|
|
95
|
+
entries = await fsp.readdir(OUT_DIR);
|
|
96
|
+
} catch (_) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const deletions = entries
|
|
100
|
+
.filter((name) => /^sitemap-\d+\.xml$/i.test(name))
|
|
101
|
+
.map((name) =>
|
|
102
|
+
fsp
|
|
103
|
+
.unlink(path.join(OUT_DIR, name))
|
|
104
|
+
.catch(() => {})
|
|
105
|
+
);
|
|
106
|
+
await Promise.all(deletions);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async function ensureNoExtensionGuards(fileNames) {
|
|
110
|
+
const guards = new Map();
|
|
111
|
+
(Array.isArray(fileNames) ? fileNames : []).forEach((file) => {
|
|
112
|
+
const raw = typeof file === 'string' ? file.trim() : '';
|
|
113
|
+
if (!raw || !/\.xml$/i.test(raw)) return;
|
|
114
|
+
const base = raw.replace(/\.xml$/i, '');
|
|
115
|
+
if (!base || base === raw) return;
|
|
116
|
+
guards.set(base, raw);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
let entries;
|
|
120
|
+
try {
|
|
121
|
+
entries = await fsp.readdir(OUT_DIR, { withFileTypes: true });
|
|
122
|
+
} catch (_) {
|
|
123
|
+
entries = [];
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const markerName = '.canopy-xml-guard';
|
|
127
|
+
const staleRemovals = [];
|
|
128
|
+
for (const entry of entries) {
|
|
129
|
+
if (!entry || !entry.isDirectory()) continue;
|
|
130
|
+
const dirName = entry.name;
|
|
131
|
+
const dirPath = path.join(OUT_DIR, dirName);
|
|
132
|
+
const markerPath = path.join(dirPath, markerName);
|
|
133
|
+
let hasMarker = false;
|
|
134
|
+
try {
|
|
135
|
+
const stat = await fsp.stat(markerPath);
|
|
136
|
+
hasMarker = stat.isFile();
|
|
137
|
+
} catch (_) {}
|
|
138
|
+
if (!hasMarker) continue;
|
|
139
|
+
if (guards.has(dirName)) {
|
|
140
|
+
guards.delete(dirName);
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
staleRemovals.push(
|
|
144
|
+
fsp
|
|
145
|
+
.rm(dirPath, { recursive: true, force: true })
|
|
146
|
+
.catch(() => {})
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
await Promise.all(staleRemovals);
|
|
150
|
+
|
|
151
|
+
if (!guards.size) return;
|
|
152
|
+
|
|
153
|
+
const creations = [];
|
|
154
|
+
for (const base of guards.keys()) {
|
|
155
|
+
const dirPath = path.join(OUT_DIR, base);
|
|
156
|
+
const markerPath = path.join(dirPath, markerName);
|
|
157
|
+
creations.push(
|
|
158
|
+
fsp
|
|
159
|
+
.mkdir(dirPath, { recursive: true })
|
|
160
|
+
.then(() => fsp.writeFile(markerPath, '', 'utf8').catch(() => {}))
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
await Promise.all(creations);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
async function writeSitemap(iiifRecords, pageRecords) {
|
|
167
|
+
const urls = collectAbsoluteUrls(iiifRecords, pageRecords);
|
|
168
|
+
if (!urls.length) {
|
|
169
|
+
await cleanupLegacySitemaps();
|
|
170
|
+
await ensureNoExtensionGuards([]);
|
|
171
|
+
logLine('• No URLs to write to sitemap', 'yellow');
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
await cleanupLegacySitemaps();
|
|
175
|
+
|
|
176
|
+
const chunks = chunkList(urls, MAX_URLS_PER_SITEMAP);
|
|
177
|
+
const indexEntries = [];
|
|
178
|
+
const writtenFiles = [];
|
|
179
|
+
for (let i = 0; i < chunks.length; i += 1) {
|
|
180
|
+
const fileName = `sitemap-${i + 1}.xml`;
|
|
181
|
+
const dest = path.join(OUT_DIR, fileName);
|
|
182
|
+
await fsp.writeFile(dest, buildUrlsetXml(chunks[i]), 'utf8');
|
|
183
|
+
indexEntries.push({ loc: absoluteUrl(fileName) });
|
|
184
|
+
writtenFiles.push(fileName);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const indexDest = path.join(OUT_DIR, SITEMAP_INDEX_BASENAME);
|
|
188
|
+
await fsp.writeFile(indexDest, buildSitemapIndexXml(indexEntries), 'utf8');
|
|
189
|
+
writtenFiles.push(SITEMAP_INDEX_BASENAME);
|
|
190
|
+
await ensureNoExtensionGuards(writtenFiles);
|
|
191
|
+
logLine(
|
|
192
|
+
`✓ Wrote sitemap index (${chunks.length} files, ${urls.length} urls total)`,
|
|
193
|
+
'cyan'
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
module.exports = { writeSitemap };
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
const {
|
|
2
|
+
fs,
|
|
3
|
+
fsp,
|
|
4
|
+
path,
|
|
5
|
+
OUT_DIR,
|
|
6
|
+
CONTENT_DIR,
|
|
7
|
+
ensureDirSync,
|
|
8
|
+
} = require("../common");
|
|
9
|
+
const {withThemeBlock} = require("./theme-tokens");
|
|
10
|
+
const {copyFonts, fontFaceCss} = require("./fonts");
|
|
11
|
+
|
|
12
|
+
// The site's own Tailwind CLI where it installs one, else this package's (a dependency
|
|
13
|
+
// since 2.0, so a site need not install it).
|
|
14
|
+
function resolveTailwindCli() {
|
|
15
|
+
for (const from of [process.cwd(), __dirname]) {
|
|
16
|
+
try {
|
|
17
|
+
const pkgPath = require.resolve("@tailwindcss/cli/package.json", {paths: [from]});
|
|
18
|
+
const cliEntry = path.join(path.dirname(pkgPath), "dist", "index.mjs");
|
|
19
|
+
if (fs.existsSync(cliEntry)) {
|
|
20
|
+
return {cmd: process.execPath || "node", args: [cliEntry]};
|
|
21
|
+
}
|
|
22
|
+
} catch (_) {}
|
|
23
|
+
}
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// The theme's tokens and the package's font faces, at the top of the compiled
|
|
28
|
+
// stylesheet, with the faces' files copied beside it (`fonts.js`).
|
|
29
|
+
function injectThemeTokens(targetPath) {
|
|
30
|
+
try {
|
|
31
|
+
const {loadCanopyTheme} = require("../../ui/theme");
|
|
32
|
+
const theme = loadCanopyTheme();
|
|
33
|
+
const themeCss = theme && theme.css ? theme.css.trim() : "";
|
|
34
|
+
if (!themeCss) return;
|
|
35
|
+
|
|
36
|
+
copyFonts(path.dirname(targetPath));
|
|
37
|
+
|
|
38
|
+
let existing = "";
|
|
39
|
+
try {
|
|
40
|
+
existing = fs.readFileSync(targetPath, "utf8");
|
|
41
|
+
} catch (_) {}
|
|
42
|
+
|
|
43
|
+
const next = withThemeBlock(existing, `${fontFaceCss()}\n${themeCss}`);
|
|
44
|
+
// Dev re-runs this from a watcher on the same file; skip no-op writes.
|
|
45
|
+
if (next !== existing) fs.writeFileSync(targetPath, next, "utf8");
|
|
46
|
+
} catch (_) {}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function ensureStyles() {
|
|
50
|
+
const stylesDir = path.join(OUT_DIR, "styles");
|
|
51
|
+
const dest = path.join(stylesDir, "styles.css");
|
|
52
|
+
const customContentCss = path.join(CONTENT_DIR, "_styles.css");
|
|
53
|
+
const appStylesDir = path.join(process.cwd(), "app", "styles");
|
|
54
|
+
const customAppCss = path.join(appStylesDir, "index.css");
|
|
55
|
+
ensureDirSync(stylesDir);
|
|
56
|
+
|
|
57
|
+
const hasAppCss = fs.existsSync(customAppCss);
|
|
58
|
+
const hasContentCss = fs.existsSync(customContentCss);
|
|
59
|
+
const inputCss = hasAppCss
|
|
60
|
+
? customAppCss
|
|
61
|
+
: hasContentCss
|
|
62
|
+
? customContentCss
|
|
63
|
+
: null;
|
|
64
|
+
|
|
65
|
+
let generatedInput = null;
|
|
66
|
+
if (!inputCss) {
|
|
67
|
+
try {
|
|
68
|
+
const { CACHE_DIR } = require("../common");
|
|
69
|
+
const genDir = path.join(CACHE_DIR, "tailwind");
|
|
70
|
+
ensureDirSync(genDir);
|
|
71
|
+
generatedInput = path.join(genDir, "index.css");
|
|
72
|
+
const css = `@import 'tailwindcss';\n`;
|
|
73
|
+
fs.writeFileSync(generatedInput, css, "utf8");
|
|
74
|
+
} catch (_) {
|
|
75
|
+
generatedInput = null;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Tailwind v4 reads a JS config only through `@config` in the stylesheet.
|
|
80
|
+
function buildTailwindCli({ input, output, minify = true }) {
|
|
81
|
+
try {
|
|
82
|
+
const cli = resolveTailwindCli();
|
|
83
|
+
if (!cli) return false;
|
|
84
|
+
const { spawnSync } = require("child_process");
|
|
85
|
+
const args = ["-i", input, "-o", output];
|
|
86
|
+
if (minify) args.push("--minify");
|
|
87
|
+
const res = spawnSync(cli.cmd, [...cli.args, ...args], {
|
|
88
|
+
stdio: "inherit",
|
|
89
|
+
env: { ...process.env, BROWSERSLIST_IGNORE_OLD_DATA: "1" },
|
|
90
|
+
});
|
|
91
|
+
return !!res && res.status === 0;
|
|
92
|
+
} catch (_) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (inputCss || generatedInput) {
|
|
98
|
+
const ok = buildTailwindCli({
|
|
99
|
+
input: inputCss || generatedInput,
|
|
100
|
+
output: dest,
|
|
101
|
+
minify: true,
|
|
102
|
+
});
|
|
103
|
+
if (ok) {
|
|
104
|
+
injectThemeTokens(dest);
|
|
105
|
+
return; // Tailwind compiled CSS
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function isTailwindSource(p) {
|
|
110
|
+
try {
|
|
111
|
+
const s = fs.readFileSync(p, "utf8");
|
|
112
|
+
return /@tailwind\s+(base|components|utilities)/.test(s);
|
|
113
|
+
} catch (_) {
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (fs.existsSync(customAppCss)) {
|
|
118
|
+
if (!isTailwindSource(customAppCss)) {
|
|
119
|
+
await fsp.copyFile(customAppCss, dest);
|
|
120
|
+
injectThemeTokens(dest);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if (fs.existsSync(customContentCss)) {
|
|
125
|
+
if (!isTailwindSource(customContentCss)) {
|
|
126
|
+
await fsp.copyFile(customContentCss, dest);
|
|
127
|
+
injectThemeTokens(dest);
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const css = `:root{--max-w:760px;--muted:#6b7280}*{box-sizing:border-box}body{font-family:system-ui,-apple-system,Segoe UI,Roboto,Ubuntu,Helvetica,Arial,sans-serif;max-width:var(--max-w);margin:2rem auto;padding:0 1rem;line-height:1.6}a{color:#2563eb;text-decoration:none}a:hover{text-decoration:underline}.site-header,.site-footer{display:flex;align-items:center;justify-content:space-between;gap:.5rem;padding:1rem 0;border-bottom:1px solid #e5e7eb}.site-footer{border-bottom:0;border-top:1px solid #e5e7eb;color:var(--muted)}.brand{font-weight:600}.content pre{background:#f6f8fa;padding:1rem;overflow:auto}.content code{font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;background:#f6f8fa;padding:.1rem .3rem;border-radius:4px}.tabs{display:flex;gap:.5rem;align-items:center;border-bottom:1px solid #e5e7eb;margin:.5rem 0}.tab{background:none;border:0;color:#374151;padding:.25rem .5rem;border-radius:.375rem;cursor:pointer}.tab:hover{color:#111827}.tab-active{color:#2563eb;border:1px solid #e5e7eb;border-bottom:0;background:#fff}.masonry{column-gap:1rem;column-count:1}@media(min-width:768px){.masonry{column-count:2}}@media(min-width:1024px){.masonry{column-count:3}}.masonry>*{break-inside:avoid;margin-bottom:1rem;display:block}[data-grid-variant=masonry]{column-gap:var(--grid-gap,1rem);column-count:var(--cols-base,1)}@media(min-width:768px){[data-grid-variant=masonry]{column-count:var(--cols-md,2)}}@media(min-width:1024px){[data-grid-variant=masonry]{column-count:var(--cols-lg,3)}}[data-grid-variant=masonry]>*{break-inside:avoid;margin-bottom:var(--grid-gap,1rem);display:block}[data-grid-variant=grid]{display:grid;grid-template-columns:repeat(var(--cols-base,1),minmax(0,1fr));gap:var(--grid-gap,1rem)}@media(min-width:768px){[data-grid-variant=grid]{grid-template-columns:repeat(var(--cols-md,2),minmax(0,1fr))}}@media(min-width:1024px){[data-grid-variant=grid]{grid-template-columns:repeat(var(--cols-lg,3),minmax(0,1fr))}}`;
|
|
133
|
+
await fsp.writeFile(dest, css, "utf8");
|
|
134
|
+
injectThemeTokens(dest);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
module.exports = {
|
|
138
|
+
ensureStyles,
|
|
139
|
+
injectThemeTokens,
|
|
140
|
+
};
|