@volvel/docs-engine 0.0.0-stage → 0.7.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/ops.d.ts ADDED
@@ -0,0 +1,111 @@
1
+ import { type DocType } from "./diataxis.js";
2
+ /**
3
+ * What a change to the tree of pages writes. A tree is changed by a person in
4
+ * the console: a page or a folder renamed, moved, put before another, made,
5
+ * copied or removed. Each of those is worked out here, from the pages there
6
+ * are, into the drafts that make it so, so that it can be shown, saved as one,
7
+ * and undone. Nothing here saves anything.
8
+ *
9
+ * Paths are the workspace's: `handbook/support/refunds`. A folder is the
10
+ * pages under it, and its own page is `<folder>/index`.
11
+ */
12
+ /** A page as it is now, drafts included. */
13
+ export interface PageSource {
14
+ path: string;
15
+ source: string;
16
+ }
17
+ /** One page's draft. */
18
+ export interface Change {
19
+ /** The page's path now, or the path of a page that is new. */
20
+ path: string;
21
+ /** Where it goes, when it moves. */
22
+ to?: string;
23
+ /** Its whole source, as it is to be. Given for every page that is not removed. */
24
+ source?: string;
25
+ deleted?: true;
26
+ /** A page that was not there. */
27
+ created?: true;
28
+ }
29
+ export type Plan = {
30
+ changes: Change[];
31
+ } | {
32
+ problem: string;
33
+ };
34
+ /** Where something is put among the items of a folder. */
35
+ export type Place = {
36
+ folder: string;
37
+ } | {
38
+ folder: string;
39
+ before: string;
40
+ } | {
41
+ folder: string;
42
+ after: string;
43
+ };
44
+ export declare const NAME_PATTERN: RegExp;
45
+ /** A name made from a title, as a file would be named: `Customer Support!` is `customer-support`. */
46
+ export declare function slugOf(title: string): string;
47
+ /** What is wrong with a name, or nothing. */
48
+ export declare function nameProblem(name: string): string | undefined;
49
+ /** What a folder holds: its pages and its folders, each by name. Its own page is not among them. */
50
+ export declare function childrenOf(pages: readonly PageSource[], folder: string): {
51
+ name: string;
52
+ title: string;
53
+ folder: boolean;
54
+ }[];
55
+ /** The names of what a folder holds, in the order a reader meets them. */
56
+ export declare function orderOf(pages: readonly PageSource[], folder: string): string[];
57
+ /**
58
+ * A new page's source: its title, and nothing written under it. Given a type,
59
+ * the page starts from that type's template, with `type:` on it only when the
60
+ * folder does not already say it (`inherited`).
61
+ */
62
+ export declare function newPageSource(title: string, type?: DocType, inherited?: DocType): string;
63
+ /** A changelog entry's file name: the day it is made, then its title. `2026-11-24-new-intake`. */
64
+ export declare function changelogEntryName(title: string, today: string): string;
65
+ /**
66
+ * A new changelog entry for the site `folder` is in: under its `changelog/` folder (or the one
67
+ * `folder` is already in), named with today's day, from the changelog template. It is no type of
68
+ * page, so it says none.
69
+ */
70
+ export declare function planNewChangelogEntry(pages: readonly PageSource[], folder: string, next: {
71
+ title: string;
72
+ today: string;
73
+ name?: string;
74
+ }): Plan;
75
+ /** A relative link from one page to another, as a person would write it. */
76
+ export declare function relativeHref(from: string, to: string): string;
77
+ /**
78
+ * Moves a page or a folder to a folder, and among what is there when a place
79
+ * beside another is given. Both folders' lists follow, and links to and from
80
+ * what moved are rewritten.
81
+ */
82
+ export declare function planMove(pages: readonly PageSource[], path: string, where: Place): Plan;
83
+ /** Renames a page or a folder: its title, its name, or both. */
84
+ export declare function planRename(pages: readonly PageSource[], path: string, next: {
85
+ title?: string;
86
+ name?: string;
87
+ }): Plan;
88
+ /** A new page in a folder, with nothing under its title. */
89
+ export declare function planNewPage(pages: readonly PageSource[], folder: string, next: {
90
+ title: string;
91
+ name: string;
92
+ type?: DocType;
93
+ }): Plan;
94
+ /**
95
+ * A new folder. It is its own page, with nothing written in it, so that it
96
+ * lists what is put in it until someone writes there.
97
+ */
98
+ export declare function planNewFolder(pages: readonly PageSource[], parent: string, next: {
99
+ title: string;
100
+ name: string;
101
+ }): Plan;
102
+ /** Removes a page, or a folder and everything in it. */
103
+ export declare function planDelete(pages: readonly PageSource[], path: string): Plan;
104
+ /**
105
+ * A copy of a page or a folder beside it. A copy takes no topic, terms or
106
+ * key: those belong to one page.
107
+ */
108
+ export declare function planDuplicate(pages: readonly PageSource[], path: string): Plan;
109
+ /** What a plan undoes: the changes that put the pages back as they were. */
110
+ export declare function inverseOf(pages: readonly PageSource[], changes: readonly Change[]): Change[];
111
+ //# sourceMappingURL=ops.d.ts.map
package/dist/ops.js ADDED
@@ -0,0 +1,529 @@
1
+ import { linkKind, parse, topicOf, walk } from "@volvel/markup";
2
+ import { CHANGELOG_NAME } from "./changelog.js";
3
+ import { CHANGELOG_TEMPLATE, DOC_TEMPLATES, folderType } from "./diataxis.js";
4
+ import { frontmatterOf, setFrontmatter, splitFrontmatter, titleOf } from "./page.js";
5
+ import { resolveLink } from "./set.js";
6
+ export const NAME_PATTERN = /^[a-z0-9][a-z0-9_-]*$/;
7
+ /** A name made from a title, as a file would be named: `Customer Support!` is `customer-support`. */
8
+ export function slugOf(title) {
9
+ return title
10
+ .normalize("NFKD")
11
+ .replace(/[̀-ͯ]/g, "")
12
+ .toLowerCase()
13
+ .replace(/[^a-z0-9_-]+/g, "-")
14
+ .replace(/-{2,}/g, "-")
15
+ .replace(/^[-_]+|[-_]+$/g, "")
16
+ .slice(0, 80);
17
+ }
18
+ /** What is wrong with a name, or nothing. */
19
+ export function nameProblem(name) {
20
+ if (name === "")
21
+ return "A name is needed.";
22
+ if (!NAME_PATTERN.test(name)) {
23
+ const fixed = slugOf(name);
24
+ return `Use lowercase letters, numbers, - and _.${fixed ? ` Use ${fixed}` : ""}`;
25
+ }
26
+ return undefined;
27
+ }
28
+ const parentOf = (path) => path.split("/").slice(0, -1).join("/");
29
+ const lastOf = (path) => path.split("/").at(-1);
30
+ const join = (folder, name) => (folder === "" ? name : `${folder}/${name}`);
31
+ const indexOf = (folder) => join(folder, "index");
32
+ /** A page's title, as its frontmatter says it or as its path makes it. */
33
+ function titleIn(path, source) {
34
+ const said = frontmatterOf(source)?.title;
35
+ return typeof said === "string" && said !== "" ? said : titleOf(path);
36
+ }
37
+ /** What a folder holds: its pages and its folders, each by name. Its own page is not among them. */
38
+ export function childrenOf(pages, folder) {
39
+ const prefix = folder === "" ? "" : `${folder}/`;
40
+ const byName = new Map();
41
+ const sourceOf = new Map(pages.map((page) => [page.path, page.source]));
42
+ for (const page of pages) {
43
+ if (!page.path.startsWith(prefix))
44
+ continue;
45
+ const rest = page.path.slice(prefix.length).split("/");
46
+ const name = rest[0];
47
+ if (rest.length === 1) {
48
+ if (name !== "index")
49
+ byName.set(name, { name, title: titleIn(page.path, page.source), folder: false });
50
+ }
51
+ else if (!byName.get(name)?.folder) {
52
+ const own = sourceOf.get(join(join(folder, name), "index"));
53
+ byName.set(name, {
54
+ name,
55
+ title: own === undefined ? titleOf(name) : titleIn(join(folder, name), own),
56
+ folder: true,
57
+ });
58
+ }
59
+ }
60
+ return [...byName.values()];
61
+ }
62
+ /** The names of what a folder holds, in the order a reader meets them. */
63
+ export function orderOf(pages, folder) {
64
+ const own = pages.find((page) => page.path === indexOf(folder));
65
+ const listed = own ? frontmatterOf(own.source)?.pages : undefined;
66
+ const at = new Map((listed ?? []).map((name, place) => [name, place]));
67
+ const place = (name) => at.get(name) ?? Number.POSITIVE_INFINITY;
68
+ return childrenOf(pages, folder)
69
+ .sort((a, b) => place(a.name) - place(b.name) ||
70
+ a.title.localeCompare(b.title, "en", { sensitivity: "base" }))
71
+ .map((one) => one.name);
72
+ }
73
+ /**
74
+ * A new page's source: its title, and nothing written under it. Given a type,
75
+ * the page starts from that type's template, with `type:` on it only when the
76
+ * folder does not already say it (`inherited`).
77
+ */
78
+ export function newPageSource(title, type, inherited) {
79
+ const top = setFrontmatter("", {
80
+ title,
81
+ ...(type && type !== inherited ? { type } : {}),
82
+ });
83
+ const head = top ?? `---\ntitle: ${JSON.stringify(title)}\n---\n`;
84
+ return type ? `${head}\n${DOC_TEMPLATES[type]}` : head;
85
+ }
86
+ /** A changelog entry's file name: the day it is made, then its title. `2026-11-24-new-intake`. */
87
+ export function changelogEntryName(title, today) {
88
+ const slug = slugOf(title);
89
+ return slug === "" ? today : `${today}-${slug}`.slice(0, 80);
90
+ }
91
+ /**
92
+ * A new changelog entry for the site `folder` is in: under its `changelog/` folder (or the one
93
+ * `folder` is already in), named with today's day, from the changelog template. It is no type of
94
+ * page, so it says none.
95
+ */
96
+ export function planNewChangelogEntry(pages, folder, next) {
97
+ if (next.title.trim() === "")
98
+ return { problem: "A title is needed." };
99
+ if (folder === "")
100
+ return { problem: "A page goes in a folder. Top-level folders are the sites." };
101
+ const parts = folder.split("/");
102
+ const at = parts.indexOf(CHANGELOG_NAME);
103
+ const into = at === -1 ? `${parts[0]}/${CHANGELOG_NAME}` : parts.slice(0, at + 1).join("/");
104
+ const name = next.name ?? changelogEntryName(next.title, next.today);
105
+ const problem = nameProblem(name);
106
+ if (problem)
107
+ return { problem };
108
+ const path = join(into, name);
109
+ if (pages.some((page) => page.path === path)) {
110
+ return { problem: `There is already an entry named ${name}. Change its title or its name.` };
111
+ }
112
+ const top = setFrontmatter("", { title: next.title.trim() });
113
+ const head = top ?? `---\ntitle: ${JSON.stringify(next.title.trim())}\n---\n`;
114
+ return { changes: [{ path, source: `${head}\n${CHANGELOG_TEMPLATE}`, created: true }] };
115
+ }
116
+ /**
117
+ * Collects changes by page, so that a page moved, re-listed and with links
118
+ * rewritten is one draft.
119
+ */
120
+ class Draft {
121
+ sources;
122
+ moves = new Map();
123
+ created = new Set();
124
+ removed = new Set();
125
+ constructor(pages) {
126
+ this.sources = new Map(pages.map((page) => [page.path, page.source]));
127
+ }
128
+ /** The pages as they are with what is planned so far, by where they will be. */
129
+ now() {
130
+ const out = [];
131
+ for (const [path, source] of this.sources) {
132
+ if (this.removed.has(path))
133
+ continue;
134
+ out.push({ path: this.moves.get(path) ?? path, source });
135
+ }
136
+ return out;
137
+ }
138
+ /** The page's path now, from where it will be. */
139
+ from(at) {
140
+ for (const [from, to] of this.moves)
141
+ if (to === at)
142
+ return from;
143
+ if (this.sources.has(at) && !this.moves.has(at) && !this.removed.has(at))
144
+ return at;
145
+ return undefined;
146
+ }
147
+ has(at) {
148
+ return this.from(at) !== undefined;
149
+ }
150
+ read(at) {
151
+ const from = this.from(at);
152
+ return from === undefined ? undefined : this.sources.get(from);
153
+ }
154
+ write(at, source) {
155
+ const from = this.from(at);
156
+ if (from === undefined) {
157
+ this.sources.set(at, source);
158
+ this.created.add(at);
159
+ }
160
+ else
161
+ this.sources.set(from, source);
162
+ }
163
+ move(from, to) {
164
+ if (this.created.has(from)) {
165
+ const source = this.sources.get(from);
166
+ this.sources.delete(from);
167
+ this.created.delete(from);
168
+ this.sources.set(to, source);
169
+ this.created.add(to);
170
+ return;
171
+ }
172
+ this.moves.set(from, to);
173
+ }
174
+ remove(at) {
175
+ const from = this.from(at);
176
+ if (from === undefined)
177
+ return;
178
+ if (this.created.has(from)) {
179
+ this.sources.delete(from);
180
+ this.created.delete(from);
181
+ }
182
+ else
183
+ this.removed.add(from);
184
+ }
185
+ changes(before) {
186
+ const out = [];
187
+ for (const [path, source] of this.sources) {
188
+ if (this.removed.has(path)) {
189
+ out.push({ path, deleted: true });
190
+ continue;
191
+ }
192
+ const to = this.moves.get(path);
193
+ if (this.created.has(path)) {
194
+ out.push({ path, source, created: true });
195
+ }
196
+ else if (to !== undefined && to !== path) {
197
+ out.push({ path, to, source });
198
+ }
199
+ else if (before.get(path) !== source) {
200
+ out.push({ path, source });
201
+ }
202
+ }
203
+ return out.sort((a, b) => a.path.localeCompare(b.path));
204
+ }
205
+ }
206
+ /** Changes a folder's list of what it holds, giving the folder its own page when it has none. */
207
+ function setListing(draft, folder, names) {
208
+ const own = indexOf(folder);
209
+ const source = draft.read(own) ?? newPageSource(titleOf(folder === "" ? "index" : folder));
210
+ const next = setFrontmatter(source, { pages: names });
211
+ if (next !== undefined)
212
+ draft.write(own, next);
213
+ }
214
+ /** Takes a name out of its folder's list, when the folder lists it. */
215
+ function unlist(draft, folder, name) {
216
+ const own = draft.read(indexOf(folder));
217
+ if (own === undefined)
218
+ return;
219
+ const listed = frontmatterOf(own)?.pages;
220
+ if (!listed?.includes(name))
221
+ return;
222
+ setListing(draft, folder, listed.filter((one) => one !== name));
223
+ }
224
+ /** Renames a name in its folder's list, when the folder lists it. */
225
+ function relist(draft, folder, from, to) {
226
+ const own = draft.read(indexOf(folder));
227
+ if (own === undefined)
228
+ return;
229
+ const listed = frontmatterOf(own)?.pages;
230
+ if (!listed?.includes(from))
231
+ return;
232
+ setListing(draft, folder, listed.map((one) => (one === from ? to : one)));
233
+ }
234
+ /** A relative link from one page to another, as a person would write it. */
235
+ export function relativeHref(from, to) {
236
+ const fromDir = from.split("/").slice(0, -1);
237
+ const target = to === "index" ? "" : to.endsWith("/index") ? to.slice(0, -"/index".length) : to;
238
+ const parts = target === "" ? [] : target.split("/");
239
+ let same = 0;
240
+ while (same < fromDir.length && same < parts.length && fromDir[same] === parts[same])
241
+ same++;
242
+ const written = [...Array(fromDir.length - same).fill(".."), ...parts.slice(same)];
243
+ return written.length === 0 ? "." : written.join("/");
244
+ }
245
+ /**
246
+ * Rewrites the relative links of every page, so that each still leads where
247
+ * it led once the moves are made. A moved page's own links are rewritten from
248
+ * where it now is. A link written from the root (`/…`) and a topic are left.
249
+ */
250
+ function rewriteLinks(draft, before, moved) {
251
+ if (moved.size === 0)
252
+ return;
253
+ const old = { pages: new Set(before.map((page) => page.path)), topics: {} };
254
+ const where = (path) => moved.get(path) ?? path;
255
+ for (const page of before) {
256
+ const now = where(page.path);
257
+ const source = draft.read(now);
258
+ if (source === undefined)
259
+ continue;
260
+ const { yaml, body, offset } = splitFrontmatter(source);
261
+ let next = body;
262
+ const seen = new Set();
263
+ for (const { node } of walk(parse(body))) {
264
+ if (node.type !== "link")
265
+ continue;
266
+ const href = node.href;
267
+ if (seen.has(href))
268
+ continue;
269
+ seen.add(href);
270
+ if (linkKind(href) !== "relative" ||
271
+ topicOf(href) ||
272
+ href.startsWith("/") ||
273
+ href.startsWith("#")) {
274
+ continue;
275
+ }
276
+ const target = resolveLink(old, page.path, href);
277
+ if ("missing" in target)
278
+ continue;
279
+ const goes = where(target.path);
280
+ if (goes === target.path && now === page.path)
281
+ continue;
282
+ const [written = ""] = href.split("#");
283
+ let fresh = goes === now ? "" : relativeHref(now, goes);
284
+ if (written.endsWith(".md") && !goes.endsWith("/index") && goes !== "index" && fresh !== "") {
285
+ fresh = `${fresh}.md`;
286
+ }
287
+ if (target.anchor !== undefined)
288
+ fresh = `${fresh}#${target.anchor}`;
289
+ if (fresh === "")
290
+ fresh = ".";
291
+ if (fresh === href)
292
+ continue;
293
+ next = next.split(`](${href})`).join(`](${fresh})`).split(`](${href} `).join(`](${fresh} `);
294
+ }
295
+ if (next !== body) {
296
+ draft.write(now, offset === 0 ? next : `---\n${yaml}\n---\n${next}`);
297
+ }
298
+ }
299
+ }
300
+ /** Every page under a folder, its own included. */
301
+ function under(pages, folder) {
302
+ return pages.filter((page) => page.path.startsWith(`${folder}/`));
303
+ }
304
+ const isFolder = (pages, path) => pages.some((page) => page.path.startsWith(`${path}/`));
305
+ /** Puts a name at a place in a folder's order, and writes the order when it is a change. */
306
+ function place(draft, where, name) {
307
+ if (!("before" in where) && !("after" in where))
308
+ return;
309
+ const names = orderOf(draft.now(), where.folder).filter((one) => one !== name);
310
+ const beside = "before" in where ? where.before : where.after;
311
+ let at = names.indexOf(beside);
312
+ if (at === -1)
313
+ at = names.length;
314
+ else if ("after" in where)
315
+ at += 1;
316
+ names.splice(at, 0, name);
317
+ const own = draft.read(indexOf(where.folder));
318
+ const listed = own ? frontmatterOf(own)?.pages : undefined;
319
+ if (listed && listed.join("/") === names.join("/"))
320
+ return;
321
+ setListing(draft, where.folder, names);
322
+ }
323
+ /**
324
+ * Moves a page or a folder to a folder, and among what is there when a place
325
+ * beside another is given. Both folders' lists follow, and links to and from
326
+ * what moved are rewritten.
327
+ */
328
+ export function planMove(pages, path, where) {
329
+ const folder = isFolder(pages, path);
330
+ const exists = folder || pages.some((page) => page.path === path);
331
+ if (!exists)
332
+ return { problem: `There is nothing at ${path}.` };
333
+ const name = lastOf(path);
334
+ const from = parentOf(path);
335
+ if (where.folder === "" && ("before" in where || "after" in where) && from === "") {
336
+ return { problem: "Top-level folders are listed by name." };
337
+ }
338
+ if (folder && (where.folder === path || where.folder.startsWith(`${path}/`))) {
339
+ return { problem: "A folder cannot go inside itself." };
340
+ }
341
+ if (where.folder === "" && !folder) {
342
+ return { problem: "A page goes in a folder. Top-level folders are the sites." };
343
+ }
344
+ const draft = new Draft(pages);
345
+ const before = new Map(pages.map((page) => [page.path, page.source]));
346
+ const target = join(where.folder, name);
347
+ const moved = new Map();
348
+ if (target !== path) {
349
+ if (pages.some((page) => page.path === target || page.path.startsWith(`${target}/`))) {
350
+ return {
351
+ problem: `There is already something named ${name} in ${where.folder || "the top level"}.`,
352
+ };
353
+ }
354
+ if (folder) {
355
+ for (const page of under(pages, path))
356
+ moved.set(page.path, `${target}${page.path.slice(path.length)}`);
357
+ }
358
+ else
359
+ moved.set(path, target);
360
+ unlist(draft, from, name);
361
+ for (const [one, to] of moved)
362
+ draft.move(one, to);
363
+ }
364
+ place(draft, where, name);
365
+ rewriteLinks(draft, pages, moved);
366
+ return { changes: draft.changes(before) };
367
+ }
368
+ /** Renames a page or a folder: its title, its name, or both. */
369
+ export function planRename(pages, path, next) {
370
+ const folder = isFolder(pages, path);
371
+ const isPage = pages.some((page) => page.path === path);
372
+ if (!folder && !isPage)
373
+ return { problem: `There is nothing at ${path}.` };
374
+ if (next.title !== undefined && next.title.trim() === "")
375
+ return { problem: "A title is needed." };
376
+ const draft = new Draft(pages);
377
+ const before = new Map(pages.map((page) => [page.path, page.source]));
378
+ const name = lastOf(path);
379
+ const parent = parentOf(path);
380
+ const moved = new Map();
381
+ if (next.name !== undefined && next.name !== name) {
382
+ const problem = nameProblem(next.name);
383
+ if (problem)
384
+ return { problem };
385
+ if (name === "index" && !folder)
386
+ return { problem: "A folder's own page is always named index." };
387
+ const target = join(parent, next.name);
388
+ if (pages.some((page) => page.path === target || page.path.startsWith(`${target}/`))) {
389
+ return {
390
+ problem: next.name === "index"
391
+ ? "This folder already has its own page. A folder can have one."
392
+ : `There is already something named ${next.name} here.`,
393
+ };
394
+ }
395
+ if (folder) {
396
+ for (const page of under(pages, path))
397
+ moved.set(page.path, `${target}${page.path.slice(path.length)}`);
398
+ }
399
+ else
400
+ moved.set(path, target);
401
+ for (const [one, to] of moved)
402
+ draft.move(one, to);
403
+ if (next.name === "index")
404
+ unlist(draft, parent, name);
405
+ else
406
+ relist(draft, parent, name, next.name);
407
+ }
408
+ const at = moved.size === 0 ? path : join(parent, next.name);
409
+ if (next.title !== undefined) {
410
+ const own = folder ? indexOf(at) : at;
411
+ const source = draft.read(own) ?? newPageSource(next.title);
412
+ if (titleIn(own, source) !== next.title || !draft.has(own)) {
413
+ const written = setFrontmatter(source, { title: next.title.trim() });
414
+ if (written === undefined) {
415
+ return { problem: "The top of this page cannot be read. Mend it in the editor first." };
416
+ }
417
+ draft.write(own, written);
418
+ }
419
+ }
420
+ rewriteLinks(draft, pages, moved);
421
+ return { changes: draft.changes(before) };
422
+ }
423
+ /** A new page in a folder, with nothing under its title. */
424
+ export function planNewPage(pages, folder, next) {
425
+ if (next.title.trim() === "")
426
+ return { problem: "A title is needed." };
427
+ const problem = nameProblem(next.name);
428
+ if (problem)
429
+ return { problem };
430
+ if (folder === "")
431
+ return { problem: "A page goes in a folder. Top-level folders are the sites." };
432
+ const path = join(folder, next.name);
433
+ if (pages.some((page) => page.path === path || page.path.startsWith(`${path}/`))) {
434
+ return {
435
+ problem: next.name === "index"
436
+ ? "This folder already has its own page. A folder can have one."
437
+ : `There is already something named ${next.name} here.`,
438
+ };
439
+ }
440
+ const inherited = folderType(path, (at) => frontmatterOf(pages.find((page) => page.path === at)?.source ?? "")?.type);
441
+ // A page left to the folder's type starts from that type's template too, and says nothing of it.
442
+ const source = newPageSource(next.title.trim(), next.type ?? inherited, inherited);
443
+ return { changes: [{ path, source, created: true }] };
444
+ }
445
+ /**
446
+ * A new folder. It is its own page, with nothing written in it, so that it
447
+ * lists what is put in it until someone writes there.
448
+ */
449
+ export function planNewFolder(pages, parent, next) {
450
+ if (next.title.trim() === "")
451
+ return { problem: "A title is needed." };
452
+ const problem = nameProblem(next.name);
453
+ if (problem)
454
+ return { problem };
455
+ if (next.name === "index")
456
+ return { problem: "A folder cannot be named index." };
457
+ const path = join(parent, next.name);
458
+ if (pages.some((page) => page.path === path || page.path.startsWith(`${path}/`))) {
459
+ return { problem: `There is already something named ${next.name} here.` };
460
+ }
461
+ return {
462
+ changes: [{ path: indexOf(path), source: newPageSource(next.title.trim()), created: true }],
463
+ };
464
+ }
465
+ /** Removes a page, or a folder and everything in it. */
466
+ export function planDelete(pages, path) {
467
+ const folder = isFolder(pages, path);
468
+ const gone = folder ? under(pages, path) : pages.filter((page) => page.path === path);
469
+ if (gone.length === 0)
470
+ return { problem: `There is nothing at ${path}.` };
471
+ const draft = new Draft(pages);
472
+ const before = new Map(pages.map((page) => [page.path, page.source]));
473
+ for (const page of gone)
474
+ draft.remove(page.path);
475
+ const name = lastOf(path);
476
+ if (name !== "index")
477
+ unlist(draft, parentOf(path), name);
478
+ return { changes: draft.changes(before) };
479
+ }
480
+ /**
481
+ * A copy of a page or a folder beside it. A copy takes no topic, terms or
482
+ * key: those belong to one page.
483
+ */
484
+ export function planDuplicate(pages, path) {
485
+ const folder = isFolder(pages, path);
486
+ const copied = folder ? under(pages, path) : pages.filter((page) => page.path === path);
487
+ if (copied.length === 0)
488
+ return { problem: `There is nothing at ${path}.` };
489
+ const name = lastOf(path);
490
+ if (name === "index")
491
+ return { problem: "A folder's own page is copied with its folder." };
492
+ const parent = parentOf(path);
493
+ const taken = (one) => pages.some((page) => page.path === join(parent, one) || page.path.startsWith(`${join(parent, one)}/`));
494
+ let copy = `${name}-copy`;
495
+ for (let n = 2; taken(copy); n++)
496
+ copy = `${name}-copy-${n}`;
497
+ const target = join(parent, copy);
498
+ const changes = [];
499
+ for (const page of copied) {
500
+ const to = folder ? `${target}${page.path.slice(path.length)}` : target;
501
+ const own = page.path === path || page.path === indexOf(path);
502
+ const title = titleIn(page.path, page.source);
503
+ const source = setFrontmatter(page.source, {
504
+ topic: undefined,
505
+ terms: undefined,
506
+ key: undefined,
507
+ ...(own ? { title: `${title} (copy)` } : {}),
508
+ });
509
+ if (source === undefined)
510
+ return { problem: `The top of ${page.path} cannot be read.` };
511
+ changes.push({ path: to, source, created: true });
512
+ }
513
+ return { changes: changes.sort((a, b) => a.path.localeCompare(b.path)) };
514
+ }
515
+ /** What a plan undoes: the changes that put the pages back as they were. */
516
+ export function inverseOf(pages, changes) {
517
+ const before = new Map(pages.map((page) => [page.path, page.source]));
518
+ return changes.map((change) => {
519
+ if (change.created)
520
+ return { path: change.path, deleted: true };
521
+ const source = before.get(change.path);
522
+ if (change.deleted)
523
+ return { path: change.path, source, created: true };
524
+ if (change.to !== undefined)
525
+ return { path: change.to, to: change.path, source };
526
+ return { path: change.path, source };
527
+ });
528
+ }
529
+ //# sourceMappingURL=ops.js.map