@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,141 @@
|
|
|
1
|
+
const { logLine } = require('./log');
|
|
2
|
+
const { fs, path, OUT_DIR, ensureDirSync } = require('../common');
|
|
3
|
+
|
|
4
|
+
async function prepareAllRuntimes() {
|
|
5
|
+
const mdx = require('./mdx');
|
|
6
|
+
const runtimes = [
|
|
7
|
+
['Clover hydration', mdx.ensureClientRuntime],
|
|
8
|
+
// Page renderers may catch this failure; check the cached result before succeeding.
|
|
9
|
+
['Custom client component', mdx.ensureCustomClientRuntime],
|
|
10
|
+
['Timeline', mdx.ensureTimelineRuntime],
|
|
11
|
+
['Map', mdx.ensureMapRuntime],
|
|
12
|
+
['Hero', mdx.ensureHeroRuntime],
|
|
13
|
+
['RelatedItems', mdx.ensureFacetsRuntime],
|
|
14
|
+
['React globals', mdx.ensureReactGlobals],
|
|
15
|
+
];
|
|
16
|
+
for (const [label, ensureRuntime] of runtimes) {
|
|
17
|
+
try {
|
|
18
|
+
await ensureRuntime();
|
|
19
|
+
} catch (error) {
|
|
20
|
+
throw new Error(`[canopy] ${label} runtime failed to build: ${error.message || error}`, {
|
|
21
|
+
cause: error,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
await prepareSearchFormRuntime();
|
|
26
|
+
await prepareSearchEngineRuntime();
|
|
27
|
+
await prepareDiscoveryRuntime();
|
|
28
|
+
try { logLine('✓ Prepared client hydration runtimes', 'cyan', { dim: true }); } catch (_) {}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async function resolveEsbuild() {
|
|
32
|
+
try { return require('../../ui/node_modules/esbuild'); } catch (_) {
|
|
33
|
+
try { return require('esbuild'); } catch (_) {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async function prepareSearchFormRuntime() {
|
|
40
|
+
const esbuild = await resolveEsbuild();
|
|
41
|
+
if (!esbuild) throw new Error('Search form runtime bundling requires esbuild. Install dependencies before building.');
|
|
42
|
+
ensureDirSync(OUT_DIR);
|
|
43
|
+
const scriptsDir = path.join(OUT_DIR, 'scripts');
|
|
44
|
+
ensureDirSync(scriptsDir);
|
|
45
|
+
const entry = path.join(__dirname, '..', 'search', 'search-form-runtime.js');
|
|
46
|
+
const outFile = path.join(scriptsDir, 'canopy-search-form.js');
|
|
47
|
+
await esbuild.build({
|
|
48
|
+
entryPoints: [entry],
|
|
49
|
+
outfile: outFile,
|
|
50
|
+
platform: 'browser',
|
|
51
|
+
format: 'iife',
|
|
52
|
+
bundle: true,
|
|
53
|
+
sourcemap: false,
|
|
54
|
+
target: ['es2020'],
|
|
55
|
+
logLevel: 'silent',
|
|
56
|
+
minify: true,
|
|
57
|
+
});
|
|
58
|
+
try {
|
|
59
|
+
let size = 0;
|
|
60
|
+
try { const st = fs.statSync(outFile); size = st.size || 0; } catch (_) {}
|
|
61
|
+
const kb = size ? ` (${(size / 1024).toFixed(1)} KB)` : '';
|
|
62
|
+
const rel = path.relative(process.cwd(), outFile).split(path.sep).join('/');
|
|
63
|
+
logLine(`✓ Wrote ${rel}${kb}`, 'cyan');
|
|
64
|
+
} catch (_) {}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// The header's suggestions load this the first time a visitor uses the field
|
|
68
|
+
// (`search/search-engine-runtime.js`): FlexSearch's compact build and the index.
|
|
69
|
+
async function prepareSearchEngineRuntime() {
|
|
70
|
+
const esbuild = await resolveEsbuild();
|
|
71
|
+
if (!esbuild) throw new Error('Search engine runtime bundling requires esbuild. Install dependencies before building.');
|
|
72
|
+
const { flexSearchPlugin } = require('../search/bundle');
|
|
73
|
+
const scriptsDir = path.join(OUT_DIR, 'scripts');
|
|
74
|
+
ensureDirSync(scriptsDir);
|
|
75
|
+
const outFile = path.join(scriptsDir, 'canopy-search-engine.js');
|
|
76
|
+
await esbuild.build({
|
|
77
|
+
entryPoints: [path.join(__dirname, '..', 'search', 'search-engine-runtime.js')],
|
|
78
|
+
outfile: outFile,
|
|
79
|
+
platform: 'browser',
|
|
80
|
+
format: 'iife',
|
|
81
|
+
bundle: true,
|
|
82
|
+
sourcemap: false,
|
|
83
|
+
target: ['es2020'],
|
|
84
|
+
logLevel: 'silent',
|
|
85
|
+
minify: true,
|
|
86
|
+
plugins: [flexSearchPlugin()],
|
|
87
|
+
});
|
|
88
|
+
try {
|
|
89
|
+
let size = 0;
|
|
90
|
+
try { size = fs.statSync(outFile).size || 0; } catch (_) {}
|
|
91
|
+
const kb = size ? ` (${(size / 1024).toFixed(1)} KB)` : '';
|
|
92
|
+
const rel = path.relative(process.cwd(), outFile).split(path.sep).join('/');
|
|
93
|
+
logLine(`✓ Wrote ${rel}${kb}`, 'cyan');
|
|
94
|
+
} catch (_) {}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async function prepareDiscoveryRuntime() {
|
|
98
|
+
const { readWebMcpEnabled } = require('../discovery/config');
|
|
99
|
+
const outFile = path.join(OUT_DIR, 'scripts', 'canopy-webmcp.js');
|
|
100
|
+
if (!readWebMcpEnabled()) {
|
|
101
|
+
await fs.promises.rm(outFile, { force: true });
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
const esbuild = await resolveEsbuild();
|
|
105
|
+
if (!esbuild) throw new Error('WebMCP runtime bundling requires esbuild.');
|
|
106
|
+
ensureDirSync(path.dirname(outFile));
|
|
107
|
+
await esbuild.build({
|
|
108
|
+
entryPoints: [path.join(__dirname, '..', 'discovery', 'browser.js')],
|
|
109
|
+
outfile: outFile,
|
|
110
|
+
platform: 'browser',
|
|
111
|
+
format: 'iife',
|
|
112
|
+
bundle: true,
|
|
113
|
+
target: ['es2020'],
|
|
114
|
+
logLevel: 'silent',
|
|
115
|
+
minify: true,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async function prepareSearchRuntime(timeoutMs = 10000, label = '') {
|
|
120
|
+
const search = require('../search/search');
|
|
121
|
+
try { logLine(`• Writing search runtime${label ? ' (' + label + ')' : ''}...`, 'blue', { bright: true }); } catch (_) {}
|
|
122
|
+
|
|
123
|
+
let timedOut = false;
|
|
124
|
+
await Promise.race([
|
|
125
|
+
search.ensureSearchRuntime(),
|
|
126
|
+
new Promise((_, reject) => setTimeout(() => { timedOut = true; reject(new Error('timeout')); }, Number(timeoutMs)))
|
|
127
|
+
]).catch(() => {
|
|
128
|
+
try { console.warn(`Search: Bundling runtime timed out${label ? ' (' + label + ')' : ''}, skipping`); } catch (_) {}
|
|
129
|
+
});
|
|
130
|
+
if (timedOut) {
|
|
131
|
+
try { logLine(`! Search runtime not bundled${label ? ' (' + label + ')' : ''}\n`, 'yellow'); } catch (_) {}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
module.exports = {
|
|
136
|
+
prepareAllRuntimes,
|
|
137
|
+
prepareSearchFormRuntime,
|
|
138
|
+
prepareSearchEngineRuntime,
|
|
139
|
+
prepareSearchRuntime,
|
|
140
|
+
prepareDiscoveryRuntime,
|
|
141
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
|
|
2
|
+
const { logLine } = require('./log');
|
|
3
|
+
const { rootRelativeHref, buildRouteRelativePath, getDefaultRoute } = require('../common');
|
|
4
|
+
|
|
5
|
+
function pagesToRecords(pageRecords) {
|
|
6
|
+
const list = Array.isArray(pageRecords) ? pageRecords : [];
|
|
7
|
+
return list
|
|
8
|
+
.filter((p) => p && p.href && p.searchInclude)
|
|
9
|
+
.map((p) => {
|
|
10
|
+
const summary = typeof p.searchSummary === 'string' ? p.searchSummary.trim() : '';
|
|
11
|
+
const summaryMarkdown =
|
|
12
|
+
typeof p.searchSummaryMarkdown === 'string'
|
|
13
|
+
? p.searchSummaryMarkdown.trim()
|
|
14
|
+
: '';
|
|
15
|
+
const record = {
|
|
16
|
+
title: p.title || p.href,
|
|
17
|
+
href: rootRelativeHref(p.href),
|
|
18
|
+
type: p.searchType || 'page',
|
|
19
|
+
};
|
|
20
|
+
if (summary) record.summaryValue = summary;
|
|
21
|
+
if (summaryMarkdown) record.summaryMarkdown = summaryMarkdown;
|
|
22
|
+
if (typeof p.thumbnail === 'string' && p.thumbnail.trim()) record.thumbnail = p.thumbnail.trim();
|
|
23
|
+
return record;
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function maybeMockRecords() {
|
|
28
|
+
if (process.env.CANOPY_MOCK_SEARCH !== '1') return null;
|
|
29
|
+
const mock = [];
|
|
30
|
+
const svg = encodeURIComponent('<svg xmlns="http://www.w3.org/2000/svg" width="400" height="300"><rect width="400" height="300" fill="#dbeafe"/><text x="50%" y="50%" dominant-baseline="middle" text-anchor="middle" font-family="Arial" font-size="24" fill="#1d4ed8">Mock</text></svg>');
|
|
31
|
+
const thumb = `data:image/svg+xml;charset=utf-8,${svg}`;
|
|
32
|
+
for (let i = 1; i <= 120; i++) {
|
|
33
|
+
const rel = buildRouteRelativePath(getDefaultRoute('works'), `mock-${i}.html`);
|
|
34
|
+
mock.push({ title: `Mock Work #${i}`, href: rootRelativeHref(rel), type: 'work', thumbnail: thumb });
|
|
35
|
+
}
|
|
36
|
+
mock.push({ title: 'Mock Doc A', href: rootRelativeHref('getting-started/index.html'), type: 'docs' });
|
|
37
|
+
mock.push({ title: 'Mock Doc B', href: rootRelativeHref('getting-started/example.html'), type: 'docs' });
|
|
38
|
+
mock.push({ title: 'Mock Page', href: rootRelativeHref('index.html'), type: 'page' });
|
|
39
|
+
return mock;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async function buildSearchIndex(iiifRecords, pageRecords) {
|
|
43
|
+
const search = require('../search/search');
|
|
44
|
+
const iiif = Array.isArray(iiifRecords) ? iiifRecords : [];
|
|
45
|
+
const mdx = pagesToRecords(pageRecords);
|
|
46
|
+
let combined = [...iiif, ...mdx];
|
|
47
|
+
const mock = maybeMockRecords();
|
|
48
|
+
if (mock) combined = mock;
|
|
49
|
+
try { logLine(`• Building search index (${combined.length})...`, 'blue'); } catch (_) {}
|
|
50
|
+
await search.writeSearchIndex(combined);
|
|
51
|
+
try { logLine('✓ Search index built', 'cyan'); } catch (_) {}
|
|
52
|
+
return combined;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
module.exports = { buildSearchIndex };
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
// The words of an MDX page for search (`build/search.js`): the text it is found by, and
|
|
2
|
+
// the inline Markdown a search result's excerpt is drawn from (`ArticleCard`). Read with
|
|
3
|
+
// the MDX parser and GFM, as the page itself is compiled, so that prose is told apart from
|
|
4
|
+
// code, components, expressions and tables:
|
|
5
|
+
// - kept: headings (in the text only), paragraphs, list items, quotes, a table's cells
|
|
6
|
+
// but for its header row, and what a component holds between its tags;
|
|
7
|
+
// - dropped: code blocks, imports and exports, `{expressions}`, a component's attributes,
|
|
8
|
+
// HTML, images and footnote marks; a link keeps its words, not its address.
|
|
9
|
+
// In the excerpt's Markdown, inline code, emphasis and strong stay; the characters the
|
|
10
|
+
// card reads as Markdown (`*`, `_`, `` ` ``, `\`) are escaped wherever they are text.
|
|
11
|
+
//
|
|
12
|
+
// The parser is ESM, which Node 24 requires happily and Jest 29 cannot; without it, or on a
|
|
13
|
+
// page it cannot parse, `extractSearchText` falls back to `mdx.js`'s patterns.
|
|
14
|
+
|
|
15
|
+
'use strict';
|
|
16
|
+
|
|
17
|
+
let parser;
|
|
18
|
+
|
|
19
|
+
function loadParser() {
|
|
20
|
+
if (parser !== undefined) return parser;
|
|
21
|
+
try {
|
|
22
|
+
const { fromMarkdown } = require('mdast-util-from-markdown');
|
|
23
|
+
const { mdxjs } = require('micromark-extension-mdxjs');
|
|
24
|
+
const { mdxFromMarkdown } = require('mdast-util-mdx');
|
|
25
|
+
const { gfm } = require('micromark-extension-gfm');
|
|
26
|
+
const { gfmFromMarkdown } = require('mdast-util-gfm');
|
|
27
|
+
parser = (source) =>
|
|
28
|
+
fromMarkdown(source, {
|
|
29
|
+
extensions: [mdxjs(), gfm()],
|
|
30
|
+
mdastExtensions: [mdxFromMarkdown(), gfmFromMarkdown()],
|
|
31
|
+
});
|
|
32
|
+
} catch (_) {
|
|
33
|
+
parser = null;
|
|
34
|
+
}
|
|
35
|
+
return parser;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const SKIP = new Set([
|
|
39
|
+
'code',
|
|
40
|
+
'definition',
|
|
41
|
+
'footnoteReference',
|
|
42
|
+
'html',
|
|
43
|
+
'image',
|
|
44
|
+
'imageReference',
|
|
45
|
+
'mdxFlowExpression',
|
|
46
|
+
'mdxTextExpression',
|
|
47
|
+
'mdxjsEsm',
|
|
48
|
+
'thematicBreak',
|
|
49
|
+
'toml',
|
|
50
|
+
'yaml',
|
|
51
|
+
]);
|
|
52
|
+
|
|
53
|
+
const INLINE = new Set([
|
|
54
|
+
'break',
|
|
55
|
+
'delete',
|
|
56
|
+
'emphasis',
|
|
57
|
+
'inlineCode',
|
|
58
|
+
'link',
|
|
59
|
+
'linkReference',
|
|
60
|
+
'mdxJsxTextElement',
|
|
61
|
+
'strong',
|
|
62
|
+
'text',
|
|
63
|
+
]);
|
|
64
|
+
|
|
65
|
+
function escapeMarkdown(value) {
|
|
66
|
+
return String(value || '').replace(/[\\*_`]/g, (char) => `\\${char}`);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// A node's inline content, as plain text or as the excerpt's Markdown.
|
|
70
|
+
function inline(node, markdown) {
|
|
71
|
+
if (!node || SKIP.has(node.type)) return '';
|
|
72
|
+
switch (node.type) {
|
|
73
|
+
case 'text':
|
|
74
|
+
return markdown ? escapeMarkdown(node.value) : String(node.value || '');
|
|
75
|
+
case 'inlineCode': {
|
|
76
|
+
const value = String(node.value || '');
|
|
77
|
+
return markdown ? `\`${value.replace(/`/g, '')}\`` : value;
|
|
78
|
+
}
|
|
79
|
+
case 'emphasis':
|
|
80
|
+
case 'strong': {
|
|
81
|
+
const inner = children(node, markdown);
|
|
82
|
+
if (!markdown || !inner.trim()) return inner;
|
|
83
|
+
const mark = node.type === 'strong' ? '**' : '*';
|
|
84
|
+
return `${mark}${inner}${mark}`;
|
|
85
|
+
}
|
|
86
|
+
case 'break':
|
|
87
|
+
return ' ';
|
|
88
|
+
default:
|
|
89
|
+
return children(node, markdown);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function children(node, markdown) {
|
|
94
|
+
return (node.children || []).map((child) => inline(child, markdown)).join('');
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const tidy = (value) => value.replace(/\s+/g, ' ').trim();
|
|
98
|
+
|
|
99
|
+
// The page's blocks of prose, in order, each as text and as Markdown.
|
|
100
|
+
function collectBlocks(tree) {
|
|
101
|
+
const blocks = [];
|
|
102
|
+
const push = (kind, text, markdown) => {
|
|
103
|
+
const plain = tidy(text);
|
|
104
|
+
if (plain) blocks.push({ kind, text: plain, markdown: tidy(markdown) });
|
|
105
|
+
};
|
|
106
|
+
(function walk(node) {
|
|
107
|
+
if (!node || SKIP.has(node.type)) return;
|
|
108
|
+
if (node.type === 'heading' || node.type === 'paragraph') {
|
|
109
|
+
push(node.type, inline(node, false), inline(node, true));
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
if (node.type === 'table') {
|
|
113
|
+
// Its first row is the header, the columns' names: words of the table, not the page.
|
|
114
|
+
(node.children || []).slice(1).forEach(walk);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
if (node.type === 'tableRow') {
|
|
118
|
+
const cells = (node.children || [])
|
|
119
|
+
.map((cell) => ({ text: tidy(inline(cell, false)), markdown: tidy(inline(cell, true)) }))
|
|
120
|
+
.filter((cell) => cell.text);
|
|
121
|
+
push(
|
|
122
|
+
'row',
|
|
123
|
+
cells.map((cell) => cell.text).join(' · '),
|
|
124
|
+
cells.map((cell) => cell.markdown).join(' · '),
|
|
125
|
+
);
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
const kids = node.children || [];
|
|
129
|
+
// A component written on one line holds its words directly, not in a paragraph.
|
|
130
|
+
if (node.type === 'mdxJsxFlowElement' && kids.length && kids.every((kid) => INLINE.has(kid.type))) {
|
|
131
|
+
push('paragraph', children(node, false), children(node, true));
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
kids.forEach(walk);
|
|
135
|
+
})(tree);
|
|
136
|
+
return blocks;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Resolves a page's MDX (front matter already removed) to { text, excerpt, markdown }: all
|
|
140
|
+
// its words, its prose without its headings, and that prose as the excerpt's Markdown; or
|
|
141
|
+
// null when the parser is unavailable or the page does not parse.
|
|
142
|
+
function searchTextFromMdx(content) {
|
|
143
|
+
const parse = loadParser();
|
|
144
|
+
if (!parse) return null;
|
|
145
|
+
let tree;
|
|
146
|
+
try {
|
|
147
|
+
tree = parse(String(content || ''));
|
|
148
|
+
} catch (_) {
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
const blocks = collectBlocks(tree);
|
|
152
|
+
const prose = blocks.filter((block) => block.kind !== 'heading');
|
|
153
|
+
return {
|
|
154
|
+
text: blocks.map((block) => block.text).join(' '),
|
|
155
|
+
excerpt: prose.map((block) => block.text).join(' '),
|
|
156
|
+
markdown: prose.map((block) => block.markdown).join(' '),
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
module.exports = { searchTextFromMdx, escapeMarkdown };
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
const {
|
|
2
|
+
fs,
|
|
3
|
+
path,
|
|
4
|
+
OUT_DIR,
|
|
5
|
+
getLocaleRouteEntries,
|
|
6
|
+
getDefaultRoute,
|
|
7
|
+
getDefaultLocaleCode,
|
|
8
|
+
} = require('../common');
|
|
9
|
+
const search = require('../search/search');
|
|
10
|
+
const runtimes = require('./runtimes');
|
|
11
|
+
const { generateFacets } = require('./facets');
|
|
12
|
+
const { logLine } = require('./log');
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Ensure the search page and its runtime exist on first build.
|
|
16
|
+
* If `search.html` is missing, write an empty index, bundle the initial runtime,
|
|
17
|
+
* and build the page so subsequent steps can update artifacts incrementally.
|
|
18
|
+
*/
|
|
19
|
+
async function ensureSearchInitialized() {
|
|
20
|
+
const entries = getLocaleRouteEntries('search');
|
|
21
|
+
const defaultEntry = entries.length
|
|
22
|
+
? entries[0]
|
|
23
|
+
: { locale: getDefaultLocaleCode(), route: getDefaultRoute('search') };
|
|
24
|
+
const rel = search.resolveSearchOutputRelative(defaultEntry.route || '');
|
|
25
|
+
const searchPath = path.join(OUT_DIR, rel);
|
|
26
|
+
const needCreatePage = !fs.existsSync(searchPath);
|
|
27
|
+
if (!needCreatePage) return;
|
|
28
|
+
try { logLine('• Preparing search (initial)...', 'blue', { bright: true }); } catch (_) {}
|
|
29
|
+
// Build result item template (compat no-op) so it can be inlined if needed
|
|
30
|
+
try { await search.ensureResultTemplate(); } catch (_) {}
|
|
31
|
+
try { logLine(' - Writing empty index...', 'blue'); } catch (_) {}
|
|
32
|
+
await search.writeSearchIndex([]);
|
|
33
|
+
try { logLine(' - Writing runtime...', 'blue'); } catch (_) {}
|
|
34
|
+
await runtimes.prepareSearchRuntime(process.env.CANOPY_BUNDLE_TIMEOUT || 10000, 'initial');
|
|
35
|
+
try { logLine(' - Building search page...', 'blue'); } catch (_) {}
|
|
36
|
+
await search.buildSearchPage();
|
|
37
|
+
try { logLine('✓ Created search page', 'cyan'); } catch (_) {}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* After the search index is built, finalize related artifacts:
|
|
42
|
+
* - Generate facets for IIIF works
|
|
43
|
+
* - Re-bundle the search runtime (final)
|
|
44
|
+
* - Rebuild result template and the search page
|
|
45
|
+
* - Log a concise breakdown by type
|
|
46
|
+
*/
|
|
47
|
+
async function finalizeSearch(combinedRecords) {
|
|
48
|
+
const combined = Array.isArray(combinedRecords) ? combinedRecords : [];
|
|
49
|
+
await generateFacets(combined);
|
|
50
|
+
try { logLine('• Writing search runtime (final)...', 'blue', { bright: true }); } catch (_) {}
|
|
51
|
+
await runtimes.prepareSearchRuntime(process.env.CANOPY_BUNDLE_TIMEOUT || 10000, 'final');
|
|
52
|
+
try { await search.ensureResultTemplate(); } catch (_) {}
|
|
53
|
+
try { logLine('• Updating search page...', 'blue'); } catch (_) {}
|
|
54
|
+
await search.buildSearchPage();
|
|
55
|
+
try { logLine('✓ Search page updated', 'cyan'); } catch (_) {}
|
|
56
|
+
|
|
57
|
+
// Itemize counts by type for a clearer summary
|
|
58
|
+
try {
|
|
59
|
+
const counts = new Map();
|
|
60
|
+
for (const r of combined) {
|
|
61
|
+
const t = String((r && r.type) || 'page').toLowerCase();
|
|
62
|
+
counts.set(t, (counts.get(t) || 0) + 1);
|
|
63
|
+
}
|
|
64
|
+
const parts = Array.from(counts.entries())
|
|
65
|
+
.sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]))
|
|
66
|
+
.map(([t, n]) => `${t}: ${n}`);
|
|
67
|
+
const breakdown = parts.length ? `: ${parts.join(', ')}` : '';
|
|
68
|
+
logLine(`✓ Search index: ${combined.length} total records${breakdown}`, 'cyan');
|
|
69
|
+
} catch (_) {}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
module.exports = { ensureSearchInitialized, finalizeSearch };
|