roamwright 0.61.24 → 0.61.25
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/README.md +2 -1
- package/examples/title-backdrop.html +52 -0
- package/examples/title-backdrop.js +160 -0
- package/index.js +25 -0
- package/package.json +1 -1
- package/rendering/scene-flash.js +105 -0
- package/rendering/title-backdrop-sprites.js +342 -0
- package/rendering/title-backdrop.js +212 -0
- package/runtime/article-page.js +128 -0
- package/runtime/articles.js +177 -0
- package/runtime/hud-room.js +103 -0
- package/runtime/journey.js +60 -0
- package/runtime/site-pages.js +343 -0
- package/tools/ios-sim.sh +5 -1
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plain-text encyclopedia article parser, link extractor, plain-text formatter, and lazy-loaded
|
|
3
|
+
* article catalog for people and glossary terms. Worker-safe: world data and `[[link]]` resolvers
|
|
4
|
+
* enter via parameters.
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { defaultSlug } from './url-router.js';
|
|
9
|
+
|
|
10
|
+
/** Matches inline wiki links written as `[[Target]]` or `[[Target|Shown words]]`. */
|
|
11
|
+
export const ARTICLE_LINK = /\[\[([^\]|]+)(?:\|([^\]]+))?\]\]/g;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Splits plain-text article markup into sections of paragraphs.
|
|
15
|
+
*
|
|
16
|
+
* Lines starting with `## ` begin titled sections; paragraphs are separated by blank lines and
|
|
17
|
+
* internal whitespace is collapsed to single spaces. Any text preceding the first heading is
|
|
18
|
+
* returned as a section with `title: null`.
|
|
19
|
+
*
|
|
20
|
+
* @param {string} text Raw article markup.
|
|
21
|
+
* @returns {Array<{ title: string | null, paras: string[] }>}
|
|
22
|
+
*/
|
|
23
|
+
export function parseArticle(text) {
|
|
24
|
+
const sections = [];
|
|
25
|
+
for (const chunk of text.trim().split(/^## /m)) {
|
|
26
|
+
if (!chunk.trim()) continue;
|
|
27
|
+
const titled = sections.length || text.trimStart().startsWith('## ');
|
|
28
|
+
const [first, ...rest] = chunk.split('\n');
|
|
29
|
+
const body = titled ? rest.join('\n') : chunk;
|
|
30
|
+
sections.push({
|
|
31
|
+
title: titled ? first.trim() : null,
|
|
32
|
+
paras: body
|
|
33
|
+
.split(/\n\s*\n/)
|
|
34
|
+
.map(p => p.replace(/\s+/g, ' ').trim())
|
|
35
|
+
.filter(Boolean),
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
return sections;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Strips `[[Target]]` / `[[Target|Shown]]` link syntax from a paragraph, keeping the displayed words.
|
|
43
|
+
*
|
|
44
|
+
* @param {string} para Paragraph text possibly containing wiki links.
|
|
45
|
+
* @returns {string}
|
|
46
|
+
*/
|
|
47
|
+
export function plainArticleParagraph(para) {
|
|
48
|
+
return para.replace(ARTICLE_LINK, (_m, name, shown) => shown ?? name);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Flattens a parsed article entry (lede, section headings, and paragraphs) into plain text suitable
|
|
53
|
+
* for static-site indexing or search snippets.
|
|
54
|
+
*
|
|
55
|
+
* @param {{ lede?: string, sections: Array<{ title: string | null, paras: string[] }> }} entry
|
|
56
|
+
* @returns {string}
|
|
57
|
+
*/
|
|
58
|
+
export function formatArticleText(entry) {
|
|
59
|
+
return [entry.lede, ...entry.sections.flatMap(s => [s.title ? s.title + '.' : '', ...s.paras.map(plainArticleParagraph)])]
|
|
60
|
+
.filter(Boolean)
|
|
61
|
+
.join(' ');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Collects every resolved target linked from an entry's article sections, deduplicated by `path`
|
|
66
|
+
* in order of first mention (excluding self-links back to `entry.path`).
|
|
67
|
+
*
|
|
68
|
+
* @param {{ path: string, sections: Array<{ title: string | null, paras: string[] }> }} entry
|
|
69
|
+
* @param {(name: string) => ({ path: string } | null | undefined)} resolveLink
|
|
70
|
+
* @returns {Array<{ path: string }>}
|
|
71
|
+
*/
|
|
72
|
+
export function collectArticleLinks(entry, resolveLink) {
|
|
73
|
+
const seen = new Set([entry.path]);
|
|
74
|
+
const out = [];
|
|
75
|
+
for (const section of entry.sections) {
|
|
76
|
+
for (const para of section.paras) {
|
|
77
|
+
for (const [, name] of para.matchAll(ARTICLE_LINK)) {
|
|
78
|
+
const target = resolveLink(name);
|
|
79
|
+
if (target && !seen.has(target.path)) {
|
|
80
|
+
seen.add(target.path);
|
|
81
|
+
out.push(target);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return out;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Creates a lazy-loaded catalog of person and glossary-term articles backed by a caller-supplied
|
|
91
|
+
* link resolver and world data accessors.
|
|
92
|
+
*
|
|
93
|
+
* @param {object} options
|
|
94
|
+
* @param {() => Promise<{ personArticles: Record<string, string>, termArticles: Record<string, Record<string, string>> }>} options.load
|
|
95
|
+
* Async loader returning `{ personArticles, termArticles }`. Invoked at most once.
|
|
96
|
+
* @param {(name: string) => ({ kind: string, path: string, name: string, id?: string } | null | undefined)} options.resolveLink
|
|
97
|
+
* Resolves a link name or slug into a target descriptor.
|
|
98
|
+
* @param {() => Record<string, { name?: string, desc?: string }>} options.getCharacters
|
|
99
|
+
* Returns the world's character table keyed by character id.
|
|
100
|
+
* @param {() => Array<{ id: string, title: string, intro?: string, terms: Array<[string, string, object?]> }>} options.getSections
|
|
101
|
+
* Returns the glossary sections and their term tuples `[name, lede, meta]`.
|
|
102
|
+
* @param {(text: string) => string} [options.slug=defaultSlug] Slugifier matching route paths.
|
|
103
|
+
* @returns {{ load: () => Promise<void>, hasLoaded: () => boolean,
|
|
104
|
+
* findEntry: (path: string) => object | null,
|
|
105
|
+
* entryText: (entry: object) => string,
|
|
106
|
+
* entryLinks: (entry: object) => Array<{ path: string }> }}
|
|
107
|
+
*/
|
|
108
|
+
export function createArticleCatalog({ load, resolveLink, getCharacters, getSections, slug = defaultSlug }) {
|
|
109
|
+
let texts = null;
|
|
110
|
+
let fetching = null;
|
|
111
|
+
|
|
112
|
+
function loadArticles() {
|
|
113
|
+
fetching ??= Promise.resolve()
|
|
114
|
+
.then(load)
|
|
115
|
+
.then(module => {
|
|
116
|
+
texts = module;
|
|
117
|
+
});
|
|
118
|
+
return fetching;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function hasLoaded() {
|
|
122
|
+
return !!texts;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function personArticle(name) {
|
|
126
|
+
for (const [id, character] of Object.entries(getCharacters())) {
|
|
127
|
+
if (character.name === name && texts.personArticles[id]) return { id, text: texts.personArticles[id] };
|
|
128
|
+
}
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function findEntry(path) {
|
|
133
|
+
if (!texts) throw new Error('findEntry: the articles are not loaded (loadArticles)');
|
|
134
|
+
const [kind, a, b] = path.split('/');
|
|
135
|
+
if (kind === 'person') {
|
|
136
|
+
const target = resolveLink(a ?? '');
|
|
137
|
+
if (target?.kind !== 'person') return null;
|
|
138
|
+
const character = getCharacters()[target.id];
|
|
139
|
+
const art = personArticle(character.name);
|
|
140
|
+
return {
|
|
141
|
+
kind,
|
|
142
|
+
path: target.path,
|
|
143
|
+
name: character.name,
|
|
144
|
+
id: art?.id ?? target.id,
|
|
145
|
+
lede: character.desc,
|
|
146
|
+
sections: art ? parseArticle(art.text) : [],
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
if (kind !== 'wiki' || !b) return null;
|
|
150
|
+
const section = getSections().find(s => s.id === a);
|
|
151
|
+
const term = section?.terms.find(t => slug(t[0]) === b);
|
|
152
|
+
if (!term) return null;
|
|
153
|
+
const [name, lede, meta = {}] = term;
|
|
154
|
+
const own = texts.termArticles[section.id]?.[b];
|
|
155
|
+
const shared = own ? null : personArticle(name);
|
|
156
|
+
return {
|
|
157
|
+
kind: 'term',
|
|
158
|
+
path,
|
|
159
|
+
name,
|
|
160
|
+
section,
|
|
161
|
+
lede,
|
|
162
|
+
also: meta.also,
|
|
163
|
+
book: meta.book,
|
|
164
|
+
who: meta.who ?? [],
|
|
165
|
+
id: shared?.id,
|
|
166
|
+
sections: parseArticle(own ?? shared?.text ?? ''),
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return {
|
|
171
|
+
load: loadArticles,
|
|
172
|
+
hasLoaded,
|
|
173
|
+
findEntry,
|
|
174
|
+
entryText: formatArticleText,
|
|
175
|
+
entryLinks: entry => collectArticleLinks(entry, resolveLink),
|
|
176
|
+
};
|
|
177
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Periodic HUD bounding-box tracker so world-space labels, nameplates, and waypoints avoid
|
|
3
|
+
* overlapping interface chrome without forcing browser layout every frame.
|
|
4
|
+
* Worker-safe: DOM lookups and rectangle readers enter via parameters.
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Creates a throttled HUD exclusion-box tracker.
|
|
10
|
+
*
|
|
11
|
+
* Elements are read in three ordered groups:
|
|
12
|
+
* 1. `rightColumn` — boxes forming the right-hand HUD column; their union is tracked on `room.right`
|
|
13
|
+
* (`{ left, bottom }`) and counted in `room.shared`.
|
|
14
|
+
* 2. `conditional` — overlay boxes included only when they hold non-empty `textContent` (such as
|
|
15
|
+
* captions or scene buttons), also counted in `room.shared`.
|
|
16
|
+
* 3. `extra` — remaining HUD controls checked only by `overlapsAll` (or when passing `room.count`).
|
|
17
|
+
*
|
|
18
|
+
* @param {object} [options]
|
|
19
|
+
* @param {(id: string) => { textContent?: string | null, getBoundingClientRect?: () => { left: number, top: number, width: number, height: number } } | null} [options.getElement]
|
|
20
|
+
* Looks up an element by id (defaults to `document.getElementById` when available).
|
|
21
|
+
* @param {(el: any) => { left: number, top: number, width: number, height: number }} [options.getRect]
|
|
22
|
+
* Measures an element's box in stage/page pixels (defaults to `el.getBoundingClientRect()`).
|
|
23
|
+
* @param {string[]} [options.rightColumn=[]] Element ids stacked along the right edge.
|
|
24
|
+
* @param {string[]} [options.conditional=[]] Element ids kept only while `el.textContent` is non-empty.
|
|
25
|
+
* @param {string[]} [options.extra=[]] Additional HUD element ids checked by `overlapsAll`.
|
|
26
|
+
* @param {number} [options.readEvery=0.5] Minimum interval in seconds between layout reads.
|
|
27
|
+
* @returns {{ room: { right: { left: number, bottom: number }, boxes: Float32Array, count: number, shared: number, wait: number },
|
|
28
|
+
* read: () => void, update: (dt: number) => void,
|
|
29
|
+
* overlaps: (x0: number, y0: number, x1: number, y1: number, count?: number) => boolean,
|
|
30
|
+
* overlapsAll: (x0: number, y0: number, x1: number, y1: number) => boolean }}
|
|
31
|
+
*/
|
|
32
|
+
export function createHudRoom({
|
|
33
|
+
getElement = id => (typeof document !== 'undefined' ? document.getElementById(id) : null),
|
|
34
|
+
getRect = el => el.getBoundingClientRect(),
|
|
35
|
+
rightColumn = [],
|
|
36
|
+
conditional = [],
|
|
37
|
+
extra = [],
|
|
38
|
+
readEvery = 0.5,
|
|
39
|
+
} = {}) {
|
|
40
|
+
const maxBoxes = rightColumn.length + conditional.length + extra.length;
|
|
41
|
+
const room = {
|
|
42
|
+
right: { left: 1e9, bottom: 0 },
|
|
43
|
+
boxes: new Float32Array(maxBoxes * 4),
|
|
44
|
+
count: 0,
|
|
45
|
+
shared: 0,
|
|
46
|
+
wait: 0,
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
function keep(rect) {
|
|
50
|
+
if (!rect || !rect.width || room.count >= maxBoxes) return;
|
|
51
|
+
const offset = room.count++ * 4;
|
|
52
|
+
room.boxes[offset] = rect.left;
|
|
53
|
+
room.boxes[offset + 1] = rect.top;
|
|
54
|
+
room.boxes[offset + 2] = rect.left + rect.width;
|
|
55
|
+
room.boxes[offset + 3] = rect.top + rect.height;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function read() {
|
|
59
|
+
const right = room.right;
|
|
60
|
+
right.left = 1e9;
|
|
61
|
+
right.bottom = 0;
|
|
62
|
+
room.count = 0;
|
|
63
|
+
for (let i = 0; i < rightColumn.length; i++) {
|
|
64
|
+
const el = getElement(rightColumn[i]);
|
|
65
|
+
if (!el) continue;
|
|
66
|
+
const rect = getRect(el);
|
|
67
|
+
if (!rect || !rect.width) continue;
|
|
68
|
+
right.left = Math.min(right.left, rect.left);
|
|
69
|
+
right.bottom = Math.max(right.bottom, rect.top + rect.height);
|
|
70
|
+
keep(rect);
|
|
71
|
+
}
|
|
72
|
+
for (let i = 0; i < conditional.length; i++) {
|
|
73
|
+
const el = getElement(conditional[i]);
|
|
74
|
+
if (el && el.textContent) keep(getRect(el));
|
|
75
|
+
}
|
|
76
|
+
room.shared = room.count;
|
|
77
|
+
for (let i = 0; i < extra.length; i++) {
|
|
78
|
+
const el = getElement(extra[i]);
|
|
79
|
+
if (el) keep(getRect(el));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function update(dt) {
|
|
84
|
+
if ((room.wait -= dt) > 0) return;
|
|
85
|
+
room.wait = readEvery;
|
|
86
|
+
read();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function overlaps(x0, y0, x1, y1, count = room.shared) {
|
|
90
|
+
const boxes = room.boxes;
|
|
91
|
+
for (let i = 0; i < count; i++) {
|
|
92
|
+
const offset = i * 4;
|
|
93
|
+
if (x1 > boxes[offset] && x0 < boxes[offset + 2] && y1 > boxes[offset + 1] && y0 < boxes[offset + 3]) return true;
|
|
94
|
+
}
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function overlapsAll(x0, y0, x1, y1) {
|
|
99
|
+
return overlaps(x0, y0, x1, y1, room.count);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return { room, read, update, overlaps, overlapsAll };
|
|
103
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cross-land journey persistence and arrival-handler registry.
|
|
3
|
+
*
|
|
4
|
+
* When travelling between separately loaded lands causes a fresh page load, the journey's
|
|
5
|
+
* destination, travel mode (flying, altitude), return point, and arrival aim (`{ kind, id }`)
|
|
6
|
+
* are recorded in session storage and consumed on the far side once the matching landing spot loads.
|
|
7
|
+
* Worker-safe: storage access enters via an optional getter.
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Creates a journey store backed by session storage (or an injected Storage-compatible object).
|
|
13
|
+
*
|
|
14
|
+
* @param {object} [options]
|
|
15
|
+
* @param {string} [options.storageKey='roamwright:journey'] Session-storage key for the pending journey record.
|
|
16
|
+
* @param {() => ({ getItem: (k: string) => string | null, setItem: (k: string, v: string) => void, removeItem: (k: string) => void } | null | undefined)} [options.getStorage]
|
|
17
|
+
* Returns the storage backend (defaults to `globalThis.sessionStorage`).
|
|
18
|
+
* @returns {{ onArrival: (kind: string, act: (id: any) => void) => void,
|
|
19
|
+
* arrive: (aim: { kind: string, id?: any }) => void,
|
|
20
|
+
* note: (journey: { land: string, x: number, z: number, up?: number, fly?: boolean, aim?: { kind: string, id?: any } | null, from?: any }) => void,
|
|
21
|
+
* take: (land: string, x: number, z: number) => object | null }}
|
|
22
|
+
*/
|
|
23
|
+
export function createJourneyStore({
|
|
24
|
+
storageKey = 'roamwright:journey',
|
|
25
|
+
getStorage = () => (typeof sessionStorage !== 'undefined' ? sessionStorage : null),
|
|
26
|
+
} = {}) {
|
|
27
|
+
const arrivals = {};
|
|
28
|
+
|
|
29
|
+
function onArrival(kind, act) {
|
|
30
|
+
arrivals[kind] = act;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function arrive(aim) {
|
|
34
|
+
arrivals[aim.kind]?.(aim.id);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function note(journey) {
|
|
38
|
+
try {
|
|
39
|
+
const storage = getStorage();
|
|
40
|
+
if (storage) storage.setItem(storageKey, JSON.stringify(journey));
|
|
41
|
+
} catch {
|
|
42
|
+
// Storage unavailable (for example a restricted private window): travel proceeds without the arrival note.
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function take(land, x, z) {
|
|
47
|
+
let journey = null;
|
|
48
|
+
try {
|
|
49
|
+
const storage = getStorage();
|
|
50
|
+
if (!storage) return null;
|
|
51
|
+
journey = JSON.parse(storage.getItem(storageKey));
|
|
52
|
+
storage.removeItem(storageKey);
|
|
53
|
+
} catch {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
return journey && journey.land === land && journey.x === x && journey.z === z ? journey : null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return { onArrival, arrive, note, take };
|
|
60
|
+
}
|
|
@@ -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
|
+
}
|