@tenphi/starlight 0.6.0 → 0.6.1

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.
@@ -1,102 +0,0 @@
1
- ---
2
- import type { DocsRoute, NavigationItem } from "@tenphi/docs";
3
- import { normalizeNavigationPath } from "../navigation.js";
4
- import { NavigationTreeRoot } from "./LayoutComponents.js";
5
-
6
- interface Props {
7
- items: NavigationItem[];
8
- routes: DocsRoute[];
9
- current: string;
10
- base: string;
11
- nested?: boolean;
12
- }
13
-
14
- const { items, routes, current, base, nested = false } = Astro.props;
15
- const routeTitle = new Map(routes.map((route) => [route.route, route.title]));
16
- const withBase = (route: string) =>
17
- route.startsWith("/")
18
- ? `${base === "/" ? "" : `/${base.replace(/^\/+|\/+$/g, "")}`}${route}` ||
19
- "/"
20
- : route;
21
- const labelFor = (route: string) =>
22
- routeTitle.get(normalizeNavigationPath(route)) ??
23
- normalizeNavigationPath(route).split("/").filter(Boolean).at(-1) ??
24
- "Home";
25
- const isCurrent = (route: string) =>
26
- route.startsWith("/") && normalizeNavigationPath(route) === current;
27
- const generatedItems = (directory: string): NavigationItem[] => {
28
- const root = normalizeNavigationPath(directory);
29
- return routes
30
- .filter(
31
- (route) =>
32
- root === "/" ||
33
- route.route === root ||
34
- route.route.startsWith(`${root}/`),
35
- )
36
- .map((route) => route.route);
37
- };
38
- ---
39
-
40
- <NavigationTreeRoot
41
- className={nested ? undefined : "td-shell__nav-root"}
42
- data-tasty-anatomy="NavigationTree"
43
- >
44
- {
45
- items.map((item) => {
46
- if (typeof item === "string") {
47
- return (
48
- <li>
49
- <a
50
- href={withBase(item)}
51
- aria-current={isCurrent(item) ? "page" : undefined}
52
- >
53
- {labelFor(item)}
54
- </a>
55
- </li>
56
- );
57
- }
58
- if ("items" in item) {
59
- return (
60
- <li>
61
- <details open>
62
- <summary>{item.label}</summary>
63
- <Astro.self
64
- items={item.items}
65
- routes={routes}
66
- current={current}
67
- base={base}
68
- nested
69
- />
70
- </details>
71
- </li>
72
- );
73
- }
74
- if ("autogenerate" in item) {
75
- return (
76
- <li>
77
- <details open>
78
- <summary>{item.label}</summary>
79
- <Astro.self
80
- items={generatedItems(item.autogenerate.directory)}
81
- routes={routes}
82
- current={current}
83
- base={base}
84
- nested
85
- />
86
- </details>
87
- </li>
88
- );
89
- }
90
- return (
91
- <li>
92
- <a
93
- href={withBase(item.link)}
94
- aria-current={isCurrent(item.link) ? "page" : undefined}
95
- >
96
- {item.label}
97
- </a>
98
- </li>
99
- );
100
- })
101
- }
102
- </NavigationTreeRoot>
@@ -1 +0,0 @@
1
- {"version":3,"file":"navigation-CC0dlAL8.js","names":[],"sources":["../src/navigation.ts"],"sourcesContent":["import type { DocsConfig, NavigationItem, NavigationTab } from \"@tenphi/docs\";\n\nexport interface ResolvedNavigationTab extends NavigationTab {\n /** Index of the tab's resolved Starlight sidebar group. */\n sidebarGroup?: number;\n}\n\nexport interface ResolvedNavigationLayout {\n fallbackSidebarGroup?: number;\n items?: NavigationItem[];\n tabs: ResolvedNavigationTab[];\n sectioned: boolean;\n}\n\nexport function resolveNavigationLayout(\n navigation: DocsConfig[\"navigation\"],\n): ResolvedNavigationLayout {\n const items = Array.isArray(navigation) ? navigation : navigation?.items;\n const configuredTabs = Array.isArray(navigation)\n ? []\n : (navigation?.tabs ?? []);\n const fallbackSidebarGroup = items !== undefined ? 0 : undefined;\n let sidebarGroup = fallbackSidebarGroup === undefined ? 0 : 1;\n const tabs = configuredTabs.map((tab) => ({\n ...tab,\n ...(tab.items !== undefined ? { sidebarGroup: sidebarGroup++ } : {}),\n }));\n\n return {\n ...(fallbackSidebarGroup !== undefined ? { fallbackSidebarGroup } : {}),\n ...(items !== undefined ? { items } : {}),\n tabs,\n sectioned: tabs.some((tab) => tab.sidebarGroup !== undefined),\n };\n}\n\nexport function navigationItemsForPath(\n layout: ResolvedNavigationLayout,\n pathname: string,\n): NavigationItem[] | undefined {\n const active = activeNavigationTab(layout.tabs, pathname);\n return active >= 0 && layout.tabs[active]?.items !== undefined\n ? layout.tabs[active].items\n : layout.items;\n}\n\n/** Pick one tab, preferring its own URL before routes owned by its sidebar. */\nexport function activeNavigationTab(\n tabs: NavigationTab[],\n pathname: string,\n): number {\n const current = normalizeNavigationPath(pathname);\n let winner = -1;\n let winningScore = -1;\n\n for (const [index, tab] of tabs.entries()) {\n const score = Math.max(\n linkMatchScore(tab.link, current, 30_000),\n navigationMatchScore(tab.items ?? [], current),\n );\n if (score > winningScore) {\n winner = index;\n winningScore = score;\n }\n }\n\n return winningScore >= 0 ? winner : -1;\n}\n\nexport function navigationPath(pathname: string, base = \"/\"): string {\n const normalizedBase = normalizeNavigationPath(base);\n const withoutBase =\n normalizedBase !== \"/\" &&\n (pathname === normalizedBase || pathname.startsWith(`${normalizedBase}/`))\n ? pathname.slice(normalizedBase.length) || \"/\"\n : pathname;\n return normalizeNavigationPath(withoutBase);\n}\n\nexport function normalizeNavigationPath(path: string): string {\n const clean = path.split(/[?#]/, 1)[0]?.replace(/\\\\/g, \"/\") ?? \"/\";\n const normalized = `/${clean.replace(/^\\/+|\\/+$/g, \"\")}`;\n return normalized === \"/\" ? normalized : normalized.replace(/\\/$/, \"\");\n}\n\nfunction navigationMatchScore(\n items: NavigationItem[],\n current: string,\n): number {\n let score = -1;\n for (const item of items) {\n if (typeof item === \"string\") {\n if (normalizeNavigationPath(item) === current)\n score = Math.max(score, 20_000);\n continue;\n }\n if (\"items\" in item) {\n score = Math.max(score, navigationMatchScore(item.items, current));\n continue;\n }\n if (\"autogenerate\" in item) {\n score = Math.max(\n score,\n directoryMatchScore(item.autogenerate.directory, current),\n );\n continue;\n }\n score = Math.max(score, linkMatchScore(item.link, current, 20_000));\n }\n return score;\n}\n\nfunction linkMatchScore(link: string, current: string, base: number): number {\n if (!link.startsWith(\"/\")) return -1;\n const target = normalizeNavigationPath(link);\n if (target === current) return base + 1_000 + target.length;\n if (target !== \"/\" && current.startsWith(`${target}/`)) {\n return base + target.length;\n }\n return -1;\n}\n\nfunction directoryMatchScore(directory: string, current: string): number {\n const target = normalizeNavigationPath(directory);\n if (target === \"/\") return 10_000;\n return current === target || current.startsWith(`${target}/`)\n ? 10_000 + target.length\n : -1;\n}\n"],"mappings":";AAcA,SAAgB,wBACd,YAC0B;CAC1B,MAAM,QAAQ,MAAM,QAAQ,UAAU,IAAI,aAAa,YAAY;CACnE,MAAM,iBAAiB,MAAM,QAAQ,UAAU,IAC3C,CAAC,IACA,YAAY,QAAQ,CAAC;CAC1B,MAAM,uBAAuB,UAAU,KAAA,IAAY,IAAI,KAAA;CACvD,IAAI,eAAe,yBAAyB,KAAA,IAAY,IAAI;CAC5D,MAAM,OAAO,eAAe,KAAK,SAAS;EACxC,GAAG;EACH,GAAI,IAAI,UAAU,KAAA,IAAY,EAAE,cAAc,eAAe,IAAI,CAAC;CACpE,EAAE;CAEF,OAAO;EACL,GAAI,yBAAyB,KAAA,IAAY,EAAE,qBAAqB,IAAI,CAAC;EACrE,GAAI,UAAU,KAAA,IAAY,EAAE,MAAM,IAAI,CAAC;EACvC;EACA,WAAW,KAAK,MAAM,QAAQ,IAAI,iBAAiB,KAAA,CAAS;CAC9D;AACF;AAEA,SAAgB,uBACd,QACA,UAC8B;CAC9B,MAAM,SAAS,oBAAoB,OAAO,MAAM,QAAQ;CACxD,OAAO,UAAU,KAAK,OAAO,KAAK,OAAO,EAAE,UAAU,KAAA,IACjD,OAAO,KAAK,OAAO,CAAC,QACpB,OAAO;AACb;;AAGA,SAAgB,oBACd,MACA,UACQ;CACR,MAAM,UAAU,wBAAwB,QAAQ;CAChD,IAAI,SAAS;CACb,IAAI,eAAe;CAEnB,KAAK,MAAM,CAAC,OAAO,QAAQ,KAAK,QAAQ,GAAG;EACzC,MAAM,QAAQ,KAAK,IACjB,eAAe,IAAI,MAAM,SAAS,GAAM,GACxC,qBAAqB,IAAI,SAAS,CAAC,GAAG,OAAO,CAC/C;EACA,IAAI,QAAQ,cAAc;GACxB,SAAS;GACT,eAAe;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