@dogsbay/site-files 0.2.0-beta.141 → 0.2.0-beta.142
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/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/mounts.d.ts +73 -0
- package/dist/mounts.js +163 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/mounts.d.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mounted sources: a source whose pages go under a path INSIDE its version and
|
|
3
|
+
* locale, beside the Markdown it belongs to, instead of under a `/<name>/` of
|
|
4
|
+
* its own. An OpenAPI reference that replaced an API viewer page is the case:
|
|
5
|
+
*
|
|
6
|
+
* sources:
|
|
7
|
+
* - { path: ./content, version: "3.33" }
|
|
8
|
+
* - { path: ./content/reference/rest-api/openapi.yaml, from: openapi,
|
|
9
|
+
* version: "3.33", mount: reference/rest-api }
|
|
10
|
+
*
|
|
11
|
+
* → /3.33/reference/rest-api/ the API's root page
|
|
12
|
+
* /3.33/reference/rest-api/<tag>/… its tag and operation pages
|
|
13
|
+
*
|
|
14
|
+
* The Astro pipeline (dogsbay CLI, import-content.ts) and dogsbay-ssg apply the
|
|
15
|
+
* same rules from here: where the pages go, which page is the root, and where
|
|
16
|
+
* the API's menu section goes. See docs/openapi.md and docs/versioning.md.
|
|
17
|
+
*/
|
|
18
|
+
import type { PrefixableNavItem } from "./namespaces.js";
|
|
19
|
+
/** A mount path's segments, or why it cannot be one. */
|
|
20
|
+
export declare function parseMount(mount: unknown): {
|
|
21
|
+
segments: string[];
|
|
22
|
+
} | {
|
|
23
|
+
error: string;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* A mounted source's page slug: its root page (`index` or `""`) is the mount
|
|
27
|
+
* itself, every other page goes under it. `(["reference","rest-api"], "pets")`
|
|
28
|
+
* → `reference/rest-api/pets`.
|
|
29
|
+
*/
|
|
30
|
+
export declare function mountSlug(segments: readonly string[], slug: string): string;
|
|
31
|
+
/** Whether `slug` is a mounted source's root page (before mounting). */
|
|
32
|
+
export declare function isMountRoot(slug: string): boolean;
|
|
33
|
+
/**
|
|
34
|
+
* A mounted source's links to its OWN pages, moved under the mount:
|
|
35
|
+
* `/pets/list-pets` → `/reference/rest-api/pets/list-pets` when `pets/list-pets`
|
|
36
|
+
* is one of `ownSlugs` (the source's slugs before mounting; `index` or `""`
|
|
37
|
+
* is the root, linked as `/`). Every other ref is left for the source's
|
|
38
|
+
* prefix as usual: an image (`/_assets/x.png`) is the site's, and a link to a
|
|
39
|
+
* site page (`/guide/x`) stays one. Covers `href` fields and `href="…"` in raw
|
|
40
|
+
* HTML. Changes `tree` in place.
|
|
41
|
+
*/
|
|
42
|
+
export declare function mountRefs(tree: unknown, segments: readonly string[], ownSlugs: ReadonlySet<string>): void;
|
|
43
|
+
/** Where `placeMountedNav` put the API's section. */
|
|
44
|
+
export type MountPlacement = "at-page" | "in-section" | "top-level";
|
|
45
|
+
/**
|
|
46
|
+
* A mounted source's menu, placed in the menu of the source it is mounted
|
|
47
|
+
* beside (same version, locale and name):
|
|
48
|
+
*
|
|
49
|
+
* 1. The menu has an entry for the mount page: the API's sections become
|
|
50
|
+
* that entry's children, after any it has.
|
|
51
|
+
* 2. Otherwise, at the end of the deepest section with an entry from the
|
|
52
|
+
* mount's parent folder (`reference/` for `reference/rest-api`), the
|
|
53
|
+
* first in menu order when several are as deep: an entry labelled
|
|
54
|
+
* `label`, linking to the mount, holding the API's sections.
|
|
55
|
+
* 3. Otherwise (a one-segment mount, or no such section), the same entry at
|
|
56
|
+
* the end of the menu.
|
|
57
|
+
*
|
|
58
|
+
* `mounted` is the source's own menu with its hrefs already mounted and
|
|
59
|
+
* prefixed; its first entry, when it links to the mount, is the root page's
|
|
60
|
+
* and is not repeated (the section links there). `mountHref` is the mount's
|
|
61
|
+
* full href (base path, unit prefix, mount); `mount` the mount path itself
|
|
62
|
+
* (`reference/rest-api`), which decides whether there is a parent folder: the
|
|
63
|
+
* base path and the version are not one. Returns a new menu; the inputs are
|
|
64
|
+
* not changed.
|
|
65
|
+
*/
|
|
66
|
+
export declare function placeMountedNav<T extends PrefixableNavItem & {
|
|
67
|
+
label?: string;
|
|
68
|
+
}>(host: readonly T[], mounted: readonly T[], mountHref: string, label: string, mount: string): {
|
|
69
|
+
nav: T[];
|
|
70
|
+
placement: MountPlacement;
|
|
71
|
+
};
|
|
72
|
+
/** The note a build prints when a content page stands as a mounted source's root. */
|
|
73
|
+
export declare function mountRootNote(mount: string, slug: string): string;
|
package/dist/mounts.js
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/** A mount path's segments, or why it cannot be one. */
|
|
2
|
+
export function parseMount(mount) {
|
|
3
|
+
if (typeof mount !== "string")
|
|
4
|
+
return { error: "mount must be a string" };
|
|
5
|
+
const trimmed = mount.trim().replace(/^\/+|\/+$/g, "");
|
|
6
|
+
if (!trimmed)
|
|
7
|
+
return { error: "mount must name a path, like reference/rest-api" };
|
|
8
|
+
const segments = trimmed.split("/");
|
|
9
|
+
if (segments.length > 8)
|
|
10
|
+
return { error: `mount "${mount}" has more than 8 segments` };
|
|
11
|
+
for (const s of segments) {
|
|
12
|
+
if (!/^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(s) || s.length > 100) {
|
|
13
|
+
return {
|
|
14
|
+
error: `mount "${mount}": "${s}" is not a path segment ` +
|
|
15
|
+
"(letters, digits, `.`, `_` and `-`, starting with a letter or digit)",
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return { segments };
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* A mounted source's page slug: its root page (`index` or `""`) is the mount
|
|
23
|
+
* itself, every other page goes under it. `(["reference","rest-api"], "pets")`
|
|
24
|
+
* → `reference/rest-api/pets`.
|
|
25
|
+
*/
|
|
26
|
+
export function mountSlug(segments, slug) {
|
|
27
|
+
const base = segments.join("/");
|
|
28
|
+
return slug === "" || slug === "index" ? base : `${base}/${slug}`;
|
|
29
|
+
}
|
|
30
|
+
/** Whether `slug` is a mounted source's root page (before mounting). */
|
|
31
|
+
export function isMountRoot(slug) {
|
|
32
|
+
return slug === "" || slug === "index";
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* A mounted source's links to its OWN pages, moved under the mount:
|
|
36
|
+
* `/pets/list-pets` → `/reference/rest-api/pets/list-pets` when `pets/list-pets`
|
|
37
|
+
* is one of `ownSlugs` (the source's slugs before mounting; `index` or `""`
|
|
38
|
+
* is the root, linked as `/`). Every other ref is left for the source's
|
|
39
|
+
* prefix as usual: an image (`/_assets/x.png`) is the site's, and a link to a
|
|
40
|
+
* site page (`/guide/x`) stays one. Covers `href` fields and `href="…"` in raw
|
|
41
|
+
* HTML. Changes `tree` in place.
|
|
42
|
+
*/
|
|
43
|
+
export function mountRefs(tree, segments, ownSlugs) {
|
|
44
|
+
const base = segments.join("/");
|
|
45
|
+
const move = (u) => {
|
|
46
|
+
if (!u.startsWith("/") || u.startsWith("//"))
|
|
47
|
+
return u;
|
|
48
|
+
const cut = u.search(/[?#]/);
|
|
49
|
+
const path = cut === -1 ? u : u.slice(0, cut);
|
|
50
|
+
const rest = cut === -1 ? "" : u.slice(cut);
|
|
51
|
+
const slug = path.replace(/^\/+|\/+$/g, "");
|
|
52
|
+
const own = slug === "" ? ownSlugs.has("index") || ownSlugs.has("") : ownSlugs.has(slug);
|
|
53
|
+
if (!own)
|
|
54
|
+
return u;
|
|
55
|
+
return `/${slug === "" ? base : `${base}/${slug}`}${path.endsWith("/") && slug !== "" ? "/" : ""}${rest}`;
|
|
56
|
+
};
|
|
57
|
+
const walk = (node) => {
|
|
58
|
+
if (Array.isArray(node)) {
|
|
59
|
+
for (const item of node)
|
|
60
|
+
walk(item);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (!node || typeof node !== "object")
|
|
64
|
+
return;
|
|
65
|
+
const obj = node;
|
|
66
|
+
if (typeof obj.href === "string")
|
|
67
|
+
obj.href = move(obj.href);
|
|
68
|
+
if (typeof obj.html === "string") {
|
|
69
|
+
obj.html = obj.html.replace(/\bhref=("|')(.*?)\1/gi, (_m, q, val) => `href=${q}${move(val)}${q}`);
|
|
70
|
+
}
|
|
71
|
+
for (const key of Object.keys(obj)) {
|
|
72
|
+
const val = obj[key];
|
|
73
|
+
if (val && typeof val === "object")
|
|
74
|
+
walk(val);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
walk(tree);
|
|
78
|
+
}
|
|
79
|
+
/** A nav href for comparing: no trailing slash, no fragment or query. */
|
|
80
|
+
function hrefKey(href) {
|
|
81
|
+
if (href === undefined)
|
|
82
|
+
return undefined;
|
|
83
|
+
const bare = href.split(/[#?]/)[0];
|
|
84
|
+
return bare.length > 1 ? bare.replace(/\/+$/, "") : bare;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* A mounted source's menu, placed in the menu of the source it is mounted
|
|
88
|
+
* beside (same version, locale and name):
|
|
89
|
+
*
|
|
90
|
+
* 1. The menu has an entry for the mount page: the API's sections become
|
|
91
|
+
* that entry's children, after any it has.
|
|
92
|
+
* 2. Otherwise, at the end of the deepest section with an entry from the
|
|
93
|
+
* mount's parent folder (`reference/` for `reference/rest-api`), the
|
|
94
|
+
* first in menu order when several are as deep: an entry labelled
|
|
95
|
+
* `label`, linking to the mount, holding the API's sections.
|
|
96
|
+
* 3. Otherwise (a one-segment mount, or no such section), the same entry at
|
|
97
|
+
* the end of the menu.
|
|
98
|
+
*
|
|
99
|
+
* `mounted` is the source's own menu with its hrefs already mounted and
|
|
100
|
+
* prefixed; its first entry, when it links to the mount, is the root page's
|
|
101
|
+
* and is not repeated (the section links there). `mountHref` is the mount's
|
|
102
|
+
* full href (base path, unit prefix, mount); `mount` the mount path itself
|
|
103
|
+
* (`reference/rest-api`), which decides whether there is a parent folder: the
|
|
104
|
+
* base path and the version are not one. Returns a new menu; the inputs are
|
|
105
|
+
* not changed.
|
|
106
|
+
*/
|
|
107
|
+
export function placeMountedNav(host, mounted, mountHref, label, mount) {
|
|
108
|
+
const mountKey = hrefKey(mountHref);
|
|
109
|
+
const sections = hrefKey(mounted[0]?.href) === mountKey ? mounted.slice(1) : mounted.slice();
|
|
110
|
+
const clone = (items) => items.map((i) => ({ ...i, ...(i.children ? { children: clone(i.children) } : {}) }));
|
|
111
|
+
const nav = clone(host);
|
|
112
|
+
// 1. An entry for the mount page.
|
|
113
|
+
const atPage = (items) => {
|
|
114
|
+
for (const item of items) {
|
|
115
|
+
if (hrefKey(item.href) === mountKey)
|
|
116
|
+
return item;
|
|
117
|
+
const found = item.children ? atPage(item.children) : undefined;
|
|
118
|
+
if (found)
|
|
119
|
+
return found;
|
|
120
|
+
}
|
|
121
|
+
return undefined;
|
|
122
|
+
};
|
|
123
|
+
const page = atPage(nav);
|
|
124
|
+
if (page) {
|
|
125
|
+
page.children = [...(page.children ?? []), ...sections];
|
|
126
|
+
return { nav, placement: "at-page" };
|
|
127
|
+
}
|
|
128
|
+
const entry = { label, href: mountHref, children: sections };
|
|
129
|
+
// 2. The deepest section holding an entry from the parent folder: only a
|
|
130
|
+
// mount of two segments or more has one (`/3.32/rest-api`'s `/3.32` is the
|
|
131
|
+
// version, not a folder of the mount's).
|
|
132
|
+
const cut = mountKey.lastIndexOf("/");
|
|
133
|
+
const parent = cut > 0 ? mountKey.slice(0, cut) : "";
|
|
134
|
+
if (parent && mount.split("/").filter(Boolean).length > 1) {
|
|
135
|
+
let best;
|
|
136
|
+
const visit = (items, depth) => {
|
|
137
|
+
for (const item of items) {
|
|
138
|
+
const children = item.children;
|
|
139
|
+
if (!children || children.length === 0)
|
|
140
|
+
continue;
|
|
141
|
+
const holds = children.some((c) => {
|
|
142
|
+
const k = hrefKey(c.href);
|
|
143
|
+
return k !== undefined && (k === parent || k.startsWith(`${parent}/`));
|
|
144
|
+
});
|
|
145
|
+
if (holds && (!best || depth > best.depth))
|
|
146
|
+
best = { items: children, depth };
|
|
147
|
+
visit(children, depth + 1);
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
visit(nav, 0);
|
|
151
|
+
if (best) {
|
|
152
|
+
best.items.push(entry);
|
|
153
|
+
return { nav, placement: "in-section" };
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
// 3. The end of the menu.
|
|
157
|
+
nav.push(entry);
|
|
158
|
+
return { nav, placement: "top-level" };
|
|
159
|
+
}
|
|
160
|
+
/** The note a build prints when a content page stands as a mounted source's root. */
|
|
161
|
+
export function mountRootNote(mount, slug) {
|
|
162
|
+
return `${slug}: the content page is the API root of the source mounted at "${mount}"; the generated root page is skipped`;
|
|
163
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dogsbay/site-files",
|
|
3
|
-
"version": "0.2.0-beta.
|
|
3
|
+
"version": "0.2.0-beta.142",
|
|
4
4
|
"description": "robots.txt, _headers, llms.txt, sitemaps, redirects and JSON-LD for docs sites; shared by the dogsbay Astro pipeline and dogsbay-ssg, Worker-safe",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|