roamwright 0.61.24 → 0.61.26

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.
@@ -0,0 +1,343 @@
1
+ /**
2
+ * Site page catalog, slugification, inter-page link graph, fast tab-title lookup, and
3
+ * path-to-land resolution for explorable worlds and their static-site generator.
4
+ * Worker-safe: world data, copy, and article helpers enter via parameters.
5
+ * @module
6
+ */
7
+
8
+ /**
9
+ * Converts a display name into a clean lowercase URL slug, stripping diacritics and apostrophes.
10
+ *
11
+ * @param {string} text Display name (e.g. `"Harbour's Edge"` -> `"harbours-edge"`).
12
+ * @returns {string}
13
+ */
14
+ export function slugify(text) {
15
+ return String(text)
16
+ .normalize('NFD')
17
+ .replace(/[\u0300-\u036f]/g, '')
18
+ .toLowerCase()
19
+ .replace(/['’]/g, '')
20
+ .replace(/[^a-z0-9]+/g, '-')
21
+ .replace(/^-|-$/g, '');
22
+ }
23
+
24
+ const hasPage = character => !!(character?.name && character.desc);
25
+ const uniqueByPath = items => items.filter((item, index) => items.findIndex(other => other.path === item.path) === index);
26
+
27
+ /**
28
+ * Creates the page-list, link-graph, title-map, and space/path-to-land helpers for a world.
29
+ *
30
+ * @param {object} options
31
+ * @param {string} options.site Short site name appended to page titles (`"<Name> · <site>"`).
32
+ * @param {string} options.siteTitle Title of the root landing page.
33
+ * @param {string | (() => string)} options.siteDesc Description of the root landing page (or getter).
34
+ * @param {string | (() => string)} [options.eventsDesc=''] Description of the `/events` index page.
35
+ * @param {string | (() => string)} [options.mapDesc=''] Description of the `/map` index page.
36
+ * @param {string} [options.settingsDesc='Adjust display, sound and quality.'] Description of `/settings`.
37
+ * @param {string} [options.controlsDesc='How to move, fly and interact with the world.'] Description of `/controls`.
38
+ * @param {() => Array<object>} options.getEvents Returns all world scenes/events.
39
+ * @param {() => string[]} [options.getEventOrder] Returns ordered scene ids (defaults to `getEvents()` order).
40
+ * @param {() => Record<string, object>} options.getCharacters Returns character definitions keyed by id.
41
+ * @param {() => Array<object>} options.getLocations Returns named locations in flyable lands and spaces.
42
+ * @param {() => Record<string, { x: number, z: number }>} [options.getPlaces] Returns world anchor coordinates by key.
43
+ * @param {() => Array<object>} [options.getVisions] Returns standalone non-flyable spaces/visions.
44
+ * @param {() => Array<object>} [options.getAtlasPlaces] Returns atlas entries used to name standalone spaces.
45
+ * @param {() => Array<{ id: string, title: string, intro: string, terms: Array<[string, string, object?]> }>} [options.getWiki]
46
+ * Returns glossary sections and term tuples.
47
+ * @param {() => string} [options.getPeopleIntro] Returns introductory copy for the People/Wiki index page.
48
+ * @param {(path: string) => object | null} [options.findEntry] Optional lazy article lookup (for full page text/links).
49
+ * @param {(entry: object) => Array<{ path: string, name: string }>} [options.entryLinks] Extracts outgoing links from an article.
50
+ * @param {(entry: object) => string} [options.entryText] Flattens an article to plain text.
51
+ * @param {string[]} [options.lands=['world']] Ordered list of flyable land ids.
52
+ * @param {Record<string, string>} [options.setHosts={}] Maps standalone space ids to their host land id.
53
+ * @param {(space: string) => string} [options.fallbackHost] Resolves unmapped space ids to a host land id.
54
+ * @param {string} [options.defaultLand='world'] Fallback land when a route names no specific land.
55
+ * @param {number} [options.nearbyLimit=8] Maximum number of nearby-place links per location page.
56
+ * @returns {{ slug: (s: string) => string, visionName: (id: string) => string,
57
+ * scenePath: (ev: object) => string, placePath: (loc: object) => string,
58
+ * visionPath: (id: string) => string, personPath: (c: object) => string,
59
+ * placeOf: (key: string) => object | undefined, sceneWhere: (ev: object) => { path: string, name: string } | false | undefined,
60
+ * sitePages: (includeArticles?: boolean) => Array<object>,
61
+ * landOfSpace: (space: string) => string, landOfPath: (path: string) => string,
62
+ * titleMap: () => Map<string, string>, pageTitle: (path: string) => string }}
63
+ */
64
+ export function createSitePages({
65
+ site,
66
+ siteTitle,
67
+ siteDesc,
68
+ eventsDesc = '',
69
+ mapDesc = '',
70
+ settingsDesc = 'Adjust the world’s display, sound and quality.',
71
+ controlsDesc = 'How to move, fly and interact with the world.',
72
+ getEvents,
73
+ getEventOrder = null,
74
+ getCharacters,
75
+ getLocations,
76
+ getPlaces = () => ({}),
77
+ getVisions = () => [],
78
+ getAtlasPlaces = () => [],
79
+ getWiki = () => [],
80
+ getPeopleIntro = () => '',
81
+ findEntry = null,
82
+ entryLinks = () => [],
83
+ entryText = () => '',
84
+ lands = ['world'],
85
+ setHosts = {},
86
+ fallbackHost = () => lands[0] ?? 'world',
87
+ defaultLand = lands[0] ?? 'world',
88
+ nearbyLimit = 8,
89
+ }) {
90
+ const slug = slugify;
91
+ const readText = v => (typeof v === 'function' ? v() : v);
92
+
93
+ function visionName(id) {
94
+ return getAtlasPlaces().find(p => p.vision === id)?.n ?? getVisions().find(v => v.id === id)?.title ?? id;
95
+ }
96
+
97
+ const scenePath = ev => 'scene/' + slug(ev.title);
98
+ const placePath = loc => 'location/' + slug(loc.name);
99
+ const visionPath = id => 'location/' + slug(visionName(id));
100
+ const personPath = c => 'person/' + slug(c.name);
101
+
102
+ function orderedEvents() {
103
+ const events = getEvents();
104
+ if (!getEventOrder) return events;
105
+ return getEventOrder()
106
+ .map(id => events.find(e => e.id === id))
107
+ .filter(Boolean);
108
+ }
109
+
110
+ function placeOf(key) {
111
+ if (!key) return undefined;
112
+ const root = key.split('.')[0];
113
+ const locations = getLocations();
114
+ // An offset landmark can share its town's anchor; prefer the place at the anchor itself.
115
+ return locations.find(l => l.desc && (l.id === root || (l.at === key && !l.off))) ?? locations.find(l => l.desc && l.at === key);
116
+ }
117
+
118
+ function sceneWhere(ev) {
119
+ if (!ev.at) return getVisions().some(v => v.id === ev.space) && { path: visionPath(ev.space), name: visionName(ev.space) };
120
+ const loc = placeOf(ev.at);
121
+ return loc && { path: placePath(loc), name: loc.name };
122
+ }
123
+
124
+ function castOf(ev) {
125
+ const chars = getCharacters();
126
+ return ev.who.filter(id => hasPage(chars[id]));
127
+ }
128
+
129
+ function personLink(id) {
130
+ const c = getCharacters()[id];
131
+ return { path: personPath(c), name: c.name };
132
+ }
133
+
134
+ const sceneLink = ev => ({ path: scenePath(ev), name: ev.title });
135
+
136
+ function homesOf(character) {
137
+ const list = Array.isArray(character.home) ? character.home : character.home ? [character.home] : [];
138
+ return list.map(h => placeOf(h.at)).filter(Boolean);
139
+ }
140
+
141
+ function placeAt(loc) {
142
+ const anchor = getPlaces()[loc.at];
143
+ return anchor && { x: anchor.x + (loc.off?.[0] ?? 0), z: anchor.z + (loc.off?.[1] ?? 0) };
144
+ }
145
+
146
+ function sitePages(includeArticles = true) {
147
+ const scenes = orderedEvents();
148
+ const characters = getCharacters();
149
+ const locations = getLocations();
150
+ const visions = getVisions();
151
+ const wiki = getWiki();
152
+ const people = Object.entries(characters).filter(([, c]) => hasPage(c));
153
+ const places = [
154
+ ...visions.map(v => ({ path: visionPath(v.id), name: visionName(v.id) })),
155
+ ...locations.filter(l => l.name && l.desc).map(l => ({ path: placePath(l), name: l.name })),
156
+ ];
157
+ const sections = [
158
+ { path: 'events', name: 'Events' },
159
+ { path: 'map', name: 'Map' },
160
+ { path: 'wiki', name: 'People' },
161
+ ];
162
+ const pages = [{ path: '', title: siteTitle, desc: readText(siteDesc), links: [{ label: 'Explore', items: sections }] }];
163
+ const add = (section, path, name, desc, links = [], text = null) =>
164
+ pages.push({ path, name, section, title: `${name} · ${site}`, desc, text, links: links.filter(g => g.items.length) });
165
+
166
+ add(null, 'events', 'Events', readText(eventsDesc), [{ label: 'Scenes', items: scenes.map(sceneLink) }]);
167
+ add(null, 'map', 'Map', readText(mapDesc), [{ label: 'Places', items: places }]);
168
+ add(null, 'wiki', 'People', getPeopleIntro(), [
169
+ { label: 'Wiki', items: wiki.map(w => ({ path: 'wiki/' + w.id, name: w.title })) },
170
+ { label: 'People', items: people.map(([id]) => personLink(id)) },
171
+ ]);
172
+ add(null, 'settings', 'Settings', settingsDesc);
173
+ add(null, 'controls', 'Controls', controlsDesc);
174
+
175
+ for (const w of wiki) {
176
+ const terms = w.terms.map(([term]) => ({ path: `wiki/${w.id}/${slug(term)}`, name: term }));
177
+ add('Wiki', 'wiki/' + w.id, w.title, w.intro, [{ label: w.title, items: terms }]);
178
+ for (const [term, text] of w.terms) {
179
+ const path = `wiki/${w.id}/${slug(term)}`;
180
+ const entry = includeArticles && findEntry ? findEntry(path) : null;
181
+ add(
182
+ w.title,
183
+ path,
184
+ term,
185
+ text,
186
+ [
187
+ { label: 'Named here', items: entry ? entryLinks(entry) : [] },
188
+ { label: w.title, items: [{ path: 'wiki/' + w.id, name: w.title }, ...terms] },
189
+ ],
190
+ entry ? entryText(entry) : null,
191
+ );
192
+ }
193
+ }
194
+
195
+ for (const ev of scenes) {
196
+ const where = sceneWhere(ev);
197
+ add(
198
+ 'Scenes',
199
+ scenePath(ev),
200
+ ev.title,
201
+ `${ev.where}. ${ev.narration?.[0] ?? ''}`.trim(),
202
+ [
203
+ { label: 'Where', items: where ? [where] : [] },
204
+ { label: 'Who', items: castOf(ev).map(personLink) },
205
+ ],
206
+ (ev.narration ?? []).join(' '),
207
+ );
208
+ }
209
+
210
+ const nearby = loc => {
211
+ const at = placeAt(loc);
212
+ return locations
213
+ .filter(o => o !== loc && o.name && o.desc && placeAt(o) && o.space === loc.space)
214
+ .map(o => ({
215
+ o,
216
+ d:
217
+ (o.at === loc.at || o.id === loc.at || o.at === loc.id ? 0 : 1e6) +
218
+ (at ? Math.hypot(placeAt(o).x - at.x, placeAt(o).z - at.z) : 0),
219
+ }))
220
+ .sort((p, q) => p.d - q.d)
221
+ .slice(0, nearbyLimit)
222
+ .map(({ o }) => ({ path: placePath(o), name: o.name }));
223
+ };
224
+
225
+ const atPlace = (match, loc) => [
226
+ { label: 'Scenes here', items: scenes.filter(match).map(sceneLink) },
227
+ {
228
+ label: 'People here',
229
+ items: uniqueByPath([
230
+ ...scenes.filter(match).flatMap(castOf).map(personLink),
231
+ ...(loc ? people.filter(([, c]) => homesOf(c).includes(loc)).map(([id]) => personLink(id)) : []),
232
+ ]),
233
+ },
234
+ { label: 'Nearby', items: loc ? nearby(loc) : [] },
235
+ ];
236
+
237
+ for (const v of visions) {
238
+ add(
239
+ 'Places',
240
+ visionPath(v.id),
241
+ visionName(v.id),
242
+ v.desc,
243
+ atPlace(e => e.space === v.id),
244
+ );
245
+ }
246
+
247
+ for (const loc of locations) {
248
+ if (loc.name && loc.desc) {
249
+ add(
250
+ 'Places',
251
+ placePath(loc),
252
+ loc.name,
253
+ loc.desc,
254
+ atPlace(e => e.at && placeOf(e.at) === loc, loc),
255
+ );
256
+ }
257
+ }
258
+
259
+ for (const [id, c] of people) {
260
+ const seen = scenes.filter(e => e.who.includes(id));
261
+ const entry = includeArticles && findEntry ? findEntry(personPath(c)) : null;
262
+ add(
263
+ 'People',
264
+ personPath(c),
265
+ c.name,
266
+ c.role && !c.desc.includes(c.role) ? `${c.role}. ${c.desc}` : c.desc,
267
+ [
268
+ { label: 'Seen in', items: seen.map(sceneLink) },
269
+ {
270
+ label: 'Found at',
271
+ items: uniqueByPath(
272
+ [...homesOf(c), ...seen.map(sceneWhere).filter(Boolean)].map(l => (l.path ? l : { path: placePath(l), name: l.name })),
273
+ ),
274
+ },
275
+ { label: 'Named here', items: entry ? entryLinks(entry) : [] },
276
+ ],
277
+ entry?.sections.length ? entryText(entry) : null,
278
+ );
279
+ }
280
+
281
+ const seenPaths = new Set();
282
+ return pages.filter(p => !seenPaths.has(p.path) && seenPaths.add(p.path));
283
+ }
284
+
285
+ function landOfSpace(space) {
286
+ return lands.includes(space) ? space : (setHosts[space] ?? fallbackHost(space));
287
+ }
288
+
289
+ function landOfPath(path) {
290
+ const [kind, name = ''] = (path || '').split('/').filter(Boolean);
291
+ const id = slug(decodeURIComponent(name));
292
+ const is = text => text && [id, 'the-' + id].includes(slug(text));
293
+ let land = defaultLand;
294
+ if (kind === 'scene') {
295
+ land = getEvents().find(e => is(e.id) || is(e.title))?.space ?? land;
296
+ } else if (kind === 'location' || kind === 'place') {
297
+ const vision = getVisions().find(v => is(v.id) || is(v.title) || is(visionName(v.id)));
298
+ land = vision?.space ?? vision?.id ?? getLocations().find(l => is(l.id) || is(l.name))?.space ?? land;
299
+ }
300
+ return landOfSpace(land);
301
+ }
302
+
303
+ function titleMap() {
304
+ const titles = new Map([['', siteTitle]]);
305
+ const add = (path, name) => titles.has(path) || titles.set(path, `${name} · ${site}`);
306
+ add('events', 'Events');
307
+ add('map', 'Map');
308
+ add('wiki', 'People');
309
+ add('controls', 'Controls');
310
+ add('settings', 'Settings');
311
+ for (const w of getWiki()) {
312
+ add('wiki/' + w.id, w.title);
313
+ for (const [term] of w.terms) add(`wiki/${w.id}/${slug(term)}`, term);
314
+ }
315
+ for (const ev of orderedEvents()) add(scenePath(ev), ev.title);
316
+ for (const v of getVisions()) add(visionPath(v.id), visionName(v.id));
317
+ for (const l of getLocations()) if (l.name && l.desc) add(placePath(l), l.name);
318
+ for (const c of Object.values(getCharacters())) if (hasPage(c)) add(personPath(c), c.name);
319
+ return titles;
320
+ }
321
+
322
+ let cachedTitles = null;
323
+ function pageTitle(path) {
324
+ cachedTitles ??= titleMap();
325
+ return cachedTitles.get(path) ?? siteTitle;
326
+ }
327
+
328
+ return {
329
+ slug,
330
+ visionName,
331
+ scenePath,
332
+ placePath,
333
+ visionPath,
334
+ personPath,
335
+ placeOf,
336
+ sceneWhere,
337
+ sitePages,
338
+ landOfSpace,
339
+ landOfPath,
340
+ titleMap,
341
+ pageTitle,
342
+ };
343
+ }
package/tools/ios-sim.sh CHANGED
@@ -15,6 +15,7 @@
15
15
  # SIM_DEVICE simulator device name (default: "iPhone 14 Pro Max")
16
16
  # SIM_CHROME_BUNDLE Chromium iOS bundle id (default: org.chromium.ost.chrome.ios.dev)
17
17
  # SIM_PORT localhost port for the simulator server (default: 8764)
18
+ # SIM_ARRIVE seconds to wait for the browser to ask for the page (default: 30)
18
19
  # SIM_STORAGE_LOG_KEY localStorage key holding the diagnostic log JSON (default: roamwright:log)
19
20
  # SIM_DEFAULT_ADDRESS default route address when $1 is omitted
20
21
  # SIM_SERVE_SCRIPT relative path to server script supporting SERVE_ENTRY (default: scripts/serve.py)
@@ -84,12 +85,15 @@ xcrun simctl terminate "$udid" "$CHROME" 2>/dev/null || true
84
85
  began=$(date -u +%Y-%m-%dT%H:%M:%S)
85
86
  xcrun simctl launch "$udid" "$CHROME" >/dev/null
86
87
  arrived=
87
- for _ in 1 2 3 4 5 6 7 8 9 10 11 12; do
88
+ # A busy Mac (a gate's sweeps running beside it) can take well over 12 s to restore the browser's tab: wait SIM_ARRIVE seconds.
89
+ waited_for=0
90
+ while [ "$waited_for" -lt "${SIM_ARRIVE:-30}" ]; do
88
91
  grep -q "GET /?$entry " "$asked" && {
89
92
  arrived=1
90
93
  break
91
94
  }
92
95
  sleep 1
96
+ waited_for=$((waited_for + 1))
93
97
  done
94
98
  [ -n "$arrived" ] || {
95
99
  xcrun simctl openurl "$udid" "chromium://localhost:$port/" >/dev/null 2>&1 || true