@operad-io/memory 0.0.0-stage → 0.3.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/src/wiki.ts ADDED
@@ -0,0 +1,619 @@
1
+ /**
2
+ * The wiki as read-only objects: one type for its folders and one for its
3
+ * notes. The objects are a projection of the Markdown directory, rebuilt from
4
+ * it before the first read and again on every directory change a running
5
+ * `serve` watches. The wiki files remain the only copy of anything; a note's
6
+ * content is read from its file when a client opens the section.
7
+ */
8
+
9
+ import { readdirSync, readFileSync, statSync } from "node:fs";
10
+ import { basename, isAbsolute, join, relative, resolve, sep } from "node:path";
11
+ import {
12
+ ViewError,
13
+ type JsonValue,
14
+ type ObjectRow,
15
+ type ObjectStore,
16
+ type ObjectViews,
17
+ type SectionContent,
18
+ type StoredObject,
19
+ type ViewContext,
20
+ type ViewSection,
21
+ } from "@operad-io/engine";
22
+ import { placedNoteLinks, resolveNoteLink, normaliseNotePath, noteLookup } from "./links.ts";
23
+ import { splitMarkdown, titleFromName, titleOf } from "./search/frontmatter.ts";
24
+ import { untrustedFields, type MemorySearch } from "./search/service.ts";
25
+ import { wikiDirectory } from "./memory.ts";
26
+
27
+ /** The object type of a wiki folder; its title is `Wiki`. */
28
+ export const WIKI_FOLDER_TYPE = "wiki";
29
+
30
+ /** The object type of a wiki note. */
31
+ export const WIKI_NOTE_TYPE = "note";
32
+
33
+ /** The section that holds a note's Markdown. */
34
+ export const WIKI_CONTENT_SECTION = "content";
35
+
36
+ /** The section that lists the notes linking to a note; absent when none do. */
37
+ export const WIKI_BACKLINKS_SECTION = "backlinks";
38
+
39
+ /** The most characters one page of a long note holds. */
40
+ export const WIKI_PAGE_CHARS = 20_000;
41
+
42
+ /** A note as the projection stores it. */
43
+ interface NoteData {
44
+ readonly path: string;
45
+ readonly title: string;
46
+ readonly parent?: string | undefined;
47
+ readonly mtimeMs: number;
48
+ readonly untrusted: boolean;
49
+ readonly removed: boolean;
50
+ readonly backlinks: readonly BacklinkData[];
51
+ readonly links: readonly NoteLinkData[];
52
+ /** Whether the stored data has a links list, so an index from before it is read again. */
53
+ readonly linksKnown: boolean;
54
+ }
55
+
56
+ /** One note that links to a note, as the Backlinks section returns it. */
57
+ interface BacklinkData {
58
+ /** The full object id a client opens (`note:<path>`). */
59
+ readonly object: string;
60
+ readonly path: string;
61
+ readonly title: string;
62
+ readonly target: string;
63
+ readonly heading?: string | undefined;
64
+ readonly kind: "wikilink" | "markdown";
65
+ readonly line: number;
66
+ readonly excerpt: string;
67
+ readonly untrusted: boolean;
68
+ }
69
+
70
+ /** A folder as the projection stores it. */
71
+ interface FolderData {
72
+ readonly path: string;
73
+ readonly title: string;
74
+ readonly parent?: string | undefined;
75
+ readonly noteCount: number;
76
+ readonly removed: boolean;
77
+ }
78
+
79
+ /** One link in a note's content, with the note it points at when resolved. */
80
+ interface LinkData {
81
+ readonly target: string;
82
+ readonly heading?: string | undefined;
83
+ readonly kind: "wikilink" | "markdown";
84
+ /** The full object id of the target note; absent while nothing matches it. */
85
+ readonly object?: string | undefined;
86
+ }
87
+
88
+ /** A link as the projection keeps it, with the place a backlink will show. */
89
+ interface NoteLinkData {
90
+ readonly target: string;
91
+ readonly heading?: string | undefined;
92
+ readonly kind: "wikilink" | "markdown";
93
+ readonly line: number;
94
+ readonly excerpt: string;
95
+ readonly object?: string | undefined;
96
+ }
97
+
98
+ function parentPath(path: string): string | undefined {
99
+ const at = path.lastIndexOf("/");
100
+ return at < 0 ? undefined : path.slice(0, at);
101
+ }
102
+
103
+ function textOf(value: unknown): string | undefined {
104
+ return typeof value === "string" && value.length > 0 ? value : undefined;
105
+ }
106
+
107
+ function numberField(value: unknown): number {
108
+ return typeof value === "number" && Number.isFinite(value) ? value : 0;
109
+ }
110
+
111
+ function removedFlag(value: unknown): boolean {
112
+ return value === true;
113
+ }
114
+
115
+ function folderOf(object: StoredObject): FolderData {
116
+ const data = object.data;
117
+ const parent = textOf(data["parent"]);
118
+ return {
119
+ path: textOf(data["path"]) ?? object.id,
120
+ title: textOf(data["title"]) ?? basename(object.id),
121
+ ...(parent === undefined ? {} : { parent }),
122
+ noteCount: numberField(data["noteCount"]),
123
+ removed: removedFlag(data["removed"]),
124
+ };
125
+ }
126
+
127
+ function noteOf(object: StoredObject): NoteData {
128
+ const data = object.data;
129
+ const parent = textOf(data["parent"]);
130
+ const backlinks = Array.isArray(data["backlinks"])
131
+ ? data["backlinks"].filter((entry): entry is BacklinkData => {
132
+ if (typeof entry !== "object" || entry === null) return false;
133
+ const record = entry as Record<string, unknown>;
134
+ return (
135
+ typeof record["object"] === "string" &&
136
+ typeof record["path"] === "string" &&
137
+ typeof record["title"] === "string" &&
138
+ typeof record["target"] === "string" &&
139
+ (record["kind"] === "wikilink" || record["kind"] === "markdown") &&
140
+ typeof record["line"] === "number" &&
141
+ typeof record["excerpt"] === "string" &&
142
+ typeof record["untrusted"] === "boolean"
143
+ );
144
+ })
145
+ : [];
146
+ const rawLinks = data["links"];
147
+ const linksKnown = Array.isArray(rawLinks);
148
+ const links = linksKnown
149
+ ? rawLinks.filter((entry): entry is NoteLinkData => {
150
+ if (typeof entry !== "object" || entry === null) return false;
151
+ const record = entry as Record<string, unknown>;
152
+ return (
153
+ typeof record["target"] === "string" &&
154
+ (record["heading"] === undefined || typeof record["heading"] === "string") &&
155
+ (record["kind"] === "wikilink" || record["kind"] === "markdown") &&
156
+ typeof record["line"] === "number" &&
157
+ typeof record["excerpt"] === "string" &&
158
+ (record["object"] === undefined || typeof record["object"] === "string")
159
+ );
160
+ })
161
+ : [];
162
+ return {
163
+ path: textOf(data["path"]) ?? object.id,
164
+ title: textOf(data["title"]) ?? basename(object.id),
165
+ ...(parent === undefined ? {} : { parent }),
166
+ mtimeMs: numberField(data["mtimeMs"]),
167
+ untrusted: removedFlag(data["untrusted"]),
168
+ removed: removedFlag(data["removed"]),
169
+ backlinks,
170
+ links,
171
+ linksKnown,
172
+ };
173
+ }
174
+
175
+ function noteCountText(count: number): string {
176
+ return `${count} ${count === 1 ? "note" : "notes"}`;
177
+ }
178
+
179
+ /**
180
+ * A note's modification time as `YYYY-MM-DD HH:mm` in the given time zone, or
181
+ * the process's local zone when none is given; empty for a zero time.
182
+ */
183
+ export function wikiModifiedText(mtimeMs: number, timeZone?: string): string {
184
+ if (mtimeMs <= 0) return "";
185
+ const parts = new Intl.DateTimeFormat("en-GB", {
186
+ ...(timeZone === undefined ? {} : { timeZone }),
187
+ year: "numeric",
188
+ month: "2-digit",
189
+ day: "2-digit",
190
+ hour: "2-digit",
191
+ minute: "2-digit",
192
+ hourCycle: "h23",
193
+ }).formatToParts(new Date(mtimeMs));
194
+ const part = (type: Intl.DateTimeFormatPartTypes): string =>
195
+ parts.find((entry) => entry.type === type)?.value ?? "";
196
+ return `${part("year")}-${part("month")}-${part("day")} ${part("hour")}:${part("minute")}`;
197
+ }
198
+
199
+ function noteRow(object: StoredObject): ObjectRow {
200
+ const note = noteOf(object);
201
+ const modified = wikiModifiedText(note.mtimeMs);
202
+ const noteLine = note.untrusted
203
+ ? `${modified}${modified.length === 0 ? "" : " "}(untrusted)`
204
+ : modified;
205
+ return {
206
+ id: object.id,
207
+ title: note.title,
208
+ state: note.removed ? "removed" : "open",
209
+ ...(noteLine.length === 0 ? {} : { note: noteLine }),
210
+ ...(note.parent === undefined || note.removed ? {} : { parent: note.parent }),
211
+ };
212
+ }
213
+
214
+ function folderRow(object: StoredObject): ObjectRow {
215
+ const folder = folderOf(object);
216
+ return {
217
+ id: object.id,
218
+ title: folder.title,
219
+ state: folder.removed ? "removed" : "open",
220
+ note: noteCountText(folder.noteCount),
221
+ ...(folder.parent === undefined || folder.removed ? {} : { parent: folder.parent }),
222
+ };
223
+ }
224
+
225
+ /** Whether a path a client named is a path inside the wiki, as an id. */
226
+ export function wikiObjectPath(id: string): string | undefined {
227
+ if (isAbsolute(id) || id.startsWith("/") || /^[A-Za-z]:[\\/]/u.test(id)) return undefined;
228
+ return normaliseNotePath(id);
229
+ }
230
+
231
+ /** The folder path an id names; whether the wiki holds that folder is the caller's lookup. */
232
+ export function resolveWikiFolderId(id: string): string | undefined {
233
+ const path = wikiObjectPath(id);
234
+ if (path === undefined) {
235
+ throw new ViewError("args", `"${id}" is not a path inside the wiki`);
236
+ }
237
+ return path;
238
+ }
239
+
240
+ /** The note path an id names; whether the wiki holds that note is the caller's lookup. */
241
+ export function resolveWikiNoteId(id: string): string | undefined {
242
+ const path = wikiObjectPath(id);
243
+ if (path === undefined) {
244
+ throw new ViewError("args", `"${id}" is not a path inside the wiki`);
245
+ }
246
+ return path;
247
+ }
248
+
249
+ /** The file an id names under `root`, or undefined when it leaves the wiki. */
250
+ export function wikiNoteFile(root: string, id: string): string | undefined {
251
+ const path = wikiObjectPath(id);
252
+ if (path === undefined) return undefined;
253
+ const file = resolve(root, path);
254
+ const rel = relative(root, file);
255
+ if (rel.length === 0 || rel.startsWith("..") || isAbsolute(rel)) return undefined;
256
+ return file;
257
+ }
258
+
259
+ interface ScannedNote {
260
+ readonly path: string;
261
+ readonly file: string;
262
+ readonly title: string;
263
+ readonly mtimeMs: number;
264
+ readonly untrusted: boolean;
265
+ readonly links: readonly NoteLinkData[];
266
+ }
267
+
268
+ interface WikiScan {
269
+ readonly folders: readonly string[];
270
+ readonly notes: readonly ScannedNote[];
271
+ }
272
+
273
+ /**
274
+ * Every folder and Markdown note under the wiki, hidden directories left out.
275
+ * A note whose modification time matches the projection is not read again: its
276
+ * title, mark and links are already stored.
277
+ */
278
+ function scanWiki(root: string, known: ReadonlyMap<string, NoteData>): WikiScan {
279
+ const folders: string[] = [];
280
+ const notes: ScannedNote[] = [];
281
+ const visit = (directory: string): void => {
282
+ let entries;
283
+ try {
284
+ entries = readdirSync(directory, { withFileTypes: true });
285
+ } catch {
286
+ return;
287
+ }
288
+ for (const entry of [...entries].sort((left, right) => left.name.localeCompare(right.name))) {
289
+ if (entry.name.startsWith(".") || entry.name === "node_modules") continue;
290
+ const file = join(directory, entry.name);
291
+ if (entry.isDirectory()) {
292
+ const path = relative(root, file).split(sep).join("/");
293
+ folders.push(path);
294
+ visit(file);
295
+ continue;
296
+ }
297
+ if (!entry.isFile() || !/\.(?:md|markdown)$/iu.test(entry.name)) continue;
298
+ let stat;
299
+ try {
300
+ stat = statSync(file);
301
+ } catch {
302
+ continue;
303
+ }
304
+ const path = relative(root, file).split(sep).join("/");
305
+ const before = known.get(path);
306
+ if (before !== undefined && before.linksKnown && before.mtimeMs === stat.mtimeMs) {
307
+ notes.push({
308
+ path,
309
+ file,
310
+ title: before.title,
311
+ mtimeMs: stat.mtimeMs,
312
+ untrusted: before.untrusted,
313
+ links: before.links,
314
+ });
315
+ continue;
316
+ }
317
+ let raw: string;
318
+ try {
319
+ raw = readFileSync(file, "utf8");
320
+ } catch {
321
+ continue;
322
+ }
323
+ const { fields, body } = splitMarkdown(raw);
324
+ notes.push({
325
+ path,
326
+ file,
327
+ title: titleOf({}, body, titleFromName(entry.name)),
328
+ mtimeMs: stat.mtimeMs,
329
+ untrusted: untrustedFields(fields),
330
+ links: placedNoteLinks(body).map(({ at: _at, ...link }) => link),
331
+ });
332
+ }
333
+ };
334
+ visit(root);
335
+ folders.sort((left, right) => left.localeCompare(right));
336
+ notes.sort((left, right) => left.path.localeCompare(right.path));
337
+ return { folders, notes };
338
+ }
339
+
340
+ /** The notes the store still shows, by path, so a reader does not scan them again. */
341
+ async function projectedNotes(store: ObjectStore): Promise<ReadonlyMap<string, NoteData>> {
342
+ const found = new Map<string, NoteData>();
343
+ let cursor: string | undefined;
344
+ do {
345
+ const page = await store.list(WIKI_NOTE_TYPE, {
346
+ ...(cursor === undefined ? {} : { cursor }),
347
+ limit: 1000,
348
+ });
349
+ for (const object of page.objects) {
350
+ const note = noteOf(object);
351
+ if (!note.removed) found.set(note.path, note);
352
+ }
353
+ cursor = page.next;
354
+ } while (cursor !== undefined);
355
+ return found;
356
+ }
357
+
358
+ async function putIfChanged(
359
+ store: ObjectStore,
360
+ type: string,
361
+ id: string,
362
+ data: Readonly<Record<string, unknown>>,
363
+ ): Promise<void> {
364
+ const current = await store.get(type, id);
365
+ if (current !== undefined && JSON.stringify(current.data) === JSON.stringify(data)) return;
366
+ await store.put(type, id, data, "memory");
367
+ }
368
+
369
+ async function markRemoved(
370
+ store: ObjectStore,
371
+ type: string,
372
+ seen: ReadonlySet<string>,
373
+ ): Promise<void> {
374
+ let cursor: string | undefined;
375
+ do {
376
+ const page = await store.list(type, { ...(cursor === undefined ? {} : { cursor }) });
377
+ for (const object of page.objects) {
378
+ if (seen.has(object.id) || object.data["removed"] === true) continue;
379
+ await store.put(type, object.id, { ...object.data, removed: true }, "memory");
380
+ }
381
+ cursor = page.next;
382
+ } while (cursor !== undefined);
383
+ }
384
+
385
+ /** The links each note writes, resolved, and the backlinks they give their targets. */
386
+ function projectLinks(notes: readonly ScannedNote[]): {
387
+ readonly links: ReadonlyMap<string, readonly NoteLinkData[]>;
388
+ readonly backlinks: ReadonlyMap<string, readonly BacklinkData[]>;
389
+ } {
390
+ const lookup = noteLookup(notes.map((note) => note.path));
391
+ const links = new Map<string, readonly NoteLinkData[]>();
392
+ const backlinks = new Map<string, BacklinkData[]>();
393
+ for (const note of notes) {
394
+ const resolved = note.links.map((link): NoteLinkData => {
395
+ const target = resolveNoteLink(note.path, link, lookup);
396
+ return {
397
+ target: link.target,
398
+ ...(link.heading === undefined ? {} : { heading: link.heading }),
399
+ kind: link.kind,
400
+ line: link.line,
401
+ excerpt: link.excerpt,
402
+ ...(target === undefined ? {} : { object: `${WIKI_NOTE_TYPE}:${target}` }),
403
+ };
404
+ });
405
+ links.set(note.path, resolved);
406
+ for (const link of resolved) {
407
+ const target = link.object?.slice(WIKI_NOTE_TYPE.length + 1);
408
+ if (target === undefined || target === note.path) continue;
409
+ const found = backlinks.get(target) ?? [];
410
+ found.push({
411
+ object: `${WIKI_NOTE_TYPE}:${note.path}`,
412
+ path: note.path,
413
+ title: note.title,
414
+ target: link.target,
415
+ ...(link.heading === undefined ? {} : { heading: link.heading }),
416
+ kind: link.kind,
417
+ line: link.line,
418
+ excerpt: link.excerpt,
419
+ untrusted: note.untrusted,
420
+ });
421
+ backlinks.set(target, found);
422
+ }
423
+ }
424
+ return { links, backlinks };
425
+ }
426
+
427
+ /**
428
+ * Brings the wiki objects up to date with the Markdown directory. The scan and
429
+ * the links in it are the projection's own, so a list or get does not read the
430
+ * search index. With a search given, the caller owns it and this refreshes the
431
+ * index too; a list or get passes none.
432
+ */
433
+ export async function syncWikiObjects(
434
+ store: ObjectStore,
435
+ home: string,
436
+ settings: Readonly<Record<string, JsonValue>> = {},
437
+ search: MemorySearch | undefined = undefined,
438
+ ): Promise<void> {
439
+ const root = wikiDirectory(home, settings);
440
+ const scanned = scanWiki(root, await projectedNotes(store));
441
+ const projected = projectLinks(scanned.notes);
442
+ const counts = new Map<string, number>();
443
+ for (const note of scanned.notes) {
444
+ let parent = parentPath(note.path);
445
+ while (parent !== undefined) {
446
+ counts.set(parent, (counts.get(parent) ?? 0) + 1);
447
+ parent = parentPath(parent);
448
+ }
449
+ }
450
+
451
+ const seenFolders = new Set<string>();
452
+ for (const path of scanned.folders) {
453
+ const parent = parentPath(path);
454
+ const data: Record<string, unknown> = {
455
+ path,
456
+ title: path.slice(path.lastIndexOf("/") + 1),
457
+ noteCount: counts.get(path) ?? 0,
458
+ removed: false,
459
+ };
460
+ if (parent !== undefined) data["parent"] = parent;
461
+ seenFolders.add(path);
462
+ await putIfChanged(store, WIKI_FOLDER_TYPE, path, data);
463
+ }
464
+
465
+ const seenNotes = new Set<string>();
466
+ for (const note of scanned.notes) {
467
+ const parent = parentPath(note.path);
468
+ const data: Record<string, unknown> = {
469
+ path: note.path,
470
+ title: note.title,
471
+ mtimeMs: note.mtimeMs,
472
+ untrusted: note.untrusted,
473
+ removed: false,
474
+ backlinks: projected.backlinks.get(note.path) ?? [],
475
+ links: projected.links.get(note.path) ?? [],
476
+ };
477
+ if (parent !== undefined) data["parent"] = parent;
478
+ seenNotes.add(note.path);
479
+ await putIfChanged(store, WIKI_NOTE_TYPE, note.path, data);
480
+ }
481
+
482
+ await markRemoved(store, WIKI_FOLDER_TYPE, seenFolders);
483
+ await markRemoved(store, WIKI_NOTE_TYPE, seenNotes);
484
+ if (search !== undefined) await search.refresh(store);
485
+ }
486
+
487
+ function escapeLabel(text: string): string {
488
+ return text.replace(/[\\[\]]/gu, "\\$&");
489
+ }
490
+
491
+ function backlinkMarkdown(entries: readonly BacklinkData[]): string {
492
+ return entries
493
+ .map((entry) => {
494
+ const line = entry.line > 0 ? ` line ${entry.line}` : "";
495
+ const mark = entry.untrusted ? " (untrusted)" : "";
496
+ const excerpt = entry.excerpt.length > 0 ? ` — ${entry.excerpt}` : "";
497
+ const href = `operad://objects/${encodeURIComponent(entry.object)}`;
498
+ return `- [${escapeLabel(entry.title)}](${href})${line}${mark}${excerpt}`;
499
+ })
500
+ .join("\n");
501
+ }
502
+
503
+ /** Splits a note's text into pages of at most {@link WIKI_PAGE_CHARS}, at a line when possible. */
504
+ function textPages(text: string): readonly string[] {
505
+ if (text.length <= WIKI_PAGE_CHARS) return [text];
506
+ const pages: string[] = [];
507
+ let start = 0;
508
+ while (start < text.length) {
509
+ let end = Math.min(text.length, start + WIKI_PAGE_CHARS);
510
+ if (end < text.length) {
511
+ const at = text.lastIndexOf("\n", end);
512
+ if (at > start) end = at + 1;
513
+ }
514
+ pages.push(text.slice(start, end));
515
+ start = end;
516
+ }
517
+ return pages.length === 0 ? [""] : pages;
518
+ }
519
+
520
+ function pageOf(text: string, cursor: string | undefined): SectionContent {
521
+ const pages = textPages(text);
522
+ const index = cursor === undefined ? 0 : Number(cursor);
523
+ if (!Number.isInteger(index) || index < 0 || index >= pages.length) {
524
+ return { markdown: "" };
525
+ }
526
+ return {
527
+ markdown: pages[index] ?? "",
528
+ ...(index + 1 >= pages.length ? {} : { next: String(index + 1) }),
529
+ ...(pages.length <= 1
530
+ ? {}
531
+ : {
532
+ pages: pages.map((_page, at) => ({
533
+ label: String(at + 1),
534
+ ...(at === 0 ? {} : { cursor: String(at) }),
535
+ })),
536
+ }),
537
+ };
538
+ }
539
+
540
+ async function loadNote(
541
+ ctx: ViewContext,
542
+ object: StoredObject,
543
+ sectionId: string,
544
+ cursor: string | undefined,
545
+ ): Promise<SectionContent> {
546
+ let note = noteOf(object);
547
+ if (note.removed) throw new Error(`the wiki note "${note.path}" is gone`);
548
+ const root = wikiDirectory(ctx.home, ctx.settings);
549
+ const file = wikiNoteFile(root, note.path);
550
+ if (file !== undefined) {
551
+ let mtimeMs: number | undefined;
552
+ try {
553
+ mtimeMs = statSync(file).mtimeMs;
554
+ } catch {
555
+ mtimeMs = undefined;
556
+ }
557
+ if (
558
+ mtimeMs !== undefined &&
559
+ mtimeMs !== note.mtimeMs &&
560
+ ctx.refreshedObject !== undefined
561
+ ) {
562
+ // The folder type owns the scan that projects the notes too.
563
+ await ctx.refreshedObject(WIKI_FOLDER_TYPE, note.path);
564
+ const refreshed = await ctx.store.get(WIKI_NOTE_TYPE, note.path);
565
+ if (refreshed !== undefined) note = noteOf(refreshed);
566
+ if (note.removed) throw new Error(`the wiki note "${note.path}" is gone`);
567
+ }
568
+ }
569
+ if (sectionId === WIKI_BACKLINKS_SECTION) {
570
+ return {
571
+ markdown: backlinkMarkdown(note.backlinks),
572
+ data: { backlinks: note.backlinks as unknown as JsonValue },
573
+ };
574
+ }
575
+ if (sectionId !== WIKI_CONTENT_SECTION) return { markdown: "" };
576
+ if (file === undefined) {
577
+ throw new ViewError("args", `the wiki note "${note.path}" is outside the wiki`);
578
+ }
579
+ let raw: string;
580
+ try {
581
+ raw = readFileSync(file, "utf8");
582
+ } catch {
583
+ throw new Error(`the wiki note "${note.path}" cannot be read`);
584
+ }
585
+ const { body } = splitMarkdown(raw);
586
+ const links: readonly LinkData[] = note.links.map(
587
+ ({ line: _line, excerpt: _excerpt, ...link }) => link,
588
+ );
589
+ return {
590
+ ...pageOf(body, cursor),
591
+ data: { links: links as unknown as JsonValue },
592
+ };
593
+ }
594
+
595
+ /** How a wiki folder appears: a row, its children and no actions. */
596
+ export const wikiFolderViews: ObjectViews = {
597
+ row: folderRow,
598
+ sections(): readonly ViewSection[] {
599
+ return [];
600
+ },
601
+ load: (): SectionContent => ({ markdown: "" }),
602
+ };
603
+
604
+ /** How a wiki note appears: its content, then its backlinks when it has any. */
605
+ export const wikiNoteViews: ObjectViews = {
606
+ row: noteRow,
607
+ sections(object: StoredObject): readonly ViewSection[] {
608
+ const note = noteOf(object);
609
+ if (note.removed) return [];
610
+ return [
611
+ { id: WIKI_CONTENT_SECTION, title: "Note" },
612
+ ...(note.backlinks.length === 0
613
+ ? []
614
+ : [{ id: WIKI_BACKLINKS_SECTION, title: "Backlinks" }]),
615
+ ];
616
+ },
617
+ load: (ctx, object, sectionId, options) =>
618
+ loadNote(ctx, object, sectionId, options.cursor),
619
+ };