@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/LICENSE +202 -0
- package/THIRD_PARTY_NOTICES.md +23036 -0
- package/dist/among.d.ts +44 -0
- package/dist/among.js +128 -0
- package/dist/changelog.d.ts +54 -0
- package/dist/changelog.js +88 -0
- package/dist/diataxis.d.ts +52 -0
- package/dist/diataxis.js +155 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +14 -0
- package/dist/ops.d.ts +111 -0
- package/dist/ops.js +529 -0
- package/dist/output.d.ts +57 -0
- package/dist/output.js +56 -0
- package/dist/package.d.ts +34 -0
- package/dist/package.js +129 -0
- package/dist/page.d.ts +75 -0
- package/dist/page.js +189 -0
- package/dist/redirects.d.ts +27 -0
- package/dist/redirects.js +79 -0
- package/dist/schema.d.ts +210 -0
- package/dist/schema.js +238 -0
- package/dist/search-query.d.ts +103 -0
- package/dist/search-query.js +197 -0
- package/dist/search.d.ts +17 -0
- package/dist/search.js +98 -0
- package/dist/set.d.ts +198 -0
- package/dist/set.js +566 -0
- package/dist/stack.d.ts +45 -0
- package/dist/stack.js +73 -0
- package/dist/workspace.d.ts +122 -0
- package/dist/workspace.js +207 -0
- package/package.json +34 -3
- package/README.md +0 -3
package/dist/output.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { Root } from "@volvel/markup";
|
|
2
|
+
import { type DocSet, type Nav, type Term } from "./set.js";
|
|
3
|
+
/** What a built set says about itself: enough to list, link and look up without opening a page. */
|
|
4
|
+
export interface Manifest {
|
|
5
|
+
/** The version of what the pages describe. */
|
|
6
|
+
version: string;
|
|
7
|
+
nav: Nav;
|
|
8
|
+
pages: {
|
|
9
|
+
path: string;
|
|
10
|
+
url: string;
|
|
11
|
+
title: string;
|
|
12
|
+
description?: string;
|
|
13
|
+
topic?: string;
|
|
14
|
+
since?: string;
|
|
15
|
+
date?: string;
|
|
16
|
+
updated?: string;
|
|
17
|
+
/** For a typed page: its type, and what it is about. */
|
|
18
|
+
docType?: string;
|
|
19
|
+
subjects?: string[];
|
|
20
|
+
}[];
|
|
21
|
+
topics: Record<string, string>;
|
|
22
|
+
terms: Record<string, Term>;
|
|
23
|
+
attachments: Record<string, string[]>;
|
|
24
|
+
extensions: Record<string, string[]>;
|
|
25
|
+
/** For a site with pages that moved: an old path to the page's path now. */
|
|
26
|
+
redirects?: Record<string, string>;
|
|
27
|
+
/**
|
|
28
|
+
* The names of the screenshots that were taken for this version. A page that
|
|
29
|
+
* shows one that is not here shows what the picture would have been of.
|
|
30
|
+
*/
|
|
31
|
+
screenshots?: string[];
|
|
32
|
+
/**
|
|
33
|
+
* The file of each of those, by look: `{ "console/table-help": { light: "…png", dark: "…png" } }`,
|
|
34
|
+
* under `assets/screenshots/`. The names carry a hash of the picture, so a file never changes.
|
|
35
|
+
* A build before these were kept has none, and a page then shows its words.
|
|
36
|
+
*/
|
|
37
|
+
screenshotFiles?: Record<string, Record<string, string>>;
|
|
38
|
+
}
|
|
39
|
+
/** What is written for one page: its tree, and what its frontmatter said. */
|
|
40
|
+
export interface PageFile {
|
|
41
|
+
path: string;
|
|
42
|
+
title: string;
|
|
43
|
+
description?: string;
|
|
44
|
+
tree: Root;
|
|
45
|
+
}
|
|
46
|
+
export declare function manifestOf(set: DocSet, version: string): Manifest;
|
|
47
|
+
/**
|
|
48
|
+
* A set as files to ship: `manifest.json`, one `pages/<path>.json` for each
|
|
49
|
+
* page, and `search.json`. A reader of the files loads the manifest, then
|
|
50
|
+
* only the pages that are opened.
|
|
51
|
+
*/
|
|
52
|
+
export declare function packageOutput(set: DocSet, options: {
|
|
53
|
+
version: string;
|
|
54
|
+
productName?: string;
|
|
55
|
+
generated?: ReadonlySet<string>;
|
|
56
|
+
}): Map<string, string>;
|
|
57
|
+
//# sourceMappingURL=output.d.ts.map
|
package/dist/output.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { buildSearchIndex } from "./search.js";
|
|
2
|
+
import { pagesInOrder, urlOf } from "./set.js";
|
|
3
|
+
export function manifestOf(set, version) {
|
|
4
|
+
const order = pagesInOrder(set.nav).map((page) => page.path);
|
|
5
|
+
const rest = [...set.pages.keys()].filter((path) => !order.includes(path)).sort();
|
|
6
|
+
return {
|
|
7
|
+
version,
|
|
8
|
+
nav: set.nav,
|
|
9
|
+
pages: [...order, ...rest].flatMap((path) => {
|
|
10
|
+
const entry = set.pages.get(path);
|
|
11
|
+
if (!entry)
|
|
12
|
+
return [];
|
|
13
|
+
const { title, description, topic, since, date } = entry.frontmatter;
|
|
14
|
+
return [
|
|
15
|
+
{
|
|
16
|
+
path,
|
|
17
|
+
url: urlOf(path),
|
|
18
|
+
title,
|
|
19
|
+
...(description ? { description } : {}),
|
|
20
|
+
...(topic ? { topic } : {}),
|
|
21
|
+
...(since ? { since } : {}),
|
|
22
|
+
...(date ? { date } : {}),
|
|
23
|
+
...(entry.updated ? { updated: entry.updated } : {}),
|
|
24
|
+
...(set.types[path] ? { docType: set.types[path] } : {}),
|
|
25
|
+
...(entry.frontmatter.subjects?.length ? { subjects: entry.frontmatter.subjects } : {}),
|
|
26
|
+
},
|
|
27
|
+
];
|
|
28
|
+
}),
|
|
29
|
+
topics: set.topics,
|
|
30
|
+
terms: set.terms,
|
|
31
|
+
attachments: set.attachments,
|
|
32
|
+
extensions: set.extensions,
|
|
33
|
+
...(Object.keys(set.redirects).length > 0 ? { redirects: set.redirects } : {}),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* A set as files to ship: `manifest.json`, one `pages/<path>.json` for each
|
|
38
|
+
* page, and `search.json`. A reader of the files loads the manifest, then
|
|
39
|
+
* only the pages that are opened.
|
|
40
|
+
*/
|
|
41
|
+
export function packageOutput(set, options) {
|
|
42
|
+
const files = new Map();
|
|
43
|
+
files.set("manifest.json", `${JSON.stringify(manifestOf(set, options.version))}\n`);
|
|
44
|
+
for (const [path, entry] of [...set.pages].sort(([a], [b]) => a.localeCompare(b))) {
|
|
45
|
+
const page = { path, title: entry.frontmatter.title, tree: entry.tree };
|
|
46
|
+
if (entry.frontmatter.description)
|
|
47
|
+
page.description = entry.frontmatter.description;
|
|
48
|
+
files.set(`pages/${path}.json`, `${JSON.stringify(page)}\n`);
|
|
49
|
+
}
|
|
50
|
+
files.set("search.json", buildSearchIndex(set, {
|
|
51
|
+
...(options.productName ? { productName: options.productName } : {}),
|
|
52
|
+
...(options.generated ? { generated: options.generated } : {}),
|
|
53
|
+
}));
|
|
54
|
+
return files;
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=output.js.map
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type Root } from "@volvel/markup";
|
|
2
|
+
import type { Manifest } from "./output.js";
|
|
3
|
+
import { loadSearchIndex } from "./search.js";
|
|
4
|
+
import type { DocSet } from "./set.js";
|
|
5
|
+
/** A package of documentation as `packageOutput` wrote it, opened to be served. */
|
|
6
|
+
export interface OpenedPackage {
|
|
7
|
+
/** The pages. Each page's tree is read from its file when it is first used, and not all are kept. */
|
|
8
|
+
set: DocSet;
|
|
9
|
+
manifest: Manifest;
|
|
10
|
+
version: string;
|
|
11
|
+
/** Old path to the path now, from the manifest and from `redirects.json`. */
|
|
12
|
+
redirects: Record<string, string>;
|
|
13
|
+
/** The search index, read on the first search. */
|
|
14
|
+
search(): ReturnType<typeof loadSearchIndex>;
|
|
15
|
+
/** A page's source as written, for "Copy as Markdown". Undefined where the package has none. */
|
|
16
|
+
source(path: string): string | undefined;
|
|
17
|
+
/** The path an old address now has, if the package knows it. */
|
|
18
|
+
moved(address: string): string | undefined;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The set a manifest describes: every page's frontmatter, kind and subjects, the navigation, the
|
|
22
|
+
* terms. A page's tree is `treeOf(path)`, asked for when it is first used; left out, a page's tree
|
|
23
|
+
* is empty, which is all a caller needs who reads only titles, order and what pages are about
|
|
24
|
+
* (the console's help panel, which draws a page from its own file).
|
|
25
|
+
*/
|
|
26
|
+
export declare function setFromManifest(manifest: Manifest, treeOf?: (path: string) => Root): DocSet;
|
|
27
|
+
/**
|
|
28
|
+
* Opens the files `packageOutput` wrote, by a function that reads one by its name (`manifest.json`,
|
|
29
|
+
* `pages/<path>.json`, `search.json`, `md/<path>.md`, `redirects.json`). The manifest is read at
|
|
30
|
+
* once, a page's tree when the page is drawn, the search index when somebody searches. A file that
|
|
31
|
+
* is missing is a page with nothing on it, or no search results, never a thrown error.
|
|
32
|
+
*/
|
|
33
|
+
export declare function openPackage(read: (name: string) => string | undefined): OpenedPackage;
|
|
34
|
+
//# sourceMappingURL=package.d.ts.map
|
package/dist/package.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { TREE_VERSION } from "@volvel/markup";
|
|
2
|
+
import { redirectedPath } from "./redirects.js";
|
|
3
|
+
import { loadSearchIndex } from "./search.js";
|
|
4
|
+
/** How many page trees are held at once. Others are read again from the files when asked for. */
|
|
5
|
+
const TREES_KEPT = 64;
|
|
6
|
+
/**
|
|
7
|
+
* The set a manifest describes: every page's frontmatter, kind and subjects, the navigation, the
|
|
8
|
+
* terms. A page's tree is `treeOf(path)`, asked for when it is first used; left out, a page's tree
|
|
9
|
+
* is empty, which is all a caller needs who reads only titles, order and what pages are about
|
|
10
|
+
* (the console's help panel, which draws a page from its own file).
|
|
11
|
+
*/
|
|
12
|
+
export function setFromManifest(manifest, treeOf = () => ({ type: "root", v: TREE_VERSION, children: [] })) {
|
|
13
|
+
const pages = new Map();
|
|
14
|
+
const types = {};
|
|
15
|
+
const subjects = {};
|
|
16
|
+
for (const page of manifest.pages) {
|
|
17
|
+
const frontmatter = {
|
|
18
|
+
title: page.title,
|
|
19
|
+
...(page.description ? { description: page.description } : {}),
|
|
20
|
+
...(page.topic ? { topic: page.topic } : {}),
|
|
21
|
+
...(page.since ? { since: page.since } : {}),
|
|
22
|
+
...(page.date ? { date: page.date } : {}),
|
|
23
|
+
...(page.subjects ? { subjects: page.subjects } : {}),
|
|
24
|
+
};
|
|
25
|
+
const entry = { path: page.path, frontmatter };
|
|
26
|
+
Object.defineProperty(entry, "tree", {
|
|
27
|
+
enumerable: true,
|
|
28
|
+
get: () => treeOf(page.path),
|
|
29
|
+
});
|
|
30
|
+
if (page.updated)
|
|
31
|
+
entry.updated = page.updated;
|
|
32
|
+
pages.set(page.path, entry);
|
|
33
|
+
if (page.docType)
|
|
34
|
+
types[page.path] = page.docType;
|
|
35
|
+
for (const subject of page.subjects ?? []) {
|
|
36
|
+
subjects[subject] = [...(subjects[subject] ?? []), page.path];
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
pages,
|
|
41
|
+
nav: manifest.nav,
|
|
42
|
+
topics: manifest.topics,
|
|
43
|
+
terms: manifest.terms,
|
|
44
|
+
attachments: manifest.attachments,
|
|
45
|
+
extensions: manifest.extensions,
|
|
46
|
+
types,
|
|
47
|
+
subjects,
|
|
48
|
+
redirects: manifest.redirects ?? {},
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Opens the files `packageOutput` wrote, by a function that reads one by its name (`manifest.json`,
|
|
53
|
+
* `pages/<path>.json`, `search.json`, `md/<path>.md`, `redirects.json`). The manifest is read at
|
|
54
|
+
* once, a page's tree when the page is drawn, the search index when somebody searches. A file that
|
|
55
|
+
* is missing is a page with nothing on it, or no search results, never a thrown error.
|
|
56
|
+
*/
|
|
57
|
+
export function openPackage(read) {
|
|
58
|
+
const text = read("manifest.json");
|
|
59
|
+
if (text === undefined)
|
|
60
|
+
throw new Error("The package has no manifest.json.");
|
|
61
|
+
const manifest = JSON.parse(text);
|
|
62
|
+
const trees = new Map();
|
|
63
|
+
const treeOf = (path) => {
|
|
64
|
+
const kept = trees.get(path);
|
|
65
|
+
if (kept) {
|
|
66
|
+
trees.delete(path);
|
|
67
|
+
trees.set(path, kept);
|
|
68
|
+
return kept;
|
|
69
|
+
}
|
|
70
|
+
let tree = { type: "root", v: TREE_VERSION, children: [] };
|
|
71
|
+
try {
|
|
72
|
+
const file = read(`pages/${path}.json`);
|
|
73
|
+
if (file !== undefined)
|
|
74
|
+
tree = JSON.parse(file).tree;
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
// A page file that cannot be read is an empty page.
|
|
78
|
+
}
|
|
79
|
+
trees.set(path, tree);
|
|
80
|
+
if (trees.size > TREES_KEPT)
|
|
81
|
+
trees.delete(trees.keys().next().value);
|
|
82
|
+
return tree;
|
|
83
|
+
};
|
|
84
|
+
const set = setFromManifest(manifest, treeOf);
|
|
85
|
+
const pages = set.pages;
|
|
86
|
+
let moves;
|
|
87
|
+
const redirects = () => {
|
|
88
|
+
if (moves)
|
|
89
|
+
return moves;
|
|
90
|
+
moves = { ...(manifest.redirects ?? {}) };
|
|
91
|
+
try {
|
|
92
|
+
const file = read("redirects.json");
|
|
93
|
+
if (file !== undefined)
|
|
94
|
+
Object.assign(moves, JSON.parse(file));
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
// Without the file the manifest's own moves stand.
|
|
98
|
+
}
|
|
99
|
+
return moves;
|
|
100
|
+
};
|
|
101
|
+
let index;
|
|
102
|
+
const none = { search: () => [], grouped: () => [] };
|
|
103
|
+
return {
|
|
104
|
+
set,
|
|
105
|
+
manifest,
|
|
106
|
+
version: manifest.version,
|
|
107
|
+
get redirects() {
|
|
108
|
+
return redirects();
|
|
109
|
+
},
|
|
110
|
+
search() {
|
|
111
|
+
if (index)
|
|
112
|
+
return index;
|
|
113
|
+
try {
|
|
114
|
+
const file = read("search.json");
|
|
115
|
+
index = file === undefined ? none : loadSearchIndex(file);
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
index = none;
|
|
119
|
+
}
|
|
120
|
+
return index;
|
|
121
|
+
},
|
|
122
|
+
source: (path) => read(`md/${path}.md`),
|
|
123
|
+
moved(address) {
|
|
124
|
+
const found = redirectedPath(redirects(), address);
|
|
125
|
+
return found !== undefined && pages.has(found) ? found : undefined;
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
//# sourceMappingURL=package.js.map
|
package/dist/page.d.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { type Issue, type ProfileName, type Root } from "@volvel/markup";
|
|
2
|
+
import { type Frontmatter } from "./schema.js";
|
|
3
|
+
/** Something wrong with a page or with a set of them, and where. */
|
|
4
|
+
export interface DocIssue {
|
|
5
|
+
/** The page, by its path under the docs root. */
|
|
6
|
+
path: string;
|
|
7
|
+
line: number;
|
|
8
|
+
column: number;
|
|
9
|
+
code: string;
|
|
10
|
+
message: string;
|
|
11
|
+
/** An error stops a deploy. A warning is shown and does not. */
|
|
12
|
+
severity: "error" | "warning";
|
|
13
|
+
}
|
|
14
|
+
/** One page, read. */
|
|
15
|
+
export interface DocEntry {
|
|
16
|
+
/** Its path under the docs root, without `.md`: `guides/refunds`, `guides/index`, `index`. */
|
|
17
|
+
path: string;
|
|
18
|
+
frontmatter: Frontmatter;
|
|
19
|
+
tree: Root;
|
|
20
|
+
/** When the page last changed, if whoever read it knows. An ISO date. */
|
|
21
|
+
updated?: string;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Parts a file into what is between its first two `---` lines and what comes
|
|
25
|
+
* after. `offset` is how many lines of the file come before the body.
|
|
26
|
+
*/
|
|
27
|
+
export declare function splitFrontmatter(text: string): {
|
|
28
|
+
yaml: string;
|
|
29
|
+
body: string;
|
|
30
|
+
offset: number;
|
|
31
|
+
};
|
|
32
|
+
/** What a page's frontmatter says, as it is written, when it can be read at all. */
|
|
33
|
+
export declare function frontmatterOf(text: string): Record<string, unknown> | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* A page's source with some of its frontmatter changed, for a form that
|
|
36
|
+
* writes what a person would have typed. A key given nothing, an empty text
|
|
37
|
+
* or an empty list is taken out. Everything else stays as it was written:
|
|
38
|
+
* the other keys, their order, the comments among them, and the page under
|
|
39
|
+
* them. A page with no frontmatter is given one. Undefined when what is
|
|
40
|
+
* there cannot be read, which a form cannot mend.
|
|
41
|
+
*/
|
|
42
|
+
export declare function setFrontmatter(text: string, changes: Record<string, unknown>): string | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* A file's path under the docs root as a page's path: without `.md`, and
|
|
45
|
+
* refused when it is not written as a resource's path is. Nothing is changed
|
|
46
|
+
* to make a name fit, so that a file and its page always have the one name.
|
|
47
|
+
*/
|
|
48
|
+
export declare function pagePath(file: string): {
|
|
49
|
+
path: string;
|
|
50
|
+
} | {
|
|
51
|
+
problem: string;
|
|
52
|
+
};
|
|
53
|
+
export interface ReadOptions {
|
|
54
|
+
/** What the body may hold. `docs` when left out. */
|
|
55
|
+
profile?: ProfileName;
|
|
56
|
+
updated?: string;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Reads one file into a page. It always gives a page back, with whatever
|
|
60
|
+
* could be read: a page that fails here can still be shown to its author
|
|
61
|
+
* beside what is wrong with it.
|
|
62
|
+
*/
|
|
63
|
+
export declare function readPage(path: string, text: string, options?: ReadOptions): {
|
|
64
|
+
entry: DocEntry;
|
|
65
|
+
issues: DocIssue[];
|
|
66
|
+
};
|
|
67
|
+
/** A title made from a path's last part, for a page or a folder that gives none. */
|
|
68
|
+
export declare function titleOf(path: string): string;
|
|
69
|
+
/**
|
|
70
|
+
* A folder's own page with nothing written in it lists what the folder holds:
|
|
71
|
+
* its tree, with `::pages` in place of nothing. Any other page is as it is.
|
|
72
|
+
*/
|
|
73
|
+
export declare function asDirectory(path: string, tree: Root): Root;
|
|
74
|
+
export type { Issue };
|
|
75
|
+
//# sourceMappingURL=page.d.ts.map
|
package/dist/page.js
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { parse, validate } from "@volvel/markup";
|
|
2
|
+
import { isMap, parseDocument, parse as parseYaml } from "yaml";
|
|
3
|
+
import { isDay } from "./changelog.js";
|
|
4
|
+
import { check, FRONTMATTER_SCHEMA, PATH_PATTERN } from "./schema.js";
|
|
5
|
+
const FENCE = /^---[ \t]*$/;
|
|
6
|
+
/**
|
|
7
|
+
* Parts a file into what is between its first two `---` lines and what comes
|
|
8
|
+
* after. `offset` is how many lines of the file come before the body.
|
|
9
|
+
*/
|
|
10
|
+
export function splitFrontmatter(text) {
|
|
11
|
+
const lines = text.replace(/\r\n?/g, "\n").split("\n");
|
|
12
|
+
if (!FENCE.test(lines[0] ?? ""))
|
|
13
|
+
return { yaml: "", body: lines.join("\n"), offset: 0 };
|
|
14
|
+
const close = lines.findIndex((line, index) => index > 0 && FENCE.test(line));
|
|
15
|
+
if (close === -1)
|
|
16
|
+
return { yaml: "", body: lines.join("\n"), offset: 0 };
|
|
17
|
+
return {
|
|
18
|
+
yaml: lines.slice(1, close).join("\n"),
|
|
19
|
+
body: lines.slice(close + 1).join("\n"),
|
|
20
|
+
offset: close + 1,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/** What a page's frontmatter says, as it is written, when it can be read at all. */
|
|
24
|
+
export function frontmatterOf(text) {
|
|
25
|
+
const { yaml, offset } = splitFrontmatter(text);
|
|
26
|
+
if (offset === 0)
|
|
27
|
+
return {};
|
|
28
|
+
try {
|
|
29
|
+
const read = parseYaml(yaml);
|
|
30
|
+
if (read === null || read === undefined)
|
|
31
|
+
return {};
|
|
32
|
+
return typeof read === "object" && !Array.isArray(read)
|
|
33
|
+
? read
|
|
34
|
+
: undefined;
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const empty = (value) => value === undefined ||
|
|
41
|
+
value === null ||
|
|
42
|
+
value === "" ||
|
|
43
|
+
(Array.isArray(value) && value.length === 0);
|
|
44
|
+
/**
|
|
45
|
+
* A page's source with some of its frontmatter changed, for a form that
|
|
46
|
+
* writes what a person would have typed. A key given nothing, an empty text
|
|
47
|
+
* or an empty list is taken out. Everything else stays as it was written:
|
|
48
|
+
* the other keys, their order, the comments among them, and the page under
|
|
49
|
+
* them. A page with no frontmatter is given one. Undefined when what is
|
|
50
|
+
* there cannot be read, which a form cannot mend.
|
|
51
|
+
*/
|
|
52
|
+
export function setFrontmatter(text, changes) {
|
|
53
|
+
const { yaml, body, offset } = splitFrontmatter(text);
|
|
54
|
+
// An empty top is read as an empty map, so that there is something to set keys on.
|
|
55
|
+
const written0 = offset === 0 || yaml.trim() === "" ? "{}" : yaml;
|
|
56
|
+
const document = parseDocument(written0);
|
|
57
|
+
if (document.errors.length > 0)
|
|
58
|
+
return undefined;
|
|
59
|
+
if (!isMap(document.contents))
|
|
60
|
+
return undefined;
|
|
61
|
+
document.contents.flow = false;
|
|
62
|
+
for (const [key, value] of Object.entries(changes)) {
|
|
63
|
+
if (empty(value))
|
|
64
|
+
document.delete(key);
|
|
65
|
+
else
|
|
66
|
+
document.set(key, value);
|
|
67
|
+
}
|
|
68
|
+
const written = isMap(document.contents) && document.contents.items.length === 0
|
|
69
|
+
? ""
|
|
70
|
+
: document.toString({ lineWidth: 0 });
|
|
71
|
+
const under = offset === 0 && body !== "" ? `\n${body}` : body;
|
|
72
|
+
return `---\n${written}---\n${under}`;
|
|
73
|
+
}
|
|
74
|
+
const PATH = new RegExp(PATH_PATTERN);
|
|
75
|
+
/**
|
|
76
|
+
* A file's path under the docs root as a page's path: without `.md`, and
|
|
77
|
+
* refused when it is not written as a resource's path is. Nothing is changed
|
|
78
|
+
* to make a name fit, so that a file and its page always have the one name.
|
|
79
|
+
*/
|
|
80
|
+
export function pagePath(file) {
|
|
81
|
+
if (!file.endsWith(".md"))
|
|
82
|
+
return { problem: `${file}: a page is a file ending in .md.` };
|
|
83
|
+
const path = file.slice(0, -3);
|
|
84
|
+
if (!PATH.test(path)) {
|
|
85
|
+
return {
|
|
86
|
+
problem: `${file}: a page's name and its folders are written in lowercase letters, digits, hyphens and underscores. Rename the file: nothing renames it for you.`,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
return { path };
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Reads one file into a page. It always gives a page back, with whatever
|
|
93
|
+
* could be read: a page that fails here can still be shown to its author
|
|
94
|
+
* beside what is wrong with it.
|
|
95
|
+
*/
|
|
96
|
+
export function readPage(path, text, options = {}) {
|
|
97
|
+
const issues = [];
|
|
98
|
+
const { yaml, body, offset } = splitFrontmatter(text);
|
|
99
|
+
const yamlLines = yaml.split("\n");
|
|
100
|
+
const lineOfKey = (key) => {
|
|
101
|
+
if (key === undefined)
|
|
102
|
+
return 1;
|
|
103
|
+
const at = yamlLines.findIndex((line) => new RegExp(`^\\s*(-\\s+)?${key}\\s*:`).test(line));
|
|
104
|
+
return at === -1 ? 1 : at + 2;
|
|
105
|
+
};
|
|
106
|
+
const error = (line, code, message) => {
|
|
107
|
+
issues.push({ path, line, column: 1, code, message, severity: "error" });
|
|
108
|
+
};
|
|
109
|
+
let frontmatter = { title: "" };
|
|
110
|
+
if (offset === 0) {
|
|
111
|
+
error(1, "frontmatter_missing", 'A page starts with its title between two lines of "---":\n---\ntitle: …\n---');
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
let read;
|
|
115
|
+
try {
|
|
116
|
+
read = parseYaml(yaml, { version: "1.2" }) ?? {};
|
|
117
|
+
}
|
|
118
|
+
catch (caught) {
|
|
119
|
+
const failure = caught;
|
|
120
|
+
error((failure.linePos?.[0]?.line ?? 0) + 1, "frontmatter_unreadable", `The top of the page is not valid YAML. ${failure.message.split("\n")[0]}`);
|
|
121
|
+
read = undefined;
|
|
122
|
+
}
|
|
123
|
+
if (read !== undefined) {
|
|
124
|
+
const problems = check(read, FRONTMATTER_SCHEMA);
|
|
125
|
+
for (const problem of problems) {
|
|
126
|
+
error(lineOfKey(problem.key), "frontmatter_invalid", problem.message);
|
|
127
|
+
}
|
|
128
|
+
if (typeof read === "object" && read !== null && !Array.isArray(read)) {
|
|
129
|
+
// What could be read is kept, so that the page can still be listed and drawn.
|
|
130
|
+
frontmatter = {
|
|
131
|
+
title: "",
|
|
132
|
+
...(problems.length === 0 ? read : usable(read)),
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
if (frontmatter.date !== undefined && !isDay(frontmatter.date)) {
|
|
138
|
+
// Right in shape (the schema said so) and not a day: 2026-02-30. A changelog would skip it silently.
|
|
139
|
+
error(lineOfKey("date"), "frontmatter_invalid", `The date is not a day on the calendar: "${frontmatter.date}". Write it as YYYY-MM-DD.`);
|
|
140
|
+
delete frontmatter.date;
|
|
141
|
+
}
|
|
142
|
+
if (frontmatter.title === "")
|
|
143
|
+
frontmatter.title = titleOf(path);
|
|
144
|
+
const tree = parse(body, { lineOffset: offset });
|
|
145
|
+
const profile = options.profile ?? "docs";
|
|
146
|
+
for (const issue of validate(tree, profile, { source: body, lineOffset: offset })) {
|
|
147
|
+
issues.push({ path, ...issue, severity: "error" });
|
|
148
|
+
}
|
|
149
|
+
for (const term of frontmatter.terms ?? []) {
|
|
150
|
+
const short = validate(parse(term.short), "short");
|
|
151
|
+
if (short.length > 0) {
|
|
152
|
+
error(lineOfKey("terms"), "term_short", `The term "${term.id}": what it means is plain text, with no Markdown.`);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
const entry = { path, frontmatter, tree };
|
|
156
|
+
if (options.updated !== undefined)
|
|
157
|
+
entry.updated = options.updated;
|
|
158
|
+
return { entry, issues };
|
|
159
|
+
}
|
|
160
|
+
/** The values of a frontmatter that are each right, when the whole is not. */
|
|
161
|
+
function usable(read) {
|
|
162
|
+
const out = {};
|
|
163
|
+
for (const [key, value] of Object.entries(read)) {
|
|
164
|
+
const schema = FRONTMATTER_SCHEMA.properties[key];
|
|
165
|
+
if (schema && check(value, schema, key, key).length === 0)
|
|
166
|
+
out[key] = value;
|
|
167
|
+
}
|
|
168
|
+
return out;
|
|
169
|
+
}
|
|
170
|
+
/** A title made from a path's last part, for a page or a folder that gives none. */
|
|
171
|
+
export function titleOf(path) {
|
|
172
|
+
const parts = path.split("/");
|
|
173
|
+
const last = parts.at(-1) === "index" ? parts.at(-2) : parts.at(-1);
|
|
174
|
+
const words = (last ?? "").replace(/[-_]+/g, " ").trim();
|
|
175
|
+
return words === "" ? "Home" : words.charAt(0).toUpperCase() + words.slice(1);
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* A folder's own page with nothing written in it lists what the folder holds:
|
|
179
|
+
* its tree, with `::pages` in place of nothing. Any other page is as it is.
|
|
180
|
+
*/
|
|
181
|
+
export function asDirectory(path, tree) {
|
|
182
|
+
if (tree.children.length > 0 || !(path === "index" || path.endsWith("/index")))
|
|
183
|
+
return tree;
|
|
184
|
+
return {
|
|
185
|
+
...tree,
|
|
186
|
+
children: [{ type: "component", name: "pages", form: "leaf", props: {}, children: [] }],
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
//# sourceMappingURL=page.js.map
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a page that moved can be found. A page says where it was (`moved_from`), and a
|
|
3
|
+
* file beside the pages says where a page that was deleted went. Both become one map of old
|
|
4
|
+
* path to new path, which the console, `/docs` and the static export all read.
|
|
5
|
+
*/
|
|
6
|
+
export interface MovedPage {
|
|
7
|
+
/** The page's path now. */
|
|
8
|
+
path: string;
|
|
9
|
+
/** The paths it was at before. */
|
|
10
|
+
movedFrom: readonly string[];
|
|
11
|
+
}
|
|
12
|
+
export interface RedirectProblem {
|
|
13
|
+
path: string;
|
|
14
|
+
code: "redirect_taken" | "redirect_duplicate" | "redirect_unknown" | "redirect_loop";
|
|
15
|
+
message: string;
|
|
16
|
+
}
|
|
17
|
+
/** Every old path and the page that now answers for it, with what is wrong with the map. */
|
|
18
|
+
export declare function redirectMap(pages: ReadonlySet<string>, moved: readonly MovedPage[], files?: Readonly<Record<string, string>>): {
|
|
19
|
+
redirects: Record<string, string>;
|
|
20
|
+
problems: RedirectProblem[];
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* The path a page is at now, for an address that may be an old one. Undefined when the map
|
|
24
|
+
* does not know it. `a/b/index` and `a/b` are one address.
|
|
25
|
+
*/
|
|
26
|
+
export declare function redirectedPath(redirects: Readonly<Record<string, string>>, path: string): string | undefined;
|
|
27
|
+
//# sourceMappingURL=redirects.d.ts.map
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a page that moved can be found. A page says where it was (`moved_from`), and a
|
|
3
|
+
* file beside the pages says where a page that was deleted went. Both become one map of old
|
|
4
|
+
* path to new path, which the console, `/docs` and the static export all read.
|
|
5
|
+
*/
|
|
6
|
+
/** Every old path and the page that now answers for it, with what is wrong with the map. */
|
|
7
|
+
export function redirectMap(pages, moved, files = {}) {
|
|
8
|
+
const redirects = {};
|
|
9
|
+
const problems = [];
|
|
10
|
+
const add = (from, to, where) => {
|
|
11
|
+
if (pages.has(from)) {
|
|
12
|
+
problems.push({
|
|
13
|
+
path: where,
|
|
14
|
+
code: "redirect_taken",
|
|
15
|
+
message: `"${from}" is said to have moved to "${to}", and a page is at "${from}" now. Remove it from the moved list, or move that page.`,
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
else if (redirects[from] !== undefined && redirects[from] !== to) {
|
|
19
|
+
problems.push({
|
|
20
|
+
path: where,
|
|
21
|
+
code: "redirect_duplicate",
|
|
22
|
+
message: `"${from}" is said to have moved to both "${redirects[from]}" and "${to}". An old path leads to one page.`,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
else
|
|
26
|
+
redirects[from] = to;
|
|
27
|
+
};
|
|
28
|
+
for (const page of moved)
|
|
29
|
+
for (const from of page.movedFrom)
|
|
30
|
+
add(from, page.path, page.path);
|
|
31
|
+
for (const [from, to] of Object.entries(files))
|
|
32
|
+
add(from, to, "redirects.json");
|
|
33
|
+
// Every old path must end at a page, by however many moves.
|
|
34
|
+
for (const from of Object.keys(redirects)) {
|
|
35
|
+
const end = follow(redirects, from);
|
|
36
|
+
if (end === "loop") {
|
|
37
|
+
problems.push({
|
|
38
|
+
path: from,
|
|
39
|
+
code: "redirect_loop",
|
|
40
|
+
message: `"${from}" leads back to itself through the moves.`,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
else if (!pages.has(end)) {
|
|
44
|
+
problems.push({
|
|
45
|
+
path: from,
|
|
46
|
+
code: "redirect_unknown",
|
|
47
|
+
message: `"${from}" leads to "${end}", and there is no such page.`,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return { redirects, problems };
|
|
52
|
+
}
|
|
53
|
+
function follow(redirects, path) {
|
|
54
|
+
const seen = new Set();
|
|
55
|
+
let at = path;
|
|
56
|
+
while (redirects[at] !== undefined) {
|
|
57
|
+
if (seen.has(at))
|
|
58
|
+
return "loop";
|
|
59
|
+
seen.add(at);
|
|
60
|
+
at = redirects[at];
|
|
61
|
+
}
|
|
62
|
+
return at;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The path a page is at now, for an address that may be an old one. Undefined when the map
|
|
66
|
+
* does not know it. `a/b/index` and `a/b` are one address.
|
|
67
|
+
*/
|
|
68
|
+
export function redirectedPath(redirects, path) {
|
|
69
|
+
const bare = path.replace(/\/+$/, "");
|
|
70
|
+
for (const candidate of [bare, `${bare}/index`, bare.replace(/\/index$/, "")]) {
|
|
71
|
+
if (candidate === "" || redirects[candidate] === undefined)
|
|
72
|
+
continue;
|
|
73
|
+
const end = follow(redirects, candidate);
|
|
74
|
+
if (end !== "loop")
|
|
75
|
+
return end;
|
|
76
|
+
}
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
//# sourceMappingURL=redirects.js.map
|