@dogsbay/docs-layout 0.2.0-beta.151 → 0.2.0-beta.153

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.
@@ -38,3 +38,26 @@ export const ANNOUNCEMENT_TONE_CLASS: Record<string, string> = {
38
38
  warning: "border-warning/40 bg-warning/20",
39
39
  caution: "border-danger/40 bg-danger/15",
40
40
  };
41
+
42
+ /** The width the header's links show in the bar from (in the sidebar's list below it); `never`: the list only. */
43
+ export type LinksFrom = "lg" | "xl" | "2xl" | "never";
44
+
45
+ /**
46
+ * Where the header's links fit: the first width at which their estimated width fits beside what
47
+ * else the header holds, or `never`. Estimated from the labels (about 6.2 px a character at the
48
+ * header's text size, 28 px padding a link, 14 px a dropdown's chevron) against the header beside
49
+ * the 256 px sidebar, less the trigger and padding (80 px) and the menus: about 520 px for a
50
+ * crowded header (product, version, page actions, icons), 210 px otherwise (search with its
51
+ * text, the icons). A crowded header starts at xl. Measured on Tigera's (47 characters, 4 links:
52
+ * xl) and the products fixture; a long navbar stays in the sidebar rather than push the page wide.
53
+ */
54
+ export function headerLinksFrom(links: Array<SiteChromeLink | SiteChromeMenu>, crowded: boolean): LinksFrom {
55
+ const est = links.reduce((n, l) => n + l.label.length * 6.2 + 28 + (isMenu(l) ? 14 : 0), 0);
56
+ const menus = crowded ? 520 : 210;
57
+ const widths: Array<[LinksFrom, number]> = [["lg", 1024], ["xl", 1280], ["2xl", 1536]];
58
+ for (const [from, width] of widths) {
59
+ if (crowded && from === "lg") continue;
60
+ if (est <= width - 256 - 80 - menus) return from;
61
+ }
62
+ return "never";
63
+ }
package/src/switcher.ts CHANGED
@@ -62,8 +62,28 @@ export interface SwitcherMap {
62
62
  products?: Record<string, ProductMenuEntry[]>;
63
63
  /** Per product name (`_`: the site-wide versions), the version menu's links and limit. */
64
64
  versionMenu?: Record<string, VersionMenuData>;
65
+ /** Each unit's version banner, keyed `<product|_>/<locale|_>/<version>` (format-astro `versionBanners`). */
66
+ banners?: Record<string, VersionBannerData>;
65
67
  }
66
68
 
69
+ /** A unit's version banner: its own text or a kind worded here, and the default version it points to. */
70
+ export type VersionBannerData = ({ kind: "custom"; text: string } | { kind: "eol" | "prerelease" | "old" }) & { latest?: { id: string; label: string; home: string } };
71
+
72
+ /** The banner's words, as dogsbay-ssg's English strings. */
73
+ export interface VersionBannerWording {
74
+ old: string;
75
+ eol: string;
76
+ prerelease: string;
77
+ goLatest: string;
78
+ }
79
+
80
+ export const VERSION_BANNER_WORDING: VersionBannerWording = {
81
+ old: "You are viewing documentation for an older version.",
82
+ eol: "This version is no longer maintained.",
83
+ prerelease: "This version is not released yet.",
84
+ goLatest: "Go to the latest version",
85
+ };
86
+
67
87
  export interface ProductMenuEntry {
68
88
  name: string;
69
89
  label: string;
@@ -324,3 +344,37 @@ export function versionBadge(switcherMap: SwitcherMap | undefined, ms: MultiSour
324
344
  const shown = target ?? entry;
325
345
  return wording.split("{label}").join(shown.label ?? shown.id);
326
346
  }
347
+
348
+ /**
349
+ * The version banner over a page (dogsbay-ssg's `versionBanner`): the text, and a link to the same
350
+ * page in the default version, or to that version's home when the page is not there. Undefined on
351
+ * the default version, a hidden one, and a page outside every version.
352
+ */
353
+ export function versionBanner(
354
+ switcherMap: SwitcherMap | undefined,
355
+ ms: MultiSourceMeta | undefined,
356
+ wording: Partial<VersionBannerWording> = {},
357
+ ): { text: string; link?: { href: string; label: string } } | undefined {
358
+ if (!switcherMap?.banners || !ms?.version) return undefined;
359
+ const w = { ...VERSION_BANNER_WORDING, ...wording };
360
+ // The page's own unit, exactly: a fallback would hand it another product's or language's banner
361
+ const b = switcherMap.banners[`${ms.namespace ?? "_"}/${ms.locale ?? "_"}/${ms.version}`];
362
+ if (!b) return undefined;
363
+ const text = b.kind === "custom" ? b.text : b.kind === "eol" ? w.eol : b.kind === "prerelease" ? w.prerelease : w.old;
364
+ if (!b.latest) return { text };
365
+ const variants = switcherMap.byLogicalKey[`${ms.namespace ?? "docs"}/${ms.originalSlug}`] ?? [];
366
+ const same = variants.find((v) => v.version === b.latest!.id && (v.locale ?? undefined) === (ms.locale ?? undefined));
367
+ return { text, link: { href: same ? servedHref(same.url) : b.latest.home, label: w.goLatest } };
368
+ }
369
+
370
+ /**
371
+ * A variant's URL as the site serves the page (format-astro `pageHref`): a page is a folder, so
372
+ * `…/index` is its folder and the slash is kept; otherwise each link costs a redirect.
373
+ */
374
+ export function servedHref(url: string): string {
375
+ const at = url.search(/[?#]/);
376
+ const [path, rest] = at < 0 ? [url, ""] : [url.slice(0, at), url.slice(at)];
377
+ let p = path.replace(/\/+$/, "");
378
+ if (p === "index" || p.endsWith("/index")) p = p.slice(0, -"index".length).replace(/\/+$/, "");
379
+ return `${p}/${rest}`;
380
+ }
@@ -132,39 +132,11 @@ export function resolveTagKeywords(
132
132
  }
133
133
 
134
134
  /**
135
- * Resolve the display label for a term identified by its fullPath
136
- * array (e.g. `["concept"]` or `["concept", "a11y"]`).
137
- *
138
- * Used by `<TaxonomyIndex>` term-cloud chips, `<TaxonomyTerm>`
139
- * page headings and breadcrumbs, and sub-tag pills — anywhere a
140
- * structural slug needs to render as a human label.
141
- *
142
- * Resolution order:
143
- * 1. `labels[fullSlug]` (joined fullPath) — explicit override
144
- * wins, regardless of depth.
145
- * 2. For top-level terms (`fullPath.length === 1`):
146
- * `prefixes[segment].label` — lets a top-level segment
147
- * (`concept`, `difficulty`) render as "Concept", "Difficulty".
148
- * 3. The leaf segment of the path — the rendered fallback for
149
- * deeper terms with no override.
150
- *
151
- * URLs continue to use the slug-form `fullPath`. Display only.
135
+ * Display label of a term path (`labels` override, then a top-level
136
+ * `prefixes` label, then the leaf). One implementation, shared with the
137
+ * search facet column: `@dogsbay/search/facets`.
152
138
  */
153
- export function resolveTermLabel(
154
- fullPath: readonly string[],
155
- prefixes?: Record<string, PrefixDisplay>,
156
- labels?: Record<string, string>,
157
- ): string {
158
- if (fullPath.length === 0) return "";
159
- const slug = fullPath.join("/");
160
- const override = labels?.[slug];
161
- if (override && override.length > 0) return override;
162
- if (fullPath.length === 1) {
163
- const prefixLabel = prefixes?.[fullPath[0]]?.label;
164
- if (prefixLabel && prefixLabel.length > 0) return prefixLabel;
165
- }
166
- return fullPath[fullPath.length - 1];
167
- }
139
+ export { resolveTermLabel } from "@dogsbay/search/facets";
168
140
 
169
141
  /**
170
142
  * Map raw tag slugs to render-ready chip models, applying prefix