@volvel/docs-engine 0.0.0-stage → 0.6.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/dist/set.js ADDED
@@ -0,0 +1,566 @@
1
+ import { imageKind, linkKind, TERM_RESOURCES, topicOf, walk } from "@volvel/markup";
2
+ import { CHANGELOG_NAME, changelogDate, newestFirst } from "./changelog.js";
3
+ import { DOC_TYPE_PLURALS, DOC_TYPE_SUMMARIES, DOC_TYPES, diataxisIssues, effectiveType, folderType, MAX_SUBJECTS, } from "./diataxis.js";
4
+ import { titleOf } from "./page.js";
5
+ import { ABOUT_KINDS } from "./schema.js";
6
+ /** Every new term and topic of the product's own help starts with this, so none of a tenant's can collide with it. */
7
+ export const PRODUCT_PREFIX = "volvel-";
8
+ export const UNCATEGORIZED = "Uncategorized";
9
+ /** A page's address under its set's: `guides/index` is `guides`, and `index` is the set's own. */
10
+ export function urlOf(path) {
11
+ if (path === "index")
12
+ return "";
13
+ return path.endsWith("/index") ? path.slice(0, -"/index".length) : path;
14
+ }
15
+ function ref(entry, docType) {
16
+ const made = {
17
+ type: "page",
18
+ path: entry.path,
19
+ url: urlOf(entry.path),
20
+ title: entry.frontmatter.title,
21
+ };
22
+ if (entry.frontmatter.description)
23
+ made.description = entry.frontmatter.description;
24
+ if (docType)
25
+ made.docType = docType;
26
+ return made;
27
+ }
28
+ /** The name an item is listed by in its folder's `pages:`: a page's file name, or a folder's own. */
29
+ export function nameOf(item) {
30
+ return item.type === "folder" ? item.id : item.path.split("/").at(-1);
31
+ }
32
+ /**
33
+ * Puts a folder's items in order: those its own page lists, as listed, and
34
+ * then the rest by title.
35
+ */
36
+ export function ordered(items, listed) {
37
+ const at = new Map((listed ?? []).map((name, place) => [name, place]));
38
+ const place = (item) => at.get(nameOf(item)) ?? Number.POSITIVE_INFINITY;
39
+ return [...items].sort((a, b) => place(a) - place(b) || a.title.localeCompare(b.title, "en", { sensitivity: "base" }));
40
+ }
41
+ /** A path with `.` and `..` worked out, or nothing when it climbs out of the root. */
42
+ function normalize(from, to) {
43
+ const parts = to.startsWith("/") ? [] : [...from];
44
+ for (const part of to.split("/")) {
45
+ if (part === "" || part === ".")
46
+ continue;
47
+ if (part === "..") {
48
+ if (parts.length === 0)
49
+ return undefined;
50
+ parts.pop();
51
+ }
52
+ else
53
+ parts.push(part);
54
+ }
55
+ return parts;
56
+ }
57
+ /**
58
+ * Works out which page a link goes to. A link is written from where its file
59
+ * is: on `guides/refunds`, `returns` is `guides/returns` and `../index` is the
60
+ * home page. `.md` on the end is allowed, and a folder means its index.
61
+ */
62
+ export function resolveLink(set, from, href) {
63
+ const topic = topicOf(href);
64
+ if (topic) {
65
+ const path = set.topics[topic.topic];
66
+ if (path === undefined)
67
+ return { missing: "topic", wanted: topic.topic };
68
+ return topic.anchor ? { path, anchor: topic.anchor } : { path };
69
+ }
70
+ const [beforeHash = "", ...hash] = href.trim().split("#");
71
+ const anchor = hash.join("#");
72
+ const written = decodeURI(beforeHash.split("?")[0] ?? "");
73
+ let path = from;
74
+ if (written !== "") {
75
+ const parts = normalize(from.split("/").slice(0, -1), written.replace(/\.md$/, ""));
76
+ if (!parts)
77
+ return { missing: "outside", wanted: written };
78
+ const plain = parts.join("/");
79
+ const found = [plain, plain === "" ? "index" : `${plain}/index`].find((one) => set.pages.has(one));
80
+ if (found === undefined)
81
+ return { missing: "page", wanted: plain };
82
+ path = found;
83
+ }
84
+ return anchor === "" ? { path } : { path, anchor };
85
+ }
86
+ /** Where a picture written on one page is, as a path under the docs root. */
87
+ export function resolveAsset(from, src) {
88
+ const parts = normalize(from.split("/").slice(0, -1), decodeURI(src.trim().split(/[?#]/)[0] ?? ""));
89
+ return parts && parts.length > 0 ? parts.join("/") : undefined;
90
+ }
91
+ function headings(tree) {
92
+ const ids = new Set();
93
+ for (const { node } of walk(tree))
94
+ if (node.type === "heading")
95
+ ids.add(node.id);
96
+ return ids;
97
+ }
98
+ /** A resource as an attachment names it. */
99
+ export function resourceKey(kind, path) {
100
+ return `${kind}:${path}`;
101
+ }
102
+ /** What is wrong with one page's links, pictures and terms, beside the pages around it. */
103
+ export function crossIssues(entry, around) {
104
+ const issues = [];
105
+ const error = (code, message, line) => {
106
+ issues.push({ path: entry.path, line, column: 1, code, message, severity: "error" });
107
+ };
108
+ for (const { node } of walk(entry.tree)) {
109
+ const line = node.line ?? 1;
110
+ if (node.type === "link" && linkKind(node.href) === "relative") {
111
+ const target = resolveLink(around, entry.path, node.href);
112
+ if ("missing" in target) {
113
+ error(target.missing === "topic" ? "topic_unknown" : "link_broken", target.missing === "topic"
114
+ ? `The link to the topic "${target.wanted}" leads nowhere: no page has that topic.`
115
+ : target.missing === "outside"
116
+ ? `The link to "${node.href}" leads out of the documentation.`
117
+ : `The link to "${node.href}" leads nowhere: there is no page at ${target.wanted || "the home page"}.`, line);
118
+ }
119
+ else if (target.anchor !== undefined &&
120
+ !around.headingsOf(target.path).has(target.anchor)) {
121
+ error("anchor_missing", `The link to "${node.href}" names a heading that ${target.path === entry.path ? "this page" : target.path} does not have: #${target.anchor}.`, line);
122
+ }
123
+ }
124
+ if (node.type === "image" && around.asset && imageKind(node.src) === "relative") {
125
+ const asset = resolveAsset(entry.path, node.src);
126
+ if (asset === undefined || !around.asset(asset)) {
127
+ error("image_missing", `The picture "${node.src}" is not there${asset ? `: no file at ${asset}` : ""}.`, line);
128
+ }
129
+ }
130
+ if (node.type === "component" && node.name === "term") {
131
+ const { id } = node.props;
132
+ if (id !== undefined && !around.knownTerms.has(id)) {
133
+ error("term_unknown", `There is no term "${id}". A term is defined in the terms: of a page's frontmatter.`, line);
134
+ }
135
+ const kind = TERM_RESOURCES.find((one) => node.props[one] !== undefined);
136
+ if (kind && around.resource) {
137
+ const path = node.props[kind];
138
+ if (!around.resource(kind, path, node.props.column)) {
139
+ error("term_unknown", node.props.column
140
+ ? `There is no column ${node.props.column} on the ${kind} ${path}.`
141
+ : `There is no ${kind} ${path}.`, line);
142
+ }
143
+ }
144
+ }
145
+ }
146
+ return issues;
147
+ }
148
+ /** Builds a set from pages already read, and says what is wrong across them. */
149
+ export function buildDocSet(entries, options = {}) {
150
+ const issues = [];
151
+ const error = (path, code, message, line = 1, column = 1) => {
152
+ issues.push({ path, line, column, code, message, severity: "error" });
153
+ };
154
+ const warn = (path, code, message) => {
155
+ issues.push({ path, line: 1, column: 1, code, message, severity: "warning" });
156
+ };
157
+ const pages = new Map();
158
+ for (const entry of entries)
159
+ pages.set(entry.path, entry);
160
+ // Types: a page's own, else its folder's. An index is a landing page and has none.
161
+ const types = {};
162
+ const own = (path) => pages.get(path)?.frontmatter.type;
163
+ for (const path of [...pages.keys()].sort()) {
164
+ const found = effectiveType(path, own);
165
+ if (found)
166
+ types[path] = found;
167
+ }
168
+ const refOf = (entry) => ref(entry, types[entry.path]);
169
+ // Structure.
170
+ const folders = new Set();
171
+ for (const path of pages.keys()) {
172
+ const parts = path.split("/");
173
+ for (let depth = 1; depth < parts.length; depth++)
174
+ folders.add(parts.slice(0, depth).join("/"));
175
+ }
176
+ const inFolder = new Map();
177
+ const held = (folder) => {
178
+ let found = inFolder.get(folder);
179
+ if (!found) {
180
+ found = { pages: [], folders: new Set() };
181
+ inFolder.set(folder, found);
182
+ }
183
+ return found;
184
+ };
185
+ for (const entry of pages.values()) {
186
+ const parts = entry.path.split("/");
187
+ const name = parts.at(-1);
188
+ if (name !== "index" && folders.has(entry.path)) {
189
+ error(entry.path, "file_and_folder", `${entry.path}.md sits beside a folder named ${name}. A folder's own page is the index.md inside it: move this file to ${entry.path}/index.md.`);
190
+ }
191
+ if (name !== "index")
192
+ held(parts.slice(0, -1).join("/")).pages.push(entry);
193
+ }
194
+ for (const folder of folders) {
195
+ const parts = folder.split("/");
196
+ held(parts.slice(0, -1).join("/")).folders.add(parts.at(-1));
197
+ }
198
+ const indexOf = (folder) => pages.get(folder === "" ? "index" : `${folder}/index`);
199
+ const itemsOf = (folder) => {
200
+ const here = inFolder.get(folder);
201
+ if (!here)
202
+ return [];
203
+ const prefix = folder === "" ? "" : `${folder}/`;
204
+ const items = [
205
+ ...here.pages.map(refOf),
206
+ ...[...here.folders].map((name) => folderAt(`${prefix}${name}`)),
207
+ ];
208
+ const index = indexOf(folder);
209
+ const listed = index?.frontmatter.pages;
210
+ if (index && listed) {
211
+ const names = new Set(items.map(nameOf));
212
+ for (const name of listed) {
213
+ if (name !== "index" && !names.has(name)) {
214
+ warn(index.path, "pages_unknown", `pages: lists "${name}", and there is no page or folder of that name in ${folder === "" ? "this folder" : folder}.`);
215
+ }
216
+ }
217
+ }
218
+ const sorted = ordered(items, listed);
219
+ // A changelog folder's pages that its index does not place run newest first, as `::changelog` lists them.
220
+ if (folder.split("/").at(-1) === CHANGELOG_NAME) {
221
+ const day = (item) => {
222
+ const page = item.type === "page" ? pages.get(item.path) : undefined;
223
+ return page ? changelogDate(item.path, page)?.date : undefined;
224
+ };
225
+ const place = (item) => (listed ?? []).indexOf(nameOf(item));
226
+ return sorted.sort((a, b) => {
227
+ const one = place(a) < 0 ? Number.POSITIVE_INFINITY : place(a);
228
+ const two = place(b) < 0 ? Number.POSITIVE_INFINITY : place(b);
229
+ if (one !== two)
230
+ return one - two;
231
+ if ((a.type === "page") !== (b.type === "page"))
232
+ return a.type === "page" ? -1 : 1;
233
+ if (a.type !== "page" || b.type !== "page")
234
+ return 0;
235
+ return newestFirst({ path: a.path, date: day(a) }, { path: b.path, date: day(b) });
236
+ });
237
+ }
238
+ return sorted;
239
+ };
240
+ const folderAt = (path) => {
241
+ const name = path.split("/").at(-1);
242
+ const index = indexOf(path);
243
+ const made = {
244
+ type: "folder",
245
+ id: name,
246
+ path,
247
+ title: index?.frontmatter.title ?? titleOf(name),
248
+ items: itemsOf(path),
249
+ };
250
+ if (index?.frontmatter.icon)
251
+ made.icon = index.frontmatter.icon;
252
+ if (index?.frontmatter.description)
253
+ made.description = index.frontmatter.description;
254
+ if (index)
255
+ made.index = refOf(index);
256
+ return made;
257
+ };
258
+ const nav = { collections: [] };
259
+ const home = pages.get("index");
260
+ if (home)
261
+ nav.home = refOf(home);
262
+ const top = itemsOf("");
263
+ for (const item of top)
264
+ if (item.type === "folder")
265
+ nav.collections.push(item);
266
+ const loose = top.filter((item) => item.type === "page");
267
+ // Pages in no folder come last, as a collection of their own.
268
+ if (loose.length > 0) {
269
+ nav.collections.push({ type: "folder", id: "", path: "", title: UNCATEGORIZED, items: loose });
270
+ }
271
+ // Topics, terms, attachments and extensions.
272
+ const reservedTerms = new Set(options.reservedTerms ?? []);
273
+ const reservedTopics = new Set(options.reservedTopics ?? []);
274
+ const prefixed = (name) => options.reservedPrefix !== undefined && name.startsWith(options.reservedPrefix);
275
+ const topics = {};
276
+ const terms = {};
277
+ const attachments = {};
278
+ const extensions = {};
279
+ const byPath = [...pages.values()].sort((a, b) => a.path.localeCompare(b.path));
280
+ for (const entry of byPath) {
281
+ const { topic } = entry.frontmatter;
282
+ if (topic !== undefined) {
283
+ if (reservedTopics.has(topic) || prefixed(topic)) {
284
+ error(entry.path, "topic_reserved", `The topic "${topic}" is one of the product's own. A page can add notes to it with extends: [${topic}], and cannot take its name.`);
285
+ }
286
+ else if (topics[topic] !== undefined) {
287
+ error(entry.path, "topic_duplicate", `The topic "${topic}" is also the topic of ${topics[topic]}. A topic belongs to one page.`);
288
+ }
289
+ else
290
+ topics[topic] = entry.path;
291
+ }
292
+ for (const term of entry.frontmatter.terms ?? []) {
293
+ if (reservedTerms.has(term.id) || prefixed(term.id)) {
294
+ error(entry.path, "term_reserved", `The term "${term.id}" is one of the product's own, and cannot be defined again. Give yours another id.`);
295
+ }
296
+ else if (terms[term.id] !== undefined) {
297
+ error(entry.path, "term_duplicate", `The term "${term.id}" is also defined on ${terms[term.id]?.path}. A term is defined once.`);
298
+ }
299
+ else
300
+ terms[term.id] = { ...term, path: entry.path };
301
+ }
302
+ for (const about of entry.frontmatter.about ?? []) {
303
+ for (const kind of ABOUT_KINDS) {
304
+ const path = about[kind];
305
+ if (path === undefined)
306
+ continue;
307
+ if (options.resource && !options.resource(kind, path)) {
308
+ error(entry.path, "about_unknown", `This page is about the ${kind} ${path}, and there is no such ${kind}.`);
309
+ continue;
310
+ }
311
+ const key = resourceKey(kind, path);
312
+ attachments[key] = [...(attachments[key] ?? []), entry.path];
313
+ }
314
+ }
315
+ for (const extended of entry.frontmatter.extends ?? []) {
316
+ if (options.reservedTopics !== undefined && !reservedTopics.has(extended)) {
317
+ error(entry.path, "extends_unknown", `This page extends "${extended}", and the product's help has no such topic.`);
318
+ continue;
319
+ }
320
+ extensions[extended] = [...(extensions[extended] ?? []), entry.path];
321
+ }
322
+ }
323
+ // Subjects, and where moved pages used to be.
324
+ const subjects = {};
325
+ const redirects = {};
326
+ for (const entry of byPath) {
327
+ for (const subject of (entry.frontmatter.subjects ?? []).slice(0, MAX_SUBJECTS)) {
328
+ subjects[subject] = [...(subjects[subject] ?? []), entry.path];
329
+ }
330
+ for (const old of entry.frontmatter.moved_from ?? []) {
331
+ if (old === entry.path) {
332
+ error(entry.path, "moved_from_self", `moved_from lists "${old}", which is this page's own path.`);
333
+ }
334
+ else if (pages.has(old)) {
335
+ error(entry.path, "moved_from_exists", `moved_from lists "${old}", and there is a page at that path. A path that is still a page cannot lead somewhere else: take it out of moved_from, or move that page.`);
336
+ }
337
+ else if (redirects[old] !== undefined) {
338
+ error(entry.path, "moved_from_duplicate", `moved_from lists "${old}", which ${redirects[old]} also lists. An old path leads to one page.`);
339
+ }
340
+ else
341
+ redirects[old] = entry.path;
342
+ }
343
+ }
344
+ // Types and subjects, said for what they are: warnings.
345
+ for (const entry of byPath) {
346
+ issues.push(...diataxisIssues(entry, {
347
+ type: types[entry.path],
348
+ folderTyped: folderType(entry.path, own) !== undefined,
349
+ subjectUses: (subject) => subjects[subject]?.length ?? 0,
350
+ }));
351
+ }
352
+ // Links, pictures and terms, now that every page and topic is known.
353
+ const set = {
354
+ pages,
355
+ nav,
356
+ topics,
357
+ terms,
358
+ attachments,
359
+ extensions,
360
+ types,
361
+ subjects,
362
+ redirects,
363
+ };
364
+ const knownTerms = new Set([
365
+ ...reservedTerms,
366
+ ...(options.knownTerms ?? []),
367
+ ...Object.keys(terms),
368
+ ]);
369
+ const anchors = new Map();
370
+ const around = {
371
+ pages,
372
+ topics,
373
+ knownTerms,
374
+ headingsOf: (path) => {
375
+ let found = anchors.get(path);
376
+ if (!found) {
377
+ found = headings(pages.get(path).tree);
378
+ anchors.set(path, found);
379
+ }
380
+ return found;
381
+ },
382
+ ...(options.asset ? { asset: options.asset } : {}),
383
+ ...(options.resource ? { resource: options.resource } : {}),
384
+ };
385
+ for (const entry of byPath)
386
+ issues.push(...crossIssues(entry, around));
387
+ issues.sort((a, b) => a.path.localeCompare(b.path) || a.line - b.line || a.column - b.column);
388
+ return { set, issues };
389
+ }
390
+ /** Every page of a navigation, in the order a reader meets them. */
391
+ export function pagesInOrder(nav) {
392
+ const out = [];
393
+ const visit = (item) => {
394
+ if (item.type === "page")
395
+ out.push(item);
396
+ else {
397
+ if (item.index)
398
+ out.push(item.index);
399
+ for (const inner of item.items)
400
+ visit(inner);
401
+ }
402
+ };
403
+ if (nav.home)
404
+ out.push(nav.home);
405
+ for (const collection of nav.collections)
406
+ visit(collection);
407
+ return out;
408
+ }
409
+ /**
410
+ * The page before and the page after one, in the order of its collection: the first page of a
411
+ * collection has no page before it, and the last none after, so a reader is never carried into
412
+ * another collection by "Next". The home page, which is in none, goes on to the first of all.
413
+ * The one function the served site's pager and the console's help panel both use.
414
+ */
415
+ export function neighboursOf(nav, path) {
416
+ const own = trailOf(nav, path)[0];
417
+ const order = pagesInOrder(own ? { collections: [own] } : nav);
418
+ const at = order.findIndex((one) => one.path === path);
419
+ const before = at > 0 ? order[at - 1] : undefined;
420
+ const after = at >= 0 ? order[at + 1] : undefined;
421
+ return { ...(before ? { before } : {}), ...(after ? { after } : {}) };
422
+ }
423
+ /** The folders from a collection down to a page, outermost first. Empty when the page is in none. */
424
+ export function trailOf(nav, path) {
425
+ const find = (folder, trail) => {
426
+ const here = [...trail, folder];
427
+ if (folder.index?.path === path)
428
+ return here;
429
+ for (const item of folder.items) {
430
+ if (item.type === "page" && item.path === path)
431
+ return here;
432
+ if (item.type === "folder") {
433
+ const found = find(item, here);
434
+ if (found)
435
+ return found;
436
+ }
437
+ }
438
+ return undefined;
439
+ };
440
+ for (const collection of nav.collections) {
441
+ const found = find(collection, []);
442
+ if (found)
443
+ return found;
444
+ }
445
+ return [];
446
+ }
447
+ /**
448
+ * What a page's folder holds, for `::pages`: for a folder's own page, that
449
+ * folder; for any other page, the folder it is in. For the home page, the
450
+ * collections and the pages in no folder.
451
+ */
452
+ export function folderItems(nav, path) {
453
+ if (path === "index")
454
+ return nav.collections.flatMap((one) => (one.id === "" ? one.items : [one]));
455
+ return trailOf(nav, path).at(-1)?.items ?? [];
456
+ }
457
+ /**
458
+ * The terms `::glossary` lists on a page: those defined by the pages of the
459
+ * page's collection, or by every page of the set. A page in no collection, and
460
+ * the home page, list the set's.
461
+ */
462
+ export function glossaryOf(set, path, scope) {
463
+ const collection = path.includes("/") ? path.split("/")[0] : undefined;
464
+ return Object.values(set.terms).filter((term) => scope === "site" || collection === undefined || term.path.startsWith(`${collection}/`));
465
+ }
466
+ /** The most related pages shown of one type. */
467
+ export const RELATED_PER_TYPE = 4;
468
+ /**
469
+ * The pages of other types that are about the same things as a page: those
470
+ * that share its `subjects`, or the resources it is `about`. For each type,
471
+ * the pages with the most in common first, then those in the page's own
472
+ * collection, then in the order of the navigation. An untyped page, and a
473
+ * folder's index, have none.
474
+ */
475
+ export function relatedTo(set, path) {
476
+ const mine = set.types[path];
477
+ const entry = set.pages.get(path);
478
+ if (!mine || !entry)
479
+ return {};
480
+ const keys = (page) => {
481
+ const out = new Set();
482
+ for (const subject of (page.frontmatter.subjects ?? []).slice(0, MAX_SUBJECTS)) {
483
+ out.add(`subject:${subject}`);
484
+ }
485
+ for (const about of page.frontmatter.about ?? []) {
486
+ for (const kind of ABOUT_KINDS) {
487
+ const target = about[kind];
488
+ if (target !== undefined)
489
+ out.add(resourceKey(kind, target));
490
+ }
491
+ }
492
+ return out;
493
+ };
494
+ const wanted = keys(entry);
495
+ if (wanted.size === 0)
496
+ return {};
497
+ const order = pagesInOrder(set.nav);
498
+ const place = new Map(order.map((one, at) => [one.path, at]));
499
+ const collection = (at) => (at.includes("/") ? at.split("/")[0] : "");
500
+ const found = [];
501
+ for (const other of order) {
502
+ const type = set.types[other.path];
503
+ if (!type || type === mine || other.path === path)
504
+ continue;
505
+ const page = set.pages.get(other.path);
506
+ if (!page)
507
+ continue;
508
+ let shared = 0;
509
+ for (const key of keys(page))
510
+ if (wanted.has(key))
511
+ shared++;
512
+ if (shared === 0)
513
+ continue;
514
+ found.push({
515
+ page: other,
516
+ shared,
517
+ same: collection(other.path) === collection(path),
518
+ at: place.get(other.path) ?? 0,
519
+ });
520
+ }
521
+ found.sort((a, b) => b.shared - a.shared || Number(b.same) - Number(a.same) || a.at - b.at);
522
+ const out = {};
523
+ for (const { page } of found) {
524
+ const type = set.types[page.path];
525
+ const list = out[type] ?? [];
526
+ if (list.length < RELATED_PER_TYPE)
527
+ out[type] = [...list, page];
528
+ }
529
+ return out;
530
+ }
531
+ /**
532
+ * On a collection's own page, when its pages are of two types or more: a card for each, so that a
533
+ * reader says what they are doing and is taken to the first page that does it. Nothing for any
534
+ * other page. The served site and the editor's preview both draw these.
535
+ */
536
+ export function typeCardsOf(set, path) {
537
+ const trail = trailOf(set.nav, path);
538
+ const folder = trail.at(-1);
539
+ if (!folder || folder.index?.path !== path || trail.length !== 1 || folder.id === "")
540
+ return [];
541
+ return cardsOf(set, pagesInOrder({ collections: [folder] }));
542
+ }
543
+ /**
544
+ * The same cards for a site's home page: one for each type that the site's pages have, when it has
545
+ * two or more, taken to the first page of that type in the order of the whole site.
546
+ */
547
+ export function homeTypeCardsOf(set) {
548
+ return cardsOf(set, pagesInOrder(set.nav));
549
+ }
550
+ function cardsOf(set, pages) {
551
+ const inside = pages.filter((page) => set.types[page.path]);
552
+ const present = DOC_TYPES.filter((one) => inside.some((page) => set.types[page.path] === one));
553
+ if (present.length < 2)
554
+ return [];
555
+ return present.map((type) => {
556
+ const ofType = inside.filter((page) => set.types[page.path] === type);
557
+ return {
558
+ type,
559
+ title: DOC_TYPE_PLURALS[type],
560
+ summary: DOC_TYPE_SUMMARIES[type],
561
+ count: ofType.length,
562
+ first: ofType[0],
563
+ };
564
+ });
565
+ }
566
+ //# sourceMappingURL=set.js.map
@@ -0,0 +1,45 @@
1
+ import type { DocEntry } from "./page.js";
2
+ import { type DocSet, type Term } from "./set.js";
3
+ /**
4
+ * The help one reader can see: the product's own set first, then each of the
5
+ * tenant's sets the reader may open. Which sets those are is decided by
6
+ * whoever builds the stack, from what the reader is allowed. Nothing here
7
+ * knows about permissions.
8
+ *
9
+ * The product's set is never replaced. A tenant's set adds terms and topics of
10
+ * its own, attaches pages to resources, and adds notes to the product's topics.
11
+ */
12
+ export interface Layer {
13
+ /** A name for where the set is from: `product`, or a docs app's path. */
14
+ id: string;
15
+ set: DocSet;
16
+ }
17
+ export type Stack = readonly Layer[];
18
+ export interface Found {
19
+ layer: string;
20
+ page: DocEntry;
21
+ }
22
+ /** The page that has a topic. The first layer that has it wins, and the product's is first. */
23
+ export declare function findTopic(stack: Stack, topic: string): Found | undefined;
24
+ /** A term, and the layer it is from. */
25
+ export declare function findTerm(stack: Stack, id: string): (Term & {
26
+ layer: string;
27
+ }) | undefined;
28
+ /** Every term a reader can see, the product's first. A later layer never hides an earlier one's. */
29
+ export declare function allTerms(stack: Stack): (Term & {
30
+ layer: string;
31
+ })[];
32
+ /** The tenant's pages that add notes to one of the product's topics: "From your team". */
33
+ export declare function extensionsOf(stack: Stack, topic: string): Found[];
34
+ /** The pages that say they document a resource. */
35
+ export declare function attachedTo(stack: Stack, kind: string, path: string): Found[];
36
+ /**
37
+ * What two of a tenant's sets both define, which a workspace may not have:
38
+ * terms and topics are the workspace's, not one docs app's.
39
+ */
40
+ export declare function clashes(layers: readonly Layer[]): {
41
+ kind: "term" | "topic";
42
+ id: string;
43
+ layers: [string, string];
44
+ }[];
45
+ //# sourceMappingURL=stack.d.ts.map
package/dist/stack.js ADDED
@@ -0,0 +1,73 @@
1
+ import { resourceKey } from "./set.js";
2
+ function pageOf(layer, path) {
3
+ const page = path === undefined ? undefined : layer.set.pages.get(path);
4
+ return page ? { layer: layer.id, page } : undefined;
5
+ }
6
+ /** The page that has a topic. The first layer that has it wins, and the product's is first. */
7
+ export function findTopic(stack, topic) {
8
+ for (const layer of stack) {
9
+ const found = pageOf(layer, layer.set.topics[topic]);
10
+ if (found)
11
+ return found;
12
+ }
13
+ return undefined;
14
+ }
15
+ /** A term, and the layer it is from. */
16
+ export function findTerm(stack, id) {
17
+ for (const layer of stack) {
18
+ const term = layer.set.terms[id];
19
+ if (term)
20
+ return { ...term, layer: layer.id };
21
+ }
22
+ return undefined;
23
+ }
24
+ /** Every term a reader can see, the product's first. A later layer never hides an earlier one's. */
25
+ export function allTerms(stack) {
26
+ const seen = new Set();
27
+ const out = [];
28
+ for (const layer of stack) {
29
+ for (const term of Object.values(layer.set.terms)) {
30
+ if (seen.has(term.id))
31
+ continue;
32
+ seen.add(term.id);
33
+ out.push({ ...term, layer: layer.id });
34
+ }
35
+ }
36
+ return out;
37
+ }
38
+ /** The tenant's pages that add notes to one of the product's topics: "From your team". */
39
+ export function extensionsOf(stack, topic) {
40
+ // The first layer is the product's, and does not extend itself.
41
+ return stack
42
+ .slice(1)
43
+ .flatMap((layer) => (layer.set.extensions[topic] ?? []).map((path) => pageOf(layer, path)))
44
+ .filter((found) => found !== undefined);
45
+ }
46
+ /** The pages that say they document a resource. */
47
+ export function attachedTo(stack, kind, path) {
48
+ const key = resourceKey(kind, path);
49
+ return stack
50
+ .flatMap((layer) => (layer.set.attachments[key] ?? []).map((one) => pageOf(layer, one)))
51
+ .filter((found) => found !== undefined);
52
+ }
53
+ /**
54
+ * What two of a tenant's sets both define, which a workspace may not have:
55
+ * terms and topics are the workspace's, not one docs app's.
56
+ */
57
+ export function clashes(layers) {
58
+ const out = [];
59
+ for (const kind of ["term", "topic"]) {
60
+ const owner = new Map();
61
+ for (const layer of layers) {
62
+ for (const id of Object.keys(kind === "term" ? layer.set.terms : layer.set.topics)) {
63
+ const first = owner.get(id);
64
+ if (first !== undefined && first !== layer.id)
65
+ out.push({ kind, id, layers: [first, layer.id] });
66
+ else
67
+ owner.set(id, layer.id);
68
+ }
69
+ }
70
+ }
71
+ return out;
72
+ }
73
+ //# sourceMappingURL=stack.js.map