@dogsbay/docs-layout 0.2.0-beta.170 → 0.2.0-beta.172

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dogsbay/docs-layout",
3
- "version": "0.2.0-beta.170",
3
+ "version": "0.2.0-beta.172",
4
4
  "description": "Standard documentation layout components for Dogsbay",
5
5
  "type": "module",
6
6
  "exports": {
@@ -32,14 +32,16 @@
32
32
  "./AnnouncementBar.astro": "./src/AnnouncementBar.astro",
33
33
  "./HeaderLinks.astro": "./src/HeaderLinks.astro",
34
34
  "./SocialLinks.astro": "./src/SocialLinks.astro",
35
- "./site-chrome": "./src/site-chrome.ts"
35
+ "./site-chrome": "./src/site-chrome.ts",
36
+ "./section-switcher": "./src/section-switcher.ts",
37
+ "./section-switcher.css": "./src/section-switcher.css"
36
38
  },
37
39
  "dependencies": {
38
- "@dogsbay/elements": "0.2.0-beta.170",
39
- "@dogsbay/primitives": "0.2.0-beta.170",
40
- "@dogsbay/search": "0.2.0-beta.170",
41
- "@dogsbay/site-files": "0.2.0-beta.170",
42
- "@dogsbay/ui": "0.2.0-beta.170"
40
+ "@dogsbay/elements": "0.2.0-beta.172",
41
+ "@dogsbay/primitives": "0.2.0-beta.172",
42
+ "@dogsbay/search": "0.2.0-beta.172",
43
+ "@dogsbay/site-files": "0.2.0-beta.172",
44
+ "@dogsbay/ui": "0.2.0-beta.172"
43
45
  },
44
46
  "devDependencies": {
45
47
  "happy-dom": "^20.10.6",
@@ -35,6 +35,7 @@ import DocsNavClient from "./DocsNavClient.astro";
35
35
  import Separator from "@dogsbay/ui/separator/Separator.astro";
36
36
  import ThemeToggle from "@dogsbay/ui/theme-toggle/ThemeToggle.astro";
37
37
  import DocsToc from "./DocsToc.astro";
38
+ import "./section-switcher.css";
38
39
  import { resolveTocPlacement, hasDisplayableToc, type TocMode } from "./toc-placement.js";
39
40
  import { linkIconAttrs } from "./link-icons.js";
40
41
  import DocsFooter from "./DocsFooter.astro";
@@ -56,7 +57,8 @@ import { filterNavByAxis, rootVersionFilter } from "./nav-filter.js";
56
57
  import { versionBadge, versionBanner, type VersionBannerWording } from "./switcher.js";
57
58
  import type { LlmProviderName } from "./llm-actions.js";
58
59
  import { jsonLdTypeFor, normalizeCustomJsonLd, buildArticleJsonLd, buildWebSiteJsonLd } from "./json-ld.js";
59
- import { pageFacets, type PageFacets } from "@dogsbay/site-files";
60
+ import { pageFacets, scopeNav, sectionMenu, type PageFacets } from "@dogsbay/site-files";
61
+ import { sectionSwitcherHtml } from "./section-switcher.js";
60
62
  import { resolveTagKeywords } from "./tag-list-data.js";
61
63
 
62
64
  interface NavItem {
@@ -424,6 +426,13 @@ interface Props {
424
426
  * See plans/client-rendered-nav.md.
425
427
  */
426
428
  navMode?: "client" | "ssr-full";
429
+ /**
430
+ * `"section"`: the sidebar holds only the page's section (the top-level
431
+ * nav entry that holds it), under a section menu and a link home; the home
432
+ * page and pages in no section list the sections. Default `"site"`.
433
+ * Rules: `scopeNav` in @dogsbay/site-files. See plans/nav-scope.md.
434
+ */
435
+ navScope?: "site" | "section";
427
436
  /**
428
437
  * Per-page LLM action UI. When set and `enabled !== false`, renders
429
438
  * the PageActions cluster (Copy markdown + Open in Claude/ChatGPT/
@@ -589,6 +598,7 @@ const {
589
598
  switcherMap,
590
599
  basePath,
591
600
  navMode = "client",
601
+ navScope = "site",
592
602
  wideLayout = false,
593
603
  chrome = "docs",
594
604
  author,
@@ -625,6 +635,28 @@ const badgeText = versionBadge(switcherMap as never, multiSource as never, versi
625
635
  const bannerData = versionBanner(switcherMap as never, multiSource as never, versionBannerWording);
626
636
  const navVersion = multiSource?.versionPath ? multiSource.versionPath : multiSource?.version;
627
637
 
638
+ // Section scope (build.navScope: section). In ssr-full the section and its
639
+ // menu are worked out here; in client mode docs-nav-client.ts does the same
640
+ // with the same functions. The home page lists the sections.
641
+ const navHome = basePath || "/";
642
+ const sectionScoped = navScope === "section";
643
+ const ssrSection = sectionScoped && navMode !== "client" && nav
644
+ ? (() => {
645
+ const axisNav = filterNavByAxis(nav, {
646
+ basePath: basePath ?? "/docs",
647
+ namespace: multiSource?.namespace,
648
+ version: navVersion,
649
+ locale: multiSource?.locale,
650
+ ...navRootVersion,
651
+ });
652
+ const opts = { home: navHome };
653
+ return {
654
+ items: scopeNav(axisNav, currentPath, "section", opts).items,
655
+ switcher: sectionSwitcherHtml({ ...sectionMenu(axisNav, currentPath, opts), homeHref: navHome }),
656
+ };
657
+ })()
658
+ : undefined;
659
+
628
660
  // Link icons — classified in CSS by href shape. `data-link-icons`
629
661
  // lists which kinds are active; the glyphs ride in CSS custom props
630
662
  // (single-quoted so they're valid `content:` string values).
@@ -1190,9 +1222,19 @@ const siteIcon = '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16"
1190
1222
  locale={multiSource?.locale}
1191
1223
  rootVersion={navRootVersion.rootVersion}
1192
1224
  versionSegments={navRootVersion.versionSegments}
1225
+ navScope={navScope}
1226
+ home={navHome}
1193
1227
  />
1194
1228
  </SidebarGroupContent>
1195
1229
  </SidebarGroup>
1230
+ ) : ssrSection ? (
1231
+ // One section's tree under its menu; group labels do not apply.
1232
+ <SidebarGroup>
1233
+ <SidebarGroupContent>
1234
+ <Fragment set:html={ssrSection.switcher} />
1235
+ <SidebarNavTree items={ssrSection.items} currentPath={currentPath} />
1236
+ </SidebarGroupContent>
1237
+ </SidebarGroup>
1196
1238
  ) : navGroups ? (
1197
1239
  navGroups.map(group => (
1198
1240
  <SidebarGroup>
@@ -47,9 +47,13 @@ interface Props {
47
47
  rootVersion?: string;
48
48
  /** The other versions' URL segments (left out of the root version's nav). */
49
49
  versionSegments?: string[];
50
+ /** `"section"`: only the page's section, under the section menu (plans/nav-scope.md). */
51
+ navScope?: "site" | "section";
52
+ /** The home page, which lists the sections. */
53
+ home?: string;
50
54
  }
51
55
 
52
- const { currentPath, basePath = "", namespace, version, locale, rootVersion, versionSegments } = Astro.props;
56
+ const { currentPath, basePath = "", namespace, version, locale, rootVersion, versionSegments, navScope, home } = Astro.props;
53
57
  const navUrl = `${basePath}/_dogsbay/nav.json`;
54
58
  ---
55
59
 
@@ -71,6 +75,8 @@ const navUrl = `${basePath}/_dogsbay/nav.json`;
71
75
  data-locale={locale ?? ""}
72
76
  data-root-version={rootVersion ?? ""}
73
77
  data-version-segments={(versionSegments ?? []).join(" ")}
78
+ data-nav-scope={navScope === "section" ? "section" : undefined}
79
+ data-home={navScope === "section" ? (home ?? (basePath || "/")) : undefined}
74
80
  aria-busy="true"
75
81
  aria-label="Documentation navigation"
76
82
  >
@@ -23,6 +23,8 @@
23
23
  * See plans/client-rendered-nav.md.
24
24
  */
25
25
  import { filterNavByAxis } from "./nav-filter.js";
26
+ import { scopeNav, sectionMenu } from "@dogsbay/site-files";
27
+ import { sectionSwitcherHtml } from "./section-switcher.js";
26
28
 
27
29
  interface NavMark {
28
30
  kind: "added" | "changed" | "removed" | "moved";
@@ -295,6 +297,17 @@ export function renderItem(item: NavItem, current: string, level: number): HTMLL
295
297
  return li;
296
298
  }
297
299
 
300
+ /**
301
+ * Section scope (`data-nav-scope="section"`): the page's section only, under
302
+ * the section menu — the same functions DocsLayout's server render uses.
303
+ */
304
+ export function renderScoped(nav: NavItem[], current: string, home: string, root: HTMLElement): void {
305
+ const opts = { home };
306
+ renderTree(scopeNav(nav, current, "section", opts).items, current, root);
307
+ const menu = sectionSwitcherHtml({ ...sectionMenu(nav, current, opts), homeHref: home });
308
+ if (menu) root.insertAdjacentHTML("afterbegin", menu);
309
+ }
310
+
298
311
  export function renderTree(items: NavItem[], current: string, root: HTMLElement): void {
299
312
  const ul = document.createElement("ul");
300
313
  ul.className =
@@ -397,7 +410,8 @@ export async function hydrateDocsNav(): Promise<void> {
397
410
  locale: locale || undefined,
398
411
  ...(rootVersion ? { rootVersion, versionSegments } : {}),
399
412
  });
400
- renderTree(filtered, current, root);
413
+ if (root.dataset.navScope === "section") renderScoped(filtered, current, root.dataset.home || basePath || "/", root);
414
+ else renderTree(filtered, current, root);
401
415
  } catch (err) {
402
416
  console.error("docs-nav: hydration failed", err);
403
417
  root.setAttribute("aria-busy", "false");
@@ -413,7 +427,9 @@ export async function hydrateDocsNav(): Promise<void> {
413
427
  document.addEventListener("astro:page-load", () => {
414
428
  const root = document.getElementById("docs-nav-root");
415
429
  if (!root) return;
416
- // If tree is already rendered (no aria-busy), just re-highlight.
430
+ // If tree is already rendered (no aria-busy), just re-highlight. (The root
431
+ // is not persisted across transitions, so a new page always hydrates, from
432
+ // the cached nav: a section-scoped sidebar is rebuilt for its own section.)
417
433
  if (root.getAttribute("aria-busy") === null) {
418
434
  const current = normalize(root.dataset.currentPath || location.pathname);
419
435
  rehighlight(root, current);
package/src/pagination.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { scopeNav, type NavScope } from "@dogsbay/site-files";
2
+
1
3
  interface NavItem {
2
4
  label: string;
3
5
  href?: string;
@@ -16,10 +18,12 @@ interface PaginationResult {
16
18
 
17
19
  /**
18
20
  * Flatten a nav tree into an ordered list of pages.
19
- * Find prev/next for the given path.
21
+ * Find prev/next for the given path. With `scope: "section"` they walk the
22
+ * page's section only (`scopeNav`, shared with dogsbay-ssg), as its sidebar;
23
+ * `home` is the home page, which lists the sections.
20
24
  */
21
- export function getPagination(currentPath: string, nav: NavItem[]): PaginationResult {
22
- const allPages = flattenNav(nav);
25
+ export function getPagination(currentPath: string, nav: NavItem[], scope?: NavScope, home?: string): PaginationResult {
26
+ const allPages = flattenNav(scopeNav(nav, currentPath, scope, home === undefined ? {} : { home }).items);
23
27
  const normalized = currentPath.replace(/\/$/, "") || "/";
24
28
  const index = allPages.findIndex(p => p.href.replace(/\/$/, "") === normalized);
25
29
 
@@ -0,0 +1,20 @@
1
+ /*
2
+ * The section menu's entries (section-switcher.ts). One short class per
3
+ * entry instead of a Tailwind list repeated on each: Cloudflare's menu has
4
+ * 115 entries, and the repeated classes were 17.7 KB of every page.
5
+ * Plain CSS over the theme tokens, so dogsbay-ssg copies this file as it is.
6
+ */
7
+ .db-section-link {
8
+ display: flex;
9
+ border-radius: calc(var(--radius) - 4px);
10
+ padding: 0.375rem 0.5rem;
11
+ color: var(--foreground);
12
+ text-decoration: none;
13
+ }
14
+ .db-section-link:hover {
15
+ background-color: var(--accent);
16
+ }
17
+ .db-section-link[aria-current="true"] {
18
+ background-color: var(--accent);
19
+ font-weight: 500;
20
+ }
@@ -0,0 +1,70 @@
1
+ /**
2
+ * The section menu of a sidebar scoped to one section (`build.navScope:
3
+ * section`): a link home, where the sidebar lists every section, and a
4
+ * `<details>` list of the sections like ProductSwitcher's, which works
5
+ * without JS. The markup is built HERE ONLY, as a string, so the server
6
+ * render (DocsLayout), the client-built sidebar (docs-nav-client.ts) and
7
+ * dogsbay-ssg (which copies this file, as it copies site-chrome.ts) cannot
8
+ * drift. No imports, so a copy runs on its own.
9
+ *
10
+ * The data comes from `sectionMenu` in @dogsbay/site-files. The entries are
11
+ * styled by section-switcher.css (a host includes it once), not per entry.
12
+ */
13
+
14
+ export interface SectionSwitcherEntry {
15
+ label: string;
16
+ href: string;
17
+ /** The section's position (`sectionMenu` gives it): the current one is matched by it when set. */
18
+ index?: number;
19
+ }
20
+
21
+ export interface SectionSwitcherOptions {
22
+ entries: readonly SectionSwitcherEntry[];
23
+ /** The page's section; none on a page outside every section. */
24
+ current?: SectionSwitcherEntry;
25
+ /** The site's home page: where the sidebar lists every section. */
26
+ homeHref: string;
27
+ /** Default "All sections". */
28
+ homeLabel?: string;
29
+ /** Read before the section's name, and shown when there is none. Default "Section". */
30
+ placeholder?: string;
31
+ }
32
+
33
+ const ESC: Record<string, string> = { "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" };
34
+ function esc(s: string): string {
35
+ return s.replace(/[&<>"']/g, (c) => ESC[c]!);
36
+ }
37
+
38
+ const CHEVRON =
39
+ '<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="shrink-0 transition-transform group-open/section:rotate-180" aria-hidden="true"><polyline points="6 9 12 15 18 9"/></svg>';
40
+ const BACK =
41
+ '<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><polyline points="15 18 9 12 15 6"/></svg>';
42
+
43
+ /** The menu's HTML; empty when there are no sections to list. */
44
+ export function sectionSwitcherHtml(o: SectionSwitcherOptions): string {
45
+ if (!o.entries.length) return "";
46
+ const placeholder = o.placeholder ?? "Section";
47
+ const home = o.homeLabel ?? "All sections";
48
+ const items = o.entries
49
+ .map((e) => {
50
+ const cur =
51
+ o.current !== undefined &&
52
+ (e.index !== undefined && o.current.index !== undefined ? e.index === o.current.index : e.href === o.current.href);
53
+ // styled by section-switcher.css: one short class, not a class list per entry
54
+ return `<li><a href="${esc(e.href)}"${cur ? ' aria-current="true"' : ""} class="db-section-link">${esc(e.label)}</a></li>`;
55
+ })
56
+ .join("");
57
+ const name = o.current
58
+ ? `<span class="sr-only">${esc(placeholder)}: </span><span class="min-w-0 flex-1 truncate text-left font-medium">${esc(o.current.label)}</span>`
59
+ : `<span class="min-w-0 flex-1 truncate text-left text-muted-foreground">${esc(placeholder)}</span>`;
60
+ return (
61
+ `<div class="db-section-switcher flex flex-col gap-1 px-2 pb-2 group-data-[collapsible=icon]:hidden" data-section-switcher>` +
62
+ (o.current
63
+ ? `<a href="${esc(o.homeHref)}" class="flex items-center gap-1 px-1 text-xs text-sidebar-foreground/70 no-underline hover:text-sidebar-foreground" data-section-home>${BACK}${esc(home)}</a>`
64
+ : "") +
65
+ `<details class="group/section relative">` +
66
+ `<summary class="flex w-full cursor-pointer list-none items-center gap-1 rounded-md border border-border px-3 py-1.5 text-sm hover:bg-sidebar-accent [&::-webkit-details-marker]:hidden">${name}${CHEVRON}</summary>` +
67
+ `<ul class="absolute inset-x-0 z-50 mt-1 max-h-[60vh] overflow-y-auto rounded-md border border-border bg-popover p-1 text-sm shadow-md">${items}</ul>` +
68
+ `</details></div>`
69
+ );
70
+ }