@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
package/lib/common.js
ADDED
|
@@ -0,0 +1,580 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const fsp = fs.promises;
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const yaml = require('js-yaml');
|
|
5
|
+
const { resolveCanopyConfigPath } = require('./config-path');
|
|
6
|
+
const { readCanopyLocales, readLocaleRoutes, DEFAULT_LOCALE_ROUTES } = require('./locales');
|
|
7
|
+
|
|
8
|
+
const CONTENT_DIR = path.resolve('content');
|
|
9
|
+
const OUT_DIR = path.resolve('site');
|
|
10
|
+
const CACHE_DIR = path.resolve('.cache/mdx');
|
|
11
|
+
const ASSETS_DIR = path.resolve('assets');
|
|
12
|
+
|
|
13
|
+
const { readBasePath, withBasePath } = require('./base-path');
|
|
14
|
+
|
|
15
|
+
const BASE_PATH = readBasePath();
|
|
16
|
+
let cachedAppearance = null;
|
|
17
|
+
let cachedAccent = null;
|
|
18
|
+
let cachedSiteMetadata = null;
|
|
19
|
+
let cachedSearchPageMetadata = null;
|
|
20
|
+
const DEFAULT_SITE_TITLE = 'Site title';
|
|
21
|
+
const DEFAULT_SEARCH_PAGE_TITLE = 'Search';
|
|
22
|
+
const DEFAULT_SEARCH_PAGE_DESCRIPTION = '';
|
|
23
|
+
const DEFAULT_LANG_FALLBACK = 'en';
|
|
24
|
+
|
|
25
|
+
let cachedLocaleState = null;
|
|
26
|
+
let cachedLocaleRoutesState = null;
|
|
27
|
+
|
|
28
|
+
function getLocaleState() {
|
|
29
|
+
if (cachedLocaleState) return cachedLocaleState;
|
|
30
|
+
let locales = [];
|
|
31
|
+
try {
|
|
32
|
+
const data = readCanopyLocales();
|
|
33
|
+
if (Array.isArray(data) && data.length) {
|
|
34
|
+
locales = data;
|
|
35
|
+
}
|
|
36
|
+
} catch (_) {}
|
|
37
|
+
if (!Array.isArray(locales) || !locales.length) {
|
|
38
|
+
locales = [
|
|
39
|
+
{
|
|
40
|
+
lang: DEFAULT_LANG_FALLBACK,
|
|
41
|
+
label: 'English',
|
|
42
|
+
default: true,
|
|
43
|
+
},
|
|
44
|
+
];
|
|
45
|
+
}
|
|
46
|
+
const normalized = locales
|
|
47
|
+
.map((locale) => {
|
|
48
|
+
if (!locale || typeof locale !== 'object') return null;
|
|
49
|
+
const lang = typeof locale.lang === 'string' ? locale.lang.trim() : '';
|
|
50
|
+
if (!lang) return null;
|
|
51
|
+
return {...locale, lang};
|
|
52
|
+
})
|
|
53
|
+
.filter(Boolean);
|
|
54
|
+
if (!normalized.length) {
|
|
55
|
+
normalized.push({lang: DEFAULT_LANG_FALLBACK, label: 'English', default: true});
|
|
56
|
+
}
|
|
57
|
+
const defaultLocale =
|
|
58
|
+
normalized.find((loc) => loc && loc.default) ||
|
|
59
|
+
normalized[0] ||
|
|
60
|
+
{lang: DEFAULT_LANG_FALLBACK};
|
|
61
|
+
const lookup = new Map();
|
|
62
|
+
normalized.forEach((locale) => {
|
|
63
|
+
const key = locale.lang.toLowerCase();
|
|
64
|
+
if (!lookup.has(key)) {
|
|
65
|
+
lookup.set(key, locale.lang);
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
cachedLocaleState = {
|
|
69
|
+
locales: normalized,
|
|
70
|
+
defaultLocale,
|
|
71
|
+
lookup,
|
|
72
|
+
};
|
|
73
|
+
return cachedLocaleState;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function getLocaleRoutesState() {
|
|
77
|
+
if (cachedLocaleRoutesState) return cachedLocaleRoutesState;
|
|
78
|
+
const localeState = getLocaleState();
|
|
79
|
+
const order = Array.isArray(localeState.locales)
|
|
80
|
+
? localeState.locales.map((loc) => loc.lang)
|
|
81
|
+
: [];
|
|
82
|
+
const routeMap = new Map();
|
|
83
|
+
order.forEach((lang) => {
|
|
84
|
+
try {
|
|
85
|
+
routeMap.set(lang, {...readLocaleRoutes(lang)});
|
|
86
|
+
} catch (_) {
|
|
87
|
+
routeMap.set(lang, {...DEFAULT_LOCALE_ROUTES});
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
if (!order.length) {
|
|
91
|
+
routeMap.set(DEFAULT_LANG_FALLBACK, {...DEFAULT_LOCALE_ROUTES});
|
|
92
|
+
}
|
|
93
|
+
cachedLocaleRoutesState = {
|
|
94
|
+
order: order.length ? order : [DEFAULT_LANG_FALLBACK],
|
|
95
|
+
map: routeMap,
|
|
96
|
+
};
|
|
97
|
+
return cachedLocaleRoutesState;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function getSiteLocales() {
|
|
101
|
+
const state = getLocaleState();
|
|
102
|
+
return state.locales.slice();
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function getDefaultLocaleCode() {
|
|
106
|
+
const state = getLocaleState();
|
|
107
|
+
const lang = state && state.defaultLocale && state.defaultLocale.lang;
|
|
108
|
+
return lang || DEFAULT_LANG_FALLBACK;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function canonicalizeLocaleCode(value) {
|
|
112
|
+
if (value == null) return '';
|
|
113
|
+
let raw = '';
|
|
114
|
+
try {
|
|
115
|
+
raw = String(value).trim();
|
|
116
|
+
} catch (_) {
|
|
117
|
+
raw = '';
|
|
118
|
+
}
|
|
119
|
+
if (!raw) return '';
|
|
120
|
+
const state = getLocaleState();
|
|
121
|
+
const canonical = state.lookup.get(raw.toLowerCase());
|
|
122
|
+
return canonical || '';
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const ROUTE_KEYS = Object.keys(DEFAULT_LOCALE_ROUTES);
|
|
126
|
+
|
|
127
|
+
function normalizeRouteKey(key) {
|
|
128
|
+
if (!key) return '';
|
|
129
|
+
const str = String(key).trim().toLowerCase();
|
|
130
|
+
return ROUTE_KEYS.includes(str) ? str : '';
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function getLocaleRouteConfig(localeCode) {
|
|
134
|
+
const state = getLocaleRoutesState();
|
|
135
|
+
const normalizedLocale = canonicalizeLocaleCode(localeCode) || getDefaultLocaleCode();
|
|
136
|
+
const entry = state.map.get(normalizedLocale);
|
|
137
|
+
if (entry) return {...entry};
|
|
138
|
+
const fallback = state.map.get(getDefaultLocaleCode());
|
|
139
|
+
return fallback ? {...fallback} : {...DEFAULT_LOCALE_ROUTES};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function getLocaleRoute(localeCode, key) {
|
|
143
|
+
const normalizedKey = normalizeRouteKey(key);
|
|
144
|
+
if (!normalizedKey) return DEFAULT_LOCALE_ROUTES.works;
|
|
145
|
+
const config = getLocaleRouteConfig(localeCode);
|
|
146
|
+
return config[normalizedKey] || DEFAULT_LOCALE_ROUTES[normalizedKey];
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function getLocaleRouteEntries(key) {
|
|
150
|
+
const normalizedKey = normalizeRouteKey(key);
|
|
151
|
+
if (!normalizedKey) return [];
|
|
152
|
+
const state = getLocaleRoutesState();
|
|
153
|
+
const entries = [];
|
|
154
|
+
const defaultLocale = getDefaultLocaleCode();
|
|
155
|
+
state.order.forEach((locale) => {
|
|
156
|
+
const routes = state.map.get(locale) || DEFAULT_LOCALE_ROUTES;
|
|
157
|
+
const routeValue = routes[normalizedKey] || DEFAULT_LOCALE_ROUTES[normalizedKey];
|
|
158
|
+
entries.push({
|
|
159
|
+
locale,
|
|
160
|
+
route: routeValue,
|
|
161
|
+
isDefault: locale === defaultLocale,
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
entries.sort((a, b) => {
|
|
165
|
+
if (a.isDefault === b.isDefault) return 0;
|
|
166
|
+
return a.isDefault ? -1 : 1;
|
|
167
|
+
});
|
|
168
|
+
return entries;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function getDefaultRoute(key) {
|
|
172
|
+
return getLocaleRoute(getDefaultLocaleCode(), key);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function buildRouteRelativePath(route, suffix) {
|
|
176
|
+
const base = typeof route === 'string' ? route.trim() : '';
|
|
177
|
+
const cleanedBase = base.replace(/^\/+/, '').replace(/\/+$/, '');
|
|
178
|
+
const cleanedSuffix = suffix ? String(suffix).replace(/^\/+/, '') : '';
|
|
179
|
+
if (cleanedBase && cleanedSuffix) return `${cleanedBase}/${cleanedSuffix}`;
|
|
180
|
+
if (cleanedBase) return cleanedBase;
|
|
181
|
+
if (cleanedSuffix) return cleanedSuffix;
|
|
182
|
+
return '';
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function resolveLocaleFromContentPath(relPath) {
|
|
186
|
+
const defaultLang = getDefaultLocaleCode();
|
|
187
|
+
if (!relPath) return defaultLang;
|
|
188
|
+
let normalized = '';
|
|
189
|
+
try {
|
|
190
|
+
normalized = String(relPath || '');
|
|
191
|
+
} catch (_) {
|
|
192
|
+
normalized = '';
|
|
193
|
+
}
|
|
194
|
+
if (!normalized) return defaultLang;
|
|
195
|
+
normalized = normalized.replace(/\\+/g, '/').replace(/^\/+/, '');
|
|
196
|
+
if (!normalized) return defaultLang;
|
|
197
|
+
const firstSegment = normalized.split('/')[0] || '';
|
|
198
|
+
if (!firstSegment) return defaultLang;
|
|
199
|
+
const canonical = canonicalizeLocaleCode(firstSegment);
|
|
200
|
+
return canonical || defaultLang;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function normalizePathForLocaleDetection(input) {
|
|
204
|
+
if (!input && input !== 0) return '';
|
|
205
|
+
let raw = '';
|
|
206
|
+
try {
|
|
207
|
+
raw = String(input || '');
|
|
208
|
+
} catch (_) {
|
|
209
|
+
raw = '';
|
|
210
|
+
}
|
|
211
|
+
if (!raw) return '';
|
|
212
|
+
try {
|
|
213
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(raw)) {
|
|
214
|
+
const url = new URL(raw);
|
|
215
|
+
raw = url.pathname || '';
|
|
216
|
+
}
|
|
217
|
+
} catch (_) {}
|
|
218
|
+
const hashIndex = raw.indexOf('#');
|
|
219
|
+
if (hashIndex >= 0) raw = raw.slice(0, hashIndex);
|
|
220
|
+
const queryIndex = raw.indexOf('?');
|
|
221
|
+
if (queryIndex >= 0) raw = raw.slice(0, queryIndex);
|
|
222
|
+
raw = raw.replace(/\\+/g, '/');
|
|
223
|
+
if (!raw.startsWith('/')) raw = '/' + raw.replace(/^\/+/, '');
|
|
224
|
+
let base = BASE_PATH || '';
|
|
225
|
+
if (base && !base.startsWith('/')) base = '/' + base;
|
|
226
|
+
base = base.replace(/\/+$/, '');
|
|
227
|
+
if (base && base !== '/') {
|
|
228
|
+
if (raw === base) {
|
|
229
|
+
raw = '/';
|
|
230
|
+
} else if (raw.startsWith(base + '/')) {
|
|
231
|
+
raw = raw.slice(base.length) || '/';
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
raw = raw.replace(/^\/+/, '');
|
|
235
|
+
return raw;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function resolveLocaleFromHref(href) {
|
|
239
|
+
const defaultLang = getDefaultLocaleCode();
|
|
240
|
+
const normalized = normalizePathForLocaleDetection(href);
|
|
241
|
+
if (!normalized) return defaultLang;
|
|
242
|
+
const firstSegment = normalized.split('/')[0] || '';
|
|
243
|
+
if (!firstSegment) return defaultLang;
|
|
244
|
+
const canonical = canonicalizeLocaleCode(firstSegment);
|
|
245
|
+
return canonical || defaultLang;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// `light`, `dark`, or `auto` (the visitor's system setting, and a switch in the footer).
|
|
249
|
+
function resolveThemeAppearance() {
|
|
250
|
+
if (cachedAppearance) return cachedAppearance;
|
|
251
|
+
cachedAppearance = 'light';
|
|
252
|
+
try {
|
|
253
|
+
const { loadCanopyTheme } = require('../ui/theme');
|
|
254
|
+
if (typeof loadCanopyTheme === 'function') {
|
|
255
|
+
const theme = loadCanopyTheme();
|
|
256
|
+
const appearance = theme && theme.appearance ? String(theme.appearance).toLowerCase() : '';
|
|
257
|
+
if (appearance === 'dark' || appearance === 'auto') {
|
|
258
|
+
cachedAppearance = appearance;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
} catch (_) {}
|
|
262
|
+
return cachedAppearance;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function resolveThemeAccent() {
|
|
266
|
+
if (cachedAccent) return cachedAccent;
|
|
267
|
+
cachedAccent = 'indigo';
|
|
268
|
+
try {
|
|
269
|
+
const { loadCanopyTheme } = require('../ui/theme');
|
|
270
|
+
if (typeof loadCanopyTheme === 'function') {
|
|
271
|
+
const theme = loadCanopyTheme();
|
|
272
|
+
const accent = theme && theme.accent && theme.accent.name ? String(theme.accent.name) : '';
|
|
273
|
+
const normalized = accent.trim().toLowerCase();
|
|
274
|
+
if (normalized) cachedAccent = normalized;
|
|
275
|
+
}
|
|
276
|
+
} catch (_) {}
|
|
277
|
+
return cachedAccent;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function readYamlConfigBaseUrl() {
|
|
281
|
+
try {
|
|
282
|
+
const p = resolveCanopyConfigPath();
|
|
283
|
+
if (!fs.existsSync(p)) return '';
|
|
284
|
+
const raw = fs.readFileSync(p, 'utf8');
|
|
285
|
+
const data = yaml.load(raw) || {};
|
|
286
|
+
const site = data && data.site;
|
|
287
|
+
const url = site && site.baseUrl ? String(site.baseUrl) : '';
|
|
288
|
+
return url;
|
|
289
|
+
} catch (_) { return ''; }
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// The site's title and appearance, which components read from the page context
|
|
293
|
+
// (`CanopyFooter` shows the appearance switch where the theme is `auto`).
|
|
294
|
+
function readSiteMetadata() {
|
|
295
|
+
if (cachedSiteMetadata) return cachedSiteMetadata;
|
|
296
|
+
const appearance = resolveThemeAppearance();
|
|
297
|
+
cachedSiteMetadata = { title: DEFAULT_SITE_TITLE, appearance };
|
|
298
|
+
try {
|
|
299
|
+
const cfgPath = resolveCanopyConfigPath();
|
|
300
|
+
if (!fs.existsSync(cfgPath)) return cachedSiteMetadata;
|
|
301
|
+
const raw = fs.readFileSync(cfgPath, 'utf8');
|
|
302
|
+
const data = yaml.load(raw) || {};
|
|
303
|
+
const directTitle = data && typeof data.title === 'string' ? data.title.trim() : '';
|
|
304
|
+
const nestedTitle =
|
|
305
|
+
data && data.site && typeof data.site.title === 'string'
|
|
306
|
+
? data.site.title.trim()
|
|
307
|
+
: '';
|
|
308
|
+
const resolved = directTitle || nestedTitle || DEFAULT_SITE_TITLE;
|
|
309
|
+
cachedSiteMetadata = { title: resolved, appearance };
|
|
310
|
+
} catch (_) {}
|
|
311
|
+
return cachedSiteMetadata;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
function getSiteTitle() {
|
|
315
|
+
const site = readSiteMetadata();
|
|
316
|
+
if (site && typeof site.title === 'string' && site.title.trim()) {
|
|
317
|
+
return site.title.trim();
|
|
318
|
+
}
|
|
319
|
+
return DEFAULT_SITE_TITLE;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function readSearchPageMetadata() {
|
|
323
|
+
if (cachedSearchPageMetadata) return cachedSearchPageMetadata;
|
|
324
|
+
cachedSearchPageMetadata = {
|
|
325
|
+
title: DEFAULT_SEARCH_PAGE_TITLE,
|
|
326
|
+
description: DEFAULT_SEARCH_PAGE_DESCRIPTION,
|
|
327
|
+
};
|
|
328
|
+
try {
|
|
329
|
+
const cfgPath = resolveCanopyConfigPath();
|
|
330
|
+
if (!fs.existsSync(cfgPath)) return cachedSearchPageMetadata;
|
|
331
|
+
const raw = fs.readFileSync(cfgPath, 'utf8');
|
|
332
|
+
const data = yaml.load(raw) || {};
|
|
333
|
+
const searchCfg = data && data.search ? data.search : null;
|
|
334
|
+
const pageCfg = searchCfg && searchCfg.page ? searchCfg.page : null;
|
|
335
|
+
const title = pageCfg && typeof pageCfg.title === 'string' ? pageCfg.title.trim() : '';
|
|
336
|
+
const description =
|
|
337
|
+
pageCfg && typeof pageCfg.description === 'string'
|
|
338
|
+
? pageCfg.description.trim()
|
|
339
|
+
: '';
|
|
340
|
+
cachedSearchPageMetadata = {
|
|
341
|
+
title: title || DEFAULT_SEARCH_PAGE_TITLE,
|
|
342
|
+
description: description || DEFAULT_SEARCH_PAGE_DESCRIPTION,
|
|
343
|
+
};
|
|
344
|
+
} catch (_) {}
|
|
345
|
+
return cachedSearchPageMetadata;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const PRIMARY_NAV_CACHE = new Map();
|
|
349
|
+
|
|
350
|
+
function loadNavigationEntries(navPath) {
|
|
351
|
+
if (!navPath) return {entries: [], found: false};
|
|
352
|
+
try {
|
|
353
|
+
if (!fs.existsSync(navPath)) return {entries: [], found: false};
|
|
354
|
+
const raw = fs.readFileSync(navPath, 'utf8');
|
|
355
|
+
const data = yaml.load(raw) || {};
|
|
356
|
+
const entries = Array.isArray(data.navigation)
|
|
357
|
+
? data.navigation.filter(
|
|
358
|
+
(item) => item && typeof item === 'object' && typeof item.href === 'string',
|
|
359
|
+
)
|
|
360
|
+
: [];
|
|
361
|
+
return {entries, found: true};
|
|
362
|
+
} catch (_) {
|
|
363
|
+
return {entries: [], found: true};
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
function readPrimaryNavigation(localeCode) {
|
|
368
|
+
const normalized = canonicalizeLocaleCode(localeCode);
|
|
369
|
+
const cacheKey = normalized || '__default__';
|
|
370
|
+
if (PRIMARY_NAV_CACHE.has(cacheKey)) {
|
|
371
|
+
return PRIMARY_NAV_CACHE.get(cacheKey);
|
|
372
|
+
}
|
|
373
|
+
let resolved = [];
|
|
374
|
+
const localeNavPath = normalized
|
|
375
|
+
? path.resolve(CONTENT_DIR, normalized, 'navigation.yml')
|
|
376
|
+
: null;
|
|
377
|
+
const localeData = localeNavPath ? loadNavigationEntries(localeNavPath) : {entries: [], found: false};
|
|
378
|
+
if (localeData.found) {
|
|
379
|
+
resolved = localeData.entries;
|
|
380
|
+
} else {
|
|
381
|
+
const defaultData = loadNavigationEntries(path.resolve(CONTENT_DIR, 'navigation.yml'));
|
|
382
|
+
resolved = defaultData.entries;
|
|
383
|
+
}
|
|
384
|
+
PRIMARY_NAV_CACHE.set(cacheKey, resolved);
|
|
385
|
+
return resolved;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// Determine the absolute site origin (scheme + host[:port])
|
|
389
|
+
// Priority:
|
|
390
|
+
// 1) CANOPY_BASE_URL env
|
|
391
|
+
// 2) canopy.yml → site.baseUrl
|
|
392
|
+
// 3) dev server default http://localhost:PORT (PORT env or 5001)
|
|
393
|
+
const BASE_ORIGIN = (() => {
|
|
394
|
+
const env = String(process.env.CANOPY_BASE_URL || '').trim();
|
|
395
|
+
if (env) return env.replace(/\/$/, '');
|
|
396
|
+
const cfg = readYamlConfigBaseUrl();
|
|
397
|
+
if (cfg) return cfg.replace(/\/$/, '');
|
|
398
|
+
const port = Number(process.env.PORT || 5001);
|
|
399
|
+
return `http://localhost:${port}`;
|
|
400
|
+
})();
|
|
401
|
+
|
|
402
|
+
function ensureDirSync(dir) {
|
|
403
|
+
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
async function cleanDir(dir) {
|
|
407
|
+
if (fs.existsSync(dir)) {
|
|
408
|
+
await fsp.rm(dir, { recursive: true, force: true });
|
|
409
|
+
}
|
|
410
|
+
await fsp.mkdir(dir, { recursive: true });
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
function normalizeClassList(value) {
|
|
414
|
+
if (!value) return '';
|
|
415
|
+
const list = Array.isArray(value) ? value : [value];
|
|
416
|
+
const classes = [];
|
|
417
|
+
for (const entry of list) {
|
|
418
|
+
if (!entry && entry !== 0) continue;
|
|
419
|
+
const raw = String(entry)
|
|
420
|
+
.split(/\s+/)
|
|
421
|
+
.map((segment) => segment.trim())
|
|
422
|
+
.filter(Boolean);
|
|
423
|
+
classes.push(...raw);
|
|
424
|
+
}
|
|
425
|
+
const unique = classes.filter(Boolean);
|
|
426
|
+
return unique.length ? unique.join(' ') : '';
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
function canopyBodyClassForType(type) {
|
|
430
|
+
const raw = String(type == null || type === '' ? 'page' : type)
|
|
431
|
+
.trim()
|
|
432
|
+
.toLowerCase();
|
|
433
|
+
const slug = raw
|
|
434
|
+
.replace(/[^a-z0-9_-]+/g, '-')
|
|
435
|
+
.replace(/^-+/, '')
|
|
436
|
+
.replace(/-+$/, '');
|
|
437
|
+
if (!slug) return 'canopy-type-page';
|
|
438
|
+
return `canopy-type-${slug}`;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
function htmlShell({ title, body, cssHref, scriptHref, headExtra, bodyClass, lang }) {
|
|
442
|
+
const scriptTag = scriptHref ? `<script defer src="${scriptHref}"></script>` : '';
|
|
443
|
+
const extra = (headExtra ? String(headExtra) : '') +
|
|
444
|
+
require('./discovery/config').webMcpScriptTag(withBase);
|
|
445
|
+
const cssTag = cssHref ? `<link rel="stylesheet" href="${cssHref}">` : '';
|
|
446
|
+
const appearance = resolveThemeAppearance();
|
|
447
|
+
const accent = resolveThemeAccent();
|
|
448
|
+
const htmlAttrs = [];
|
|
449
|
+
if (appearance === 'dark') htmlAttrs.push('class="dark"');
|
|
450
|
+
if (appearance === 'auto') htmlAttrs.push('data-appearance="auto"');
|
|
451
|
+
htmlAttrs.push(`data-accent="${accent || 'indigo'}"`);
|
|
452
|
+
const htmlAttr = htmlAttrs.length ? ` ${htmlAttrs.join(' ')}` : '';
|
|
453
|
+
// `appearance: auto`: inline and ahead of the stylesheet, so the first paint is in the
|
|
454
|
+
// visitor's appearance (lib/build/appearance.js).
|
|
455
|
+
const appearanceTag =
|
|
456
|
+
appearance === 'auto'
|
|
457
|
+
? `<script>${require('./build/appearance').appearanceScript(withBase('/'))}</script>`
|
|
458
|
+
: '';
|
|
459
|
+
const hasCustomTitle = /<title\b/i.test(extra);
|
|
460
|
+
const titleTag = hasCustomTitle ? '' : `<title>${title}</title>`;
|
|
461
|
+
const bodyClassName = normalizeClassList(bodyClass);
|
|
462
|
+
const bodyAttr = bodyClassName ? ` class="${bodyClassName}"` : '';
|
|
463
|
+
const htmlLang = typeof lang === 'string' && lang.trim() ? lang.trim() : getDefaultLocaleCode();
|
|
464
|
+
return `<!doctype html><html lang="${htmlLang}"${htmlAttr}><head><meta charset="utf-8"/><meta name="viewport" content="width=device-width, initial-scale=1"/>${titleTag}${appearanceTag}${extra}${cssTag}${scriptTag}</head><body${bodyAttr}>${body}</body></html>`;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
function withBase(href) {
|
|
468
|
+
return withBasePath(href);
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
function rootRelativeHref(href) {
|
|
472
|
+
try {
|
|
473
|
+
let raw = href == null ? '' : String(href);
|
|
474
|
+
raw = raw.trim();
|
|
475
|
+
if (!raw) return '/';
|
|
476
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(raw)) return raw;
|
|
477
|
+
if (raw.startsWith('//')) return raw;
|
|
478
|
+
if (raw.startsWith('#') || raw.startsWith('?')) return raw;
|
|
479
|
+
let cleaned = raw;
|
|
480
|
+
if (cleaned.startsWith('/')) cleaned = cleaned.replace(/^\/+/, '');
|
|
481
|
+
while (cleaned.startsWith('./')) cleaned = cleaned.slice(2);
|
|
482
|
+
while (cleaned.startsWith('../')) cleaned = cleaned.slice(3);
|
|
483
|
+
if (!cleaned) return '/';
|
|
484
|
+
return '/' + cleaned;
|
|
485
|
+
} catch (_) {
|
|
486
|
+
return href;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
// Convert a site-relative path (e.g., "/api/foo.json") to an absolute URL.
|
|
491
|
+
// Handles either:
|
|
492
|
+
// - BASE_ORIGIN that may already include a path prefix (e.g., https://host/org/repo)
|
|
493
|
+
// - BASE_PATH (path prefix) when BASE_ORIGIN has no path
|
|
494
|
+
// If input is already absolute (http/https), returns as-is.
|
|
495
|
+
function absoluteUrl(p) {
|
|
496
|
+
try {
|
|
497
|
+
const raw = String(p || '');
|
|
498
|
+
if (/^https?:\/\//i.test(raw)) return raw;
|
|
499
|
+
const rel = raw.startsWith('/') ? raw : '/' + raw.replace(/^\/?/, '');
|
|
500
|
+
// Parse BASE_ORIGIN; it may include a path (e.g., GH Pages repo path)
|
|
501
|
+
let originBase = '';
|
|
502
|
+
let originPath = '';
|
|
503
|
+
try {
|
|
504
|
+
const u = new URL(BASE_ORIGIN);
|
|
505
|
+
originBase = u.origin.replace(/\/$/, '');
|
|
506
|
+
originPath = (u.pathname || '').replace(/\/$/, '');
|
|
507
|
+
} catch (_) {
|
|
508
|
+
originBase = String(BASE_ORIGIN || '').replace(/\/$/, '');
|
|
509
|
+
originPath = '';
|
|
510
|
+
}
|
|
511
|
+
// Prefer path from BASE_ORIGIN; if absent, fall back to BASE_PATH
|
|
512
|
+
let prefixPath = originPath || String(BASE_PATH || '');
|
|
513
|
+
prefixPath = prefixPath.replace(/\/$/, '');
|
|
514
|
+
const fullPath = (prefixPath ? prefixPath : '') + rel; // rel already has leading '/'
|
|
515
|
+
return originBase + fullPath;
|
|
516
|
+
} catch (_) {
|
|
517
|
+
return p;
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
// Apply BASE_PATH to key URL-bearing attributes (href/src/action/formaction) in an HTML string.
|
|
522
|
+
function applyBaseToHtml(html) {
|
|
523
|
+
if (!BASE_PATH) return html;
|
|
524
|
+
try {
|
|
525
|
+
const out = String(html || '');
|
|
526
|
+
const baseRaw = BASE_PATH.startsWith('/') ? BASE_PATH : `/${BASE_PATH}`;
|
|
527
|
+
const normalizedBase = baseRaw.replace(/\/$/, '');
|
|
528
|
+
if (!normalizedBase || normalizedBase === '/') return out;
|
|
529
|
+
|
|
530
|
+
const attrPattern = '(?:href|src|action|formaction)';
|
|
531
|
+
const pathPattern = "\\/(?!\\/)[^'\"\\s<]*";
|
|
532
|
+
const pattern = new RegExp(`(${attrPattern})=(["'])((${pathPattern}))\\2`, 'g');
|
|
533
|
+
|
|
534
|
+
return out.replace(pattern, (match, attr, quote, path) => {
|
|
535
|
+
if (path === normalizedBase || path.startsWith(`${normalizedBase}/`)) {
|
|
536
|
+
return match;
|
|
537
|
+
}
|
|
538
|
+
return `${attr}=${quote}${normalizedBase}${path}${quote}`;
|
|
539
|
+
});
|
|
540
|
+
} catch (_) {
|
|
541
|
+
return html;
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
module.exports = {
|
|
546
|
+
fs,
|
|
547
|
+
fsp,
|
|
548
|
+
path,
|
|
549
|
+
CONTENT_DIR,
|
|
550
|
+
OUT_DIR,
|
|
551
|
+
CACHE_DIR,
|
|
552
|
+
ASSETS_DIR,
|
|
553
|
+
BASE_PATH,
|
|
554
|
+
ensureDirSync,
|
|
555
|
+
cleanDir,
|
|
556
|
+
htmlShell,
|
|
557
|
+
withBase,
|
|
558
|
+
BASE_ORIGIN,
|
|
559
|
+
absoluteUrl,
|
|
560
|
+
applyBaseToHtml,
|
|
561
|
+
rootRelativeHref,
|
|
562
|
+
canopyBodyClassForType,
|
|
563
|
+
readSiteMetadata,
|
|
564
|
+
getSiteTitle,
|
|
565
|
+
DEFAULT_SITE_TITLE,
|
|
566
|
+
readSearchPageMetadata,
|
|
567
|
+
DEFAULT_SEARCH_PAGE_TITLE,
|
|
568
|
+
DEFAULT_SEARCH_PAGE_DESCRIPTION,
|
|
569
|
+
readPrimaryNavigation,
|
|
570
|
+
getSiteLocales,
|
|
571
|
+
getDefaultLocaleCode,
|
|
572
|
+
canonicalizeLocaleCode,
|
|
573
|
+
resolveLocaleFromContentPath,
|
|
574
|
+
resolveLocaleFromHref,
|
|
575
|
+
getLocaleRouteConfig,
|
|
576
|
+
getLocaleRoute,
|
|
577
|
+
getLocaleRouteEntries,
|
|
578
|
+
getDefaultRoute,
|
|
579
|
+
buildRouteRelativePath,
|
|
580
|
+
};
|