@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.
Files changed (141) hide show
  1. package/bin/canopy.js +16 -0
  2. package/lib/AGENTS.md +107 -0
  3. package/lib/base-path.js +67 -0
  4. package/lib/build/appearance.js +76 -0
  5. package/lib/build/assets.js +30 -0
  6. package/lib/build/build.js +350 -0
  7. package/lib/build/dev.js +1355 -0
  8. package/lib/build/devtoast.config.json +6 -0
  9. package/lib/build/devtoast.css +14 -0
  10. package/lib/build/facets.js +19 -0
  11. package/lib/build/fonts.js +105 -0
  12. package/lib/build/iiif.js +2990 -0
  13. package/lib/build/log.js +31 -0
  14. package/lib/build/mdx.js +1967 -0
  15. package/lib/build/package-names.js +111 -0
  16. package/lib/build/pages.js +420 -0
  17. package/lib/build/rehype-task-list-labels.js +54 -0
  18. package/lib/build/runtimes.js +141 -0
  19. package/lib/build/search-index.js +55 -0
  20. package/lib/build/search-text.js +160 -0
  21. package/lib/build/search-workflow.js +72 -0
  22. package/lib/build/search.js +302 -0
  23. package/lib/build/sitemap.js +197 -0
  24. package/lib/build/styles.js +140 -0
  25. package/lib/build/theme-tokens.js +78 -0
  26. package/lib/build/ui-styles.js +25 -0
  27. package/lib/build/upgrade-notices.js +121 -0
  28. package/lib/cli.js +64 -0
  29. package/lib/common.js +580 -0
  30. package/lib/components/bibliography.js +197 -0
  31. package/lib/components/featured.js +237 -0
  32. package/lib/components/hero-slider-runtime.js +215 -0
  33. package/lib/components/image-story-runtime-entry.js +249 -0
  34. package/lib/components/map-runtime.js +114 -0
  35. package/lib/components/metadata-index.js +32 -0
  36. package/lib/components/nav-place.js +227 -0
  37. package/lib/components/navigation.js +381 -0
  38. package/lib/components/referenced.js +217 -0
  39. package/lib/components/slider-runtime-entry.js +106 -0
  40. package/lib/components/timeline-runtime.js +80 -0
  41. package/lib/components/viewer-runtime-entry.js +88 -0
  42. package/lib/config-path.js +78 -0
  43. package/lib/default-locale.js +113 -0
  44. package/lib/discovery/browser-loader.js +107 -0
  45. package/lib/discovery/browser.js +115 -0
  46. package/lib/discovery/config.js +25 -0
  47. package/lib/discovery/envelope.js +124 -0
  48. package/lib/discovery/export.js +226 -0
  49. package/lib/discovery/ingestion.js +62 -0
  50. package/lib/discovery/pagination.js +66 -0
  51. package/lib/discovery/provenance.js +79 -0
  52. package/lib/discovery/search.js +81 -0
  53. package/lib/discovery/tools.js +155 -0
  54. package/lib/discovery/validation.js +121 -0
  55. package/lib/head.js +154 -0
  56. package/lib/iiif/thumbnail.js +402 -0
  57. package/lib/index.js +9 -0
  58. package/lib/locale-runtime.js +69 -0
  59. package/lib/locales.js +203 -0
  60. package/lib/orchestrator.js +231 -0
  61. package/lib/page-context.js +24 -0
  62. package/lib/search/bundle.js +25 -0
  63. package/lib/search/data.js +202 -0
  64. package/lib/search/engine.js +86 -0
  65. package/lib/search/index-cache.js +95 -0
  66. package/lib/search/search-app.jsx +835 -0
  67. package/lib/search/search-engine-runtime.js +44 -0
  68. package/lib/search/search-form-runtime.js +462 -0
  69. package/lib/search/search.js +676 -0
  70. package/package.json +109 -3
  71. package/types/base-path.d.ts +4 -0
  72. package/types/orchestrator.d.ts +17 -0
  73. package/ui/dist/index.mjs +7358 -0
  74. package/ui/dist/index.mjs.map +7 -0
  75. package/ui/dist/server.mjs +12731 -0
  76. package/ui/dist/server.mjs.map +7 -0
  77. package/ui/fonts/OFL.txt +98 -0
  78. package/ui/fonts/google-sans-flex-latin-ext.woff2 +0 -0
  79. package/ui/fonts/google-sans-flex-latin.woff2 +0 -0
  80. package/ui/fonts/source-serif-4-italic-latin-ext.woff2 +0 -0
  81. package/ui/fonts/source-serif-4-italic-latin.woff2 +0 -0
  82. package/ui/fonts/source-serif-4-latin-ext.woff2 +0 -0
  83. package/ui/fonts/source-serif-4-latin.woff2 +0 -0
  84. package/ui/styles/base/_common.scss +97 -0
  85. package/ui/styles/base/_heading.scss +61 -0
  86. package/ui/styles/base/_markdown.scss +188 -0
  87. package/ui/styles/base/_type.scss +51 -0
  88. package/ui/styles/base/_utilities.scss +7 -0
  89. package/ui/styles/base/index.scss +5 -0
  90. package/ui/styles/components/_article-card.scss +108 -0
  91. package/ui/styles/components/_band.scss +104 -0
  92. package/ui/styles/components/_bibliography.scss +88 -0
  93. package/ui/styles/components/_buttons.scss +143 -0
  94. package/ui/styles/components/_card.scss +109 -0
  95. package/ui/styles/components/_diagram.scss +76 -0
  96. package/ui/styles/components/_feature.scss +66 -0
  97. package/ui/styles/components/_footer.scss +136 -0
  98. package/ui/styles/components/_gallery.scss +476 -0
  99. package/ui/styles/components/_image-story.scss +56 -0
  100. package/ui/styles/components/_interstitial-hero.scss +625 -0
  101. package/ui/styles/components/_layout.scss +83 -0
  102. package/ui/styles/components/_map.scss +296 -0
  103. package/ui/styles/components/_metadata-index.scss +68 -0
  104. package/ui/styles/components/_nav-tree.scss +80 -0
  105. package/ui/styles/components/_referenced-items.scss +69 -0
  106. package/ui/styles/components/_sub-navigation.scss +208 -0
  107. package/ui/styles/components/_swoosh.scss +50 -0
  108. package/ui/styles/components/_timeline.scss +350 -0
  109. package/ui/styles/components/_work.scss +71 -0
  110. package/ui/styles/components/header/_header.scss +294 -0
  111. package/ui/styles/components/header/_logo.scss +99 -0
  112. package/ui/styles/components/header/_navbar.scss +143 -0
  113. package/ui/styles/components/header/index.scss +3 -0
  114. package/ui/styles/components/iiif/_image.scss +32 -0
  115. package/ui/styles/components/iiif/_slider.scss +5 -0
  116. package/ui/styles/components/iiif/index.scss +2 -0
  117. package/ui/styles/components/index.scss +25 -0
  118. package/ui/styles/components/modal/_modal.scss +130 -0
  119. package/ui/styles/components/modal/index.scss +1 -0
  120. package/ui/styles/components/search/_filters.scss +259 -0
  121. package/ui/styles/components/search/_form.scss +187 -0
  122. package/ui/styles/components/search/_panel.scss +7 -0
  123. package/ui/styles/components/search/_results.scss +241 -0
  124. package/ui/styles/components/search/_tabs.scss +60 -0
  125. package/ui/styles/components/search/index.scss +4 -0
  126. package/ui/styles/index.css +5007 -0
  127. package/ui/styles/index.scss +11 -0
  128. package/ui/styles/settings/_breakpoints.scss +7 -0
  129. package/ui/styles/settings/_focus.scss +15 -0
  130. package/ui/styles/settings/_tokens.scss +170 -0
  131. package/ui/styles/settings/_type.scss +126 -0
  132. package/ui/styles/version-parity.css +116 -0
  133. package/ui/tailwind-canopy-iiif-plugin.js +39 -0
  134. package/ui/tailwind-canopy-iiif-preset.js +32 -0
  135. package/ui/tailwind-config.d.ts +29 -0
  136. package/ui/tailwind-config.js +159 -0
  137. package/ui/tailwind-default.config.mjs +4 -0
  138. package/ui/theme-color.js +130 -0
  139. package/ui/theme-palette.js +337 -0
  140. package/ui/theme.js +153 -0
  141. package/README.md +0 -3
@@ -0,0 +1,249 @@
1
+ import { mountImageStory } from '../../ui/dist/index.mjs';
2
+
3
+ const IMAGE_STORY_SELECTOR = '[data-canopy-image-story]';
4
+ const nodeStates = new WeakMap();
5
+ const SIZE_EPSILON = 1;
6
+
7
+ function ready(fn) {
8
+ if (typeof document === 'undefined') return;
9
+ if (document.readyState === 'loading') {
10
+ document.addEventListener('DOMContentLoaded', fn, { once: true });
11
+ } else {
12
+ fn();
13
+ }
14
+ }
15
+
16
+ function parseProps(node) {
17
+ try {
18
+ const script = node.querySelector('script[type="application/json"]');
19
+ if (!script) return {};
20
+ const text = script.textContent || '{}';
21
+ return JSON.parse(text) || {};
22
+ } catch (_) {
23
+ return {};
24
+ }
25
+ }
26
+
27
+ function getNodeState(node) {
28
+ if (!node) return null;
29
+ let state = nodeStates.get(node);
30
+ if (!state) {
31
+ state = {
32
+ mounted: false,
33
+ cleanup: null,
34
+ resizeObserver: null,
35
+ pollId: null,
36
+ watching: false,
37
+ props: null,
38
+ lastSize: null,
39
+ };
40
+ nodeStates.set(node, state);
41
+ }
42
+ return state;
43
+ }
44
+
45
+ function disconnectWatchers(state) {
46
+ if (!state) return;
47
+ if (state.resizeObserver) {
48
+ try {
49
+ state.resizeObserver.disconnect();
50
+ } catch (_) {}
51
+ state.resizeObserver = null;
52
+ }
53
+ if (state.pollId && typeof window !== 'undefined') {
54
+ window.clearTimeout(state.pollId);
55
+ state.pollId = null;
56
+ }
57
+ state.watching = false;
58
+ }
59
+
60
+ function destroyNode(node, state) {
61
+ const currentState = state || getNodeState(node);
62
+ if (!currentState) return;
63
+ disconnectWatchers(currentState);
64
+ if (currentState.cleanup) {
65
+ try {
66
+ currentState.cleanup();
67
+ } catch (_) {}
68
+ currentState.cleanup = null;
69
+ }
70
+ currentState.mounted = false;
71
+ }
72
+
73
+ function measureSize(node) {
74
+ if (!node) return null;
75
+ const rect = node.getBoundingClientRect();
76
+ const width = rect?.width || node.offsetWidth || node.clientWidth || 0;
77
+ const height = rect?.height || node.offsetHeight || node.clientHeight || 0;
78
+ return { width, height };
79
+ }
80
+
81
+ function hasUsableSize(node, state) {
82
+ const size = measureSize(node);
83
+ if (!size) return false;
84
+ const usable = size.width > 2 && size.height > 2;
85
+ if (usable && state) {
86
+ state.lastSize = size;
87
+ }
88
+ return usable;
89
+ }
90
+
91
+ function needsSizeRefresh(node, state) {
92
+ if (!node || !state) return false;
93
+ const size = measureSize(node);
94
+ if (!size) return false;
95
+ if (size.width <= 2 || size.height <= 2) {
96
+ return true;
97
+ }
98
+ if (!state.lastSize) {
99
+ state.lastSize = size;
100
+ return true;
101
+ }
102
+ const widthDelta = Math.abs(size.width - state.lastSize.width);
103
+ const heightDelta = Math.abs(size.height - state.lastSize.height);
104
+ if (widthDelta > SIZE_EPSILON || heightDelta > SIZE_EPSILON) {
105
+ state.lastSize = size;
106
+ return true;
107
+ }
108
+ return false;
109
+ }
110
+
111
+ function attemptMount(node, state) {
112
+ if (!node || !state || state.mounted) return false;
113
+ if (!hasUsableSize(node, state)) return false;
114
+ state.mounted = true;
115
+ disconnectWatchers(state);
116
+ const props = state.props || parseProps(node);
117
+ Promise.resolve(mountImageStory(node, props)).then((destroy) => {
118
+ if (typeof destroy === 'function') {
119
+ state.cleanup = destroy;
120
+ } else {
121
+ state.cleanup = null;
122
+ }
123
+ });
124
+ return true;
125
+ }
126
+
127
+ function scheduleWatchers(node, state, tryMount) {
128
+ if (!node || !state || state.watching) return;
129
+ if (typeof window === 'undefined') return;
130
+ state.watching = true;
131
+ if (typeof window !== 'undefined' && typeof window.ResizeObserver === 'function') {
132
+ state.resizeObserver = new window.ResizeObserver(() => {
133
+ if (state.mounted) return;
134
+ tryMount();
135
+ });
136
+ try {
137
+ state.resizeObserver.observe(node);
138
+ } catch (_) {}
139
+ }
140
+ const schedulePoll = () => {
141
+ if (state.mounted) return;
142
+ state.pollId = window.setTimeout(() => {
143
+ state.pollId = null;
144
+ if (!tryMount()) {
145
+ schedulePoll();
146
+ }
147
+ }, 200);
148
+ };
149
+ schedulePoll();
150
+ }
151
+
152
+ function startMountProcess(node) {
153
+ const state = getNodeState(node);
154
+ if (!state) return;
155
+ state.props = parseProps(node);
156
+ const tryMount = () => attemptMount(node, state);
157
+ if (!tryMount()) {
158
+ scheduleWatchers(node, state, tryMount);
159
+ }
160
+ }
161
+
162
+ function mount(node) {
163
+ if (!node) return;
164
+ const state = getNodeState(node);
165
+ if (!state || state.bound) return;
166
+ state.bound = true;
167
+ startMountProcess(node);
168
+ }
169
+
170
+ function remount(node) {
171
+ const state = getNodeState(node);
172
+ if (!state) return;
173
+ state.props = parseProps(node);
174
+ destroyNode(node, state);
175
+ startMountProcess(node);
176
+ }
177
+
178
+ function scan() {
179
+ try {
180
+ document
181
+ .querySelectorAll(IMAGE_STORY_SELECTOR)
182
+ .forEach((node) => mount(node));
183
+ } catch (_) {}
184
+ }
185
+
186
+ function observe() {
187
+ try {
188
+ const obs = new MutationObserver((mutations) => {
189
+ const toMount = [];
190
+ mutations.forEach((mutation) => {
191
+ mutation.addedNodes &&
192
+ mutation.addedNodes.forEach((node) => {
193
+ if (!(node instanceof Element)) return;
194
+ if (node.matches && node.matches(IMAGE_STORY_SELECTOR)) {
195
+ toMount.push(node);
196
+ }
197
+ const inner = node.querySelectorAll
198
+ ? node.querySelectorAll(IMAGE_STORY_SELECTOR)
199
+ : [];
200
+ inner && inner.forEach && inner.forEach((el) => toMount.push(el));
201
+ });
202
+ });
203
+ if (toMount.length) Promise.resolve().then(() => toMount.forEach(mount));
204
+ });
205
+ obs.observe(document.documentElement || document.body, {
206
+ childList: true,
207
+ subtree: true,
208
+ });
209
+ } catch (_) {}
210
+ }
211
+
212
+ function refreshModal(modal) {
213
+ if (!modal || typeof modal.querySelectorAll !== 'function') return;
214
+ const nodes = modal.querySelectorAll(IMAGE_STORY_SELECTOR);
215
+ if (!nodes || !nodes.length) return;
216
+ Array.prototype.forEach.call(nodes, (node) => {
217
+ const state = getNodeState(node);
218
+ if (!state || !state.mounted) return;
219
+ if (needsSizeRefresh(node, state)) {
220
+ remount(node);
221
+ }
222
+ });
223
+ }
224
+
225
+ function handleGalleryModalChange(event) {
226
+ if (!event || typeof document === 'undefined') return;
227
+ const detail = event.detail || {};
228
+ if (detail.state !== 'open') return;
229
+ let modal = detail.modal;
230
+ if (!modal && detail.modalId) {
231
+ modal = document.getElementById(detail.modalId);
232
+ }
233
+ if (modal) {
234
+ refreshModal(modal);
235
+ }
236
+ }
237
+
238
+ function bindGalleryListener() {
239
+ if (typeof window === 'undefined' || typeof window.addEventListener !== 'function') {
240
+ return;
241
+ }
242
+ window.addEventListener('canopy:gallery:modal-change', handleGalleryModalChange);
243
+ }
244
+
245
+ ready(function onReady() {
246
+ scan();
247
+ observe();
248
+ bindGalleryListener();
249
+ });
@@ -0,0 +1,114 @@
1
+ import React from 'react';
2
+ import { createRoot } from 'react-dom/client';
3
+ import { Map } from '../../ui/dist/index.mjs';
4
+ import Leaflet from 'leaflet';
5
+ import 'leaflet/dist/leaflet.css';
6
+ import 'leaflet.markercluster';
7
+ import 'leaflet.markercluster/dist/MarkerCluster.css';
8
+ import 'leaflet.markercluster/dist/MarkerCluster.Default.css';
9
+
10
+ function broadcastLeafletReady() {
11
+ let updated = false;
12
+ try {
13
+ if (typeof globalThis !== 'undefined' && !globalThis.L) {
14
+ globalThis.L = Leaflet;
15
+ updated = true;
16
+ }
17
+ } catch (_) {}
18
+ try {
19
+ if (typeof window !== 'undefined' && !window.L) {
20
+ window.L = Leaflet;
21
+ updated = true;
22
+ }
23
+ } catch (_) {}
24
+ if (updated && typeof document !== 'undefined') {
25
+ try {
26
+ document.dispatchEvent(new CustomEvent('canopy:leaflet-ready'));
27
+ } catch (_) {
28
+ const evt = document.createEvent('CustomEvent');
29
+ evt.initCustomEvent('canopy:leaflet-ready', false, false, {});
30
+ document.dispatchEvent(evt);
31
+ }
32
+ }
33
+ }
34
+
35
+ broadcastLeafletReady();
36
+
37
+ function ready(fn) {
38
+ if (typeof document === 'undefined') return;
39
+ if (document.readyState === 'loading') {
40
+ document.addEventListener('DOMContentLoaded', fn, { once: true });
41
+ } else {
42
+ fn();
43
+ }
44
+ }
45
+
46
+ function parseProps(el) {
47
+ try {
48
+ const script = el.querySelector('script[type="application/json"]');
49
+ if (script) return JSON.parse(script.textContent || '{}');
50
+ const raw = el.getAttribute('data-props') || '{}';
51
+ return JSON.parse(raw);
52
+ } catch (_) {
53
+ return {};
54
+ }
55
+ }
56
+
57
+ function mount(el) {
58
+ try {
59
+ if (!el || el.getAttribute('data-canopy-map-mounted') === '1') return;
60
+ const props = parseProps(el);
61
+ const script = el.querySelector('script[type="application/json"]');
62
+ if (script && script.parentNode === el) {
63
+ script.parentNode.removeChild(script);
64
+ }
65
+ while (el.firstChild) {
66
+ el.removeChild(el.firstChild);
67
+ }
68
+ const root = createRoot(el);
69
+ root.render(React.createElement(Map, props));
70
+ el.setAttribute('data-canopy-map-mounted', '1');
71
+ } catch (error) {
72
+ try {
73
+ console.warn('[canopy][map] failed to mount map', error);
74
+ } catch (_) {}
75
+ }
76
+ }
77
+
78
+ function scan() {
79
+ try {
80
+ document
81
+ .querySelectorAll('[data-canopy-map]:not([data-canopy-map-mounted="1"])')
82
+ .forEach(mount);
83
+ } catch (_) {}
84
+ }
85
+
86
+ function observe() {
87
+ try {
88
+ const obs = new MutationObserver((mutations) => {
89
+ const toMount = [];
90
+ mutations.forEach((mutation) => {
91
+ mutation.addedNodes &&
92
+ mutation.addedNodes.forEach((node) => {
93
+ if (!(node instanceof Element)) return;
94
+ if (node.matches && node.matches('[data-canopy-map]')) toMount.push(node);
95
+ const inner = node.querySelectorAll
96
+ ? node.querySelectorAll('[data-canopy-map]')
97
+ : [];
98
+ inner && inner.forEach && inner.forEach((el) => toMount.push(el));
99
+ });
100
+ });
101
+ if (toMount.length) Promise.resolve().then(() => toMount.forEach(mount));
102
+ });
103
+ obs.observe(document.documentElement || document.body, {
104
+ childList: true,
105
+ subtree: true,
106
+ });
107
+ } catch (_) {}
108
+ }
109
+
110
+ ready(() => {
111
+ if (typeof document === 'undefined') return;
112
+ scan();
113
+ observe();
114
+ });
@@ -0,0 +1,32 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+
4
+ const METADATA_INDEX_PATH = path.resolve('.cache/iiif/metadata-index.json');
5
+
6
+ let cachedIndex = null;
7
+
8
+ function readMetadataIndexFromDisk() {
9
+ try {
10
+ if (!fs.existsSync(METADATA_INDEX_PATH)) return [];
11
+ const raw = fs.readFileSync(METADATA_INDEX_PATH, 'utf8');
12
+ const data = JSON.parse(raw);
13
+ return Array.isArray(data) ? data : [];
14
+ } catch (_) {
15
+ return [];
16
+ }
17
+ }
18
+
19
+ function getMetadataIndex() {
20
+ if (cachedIndex) return cachedIndex;
21
+ cachedIndex = readMetadataIndexFromDisk();
22
+ return cachedIndex;
23
+ }
24
+
25
+ function resetMetadataIndex() {
26
+ cachedIndex = null;
27
+ }
28
+
29
+ module.exports = {
30
+ getMetadataIndex,
31
+ resetMetadataIndex,
32
+ };
@@ -0,0 +1,227 @@
1
+ const crypto = require('crypto');
2
+ const {
3
+ fs,
4
+ fsp,
5
+ path,
6
+ OUT_DIR,
7
+ ensureDirSync,
8
+ rootRelativeHref,
9
+ } = require('../common');
10
+ const {firstLabelString} = require('./featured');
11
+
12
+ const NAVPLACE_RELATIVE = path.join('api', 'navplace.json');
13
+ const NAVPLACE_PATH = path.join(OUT_DIR, NAVPLACE_RELATIVE);
14
+ const NAVPLACE_PUBLIC_HREF = rootRelativeHref(
15
+ NAVPLACE_RELATIVE.split(path.sep).join('/')
16
+ );
17
+
18
+ let cachedSummary = null;
19
+
20
+ function normalizeStringValue(value) {
21
+ if (value == null) return '';
22
+ if (typeof value === 'string') return value.trim();
23
+ if (typeof value === 'number' || typeof value === 'boolean') {
24
+ return String(value);
25
+ }
26
+ if (Array.isArray(value)) {
27
+ for (const entry of value) {
28
+ const text = normalizeStringValue(entry);
29
+ if (text) return text;
30
+ }
31
+ return '';
32
+ }
33
+ if (typeof value === 'object') {
34
+ const label = firstLabelString(value);
35
+ if (label && label !== 'Untitled') return label;
36
+ const keys = Object.keys(value);
37
+ for (const key of keys) {
38
+ const text = normalizeStringValue(value[key]);
39
+ if (text) return text;
40
+ }
41
+ }
42
+ return '';
43
+ }
44
+
45
+ function normalizeCoordinate(value) {
46
+ const num = Number(value);
47
+ return Number.isFinite(num) ? num : null;
48
+ }
49
+
50
+ function sanitizeFeature(feature, index, context) {
51
+ if (!feature || typeof feature !== 'object') return null;
52
+ const geometry = feature.geometry || {};
53
+ const geomType = typeof geometry.type === 'string' ? geometry.type : '';
54
+ if (geomType.toLowerCase() !== 'point') return null;
55
+ const coords = Array.isArray(geometry.coordinates)
56
+ ? geometry.coordinates
57
+ : [];
58
+ const lng = normalizeCoordinate(coords[0]);
59
+ const lat = normalizeCoordinate(coords[1]);
60
+ if (lat == null || lng == null) return null;
61
+ const properties = feature.properties || {};
62
+ const label = normalizeStringValue(properties.label) || context.title;
63
+ const summary = normalizeStringValue(
64
+ properties.summary || properties.description || properties.note
65
+ );
66
+ const idBase = context.slug || context.id || 'manifest';
67
+ const normalizedId = feature.id
68
+ ? String(feature.id)
69
+ : `${idBase}-point-${index + 1}`;
70
+ return {
71
+ id: normalizedId,
72
+ label: label || context.title,
73
+ summary: summary || context.summary || '',
74
+ lat,
75
+ lng,
76
+ };
77
+ }
78
+
79
+ function collectManifestFeatures(manifest) {
80
+ if (!manifest) return [];
81
+ const navPlace = manifest.navPlace || manifest.navplace;
82
+ if (!navPlace) return [];
83
+ if (Array.isArray(navPlace)) return navPlace;
84
+ if (Array.isArray(navPlace.features)) return navPlace.features;
85
+ if (navPlace.type && navPlace.type.toLowerCase() === 'feature') {
86
+ return [navPlace];
87
+ }
88
+ return [];
89
+ }
90
+
91
+ function buildManifestNavPlaceRecord(options = {}) {
92
+ const manifest = options.manifest || null;
93
+ if (!manifest) return null;
94
+ const rawFeatures = collectManifestFeatures(manifest);
95
+ if (!rawFeatures.length) return null;
96
+ const manifestId = manifest.id || manifest['@id'] || '';
97
+ const title = normalizeStringValue(options.title) || firstLabelString(manifest.label);
98
+ const summary = normalizeStringValue(options.summary) || normalizeStringValue(manifest.summary);
99
+ const slug = options.slug ? String(options.slug) : '';
100
+ const href = options.href ? String(options.href) : '';
101
+ const thumbnail = options.thumbnail ? String(options.thumbnail) : '';
102
+ const thumbWidth =
103
+ typeof options.thumbnailWidth === 'number' ? options.thumbnailWidth : undefined;
104
+ const thumbHeight =
105
+ typeof options.thumbnailHeight === 'number' ? options.thumbnailHeight : undefined;
106
+ const context = {
107
+ id: manifestId,
108
+ slug,
109
+ title: title || 'Untitled',
110
+ summary: summary || '',
111
+ };
112
+ const features = rawFeatures
113
+ .map((feature, index) => sanitizeFeature(feature, index, context))
114
+ .filter(Boolean);
115
+ if (!features.length) return null;
116
+ return {
117
+ id: String(manifestId || slug || context.title || ''),
118
+ slug,
119
+ href,
120
+ title: context.title,
121
+ summary: context.summary,
122
+ thumbnail,
123
+ thumbnailWidth: thumbWidth,
124
+ thumbnailHeight: thumbHeight,
125
+ features,
126
+ };
127
+ }
128
+
129
+ function stableCopy(records) {
130
+ return records
131
+ .map((record) => ({
132
+ ...record,
133
+ features: (record.features || [])
134
+ .slice()
135
+ .sort((a, b) => (a.id || '').localeCompare(b.id || ''))
136
+ .map((feat) => ({
137
+ id: String(feat.id || ''),
138
+ label: feat.label || '',
139
+ summary: feat.summary || '',
140
+ lat: Number(feat.lat),
141
+ lng: Number(feat.lng),
142
+ })),
143
+ }))
144
+ .sort((a, b) => (a.href || '').localeCompare(b.href || ''));
145
+ }
146
+
147
+ function hashRecords(records) {
148
+ try {
149
+ const stable = stableCopy(records);
150
+ const payload = JSON.stringify(stable);
151
+ return crypto.createHash('sha1').update(payload).digest('hex');
152
+ } catch (_) {
153
+ return '';
154
+ }
155
+ }
156
+
157
+ async function removeDataset() {
158
+ cachedSummary = {
159
+ hasFeatures: false,
160
+ version: null,
161
+ href: NAVPLACE_PUBLIC_HREF,
162
+ };
163
+ try {
164
+ await fsp.rm(NAVPLACE_PATH, {force: true});
165
+ } catch (_) {}
166
+ return cachedSummary;
167
+ }
168
+
169
+ async function writeNavPlaceDataset(records = []) {
170
+ const valid = Array.isArray(records)
171
+ ? records.filter((record) => record && record.features && record.features.length)
172
+ : [];
173
+ if (!valid.length) {
174
+ return removeDataset();
175
+ }
176
+ const stable = stableCopy(valid);
177
+ const version = hashRecords(valid);
178
+ const payload = {
179
+ version,
180
+ generatedAt: new Date().toISOString(),
181
+ manifests: stable,
182
+ };
183
+ ensureDirSync(path.dirname(NAVPLACE_PATH));
184
+ await fsp.writeFile(NAVPLACE_PATH, JSON.stringify(payload, null, 2), 'utf8');
185
+ cachedSummary = {
186
+ hasFeatures: true,
187
+ version: version || null,
188
+ href: NAVPLACE_PUBLIC_HREF,
189
+ };
190
+ return cachedSummary;
191
+ }
192
+
193
+ function readSummaryFromDisk() {
194
+ try {
195
+ const raw = fs.readFileSync(NAVPLACE_PATH, 'utf8');
196
+ const data = JSON.parse(raw);
197
+ const hasFeatures = Array.isArray(data.manifests)
198
+ ? data.manifests.some((entry) => entry && entry.features && entry.features.length)
199
+ : false;
200
+ return {
201
+ hasFeatures,
202
+ version: data.version || null,
203
+ href: NAVPLACE_PUBLIC_HREF,
204
+ };
205
+ } catch (_) {
206
+ return {
207
+ hasFeatures: false,
208
+ version: null,
209
+ href: NAVPLACE_PUBLIC_HREF,
210
+ };
211
+ }
212
+ }
213
+
214
+ function getNavPlaceDatasetInfo() {
215
+ if (!cachedSummary) {
216
+ cachedSummary = readSummaryFromDisk();
217
+ }
218
+ return cachedSummary;
219
+ }
220
+
221
+ module.exports = {
222
+ buildManifestNavPlaceRecord,
223
+ writeNavPlaceDataset,
224
+ getNavPlaceDatasetInfo,
225
+ NAVPLACE_PATH,
226
+ NAVPLACE_PUBLIC_HREF,
227
+ };