@dogsbay/docs-layout 0.2.0-beta.15 → 0.2.0-beta.151
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/package.json +15 -6
- package/src/AnnouncementBar.astro +38 -0
- package/src/BlogIndex.astro +179 -0
- package/src/DocsFooter.astro +110 -16
- package/src/DocsLayout.astro +728 -51
- package/src/DocsNavClient.astro +113 -0
- package/src/DocsToc.astro +1 -1
- package/src/HeaderLinks.astro +78 -0
- package/src/ProductSwitcher.astro +63 -0
- package/src/SearchDialog.astro +317 -33
- package/src/SocialLinks.astro +34 -0
- package/src/TagList.astro +24 -3
- package/src/VersionSwitcher.astro +29 -4
- package/src/docs-nav-client.ts +422 -0
- package/src/json-ld.ts +4 -53
- package/src/link-icons.ts +54 -0
- package/src/markdown-negotiation.ts +38 -2
- package/src/nav-filter.ts +68 -127
- package/src/pagefind-base-url.ts +59 -0
- package/src/search-facets.ts +511 -9
- package/src/site-chrome.ts +40 -0
- package/src/switcher.ts +194 -6
- package/src/tag-list-data.ts +26 -3
- package/src/toc-kind.css +10 -10
- package/src/toc-placement.ts +71 -0
- package/src/version-redirect.ts +17 -4
package/src/switcher.ts
CHANGED
|
@@ -7,11 +7,37 @@
|
|
|
7
7
|
* `format-astro/src/project.ts` (the emitter side).
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
+
import { limitVersionRows } from "@dogsbay/site-files";
|
|
11
|
+
|
|
10
12
|
export interface AxisEntry {
|
|
11
13
|
id: string;
|
|
12
14
|
label?: string;
|
|
13
15
|
eol?: boolean;
|
|
16
|
+
/** Pre-release mark (version axis only) — the moving next/beta head. */
|
|
17
|
+
prerelease?: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Served but kept OFF the switcher (version axis only). Marks the number
|
|
20
|
+
* a `latest` alias points at, so the dropdown shows one "3.32 (latest)"
|
|
21
|
+
* row instead of both "latest" and "3.32".
|
|
22
|
+
*/
|
|
23
|
+
hidden?: boolean;
|
|
14
24
|
default?: boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Served at the site root (version axis only; `versions[].path: ""` on the
|
|
27
|
+
* default version): its pages have no version segment, so its landing
|
|
28
|
+
* page is `<basePath>/`, and its per-page URLs come from the variants.
|
|
29
|
+
*/
|
|
30
|
+
root?: boolean;
|
|
31
|
+
/** With a `products:` block: the version's home URL, where the row goes when the page is not in it. */
|
|
32
|
+
home?: string;
|
|
33
|
+
/** "Version: <label>" above each page's title (version axis only). */
|
|
34
|
+
badge?: boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The version menu's links (hrefs as served) and limit, from `versionMenu:` (@dogsbay/site-files version-menu.ts). */
|
|
38
|
+
export interface VersionMenuData {
|
|
39
|
+
links: Array<{ label: string; href: string }>;
|
|
40
|
+
limit?: number;
|
|
15
41
|
}
|
|
16
42
|
|
|
17
43
|
export interface SwitcherVariant {
|
|
@@ -24,6 +50,50 @@ export interface SwitcherMap {
|
|
|
24
50
|
versions: AxisEntry[];
|
|
25
51
|
locales: AxisEntry[];
|
|
26
52
|
byLogicalKey: Record<string, SwitcherVariant[]>;
|
|
53
|
+
/**
|
|
54
|
+
* With a `products:` block: each product's version rows per locale
|
|
55
|
+
* (`<product|_>/<locale|_>`), config order, hidden left out, with homes.
|
|
56
|
+
*/
|
|
57
|
+
productVersions?: Record<string, AxisEntry[]>;
|
|
58
|
+
/**
|
|
59
|
+
* With a `products:` block and two or more products: the product menu per
|
|
60
|
+
* locale (`<locale|_>`), block order, each entry the product's home.
|
|
61
|
+
*/
|
|
62
|
+
products?: Record<string, ProductMenuEntry[]>;
|
|
63
|
+
/** Per product name (`_`: the site-wide versions), the version menu's links and limit. */
|
|
64
|
+
versionMenu?: Record<string, VersionMenuData>;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface ProductMenuEntry {
|
|
68
|
+
name: string;
|
|
69
|
+
label: string;
|
|
70
|
+
home: string;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface ProductMenu {
|
|
74
|
+
/** The page's product, or undefined on a page outside every product (the menu reads "Product"). */
|
|
75
|
+
current?: ProductMenuEntry;
|
|
76
|
+
entries: ProductMenuEntry[];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The product menu for a page (the same rules as dogsbay-ssg's): only with two
|
|
81
|
+
* or more products, in the page's locale (else the site's first), each entry
|
|
82
|
+
* going to that product's home — products do not share pages, so the current
|
|
83
|
+
* page is never looked up. A page outside every product has no current entry.
|
|
84
|
+
*/
|
|
85
|
+
export function productMenu(switcherMap: SwitcherMap, ms?: Pick<MultiSourceMeta, "namespace" | "locale">): ProductMenu | undefined {
|
|
86
|
+
const byLocale = switcherMap.products;
|
|
87
|
+
if (!byLocale) return undefined;
|
|
88
|
+
const entries = byLocale[ms?.locale ?? "_"] ?? Object.values(byLocale)[0];
|
|
89
|
+
if (!entries || entries.length < 2) return undefined;
|
|
90
|
+
return { current: entries.find((e) => e.name === ms?.namespace), entries };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** The current page's product version rows, when the site has a `products:` block. */
|
|
94
|
+
export function productVersionEntries(switcherMap: SwitcherMap, ms: MultiSourceMeta): AxisEntry[] | undefined {
|
|
95
|
+
if (!switcherMap.productVersions) return undefined;
|
|
96
|
+
return switcherMap.productVersions[`${ms.namespace ?? "_"}/${ms.locale ?? "_"}`] ?? [];
|
|
27
97
|
}
|
|
28
98
|
|
|
29
99
|
export interface MultiSourceMeta {
|
|
@@ -73,9 +143,32 @@ export interface BuildRowsInput {
|
|
|
73
143
|
multiSource: MultiSourceMeta;
|
|
74
144
|
}
|
|
75
145
|
|
|
146
|
+
/**
|
|
147
|
+
* Order two version ids. Compares numeric components left-to-right
|
|
148
|
+
* (`3.32` > `3.31`, `3.23-2` > `3.23-1`); a non-numeric alias (`latest`,
|
|
149
|
+
* `next`) sorts ABOVE any numbered version so descending order puts it
|
|
150
|
+
* first. When NEITHER id has any digits — a non-semver / codename scheme
|
|
151
|
+
* (`Boron`, `Argon`) — returns 0 so the sort is a no-op and the author's
|
|
152
|
+
* DECLARED order is preserved (JS sort is stable), rather than silently
|
|
153
|
+
* alphabetising and putting the newest release at the bottom.
|
|
154
|
+
*/
|
|
155
|
+
export function compareVersionIds(a: string, b: string): number {
|
|
156
|
+
const na = (a.match(/\d+/g) ?? []).map(Number);
|
|
157
|
+
const nb = (b.match(/\d+/g) ?? []).map(Number);
|
|
158
|
+
if (na.length === 0 && nb.length === 0) return 0; // codenames → keep declared order
|
|
159
|
+
if (na.length === 0) return 1; // alias > number
|
|
160
|
+
if (nb.length === 0) return -1;
|
|
161
|
+
const len = Math.max(na.length, nb.length);
|
|
162
|
+
for (let i = 0; i < len; i++) {
|
|
163
|
+
const d = (na[i] ?? 0) - (nb[i] ?? 0);
|
|
164
|
+
if (d !== 0) return d;
|
|
165
|
+
}
|
|
166
|
+
return a.localeCompare(b);
|
|
167
|
+
}
|
|
168
|
+
|
|
76
169
|
export function buildSwitcherRows(input: BuildRowsInput): SwitcherRow[] {
|
|
77
170
|
const { axis, switcherMap, multiSource } = input;
|
|
78
|
-
const
|
|
171
|
+
const allEntries = axis === "version" ? switcherMap.versions : switcherMap.locales;
|
|
79
172
|
const currentId =
|
|
80
173
|
axis === "version" ? multiSource.version : multiSource.locale;
|
|
81
174
|
const otherAxis: SwitcherAxis = axis === "version" ? "locale" : "version";
|
|
@@ -84,6 +177,65 @@ export function buildSwitcherRows(input: BuildRowsInput): SwitcherRow[] {
|
|
|
84
177
|
|
|
85
178
|
const variants = switcherMap.byLogicalKey[logicalKeyFor(multiSource)] ?? [];
|
|
86
179
|
|
|
180
|
+
// A products: block: the product's own rows, in config order (the shared model's switcher)
|
|
181
|
+
const productRows = axis === "version" ? productVersionEntries(switcherMap, multiSource) : undefined;
|
|
182
|
+
if (productRows) {
|
|
183
|
+
return productRows.map((entry) => {
|
|
184
|
+
const match = variants.find((v) => v.version === entry.id && (otherCurrent === undefined || v.locale === otherCurrent));
|
|
185
|
+
return { entry, url: match?.url ?? null, isCurrent: entry.id === currentId };
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// On a MULTI-PRODUCT (namespace-active) site the declared version list
|
|
190
|
+
// is the UNION across products, but a page's variants are same-namespace
|
|
191
|
+
// (the logical key is namespaced). Scope the version switcher to the
|
|
192
|
+
// versions this page's product actually has, so a Calico page never
|
|
193
|
+
// offers a Calico-Enterprise version. Single-product sites (no
|
|
194
|
+
// namespace) keep the full declared list, with fallbacks for pages
|
|
195
|
+
// missing in some version.
|
|
196
|
+
let entries = allEntries;
|
|
197
|
+
if (axis === "version" && multiSource.namespace !== undefined) {
|
|
198
|
+
const available = new Set(
|
|
199
|
+
variants.map((v) => v.version).filter((v): v is string => v !== undefined),
|
|
200
|
+
);
|
|
201
|
+
entries = allEntries.filter((e) => available.has(e.id) || e.id === currentId);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// The version dropdown always reads newest → oldest (3.32, 3.31, …),
|
|
205
|
+
// regardless of the declared order. Aliases (e.g. "latest") that don't
|
|
206
|
+
// parse as versions sort to the top, ahead of the numbers.
|
|
207
|
+
//
|
|
208
|
+
// `latest` alias + its target: a version marked `hidden` is the number
|
|
209
|
+
// the `latest` alias points at (Docusaurus's lastVersion). Show ONE row
|
|
210
|
+
// "3.32 (latest)" (linking to /latest/), derive its label from the hidden
|
|
211
|
+
// sibling, and drop hidden rows from the dropdown.
|
|
212
|
+
//
|
|
213
|
+
// The hidden number is DECLARED but NOT SERVED: `migrate-docusaurus
|
|
214
|
+
// --latest-alias` emits one source for the newest branch, at `latest`.
|
|
215
|
+
// That mirrors the source, where `versions: { "3.32": { path: "latest" } }`
|
|
216
|
+
// REPLACES the segment — docs.tigera.io has /calico/latest/ and no
|
|
217
|
+
// /calico/3.32/. Serving both invented a URL tree the source never had
|
|
218
|
+
// and duplicated every page of the newest release (338 on Calico) with
|
|
219
|
+
// no canonical. So the hidden entry is label-only metadata.
|
|
220
|
+
//
|
|
221
|
+
// `currentFoldedIntoLatest` still matters for a corpus that DOES serve
|
|
222
|
+
// the number (a hand-written config may), so a reader on `/3.32/…` has
|
|
223
|
+
// the merged `latest` row highlighted rather than no row at all. No
|
|
224
|
+
// `hidden` sibling → no merge.
|
|
225
|
+
let currentFoldedIntoLatest = false;
|
|
226
|
+
if (axis === "version") {
|
|
227
|
+
entries = [...entries].sort((a, b) => compareVersionIds(b.id, a.id));
|
|
228
|
+
const hiddenTarget = entries.find((e) => e.hidden && /\d/.test(e.id));
|
|
229
|
+
currentFoldedIntoLatest = hiddenTarget?.id === currentId;
|
|
230
|
+
entries = entries
|
|
231
|
+
.filter((e) => !e.hidden)
|
|
232
|
+
.map((e) =>
|
|
233
|
+
e.id === "latest" && hiddenTarget
|
|
234
|
+
? { ...e, label: `${hiddenTarget.label ?? hiddenTarget.id} (latest)` }
|
|
235
|
+
: e,
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
|
|
87
239
|
return entries.map((entry) => {
|
|
88
240
|
const match = variants.find((v) => {
|
|
89
241
|
// Match on this axis.
|
|
@@ -98,7 +250,7 @@ export function buildSwitcherRows(input: BuildRowsInput): SwitcherRow[] {
|
|
|
98
250
|
return {
|
|
99
251
|
entry,
|
|
100
252
|
url: match?.url ?? null,
|
|
101
|
-
isCurrent: entry.id === currentId,
|
|
253
|
+
isCurrent: entry.id === currentId || (entry.id === "latest" && currentFoldedIntoLatest),
|
|
102
254
|
};
|
|
103
255
|
});
|
|
104
256
|
}
|
|
@@ -117,22 +269,58 @@ export function shouldRenderSwitcher(
|
|
|
117
269
|
multiSource: MultiSourceMeta | undefined,
|
|
118
270
|
): boolean {
|
|
119
271
|
if (!multiSource) return false;
|
|
120
|
-
const entries = axis === "version" ? switcherMap.versions : switcherMap.locales;
|
|
121
|
-
|
|
272
|
+
const entries = (axis === "version" ? productVersionEntries(switcherMap, multiSource) : undefined) ?? (axis === "version" ? switcherMap.versions : switcherMap.locales);
|
|
273
|
+
// Two or more versions a reader can pick (a hidden one is the number an alias serves), as dogsbay-ssg decides it
|
|
274
|
+
if (entries.filter((e) => !e.hidden).length < 2) return false;
|
|
122
275
|
const id = axis === "version" ? multiSource.version : multiSource.locale;
|
|
123
276
|
return id !== undefined;
|
|
124
277
|
}
|
|
125
278
|
|
|
126
279
|
/**
|
|
127
280
|
* Fallback URL for a row whose target variant doesn't exist —
|
|
128
|
-
* lands on the axis's "landing page" (`<basePath>/<id
|
|
281
|
+
* lands on the axis's "landing page" (`<basePath>/<id>/`, or
|
|
282
|
+
* `<basePath>/` for the version served at the root).
|
|
129
283
|
* Lets the switcher always be clickable even when a page is
|
|
130
284
|
* untranslated for some axis combo.
|
|
131
285
|
*/
|
|
132
286
|
export function fallbackLandingUrl(
|
|
133
287
|
basePath: string,
|
|
134
288
|
axisEntryId: string,
|
|
289
|
+
atRoot = false,
|
|
135
290
|
): string {
|
|
136
291
|
const trimmed = basePath.replace(/\/$/, "");
|
|
137
|
-
return `${trimmed}/${axisEntryId}/`;
|
|
292
|
+
return atRoot ? `${trimmed}/` : `${trimmed}/${axisEntryId}/`;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* The page's version menu extras: its product's (with a `products:` block),
|
|
297
|
+
* else the site's. Only for a menu that is shown, so its links come with it.
|
|
298
|
+
*/
|
|
299
|
+
export function versionMenuFor(switcherMap: SwitcherMap, ms: Pick<MultiSourceMeta, "namespace">): VersionMenuData | undefined {
|
|
300
|
+
const menus = switcherMap.versionMenu;
|
|
301
|
+
if (!menus) return undefined;
|
|
302
|
+
return switcherMap.productVersions ? menus[ms.namespace ?? "_"] : menus._;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* The menu's rows under `limit`: whether it shows is decided before (two or
|
|
307
|
+
* more versions), then the first `limit` rows are kept, plus the page's own.
|
|
308
|
+
*/
|
|
309
|
+
export function limitRows(rows: SwitcherRow[], limit: number | undefined): SwitcherRow[] {
|
|
310
|
+
return limitVersionRows(rows, limit, (r) => r.isCurrent);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* "Version: <label>" for a page whose version has `badge: true`, else
|
|
315
|
+
* undefined. The label is the one the version menu shows.
|
|
316
|
+
*/
|
|
317
|
+
export function versionBadge(switcherMap: SwitcherMap | undefined, ms: MultiSourceMeta | undefined, wording = "Version: {label}"): string | undefined {
|
|
318
|
+
if (!switcherMap || !ms?.version) return undefined;
|
|
319
|
+
const entries = productVersionEntries(switcherMap, ms) ?? switcherMap.versions;
|
|
320
|
+
const entry = entries.find((e) => e.id === ms.version);
|
|
321
|
+
if (!entry?.badge) return undefined;
|
|
322
|
+
// The `latest` alias stands for the hidden number it serves (migrate-docusaurus --latest-alias)
|
|
323
|
+
const target = entry.id === "latest" ? [...entries].sort((a, b) => compareVersionIds(b.id, a.id)).find((e) => e.hidden && /\d/.test(e.id)) : undefined;
|
|
324
|
+
const shown = target ?? entry;
|
|
325
|
+
return wording.split("{label}").join(shown.label ?? shown.id);
|
|
138
326
|
}
|
package/src/tag-list-data.ts
CHANGED
|
@@ -33,6 +33,29 @@ export interface BuildChipsOptions {
|
|
|
33
33
|
* text. URL still uses the slug.
|
|
34
34
|
*/
|
|
35
35
|
labels?: Record<string, string>;
|
|
36
|
+
/**
|
|
37
|
+
* Color chips whose prefix has no `color` with a palette color
|
|
38
|
+
* hashed from the first segment (`autoTagColor`). Default false.
|
|
39
|
+
*/
|
|
40
|
+
autoColor?: boolean;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Palette order for automatic colors. */
|
|
44
|
+
const AUTO_PALETTE: readonly TagPaletteName[] = ["blue", "amber", "emerald", "violet", "rose", "slate"];
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* A stable palette color for a tag segment: FNV-1a hash of the
|
|
48
|
+
* lowercased segment, modulo the palette. The same segment gets the
|
|
49
|
+
* same color on every page and every build; with more distinct tags
|
|
50
|
+
* than colors, colors repeat.
|
|
51
|
+
*/
|
|
52
|
+
export function autoTagColor(segment: string): TagPaletteName {
|
|
53
|
+
let hash = 0x811c9dc5;
|
|
54
|
+
for (const ch of segment.toLowerCase()) {
|
|
55
|
+
hash ^= ch.codePointAt(0)!;
|
|
56
|
+
hash = Math.imul(hash, 0x01000193) >>> 0;
|
|
57
|
+
}
|
|
58
|
+
return AUTO_PALETTE[hash % AUTO_PALETTE.length];
|
|
36
59
|
}
|
|
37
60
|
|
|
38
61
|
export interface ChipModel {
|
|
@@ -164,7 +187,7 @@ export function buildChips(
|
|
|
164
187
|
options: BuildChipsOptions,
|
|
165
188
|
): ChipModel[] {
|
|
166
189
|
const safe = filterTags(tags);
|
|
167
|
-
const { indexPath, prefixes, labels } = options;
|
|
190
|
+
const { indexPath, prefixes, labels, autoColor = false } = options;
|
|
168
191
|
|
|
169
192
|
return safe.map((tag): ChipModel => {
|
|
170
193
|
const href = `${indexPath}/${tag}/`;
|
|
@@ -182,7 +205,7 @@ export function buildChips(
|
|
|
182
205
|
href,
|
|
183
206
|
prefixLabel: null,
|
|
184
207
|
leafLabel: overrideLeaf ?? tag,
|
|
185
|
-
color: null,
|
|
208
|
+
color: autoColor ? autoTagColor(firstSegment) : null,
|
|
186
209
|
};
|
|
187
210
|
}
|
|
188
211
|
|
|
@@ -196,7 +219,7 @@ export function buildChips(
|
|
|
196
219
|
href,
|
|
197
220
|
prefixLabel: prefixEntry.label ?? null,
|
|
198
221
|
leafLabel: leafLabel.length > 0 ? leafLabel : tag,
|
|
199
|
-
color: prefixEntry.color ?? null,
|
|
222
|
+
color: prefixEntry.color ?? (autoColor ? autoTagColor(firstSegment) : null),
|
|
200
223
|
};
|
|
201
224
|
});
|
|
202
225
|
}
|
package/src/toc-kind.css
CHANGED
|
@@ -11,40 +11,40 @@
|
|
|
11
11
|
line-height: 1.4;
|
|
12
12
|
}
|
|
13
13
|
.toc-kind-class {
|
|
14
|
-
background-color: oklch(0.
|
|
15
|
-
color: oklch(0.
|
|
14
|
+
background-color: oklch(0.44 0.18 265 / 0.15);
|
|
15
|
+
color: oklch(0.44 0.18 265);
|
|
16
16
|
}
|
|
17
17
|
.dark .toc-kind-class {
|
|
18
18
|
background-color: oklch(0.7 0.15 265 / 0.2);
|
|
19
19
|
color: oklch(0.7 0.15 265);
|
|
20
20
|
}
|
|
21
21
|
.toc-kind-meth {
|
|
22
|
-
background-color: oklch(0.
|
|
23
|
-
color: oklch(0.
|
|
22
|
+
background-color: oklch(0.44 0.15 155 / 0.15);
|
|
23
|
+
color: oklch(0.44 0.15 155);
|
|
24
24
|
}
|
|
25
25
|
.dark .toc-kind-meth {
|
|
26
26
|
background-color: oklch(0.7 0.15 155 / 0.2);
|
|
27
27
|
color: oklch(0.7 0.15 155);
|
|
28
28
|
}
|
|
29
29
|
.toc-kind-attr {
|
|
30
|
-
background-color: oklch(0.
|
|
31
|
-
color: oklch(0.
|
|
30
|
+
background-color: oklch(0.44 0.15 55 / 0.15);
|
|
31
|
+
color: oklch(0.44 0.15 55);
|
|
32
32
|
}
|
|
33
33
|
.dark .toc-kind-attr {
|
|
34
34
|
background-color: oklch(0.75 0.15 55 / 0.2);
|
|
35
35
|
color: oklch(0.75 0.15 55);
|
|
36
36
|
}
|
|
37
37
|
.toc-kind-func {
|
|
38
|
-
background-color: oklch(0.
|
|
39
|
-
color: oklch(0.
|
|
38
|
+
background-color: oklch(0.44 0.18 330 / 0.15);
|
|
39
|
+
color: oklch(0.44 0.18 330);
|
|
40
40
|
}
|
|
41
41
|
.dark .toc-kind-func {
|
|
42
42
|
background-color: oklch(0.7 0.15 330 / 0.2);
|
|
43
43
|
color: oklch(0.7 0.15 330);
|
|
44
44
|
}
|
|
45
45
|
.toc-kind-module {
|
|
46
|
-
background-color: oklch(0.
|
|
47
|
-
color: oklch(0.
|
|
46
|
+
background-color: oklch(0.44 0.12 200 / 0.15);
|
|
47
|
+
color: oklch(0.44 0.12 200);
|
|
48
48
|
}
|
|
49
49
|
.dark .toc-kind-module {
|
|
50
50
|
background-color: oklch(0.65 0.12 200 / 0.2);
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Decide which TOC container(s) and right-rail region `DocsLayout` renders,
|
|
3
|
+
* given the `toc` placement mode and per-page facts. Factored out of
|
|
4
|
+
* `DocsLayout.astro` so the branching logic is unit-testable (the .astro
|
|
5
|
+
* file just consumes the result) — same pattern as `nav-filter.ts` etc.
|
|
6
|
+
*
|
|
7
|
+
* See plans/ask-branch1-placement-toc.md.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export type TocMode = "top" | "popover" | "rail" | "off";
|
|
11
|
+
|
|
12
|
+
export interface TocPlacement {
|
|
13
|
+
/** Classic right-hand TOC sidebar (the pre-`toc`-option layout). */
|
|
14
|
+
railToc: boolean;
|
|
15
|
+
/** Expandable "On this page" disclosure at the top of the article. */
|
|
16
|
+
topToc: boolean;
|
|
17
|
+
/** "On this page" dropdown in the header. */
|
|
18
|
+
popoverToc: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* The right-rail plugin region (host for the `right-rail` named slot, e.g.
|
|
21
|
+
* an Ask AI panel). Present whenever the rail isn't the classic TOC's, and
|
|
22
|
+
* deliberately NOT gated on headings — so a plugin can dock on heading-less
|
|
23
|
+
* pages. Never shown on wide/API pages (no rail there).
|
|
24
|
+
*/
|
|
25
|
+
regionRail: boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Whether a page has enough displayable headings to warrant a TOC. The TOC
|
|
30
|
+
* (`DocsToc`) only lists depth `minDepth`–`maxDepth` (2–3 by default), so the
|
|
31
|
+
* page H1 never counts; and a TOC of a single entry isn't useful, so require
|
|
32
|
+
* `min` (2) by default. Pages like a landing/welcome page — H1 + prose, no
|
|
33
|
+
* sub-sections — therefore get no "On this page".
|
|
34
|
+
*/
|
|
35
|
+
export function hasDisplayableToc(
|
|
36
|
+
headings: { depth: number }[],
|
|
37
|
+
opts: { minDepth?: number; maxDepth?: number; min?: number } = {},
|
|
38
|
+
): boolean {
|
|
39
|
+
const { minDepth = 2, maxDepth = 3, min = 2 } = opts;
|
|
40
|
+
let count = 0;
|
|
41
|
+
for (const h of headings) {
|
|
42
|
+
if (h.depth >= minDepth && h.depth <= maxDepth) count++;
|
|
43
|
+
if (count >= min) return true;
|
|
44
|
+
}
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface TocPlacementInput {
|
|
49
|
+
/** Page has at least one heading to list. */
|
|
50
|
+
hasHeadings: boolean;
|
|
51
|
+
/** Wide/API layout — composes its own columns, so no right rail. */
|
|
52
|
+
wideLayout: boolean;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Resolve the placement flags. A TOC container only renders when there are
|
|
57
|
+
* headings to show; the region rail is independent of headings. `rail` and
|
|
58
|
+
* the region rail are mutually exclusive (they compete for the same column),
|
|
59
|
+
* and both are suppressed on wide pages.
|
|
60
|
+
*/
|
|
61
|
+
export function resolveTocPlacement(
|
|
62
|
+
toc: TocMode,
|
|
63
|
+
{ hasHeadings, wideLayout }: TocPlacementInput,
|
|
64
|
+
): TocPlacement {
|
|
65
|
+
return {
|
|
66
|
+
railToc: toc === "rail" && hasHeadings && !wideLayout,
|
|
67
|
+
topToc: toc === "top" && hasHeadings,
|
|
68
|
+
popoverToc: toc === "popover" && hasHeadings,
|
|
69
|
+
regionRail: toc !== "rail" && !wideLayout,
|
|
70
|
+
};
|
|
71
|
+
}
|
package/src/version-redirect.ts
CHANGED
|
@@ -31,6 +31,15 @@ export interface AxisRedirectConfig {
|
|
|
31
31
|
defaultVersion?: string;
|
|
32
32
|
/** Full set of declared version ids. Empty/undefined → axis inactive. */
|
|
33
33
|
knownVersions?: string[];
|
|
34
|
+
/**
|
|
35
|
+
* The version served at the site root (`versions[].path: ""`, the
|
|
36
|
+
* default version). A URL with no version segment is one of its pages,
|
|
37
|
+
* so the version is never injected; a URL starting with another known
|
|
38
|
+
* version's segment is that version's (the longest matching prefix
|
|
39
|
+
* wins). Only the locale redirect remains. format-astro emits no version
|
|
40
|
+
* config at all in that case; this keeps the helper right when given one.
|
|
41
|
+
*/
|
|
42
|
+
rootVersion?: string;
|
|
34
43
|
/** Default locale id; required when locale axis is active. */
|
|
35
44
|
defaultLocale?: string;
|
|
36
45
|
/** Full set of declared locale ids. Empty/undefined → axis inactive. */
|
|
@@ -126,7 +135,8 @@ export function shouldRedirectToDefaultVersion(
|
|
|
126
135
|
if (
|
|
127
136
|
versionAxisActive &&
|
|
128
137
|
cursor < segments.length &&
|
|
129
|
-
knownVersions.has(segments[cursor])
|
|
138
|
+
knownVersions.has(segments[cursor]) &&
|
|
139
|
+
segments[cursor] !== config.rootVersion
|
|
130
140
|
) {
|
|
131
141
|
presentVersion = segments[cursor];
|
|
132
142
|
cursor += 1;
|
|
@@ -134,13 +144,16 @@ export function shouldRedirectToDefaultVersion(
|
|
|
134
144
|
|
|
135
145
|
// What's the rest of the path beyond the recognised axis prefixes?
|
|
136
146
|
const tailSegments = segments.slice(cursor);
|
|
147
|
+
// With a root version, a URL without a version segment is already complete.
|
|
148
|
+
const versionMissing =
|
|
149
|
+
versionAxisActive && presentVersion === undefined && !config.rootVersion;
|
|
137
150
|
if (tailSegments.length === 0) {
|
|
138
151
|
// The path is JUST a known axis prefix (e.g. /docs/en/ or
|
|
139
152
|
// /docs/v1/). Treat as the axis's landing page; no redirect
|
|
140
153
|
// needed unless other axes are MISSING from the URL too.
|
|
141
154
|
if (
|
|
142
155
|
(localeAxisActive && presentLocale === undefined) ||
|
|
143
|
-
|
|
156
|
+
versionMissing
|
|
144
157
|
) {
|
|
145
158
|
// Some axis still missing — fall through to redirect.
|
|
146
159
|
} else {
|
|
@@ -150,7 +163,7 @@ export function shouldRedirectToDefaultVersion(
|
|
|
150
163
|
|
|
151
164
|
// If all required-active axes are present, no redirect needed.
|
|
152
165
|
const localeNeeded = localeAxisActive && presentLocale === undefined;
|
|
153
|
-
const versionNeeded =
|
|
166
|
+
const versionNeeded = versionMissing;
|
|
154
167
|
if (!localeNeeded && !versionNeeded) return null;
|
|
155
168
|
|
|
156
169
|
// Compose the redirect target with missing segments injected
|
|
@@ -159,7 +172,7 @@ export function shouldRedirectToDefaultVersion(
|
|
|
159
172
|
if (localeAxisActive) {
|
|
160
173
|
out.push(presentLocale ?? config.defaultLocale!);
|
|
161
174
|
}
|
|
162
|
-
if (versionAxisActive) {
|
|
175
|
+
if (versionAxisActive && (presentVersion !== undefined || !config.rootVersion)) {
|
|
163
176
|
out.push(presentVersion ?? config.defaultVersion!);
|
|
164
177
|
}
|
|
165
178
|
out.push(...tailSegments);
|