@astrojs/starlight 0.18.1 → 0.19.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.
- package/CHANGELOG.md +24 -0
- package/components/CallToAction.astro +4 -1
- package/components/StarlightPage.astro +13 -0
- package/components.ts +1 -0
- package/integrations/virtual-user-config.ts +5 -0
- package/package.json +6 -2
- package/props.ts +1 -0
- package/schemas/hero.ts +2 -0
- package/translations/index.ts +2 -0
- package/translations/zh-TW.json +26 -0
- package/user-components/Aside.astro +38 -0
- package/utils/error-map.ts +4 -0
- package/utils/plugins.ts +1 -5
- package/utils/route-data.ts +2 -2
- package/utils/slugs.ts +18 -0
- package/utils/starlight-page.ts +304 -0
- package/virtual.d.ts +4 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,29 @@
|
|
|
1
1
|
# @astrojs/starlight
|
|
2
2
|
|
|
3
|
+
## 0.19.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#1527](https://github.com/withastro/starlight/pull/1527) [`163bc84`](https://github.com/withastro/starlight/commit/163bc848e173eecca92d1cb034045fdb42aa4ff1) Thanks [@HiDeoo](https://github.com/HiDeoo)! - Exports the `StarlightPageProps` TypeScript type representing the props expected by the `<StarlightPage />` component.
|
|
8
|
+
|
|
9
|
+
- [#1504](https://github.com/withastro/starlight/pull/1504) [`fc83a05`](https://github.com/withastro/starlight/commit/fc83a05235b74be2bfe6ba8e7f95a8a5a618ead3) Thanks [@mingjunlu](https://github.com/mingjunlu)! - Adds Traditional Chinese UI translations
|
|
10
|
+
|
|
11
|
+
- [#1534](https://github.com/withastro/starlight/pull/1534) [`aada680`](https://github.com/withastro/starlight/commit/aada6805abc0068f07393585b86978ef5200439c) Thanks [@delucis](https://github.com/delucis)! - Improves DX of the `sidebar` prop used by the new `<StarlightPage>` component.
|
|
12
|
+
|
|
13
|
+
## 0.19.0
|
|
14
|
+
|
|
15
|
+
### Minor Changes
|
|
16
|
+
|
|
17
|
+
- [#1485](https://github.com/withastro/starlight/pull/1485) [`2cb3578`](https://github.com/withastro/starlight/commit/2cb35782dace67c7c418a31005419fa95493b3d3) Thanks [@timokoessler](https://github.com/timokoessler)! - Add support for setting html attributes of hero action links
|
|
18
|
+
|
|
19
|
+
- [#1175](https://github.com/withastro/starlight/pull/1175) [`dd11b95`](https://github.com/withastro/starlight/commit/dd11b9538abdf4b5ba2ef70e07c0edda03e95add) Thanks [@HiDeoo](https://github.com/HiDeoo)! - Adds a new `<StarlightPage>` component to use the Starlight layout in custom pages.
|
|
20
|
+
|
|
21
|
+
To learn more about this new feature, check out the new [“Using Starlight’s design in custom pages” guide](https://starlight.astro.build/guides/pages/#using-starlights-design-in-custom-pages).
|
|
22
|
+
|
|
23
|
+
- [#1499](https://github.com/withastro/starlight/pull/1499) [`97bf523`](https://github.com/withastro/starlight/commit/97bf523923fb9678c12f58fcdbe36757f0e56ceb) Thanks [@delucis](https://github.com/delucis)! - Adds a new `<Aside>` component
|
|
24
|
+
|
|
25
|
+
The new component is in addition to the existing custom Markdown syntax.
|
|
26
|
+
|
|
3
27
|
## 0.18.1
|
|
4
28
|
|
|
5
29
|
### Patch Changes
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
---
|
|
2
|
+
import type { HTMLAttributes } from 'astro/types';
|
|
2
3
|
import Icon from '../user-components/Icon.astro';
|
|
3
4
|
import type { Icons } from './Icons';
|
|
4
5
|
|
|
@@ -6,12 +7,14 @@ interface Props {
|
|
|
6
7
|
variant: 'primary' | 'secondary' | 'minimal';
|
|
7
8
|
link: string;
|
|
8
9
|
icon?: undefined | { type: 'icon'; name: keyof typeof Icons } | { type: 'raw'; html: string };
|
|
10
|
+
attrs?: Omit<HTMLAttributes<'a'>, 'href'>;
|
|
9
11
|
}
|
|
10
12
|
|
|
11
13
|
const { link, variant, icon } = Astro.props;
|
|
14
|
+
const { class: customClass, ...attrs } = Astro.props.attrs || {};
|
|
12
15
|
---
|
|
13
16
|
|
|
14
|
-
<a class:list={['sl-flex action', variant]} href={link}>
|
|
17
|
+
<a class:list={['sl-flex action', variant, customClass]} href={link} {...attrs}>
|
|
15
18
|
<slot />
|
|
16
19
|
{icon?.type === 'icon' && <Icon name={icon.name} size="1.5rem" />}
|
|
17
20
|
{icon?.type === 'raw' && <Fragment set:html={icon.html} />}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
---
|
|
2
|
+
import {
|
|
3
|
+
generateStarlightPageRouteData,
|
|
4
|
+
type StarlightPageProps as Props,
|
|
5
|
+
} from '../utils/starlight-page';
|
|
6
|
+
import Page from './Page.astro';
|
|
7
|
+
|
|
8
|
+
export type StarlightPageProps = Props;
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
<Page {...await generateStarlightPageRouteData({ props: Astro.props, url: Astro.url })}>
|
|
12
|
+
<slot />
|
|
13
|
+
</Page>
|
package/components.ts
CHANGED
|
@@ -48,6 +48,11 @@ export function vitePluginStarlightUserConfig(
|
|
|
48
48
|
opts.logo.light
|
|
49
49
|
)}; export const logos = { dark, light };`
|
|
50
50
|
: 'export const logos = {};',
|
|
51
|
+
'virtual:starlight/collection-config': `let userCollections;
|
|
52
|
+
try {
|
|
53
|
+
userCollections = (await import('/src/content/config.ts')).collections;
|
|
54
|
+
} catch {}
|
|
55
|
+
export const collections = userCollections;`,
|
|
51
56
|
...virtualComponentModules,
|
|
52
57
|
} satisfies Record<string, string>;
|
|
53
58
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astrojs/starlight",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.19.1",
|
|
4
4
|
"description": "Build beautiful, high-performance documentation websites with Astro",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"docs",
|
|
@@ -98,6 +98,10 @@
|
|
|
98
98
|
"types": "./components/Page.astro.tsx",
|
|
99
99
|
"import": "./components/Page.astro"
|
|
100
100
|
},
|
|
101
|
+
"./components/StarlightPage.astro": {
|
|
102
|
+
"types": "./components/StarlightPage.astro.tsx",
|
|
103
|
+
"import": "./components/StarlightPage.astro"
|
|
104
|
+
},
|
|
101
105
|
"./components/Footer.astro": {
|
|
102
106
|
"types": "./components/Footer.astro.tsx",
|
|
103
107
|
"import": "./components/Footer.astro"
|
|
@@ -169,7 +173,7 @@
|
|
|
169
173
|
"@astrojs/markdown-remark": "^4.2.1",
|
|
170
174
|
"@types/node": "^18.16.19",
|
|
171
175
|
"@vitest/coverage-v8": "^1.2.2",
|
|
172
|
-
"astro": "^4.3.
|
|
176
|
+
"astro": "^4.3.5",
|
|
173
177
|
"vitest": "^1.2.2"
|
|
174
178
|
},
|
|
175
179
|
"dependencies": {
|
package/props.ts
CHANGED
package/schemas/hero.ts
CHANGED
|
@@ -64,6 +64,8 @@ export const HeroSchema = ({ image }: SchemaContext) =>
|
|
|
64
64
|
: ({ type: 'raw', html: icon } as const);
|
|
65
65
|
})
|
|
66
66
|
.optional(),
|
|
67
|
+
/** HTML attributes to add to the link */
|
|
68
|
+
attrs: z.record(z.union([z.string(), z.number(), z.boolean()])).optional(),
|
|
67
69
|
})
|
|
68
70
|
.array()
|
|
69
71
|
.default([]),
|
package/translations/index.ts
CHANGED
|
@@ -24,6 +24,7 @@ import ru from './ru.json';
|
|
|
24
24
|
import vi from './vi.json';
|
|
25
25
|
import uk from './uk.json';
|
|
26
26
|
import hi from './hi.json';
|
|
27
|
+
import zhTW from './zh-TW.json';
|
|
27
28
|
|
|
28
29
|
const { parse } = builtinI18nSchema();
|
|
29
30
|
|
|
@@ -54,5 +55,6 @@ export default Object.fromEntries(
|
|
|
54
55
|
vi,
|
|
55
56
|
uk,
|
|
56
57
|
hi,
|
|
58
|
+
'zh-TW': zhTW,
|
|
57
59
|
}).map(([key, dict]) => [key, parse(dict)])
|
|
58
60
|
);
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"skipLink.label": "跳到內容",
|
|
3
|
+
"search.label": "搜尋",
|
|
4
|
+
"search.shortcutLabel": "(按 / 鍵開始搜尋)",
|
|
5
|
+
"search.cancelLabel": "取消",
|
|
6
|
+
"search.devWarning": "正式版本才能使用搜尋功能。\n如要在本地測試,請先建置並預覽網站。",
|
|
7
|
+
"themeSelect.accessibleLabel": "選擇佈景主題",
|
|
8
|
+
"themeSelect.dark": "深色",
|
|
9
|
+
"themeSelect.light": "淺色",
|
|
10
|
+
"themeSelect.auto": "自動",
|
|
11
|
+
"languageSelect.accessibleLabel": "選擇語言",
|
|
12
|
+
"menuButton.accessibleLabel": "選單",
|
|
13
|
+
"sidebarNav.accessibleLabel": "主要",
|
|
14
|
+
"tableOfContents.onThisPage": "本頁內容",
|
|
15
|
+
"tableOfContents.overview": "概述",
|
|
16
|
+
"i18n.untranslatedContent": "本頁內容尚未翻譯。",
|
|
17
|
+
"page.editLink": "編輯頁面",
|
|
18
|
+
"page.lastUpdated": "最後更新於:",
|
|
19
|
+
"page.previousLink": "前一則",
|
|
20
|
+
"page.nextLink": "下一則",
|
|
21
|
+
"404.text": "找不到頁面。請檢查網址或改用搜尋功能。",
|
|
22
|
+
"aside.note": "注意",
|
|
23
|
+
"aside.tip": "提示",
|
|
24
|
+
"aside.caution": "警告",
|
|
25
|
+
"aside.danger": "危險"
|
|
26
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
---
|
|
2
|
+
import { AstroError } from 'astro/errors';
|
|
3
|
+
import { slugToLocaleData, urlToSlug } from '../utils/slugs';
|
|
4
|
+
import { useTranslations } from '../utils/translations';
|
|
5
|
+
import Icon from './Icon.astro';
|
|
6
|
+
|
|
7
|
+
const asideVariants = ['note', 'tip', 'caution', 'danger'] as const;
|
|
8
|
+
const icons = { note: 'information', tip: 'rocket', caution: 'warning', danger: 'error' } as const;
|
|
9
|
+
|
|
10
|
+
interface Props {
|
|
11
|
+
type?: (typeof asideVariants)[number];
|
|
12
|
+
title?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
let { type = 'note', title } = Astro.props;
|
|
16
|
+
|
|
17
|
+
if (!asideVariants.includes(type)) {
|
|
18
|
+
throw new AstroError(
|
|
19
|
+
'Invalid `type` prop passed to the `<Aside>` component.\n',
|
|
20
|
+
`Received: ${JSON.stringify(type)}\n` +
|
|
21
|
+
`Expected one of ${asideVariants.map((i) => JSON.stringify(i)).join(', ')}`
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (!title) {
|
|
26
|
+
const { locale } = slugToLocaleData(urlToSlug(Astro.url));
|
|
27
|
+
title = useTranslations(locale)(`aside.${type}`);
|
|
28
|
+
}
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
<aside aria-label={title} class={`starlight-aside starlight-aside--${type}`}>
|
|
32
|
+
<p class="starlight-aside__title" aria-hidden="true">
|
|
33
|
+
<Icon name={icons[type]} class="starlight-aside__icon" />{title}
|
|
34
|
+
</p>
|
|
35
|
+
<section class="starlight-aside__content">
|
|
36
|
+
<slot />
|
|
37
|
+
</section>
|
|
38
|
+
</aside>
|
package/utils/error-map.ts
CHANGED
|
@@ -11,6 +11,10 @@ type TypeOrLiteralErrByPathEntry = {
|
|
|
11
11
|
expected: unknown[];
|
|
12
12
|
};
|
|
13
13
|
|
|
14
|
+
export function throwValidationError(error: z.ZodError, message: string): never {
|
|
15
|
+
throw new Error(`${message}\n${error.issues.map((i) => i.message).join('\n')}`);
|
|
16
|
+
}
|
|
17
|
+
|
|
14
18
|
export const errorMap: z.ZodErrorMap = (baseError, ctx) => {
|
|
15
19
|
const baseErrorPath = flattenErrorPath(baseError.path);
|
|
16
20
|
if (baseError.code === 'invalid_union') {
|
package/utils/plugins.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { AstroIntegration } from 'astro';
|
|
2
2
|
import { z } from 'astro/zod';
|
|
3
3
|
import { StarlightConfigSchema, type StarlightUserConfig } from '../utils/user-config';
|
|
4
|
-
import { errorMap } from '../utils/error-map';
|
|
4
|
+
import { errorMap, throwValidationError } from '../utils/error-map';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Runs Starlight plugins in the order that they are configured after validating the user-provided
|
|
@@ -82,10 +82,6 @@ export async function runPlugins(
|
|
|
82
82
|
return { integrations, starlightConfig: starlightConfig.data };
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
-
function throwValidationError(error: z.ZodError, message: string): never {
|
|
86
|
-
throw new Error(`${message}\n${error.issues.map((i) => i.message).join('\n')}`);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
85
|
// https://github.com/withastro/astro/blob/910eb00fe0b70ca80bd09520ae100e8c78b675b5/packages/astro/src/core/config/schema.ts#L113
|
|
90
86
|
const astroIntegrationSchema = z.object({
|
|
91
87
|
name: z.string(),
|
package/utils/route-data.ts
CHANGED
|
@@ -10,7 +10,7 @@ import type { Route } from './routing';
|
|
|
10
10
|
import { localizedId } from './slugs';
|
|
11
11
|
import { useTranslations } from './translations';
|
|
12
12
|
|
|
13
|
-
interface PageProps extends Route {
|
|
13
|
+
export interface PageProps extends Route {
|
|
14
14
|
headings: MarkdownHeading[];
|
|
15
15
|
}
|
|
16
16
|
|
|
@@ -54,7 +54,7 @@ export function generateRouteData({
|
|
|
54
54
|
};
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
-
function getToC({ entry, locale, headings }: PageProps) {
|
|
57
|
+
export function getToC({ entry, locale, headings }: PageProps) {
|
|
58
58
|
const tocConfig =
|
|
59
59
|
entry.data.template === 'splash'
|
|
60
60
|
? false
|
package/utils/slugs.ts
CHANGED
|
@@ -101,3 +101,21 @@ export function localizedId(id: string, locale: string | undefined): string {
|
|
|
101
101
|
return id;
|
|
102
102
|
}
|
|
103
103
|
}
|
|
104
|
+
|
|
105
|
+
/** Extract the slug from a URL. */
|
|
106
|
+
export function urlToSlug(url: URL): string {
|
|
107
|
+
let pathname = url.pathname;
|
|
108
|
+
const base = import.meta.env.BASE_URL.replace(/\/$/, '');
|
|
109
|
+
if (pathname.startsWith(base)) pathname = pathname.replace(base, '');
|
|
110
|
+
const segments = pathname.split('/');
|
|
111
|
+
const htmlExt = '.html';
|
|
112
|
+
if (segments.at(-1) === 'index.html') {
|
|
113
|
+
// Remove trailing `index.html`.
|
|
114
|
+
segments.pop();
|
|
115
|
+
} else if (segments.at(-1)?.endsWith(htmlExt)) {
|
|
116
|
+
// Remove trailing `.html`.
|
|
117
|
+
const last = segments.pop();
|
|
118
|
+
if (last) segments.push(last.slice(0, -1 * htmlExt.length));
|
|
119
|
+
}
|
|
120
|
+
return segments.filter(Boolean).join('/');
|
|
121
|
+
}
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
import { z } from 'astro/zod';
|
|
2
|
+
import { type ContentConfig, type SchemaContext } from 'astro:content';
|
|
3
|
+
import config from 'virtual:starlight/user-config';
|
|
4
|
+
import { errorMap, throwValidationError } from './error-map';
|
|
5
|
+
import { stripLeadingAndTrailingSlashes } from './path';
|
|
6
|
+
import { getToC, type PageProps, type StarlightRouteData } from './route-data';
|
|
7
|
+
import type { StarlightDocsEntry } from './routing';
|
|
8
|
+
import { slugToLocaleData, urlToSlug } from './slugs';
|
|
9
|
+
import { getPrevNextLinks, getSidebar } from './navigation';
|
|
10
|
+
import { useTranslations } from './translations';
|
|
11
|
+
import { docsSchema } from '../schema';
|
|
12
|
+
import { BadgeConfigSchema } from '../schemas/badge';
|
|
13
|
+
import { SidebarLinkItemHTMLAttributesSchema } from '../schemas/sidebar';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The frontmatter schema for Starlight pages derived from the default schema for Starlight’s
|
|
17
|
+
* `docs` content collection.
|
|
18
|
+
* The frontmatter schema for Starlight pages cannot include some properties which will be omitted
|
|
19
|
+
* and some others needs to be refined to a stricter type.
|
|
20
|
+
*/
|
|
21
|
+
const StarlightPageFrontmatterSchema = async (context: SchemaContext) => {
|
|
22
|
+
const userDocsSchema = await getUserDocsSchema();
|
|
23
|
+
const schema = typeof userDocsSchema === 'function' ? userDocsSchema(context) : userDocsSchema;
|
|
24
|
+
|
|
25
|
+
return schema.transform((frontmatter) => {
|
|
26
|
+
/**
|
|
27
|
+
* Starlight pages can only be edited if an edit URL is explicitly provided.
|
|
28
|
+
* The `sidebar` frontmatter prop only works for pages in an autogenerated links group.
|
|
29
|
+
* Starlight pages edit links cannot be autogenerated.
|
|
30
|
+
*
|
|
31
|
+
* These changes to the schema are done using a transformer and not using the usual `omit`
|
|
32
|
+
* method because when the frontmatter schema is extended by the user, an intersection between
|
|
33
|
+
* the default schema and the user schema is created using the `and` method. Intersections in
|
|
34
|
+
* Zod returns a `ZodIntersection` object which does not have some methods like `omit` or
|
|
35
|
+
* `pick`.
|
|
36
|
+
*
|
|
37
|
+
* This transformer only sets the `editUrl` default value and removes the `sidebar` property
|
|
38
|
+
* from the validated output but does not appply any changes to the input schema type itself so
|
|
39
|
+
* this needs to be done manually.
|
|
40
|
+
*
|
|
41
|
+
* @see StarlightPageFrontmatter
|
|
42
|
+
* @see https://github.com/colinhacks/zod#intersections
|
|
43
|
+
*/
|
|
44
|
+
const { editUrl, sidebar, ...others } = frontmatter;
|
|
45
|
+
const pageEditUrl = editUrl === undefined || editUrl === true ? false : editUrl;
|
|
46
|
+
return { ...others, editUrl: pageEditUrl };
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Type of Starlight pages frontmatter schema.
|
|
52
|
+
* We manually refines the `editUrl` type and omit the `sidebar` property as it's not possible to
|
|
53
|
+
* do that on the schema itself using Zod but the proper validation is still using a transformer.
|
|
54
|
+
* @see StarlightPageFrontmatterSchema
|
|
55
|
+
*/
|
|
56
|
+
type StarlightPageFrontmatter = Omit<
|
|
57
|
+
z.input<Awaited<ReturnType<typeof StarlightPageFrontmatterSchema>>>,
|
|
58
|
+
'editUrl' | 'sidebar'
|
|
59
|
+
> & { editUrl?: string | false };
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Link configuration schema for `<StarlightPage>`.
|
|
63
|
+
* Sets default values where possible to be more user friendly than raw `SidebarEntry` type.
|
|
64
|
+
*/
|
|
65
|
+
const LinkSchema = z
|
|
66
|
+
.object({
|
|
67
|
+
/** @deprecated Specifying `type` is no longer required. */
|
|
68
|
+
type: z.literal('link').default('link'),
|
|
69
|
+
label: z.string(),
|
|
70
|
+
href: z.string(),
|
|
71
|
+
isCurrent: z.boolean().default(false),
|
|
72
|
+
badge: BadgeConfigSchema(),
|
|
73
|
+
attrs: SidebarLinkItemHTMLAttributesSchema(),
|
|
74
|
+
})
|
|
75
|
+
// Make sure badge is in the object even if undefined — Zod doesn’t seem to have a way to set `undefined` as a default.
|
|
76
|
+
.transform((item) => ({ badge: undefined, ...item }));
|
|
77
|
+
|
|
78
|
+
/** Base schema for link groups without the recursive `items` array. */
|
|
79
|
+
const LinkGroupBase = z.object({
|
|
80
|
+
/** @deprecated Specifying `type` is no longer required. */
|
|
81
|
+
type: z.literal('group').default('group'),
|
|
82
|
+
label: z.string(),
|
|
83
|
+
collapsed: z.boolean().default(false),
|
|
84
|
+
badge: BadgeConfigSchema(),
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
// These manual types are needed to correctly type the recursive link group type.
|
|
88
|
+
type ManualLinkGroupInput = Prettify<
|
|
89
|
+
z.input<typeof LinkGroupBase> &
|
|
90
|
+
// The original implementation of `<StarlightPage>` in v0.19.0 used `entries`.
|
|
91
|
+
// We want to use `items` so it matches the sidebar config in `astro.config.mjs`.
|
|
92
|
+
// Keeping `entries` support for now to not break anyone.
|
|
93
|
+
// TODO: warn about `entries` usage in a future version
|
|
94
|
+
// TODO: remove support for `entries` in a future version
|
|
95
|
+
(| {
|
|
96
|
+
/** Array of links and subcategories to display in this category. */
|
|
97
|
+
items: Array<z.input<typeof LinkSchema> | ManualLinkGroupInput>;
|
|
98
|
+
}
|
|
99
|
+
| {
|
|
100
|
+
/**
|
|
101
|
+
* @deprecated Use `items` instead of `entries`.
|
|
102
|
+
* Support for `entries` will be removed in a future version of Starlight.
|
|
103
|
+
*/
|
|
104
|
+
entries: Array<z.input<typeof LinkSchema> | ManualLinkGroupInput>;
|
|
105
|
+
}
|
|
106
|
+
)
|
|
107
|
+
>;
|
|
108
|
+
type ManualLinkGroupOutput = z.output<typeof LinkGroupBase> & {
|
|
109
|
+
entries: Array<z.output<typeof LinkSchema> | ManualLinkGroupOutput>;
|
|
110
|
+
badge: z.output<typeof LinkGroupBase>['badge'];
|
|
111
|
+
};
|
|
112
|
+
type LinkGroupSchemaType = z.ZodType<ManualLinkGroupOutput, z.ZodTypeDef, ManualLinkGroupInput>;
|
|
113
|
+
/**
|
|
114
|
+
* Link group configuration schema for `<StarlightPage>`.
|
|
115
|
+
* Sets default values where possible to be more user friendly than raw `SidebarEntry` type.
|
|
116
|
+
*/
|
|
117
|
+
const LinkGroupSchema: LinkGroupSchemaType = z.preprocess(
|
|
118
|
+
// Map `items` to `entries` as expected by the `SidebarEntry` type.
|
|
119
|
+
(arg) => {
|
|
120
|
+
if (arg && typeof arg === 'object' && 'items' in arg) {
|
|
121
|
+
const { items, ...rest } = arg;
|
|
122
|
+
return { ...rest, entries: items };
|
|
123
|
+
}
|
|
124
|
+
return arg;
|
|
125
|
+
},
|
|
126
|
+
LinkGroupBase.extend({
|
|
127
|
+
entries: z.lazy(() => z.union([LinkSchema, LinkGroupSchema]).array()),
|
|
128
|
+
})
|
|
129
|
+
// Make sure badge is in the object even if undefined.
|
|
130
|
+
.transform((item) => ({ badge: undefined, ...item }))
|
|
131
|
+
) as LinkGroupSchemaType;
|
|
132
|
+
|
|
133
|
+
/** Sidebar configuration schema for `<StarlightPage>` */
|
|
134
|
+
const StarlightPageSidebarSchema = z.union([LinkSchema, LinkGroupSchema]).array();
|
|
135
|
+
type StarlightPageSidebarUserConfig = z.input<typeof StarlightPageSidebarSchema>;
|
|
136
|
+
|
|
137
|
+
/** Parse sidebar prop to ensure all required defaults are in place. */
|
|
138
|
+
const normalizeSidebarProp = (
|
|
139
|
+
sidebarProp: StarlightPageSidebarUserConfig
|
|
140
|
+
): StarlightRouteData['sidebar'] => {
|
|
141
|
+
const sidebar = StarlightPageSidebarSchema.safeParse(sidebarProp, { errorMap });
|
|
142
|
+
if (!sidebar.success) {
|
|
143
|
+
throwValidationError(
|
|
144
|
+
sidebar.error,
|
|
145
|
+
'Invalid sidebar prop passed to the `<StarlightPage/>` component.'
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
return sidebar.data;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* The props accepted by the `<StarlightPage/>` component.
|
|
153
|
+
*/
|
|
154
|
+
export type StarlightPageProps = Prettify<
|
|
155
|
+
// Remove the index signature from `Route`, omit undesired properties and make the rest optional.
|
|
156
|
+
Partial<Omit<RemoveIndexSignature<PageProps>, 'entry' | 'entryMeta' | 'id' | 'locale' | 'slug'>> &
|
|
157
|
+
// Add the sidebar definitions for a Starlight page.
|
|
158
|
+
Partial<Pick<StarlightRouteData, 'hasSidebar'>> & {
|
|
159
|
+
sidebar?: StarlightPageSidebarUserConfig;
|
|
160
|
+
// And finally add the Starlight page frontmatter properties in a `frontmatter` property.
|
|
161
|
+
frontmatter: StarlightPageFrontmatter;
|
|
162
|
+
}
|
|
163
|
+
>;
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* A docs entry used for Starlight pages meant to be rendered by plugins and which is safe to cast
|
|
167
|
+
* to a `StarlightDocsEntry`.
|
|
168
|
+
* A Starlight page docs entry cannot be rendered like a content collection entry.
|
|
169
|
+
*/
|
|
170
|
+
type StarlightPageDocsEntry = Omit<StarlightDocsEntry, 'id' | 'render'> & {
|
|
171
|
+
/**
|
|
172
|
+
* The unique ID for this Starlight page which cannot be inferred from codegen like content
|
|
173
|
+
* collection entries.
|
|
174
|
+
*/
|
|
175
|
+
id: string;
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
export async function generateStarlightPageRouteData({
|
|
179
|
+
props,
|
|
180
|
+
url,
|
|
181
|
+
}: {
|
|
182
|
+
props: StarlightPageProps;
|
|
183
|
+
url: URL;
|
|
184
|
+
}): Promise<StarlightRouteData> {
|
|
185
|
+
const { isFallback, frontmatter, ...routeProps } = props;
|
|
186
|
+
const slug = urlToSlug(url);
|
|
187
|
+
const pageFrontmatter = await getStarlightPageFrontmatter(frontmatter);
|
|
188
|
+
const id = `${stripLeadingAndTrailingSlashes(slug)}.md`;
|
|
189
|
+
const localeData = slugToLocaleData(slug);
|
|
190
|
+
const sidebar = props.sidebar
|
|
191
|
+
? normalizeSidebarProp(props.sidebar)
|
|
192
|
+
: getSidebar(url.pathname, localeData.locale);
|
|
193
|
+
const headings = props.headings ?? [];
|
|
194
|
+
const pageDocsEntry: StarlightPageDocsEntry = {
|
|
195
|
+
id,
|
|
196
|
+
slug,
|
|
197
|
+
body: '',
|
|
198
|
+
collection: 'docs',
|
|
199
|
+
data: {
|
|
200
|
+
...pageFrontmatter,
|
|
201
|
+
sidebar: {
|
|
202
|
+
attrs: {},
|
|
203
|
+
hidden: false,
|
|
204
|
+
},
|
|
205
|
+
},
|
|
206
|
+
};
|
|
207
|
+
const entry = pageDocsEntry as StarlightDocsEntry;
|
|
208
|
+
const entryMeta: StarlightRouteData['entryMeta'] = {
|
|
209
|
+
dir: props.dir ?? localeData.dir,
|
|
210
|
+
lang: props.lang ?? localeData.lang,
|
|
211
|
+
locale: localeData.locale,
|
|
212
|
+
};
|
|
213
|
+
const editUrl = pageFrontmatter.editUrl ? new URL(pageFrontmatter.editUrl) : undefined;
|
|
214
|
+
const lastUpdated =
|
|
215
|
+
pageFrontmatter.lastUpdated instanceof Date ? pageFrontmatter.lastUpdated : undefined;
|
|
216
|
+
const routeData: StarlightRouteData = {
|
|
217
|
+
...routeProps,
|
|
218
|
+
...localeData,
|
|
219
|
+
id,
|
|
220
|
+
editUrl,
|
|
221
|
+
entry,
|
|
222
|
+
entryMeta,
|
|
223
|
+
hasSidebar: props.hasSidebar ?? entry.data.template !== 'splash',
|
|
224
|
+
headings,
|
|
225
|
+
labels: useTranslations(localeData.locale).all(),
|
|
226
|
+
lastUpdated,
|
|
227
|
+
pagination: getPrevNextLinks(sidebar, config.pagination, entry.data),
|
|
228
|
+
sidebar,
|
|
229
|
+
slug,
|
|
230
|
+
toc: getToC({
|
|
231
|
+
...routeProps,
|
|
232
|
+
...localeData,
|
|
233
|
+
entry,
|
|
234
|
+
entryMeta,
|
|
235
|
+
headings,
|
|
236
|
+
id,
|
|
237
|
+
locale: localeData.locale,
|
|
238
|
+
slug,
|
|
239
|
+
}),
|
|
240
|
+
};
|
|
241
|
+
if (isFallback) {
|
|
242
|
+
routeData.isFallback = true;
|
|
243
|
+
}
|
|
244
|
+
return routeData;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** Validates the Starlight page frontmatter properties from the props received by a Starlight page. */
|
|
248
|
+
async function getStarlightPageFrontmatter(frontmatter: StarlightPageFrontmatter) {
|
|
249
|
+
// This needs to be in sync with ImageMetadata.
|
|
250
|
+
// https://github.com/withastro/astro/blob/cf993bc263b58502096f00d383266cd179f331af/packages/astro/src/assets/types.ts#L32
|
|
251
|
+
const schema = await StarlightPageFrontmatterSchema({
|
|
252
|
+
image: () =>
|
|
253
|
+
z.object({
|
|
254
|
+
src: z.string(),
|
|
255
|
+
width: z.number(),
|
|
256
|
+
height: z.number(),
|
|
257
|
+
format: z.union([
|
|
258
|
+
z.literal('png'),
|
|
259
|
+
z.literal('jpg'),
|
|
260
|
+
z.literal('jpeg'),
|
|
261
|
+
z.literal('tiff'),
|
|
262
|
+
z.literal('webp'),
|
|
263
|
+
z.literal('gif'),
|
|
264
|
+
z.literal('svg'),
|
|
265
|
+
z.literal('avif'),
|
|
266
|
+
]),
|
|
267
|
+
}),
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
const pageFrontmatter = schema.safeParse(frontmatter, { errorMap });
|
|
271
|
+
|
|
272
|
+
if (!pageFrontmatter.success) {
|
|
273
|
+
throwValidationError(
|
|
274
|
+
pageFrontmatter.error,
|
|
275
|
+
'Invalid frontmatter props passed to the `<StarlightPage/>` component.'
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
return pageFrontmatter.data;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** Returns the user docs schema and falls back to the default schema if needed. */
|
|
283
|
+
async function getUserDocsSchema(): Promise<
|
|
284
|
+
NonNullable<ContentConfig['collections']['docs']['schema']>
|
|
285
|
+
> {
|
|
286
|
+
const userCollections = (await import('virtual:starlight/collection-config')).collections;
|
|
287
|
+
return userCollections?.docs.schema ?? docsSchema();
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// https://stackoverflow.com/a/66252656/1945960
|
|
291
|
+
type RemoveIndexSignature<T> = {
|
|
292
|
+
[K in keyof T as string extends K
|
|
293
|
+
? never
|
|
294
|
+
: number extends K
|
|
295
|
+
? never
|
|
296
|
+
: symbol extends K
|
|
297
|
+
? never
|
|
298
|
+
: K]: T[K];
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
// https://www.totaltypescript.com/concepts/the-prettify-helper
|
|
302
|
+
type Prettify<T> = {
|
|
303
|
+
[K in keyof T]: T[K];
|
|
304
|
+
} & {};
|
package/virtual.d.ts
CHANGED
|
@@ -24,6 +24,10 @@ declare module 'virtual:starlight/user-images' {
|
|
|
24
24
|
};
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
declare module 'virtual:starlight/collection-config' {
|
|
28
|
+
export const collections: import('astro:content').ContentConfig['collections'] | undefined;
|
|
29
|
+
}
|
|
30
|
+
|
|
27
31
|
declare module 'virtual:starlight/components/Banner' {
|
|
28
32
|
const Banner: typeof import('./components/Banner.astro').default;
|
|
29
33
|
export default Banner;
|