@tenphi/starlight 0.6.0 → 0.7.0

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,197 +1,54 @@
1
1
  ---
2
- import { createMarkdownProcessor } from "@astrojs/markdown-remark";
2
+ import StarlightPage from "@astrojs/starlight/components/StarlightPage.astro";
3
3
  import { content } from "virtual:cookbook/config";
4
4
  import { layout } from "virtual:cookbook/layout";
5
- import MobileNavigationTabs from "../components/MobileNavigationTabs.astro";
6
- import NavigationTree from "../components/NavigationTree.astro";
7
- import GlobalStyles from "../components/GlobalStyles.js";
8
- import PackageVersion from "../components/PackageVersion.astro";
9
- import {
10
- AppearanceControlRoot,
11
- DocsArticleRoot,
12
- DocsLayoutRoot,
13
- DocsSidebarRoot,
14
- SearchDialogRoot,
15
- SearchTriggerRoot,
16
- SkipLinkRoot,
17
- StandaloneHeaderRoot,
18
- TopNavigationRoot,
19
- VisuallyHiddenRoot,
20
- } from "../components/LayoutComponents.js";
21
- import { activeNavigationTab, navigationItemsForPath } from "../navigation.js";
22
- import "@fontsource-variable/onest/wght.css";
23
- import "@fontsource-variable/jetbrains-mono/wght.css";
5
+ import { starlightPageSidebar } from "../navigation.js";
24
6
 
25
7
  export const prerender = true;
26
8
 
27
- export async function getStaticPaths() {
28
- const processor = await createMarkdownProcessor();
29
- return Promise.all(
30
- content.entries.map(async (entry) => ({
9
+ export function getStaticPaths() {
10
+ return content.entries
11
+ .filter(
12
+ (entry) =>
13
+ import.meta.env.MODE !== "production" || !entry.frontmatter.draft,
14
+ )
15
+ .map((entry) => ({
31
16
  params: { route: entry.route === "/" ? undefined : entry.route.slice(1) },
32
- props: {
33
- entry,
34
- routes: content.routes,
35
- html: (await processor.render(entry.transformedBody)).code,
36
- site: content.site,
37
- base: content.base,
38
- search: content.search,
39
- },
40
- })),
41
- );
17
+ props: { entry, routes: content.routes },
18
+ }));
42
19
  }
43
20
 
44
- const { entry, routes, html, site, base, search } = Astro.props;
45
- const withBase = (route) =>
46
- `${base === "/" ? "" : `/${base.replace(/^\/+|\/+$/g, "")}`}${route}` || "/";
47
- const activeTab = activeNavigationTab(layout.tabs, entry.route);
48
- const navigationItems =
49
- navigationItemsForPath(layout, entry.route) ??
50
- routes.map((route) => route.route);
21
+ const { entry, routes } = Astro.props;
22
+ if (!entry.rendered) {
23
+ throw new Error(`Cookbook did not render ${entry.sourcePath}.`);
24
+ }
25
+ const frontmatter = {
26
+ title: entry.title,
27
+ ...(entry.description ? { description: entry.description } : {}),
28
+ ...(entry.frontmatter.toc !== undefined
29
+ ? { tableOfContents: entry.frontmatter.toc }
30
+ : {}),
31
+ ...(entry.frontmatter.editUrl !== undefined
32
+ ? { editUrl: entry.frontmatter.editUrl }
33
+ : {}),
34
+ ...(entry.frontmatter.prev !== undefined
35
+ ? { prev: entry.frontmatter.prev }
36
+ : {}),
37
+ ...(entry.frontmatter.next !== undefined
38
+ ? { next: entry.frontmatter.next }
39
+ : {}),
40
+ ...(entry.frontmatter.search !== undefined
41
+ ? { pagefind: entry.frontmatter.search }
42
+ : {}),
43
+ ...(entry.frontmatter.head ? { head: entry.frontmatter.head } : {}),
44
+ ...(entry.frontmatter.draft ? { draft: true } : {}),
45
+ };
51
46
  ---
52
47
 
53
- <!doctype html>
54
- <html lang="en">
55
- <head>
56
- <meta charset="utf-8" />
57
- <meta name="viewport" content="width=device-width" />
58
- <title>{entry.title} | {site.title ?? "Documentation"}</title>
59
- {
60
- entry.description && (
61
- <meta name="description" content={entry.description} />
62
- )
63
- }
64
- </head>
65
- <body class="td-shell">
66
- <GlobalStyles />
67
- <SkipLinkRoot
68
- className="td-skip"
69
- href="#main-content"
70
- data-tasty-anatomy="SkipLink"
71
- >
72
- Skip to content
73
- </SkipLinkRoot>
74
- <StandaloneHeaderRoot
75
- className="td-shell__header"
76
- data-tasty-anatomy="Header"
77
- data-docs-base={base}
78
- >
79
- <div class="td-shell__header-primary">
80
- <div class="td-shell__brand">
81
- <a href={withBase("/")}>{site.title ?? "Documentation"}</a>
82
- <PackageVersion version={site.version} />
83
- </div>
84
- <div class="td-shell__actions">
85
- <AppearanceControlRoot
86
- className="td-appearance-control"
87
- title="Color theme"
88
- data-tasty-anatomy="AppearanceControl"
89
- >
90
- <VisuallyHiddenRoot>Color theme</VisuallyHiddenRoot>
91
- <select data-docs-theme aria-label="Color theme">
92
- <option value="system">System theme</option>
93
- <option value="light">Light theme</option>
94
- <option value="dark">Dark theme</option>
95
- </select>
96
- </AppearanceControlRoot>
97
- <AppearanceControlRoot
98
- className="td-appearance-control td-appearance-control--contrast"
99
- title="Contrast"
100
- data-tasty-anatomy="AppearanceControl"
101
- >
102
- <VisuallyHiddenRoot>Contrast</VisuallyHiddenRoot>
103
- <select data-docs-contrast aria-label="Contrast">
104
- <option value="system">System contrast</option>
105
- <option value="normal">Normal contrast</option>
106
- <option value="more">High contrast</option>
107
- </select>
108
- </AppearanceControlRoot>
109
- {
110
- search && (
111
- <SearchTriggerRoot
112
- type="button"
113
- data-docs-search-open
114
- aria-haspopup="dialog"
115
- aria-keyshortcuts="Control+K"
116
- data-tasty-anatomy="SearchTrigger"
117
- >
118
- <span>Search</span>
119
- <kbd data-docs-search-shortcut>
120
- <>
121
- <kbd>Ctrl</kbd>
122
- <kbd>K</kbd>
123
- </>
124
- </kbd>
125
- </SearchTriggerRoot>
126
- )
127
- }
128
- </div>
129
- </div>
130
- {
131
- layout.tabs.length > 0 && (
132
- <TopNavigationRoot
133
- className="td-top-tabs"
134
- aria-label="Primary"
135
- data-tasty-anatomy="TopNavigation"
136
- >
137
- {layout.tabs.map((tab, index) => (
138
- <a
139
- href={tab.link.startsWith("/") ? withBase(tab.link) : tab.link}
140
- aria-current={index === activeTab ? "page" : undefined}
141
- >
142
- {tab.label}
143
- </a>
144
- ))}
145
- </TopNavigationRoot>
146
- )
147
- }
148
- </StandaloneHeaderRoot>
149
- <DocsLayoutRoot
150
- className="td-shell__layout"
151
- data-tasty-anatomy="DocsLayout"
152
- >
153
- <DocsSidebarRoot
154
- className="td-shell__nav"
155
- aria-label="Documentation"
156
- data-tasty-anatomy="DocsSidebar"
157
- >
158
- <MobileNavigationTabs base={base} currentPath={entry.route} />
159
- <NavigationTree
160
- items={navigationItems}
161
- routes={routes}
162
- current={entry.route}
163
- base={base}
164
- />
165
- </DocsSidebarRoot>
166
- <DocsArticleRoot
167
- id="main-content"
168
- className="td-shell__main"
169
- data-pagefind-body
170
- data-tasty-anatomy="DocsArticle"
171
- >
172
- <h1>{entry.title}</h1>
173
- <div set:html={html} />
174
- </DocsArticleRoot>
175
- </DocsLayoutRoot>
176
- {
177
- search && (
178
- <SearchDialogRoot
179
- data-docs-search
180
- aria-labelledby="td-search-title"
181
- data-tasty-anatomy="SearchDialog"
182
- >
183
- <form method="dialog">
184
- <button aria-label="Close search">Close</button>
185
- </form>
186
- <h2 id="td-search-title">Search documentation</h2>
187
- <label>
188
- Search{" "}
189
- <input type="search" data-docs-search-input autocomplete="off" />
190
- </label>
191
- <div data-docs-search-status aria-live="polite" />
192
- <ul data-docs-search-results />
193
- </SearchDialogRoot>
194
- )
195
- }
196
- </body>
197
- </html>
48
+ <StarlightPage
49
+ frontmatter={frontmatter}
50
+ headings={entry.rendered.headings}
51
+ sidebar={starlightPageSidebar(layout, routes)}
52
+ >
53
+ <div set:html={entry.rendered.html} />
54
+ </StarlightPage>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tenphi/starlight",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "Astro and Starlight renderer for Cookbook",
5
5
  "type": "module",
6
6
  "sideEffects": [
@@ -49,10 +49,9 @@
49
49
  "@astrojs/starlight": "^0.41.10",
50
50
  "@fontsource-variable/jetbrains-mono": "^5.3.0",
51
51
  "@fontsource-variable/onest": "^5.3.0",
52
- "@tenphi/docs": "0.6.0",
52
+ "@tenphi/docs": "0.7.0",
53
53
  "@tenphi/glaze": "2.0.0",
54
54
  "@tenphi/tasty": "3.6.0",
55
- "pagefind": "^1.5.2",
56
55
  "react": "^18.3.1 || ^19.0.0",
57
56
  "react-dom": "^18.3.1 || ^19.0.0",
58
57
  "tsx": "^4.20.6"
@@ -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,eA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