@dogsbay/docs-layout 0.2.0-beta.18 → 0.2.0-beta.180

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.
@@ -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
+ }
@@ -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
- (versionAxisActive && presentVersion === undefined)
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 = versionAxisActive && presentVersion === undefined;
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);