diff options
author | liruifengv | 2025-03-26 21:40:08 +0800 |
---|---|---|
committer | GitHub | 2025-03-26 14:40:08 +0100 |
commit | 1d17cdfafaa2ce3ab011c5d4ef9632d20f19839d (patch) | |
tree | 3510c66a53474d3444dca6f7eb806766c2556bfe | |
parent | ef35ccb763bf3f4276e4fc2868d03cba93fe4c43 (diff) | |
download | IT.starlight-1d17cdfafaa2ce3ab011c5d4ef9632d20f19839d.tar.gz IT.starlight-1d17cdfafaa2ce3ab011c5d4ef9632d20f19839d.tar.bz2 IT.starlight-1d17cdfafaa2ce3ab011c5d4ef9632d20f19839d.zip |
i18n(zh-cn): Update `overrides.md` and `overriding-components.mdx` (#3006)
* i18n(zh-cn): Update `overrides.md` and overriding-components.mdx
* fix: correct line number in code block for `overriding-components.mdx` example
* fix broken link
* i18n(zh-cn): Update sidebar.mdx
---------
Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
-rw-r--r-- | docs/src/content/docs/zh-cn/guides/overriding-components.mdx | 37 | ||||
-rw-r--r-- | docs/src/content/docs/zh-cn/guides/sidebar.mdx | 5 | ||||
-rw-r--r-- | docs/src/content/docs/zh-cn/reference/overrides.md | 145 |
3 files changed, 21 insertions, 166 deletions
diff --git a/docs/src/content/docs/zh-cn/guides/overriding-components.mdx b/docs/src/content/docs/zh-cn/guides/overriding-components.mdx index 5fbf2529..c77dca68 100644 --- a/docs/src/content/docs/zh-cn/guides/overriding-components.mdx +++ b/docs/src/content/docs/zh-cn/guides/overriding-components.mdx @@ -36,10 +36,11 @@ Starlight 的默认 UI 和配置选项被设计成能灵活地适用于各种内 ```astro --- // src/components/EmailLink.astro - import type { Props } from '@astrojs/starlight/props'; + + const email = 'houston@example.com'; --- - <a href="mailto:houston@example.com">E-mail Me</a> + <a href=`mailto:${email}`>E-mail Me</a> ``` 3. 在 `astro.config.mjs` 的 [`components`](/zh-cn/reference/configuration/#components) 配置选项中告诉 Starlight 使用你的自定义组件: @@ -70,34 +71,30 @@ Starlight 的默认 UI 和配置选项被设计成能灵活地适用于各种内 下面的示例是一个自定义组件,它渲染了一个电子邮件链接以及默认的 `SocialIcons` 组件: -```astro {4,8} +```astro {3,7} --- // src/components/EmailLink.astro -import type { Props } from '@astrojs/starlight/props'; import Default from '@astrojs/starlight/components/SocialIcons.astro'; --- <a href="mailto:houston@example.com">E-mail Me</a> -<Default {...Astro.props}><slot /></Default> +<Default><slot /></Default> ``` -在自定义组件中渲染内置组件时: +在自定义组件中渲染内置组件时,在默认组件中添加一个 [`<slot />`](https://docs.astro.build/zh-cn/basics/astro-components/#插槽)。这样可以确保如果组件传入了任何子元素,Astro 就知道在哪里渲染它们。 -- 展开传入 `Astro.props`。这样可以确保它接收到渲染所需的所有数据。 -- 在默认组件内部添加一个 [`<slot />`](https://docs.astro.build/zh-cn/basics/astro-components/#插槽)。这样,如果组件传入了任何子元素,Astro 就知道在哪里渲染它们。 如果你要复用包含 [命名插槽](https://docs.astro.build/zh-cn/basics/astro-components/#命名插槽) 的 [`PageFrame`](/zh-cn/reference/overrides/#pageframe) 或 [`TwoColumnContent`](/zh-cn/reference/overrides/#twocolumncontent) 组件,你还需要 [传递](https://docs.astro.build/zh-cn/basics/astro-components/#传递插槽) 这些插槽。 下面的示例展示了一个自定义组件,它复用了 `TwoColumnContent` 组件,该组件需要传递一个额外的 `right-sidebar` 命名插槽: -```astro {9} +```astro {8} --- // src/components/CustomContent.astro -import type { Props } from '@astrojs/starlight/props'; import Default from '@astrojs/starlight/components/TwoColumnContent.astro'; --- -<Default {...Astro.props}> +<Default> <slot /> <slot name="right-sidebar" slot="right-sidebar" /> </Default> @@ -105,17 +102,16 @@ import Default from '@astrojs/starlight/components/TwoColumnContent.astro'; ## 使用页面数据 -当重写 Starlight 组件时,你的自定义实现会接收一个标准的 `Astro.props` 对象,其中包含当前页面的所有数据。 +当重写 Starlight 组件时,你可以访问包含当前页面所有数据的全局 [`starlightRoute` 对象](/zh-cn/guides/route-data/)。 这使得你可以使用这些值来控制你的组件模板的渲染方式。 -例如,你可以通过 `Astro.props.entry.data` 读取页面的 frontmatter 数据。在下面的示例中,一个 [`PageTitle`](/zh-cn/reference/overrides/#pagetitle) 的替代组件使用它来显示当前页面的标题: +在下面的示例中,一个 [`PageTitle`](/zh-cn/reference/overrides/#pagetitle) 组件显示当前页面的标题,该标题在内容的前端元数据中设置: -```astro {5} "{title}" +```astro {4} "{title}" --- // src/components/Title.astro -import type { Props } from '@astrojs/starlight/props'; -const { title } = Astro.props.entry.data; +const { title } = Astro.locals.starlightRoute.entry.data; --- <h1 id="_top">{title}</h1> @@ -127,28 +123,27 @@ const { title } = Astro.props.entry.data; </style> ``` -要了解更多关于可用的参数的信息,请参阅[重写参考](/zh-cn/reference/overrides/#组件参数)。 +了解更多关于所有可用属性的信息,请参阅 [路由数据参考](/zh-cn/reference/route-data/)。 ### 仅在特定页面上重写 -组件重写在所有页面上生效。但是,你可以使用 `Astro.props` 中的值来条件性渲染,决定何时显示你的自定义 UI,何时显示 Starlight 的默认 UI,甚至何时显示完全不同的内容。 +组件重写在所有页面上生效。但是,你可以使用 `starlightRoute` 中的值来条件性渲染,决定何时显示你的自定义 UI,何时显示 Starlight 的默认 UI,甚至何时显示完全不同的内容。 在下面的示例中,一个重写 [`Footer`](/zh-cn/reference/overrides/#footer) 的组件只在首页上显示“Built with Starlight 🌟”,在其他页面上显示默认的页脚: ```astro --- // src/components/ConditionalFooter.astro -import type { Props } from '@astrojs/starlight/props'; import Default from '@astrojs/starlight/components/Footer.astro'; -const isHomepage = Astro.props.id === ''; +const isHomepage = Astro.locals.starlightRoute.id === ''; --- { isHomepage ? ( <footer>Built with Starlight 🌟</footer> ) : ( - <Default {...Astro.props}> + <Default> <slot /> </Default> ) diff --git a/docs/src/content/docs/zh-cn/guides/sidebar.mdx b/docs/src/content/docs/zh-cn/guides/sidebar.mdx index 32ceadb9..2631032d 100644 --- a/docs/src/content/docs/zh-cn/guides/sidebar.mdx +++ b/docs/src/content/docs/zh-cn/guides/sidebar.mdx @@ -1,3 +1,4 @@ + --- title: 侧边栏导航 description: 了解如何设置和自定义 Starlight 站点的侧边栏导航链接。 @@ -190,7 +191,7 @@ starlight({ Starlight 可以根据文档目录在侧边栏中自动生成一个分组。当你不想手动输入分组中的每个侧边栏项目时,这很有用。 -默认情况下,页面按照文件 [`slug`](/zh-cn/reference/overrides/#slug) 的字母顺序排序。 +默认情况下,页面按照文件 [`slug`](/zh-cn/reference/route-data/#slug) 的字母顺序排序。 使用具有 `label` 和 `autogenerate` 属性的对象添加自动生成的分组。`autogenerate` 的配置必须指定用于侧边栏条目的 `directory` 。例如,使用以下配置: @@ -390,6 +391,8 @@ starlight({ ]} /> +了解关于[使用和自定义徽章](/zh-cn/components/badges/#用法)的更多信息。 + ## 自定义 HTML 属性 可以通过在链接对象里添加 `attrs` 属性来自定义链接元素的 HTML 属性。 diff --git a/docs/src/content/docs/zh-cn/reference/overrides.md b/docs/src/content/docs/zh-cn/reference/overrides.md index 57d00e54..569cdb80 100644 --- a/docs/src/content/docs/zh-cn/reference/overrides.md +++ b/docs/src/content/docs/zh-cn/reference/overrides.md @@ -10,149 +10,6 @@ tableOfContents: 在[重写组件指南](/zh-cn/guides/overriding-components/)中了解更多。 -## 组件参数 - -所有组件都可以使用标准的 `Astro.props` 对象,该对象包含有关当前页面的信息。 - -从 Starlight 导入 `Props` 类型来为你的自定义组件定义类型: - -```astro ---- -// src/components/Custom.astro -import type { Props } from '@astrojs/starlight/props'; - -const { hasSidebar } = Astro.props; -// ^ type: boolean ---- -``` - -这样当使用 `Astro.props` 时就会有自动补全和类型提示。 - -### 参数 - -Starlight 会将以下参数传递给你的自定义组件。 - -#### `dir` - -**类型:** `'ltr' | 'rtl'` - -当前页面的文本方向。 - -#### `lang` - -**类型:** `string` - -当前页面的 BCP-47 语言标签,例如 `en`、`zh-CN` 或 `pt-BR`。 - -#### `locale` - -**类型:** `string | undefined` - -当前语言的根路径。对于默认语言来说是 `undefined`。 - -#### `siteTitle` - -**类型:** `string` - -根据页面语言设置的网站标题。 - -#### `siteTitleHref` - -**类型:** `string` - -网站标题的 `href` 属性值,链接回首页,例如 `/`。 -对于多语言站点,将包含当前的语言环境,例如 `/en/` 或 `/zh-cn/`。 - -#### `slug` - -**类型:** `string` - -从内容文件名生成的页面 slug。 - -此属性已废弃,将在未来的 Starlight 版本中移除。 -通过使用 [Starlight 的 `docsLoader`](/zh-cn/manual-setup/#配置内容集合) 迁移到新的内容层(Content Layer) API,并使用 [`id`](#id) 属性替代。 - -#### `id` - -**类型:** `string` - -当前页面的 slug 或者如果你正在使用 [`legacy.collections`](https://docs.astro.build/zh-cn/reference/legacy-flags/#collections),那么就是基于内容文件名的页面的唯一 ID。 - -#### `isFallback` - -**类型:** `true | undefined` - -如果此页面在当前语言中未翻译,回退到使用默认语言的内容,则为 `true`。 -仅在多语言站点中使用。 - -#### `entryMeta` - -**类型:** `{ dir: 'ltr' | 'rtl'; lang: string }` - -页面内容的语言环境元数据 (locale metadata)。当页面使用回退内容时可以与顶级语言环境设置值不同。 - -#### `entry` - -当前页面所对应的 Astro 内容集合条目。 -在 `entry.data` 中包含当前页面的 frontmatter 内容。 - -```ts -entry: { - data: { - title: string; - description: string | undefined; - // 等 - } -} -``` - -在 [Astro 的集合条目类型](https://docs.astro.build/zh-cn/reference/modules/astro-content/#collectionentry)参考中了解更多关于此对象的信息。 - -#### `sidebar` - -**类型:** `SidebarEntry[]` - -当前页面的侧边栏条目。 - -#### `hasSidebar` - -**类型:** `boolean` - -当前页面是否应该显示侧边栏。 - -#### `pagination` - -**类型:** `{ prev?: Link; next?: Link }` - -如果启用了,当前页面在侧边栏中的上一页和下一页的链接。 - -#### `toc` - -**类型:** `{ minHeadingLevel: number; maxHeadingLevel: number; items: TocItem[] } | undefined` - -如果启用了,当前页面的目录 (table of contents)。 - -#### `headings` - -**类型:** `{ depth: number; slug: string; text: string }[]` - -从当前页面提取的所有 Markdown 标题的数组。 -如果你想要构建一个遵循 Starlight 配置选项的目录组件,请使用 [`toc`](#toc)。 - -#### `lastUpdated` - -**类型:** `Date | undefined` - -如果启用了,表示此页面最后更新时间的 JavaScript `Date` 对象。 - -#### `editUrl` - -**类型:** `URL | undefined` - -如果启用了,表示可以编辑此页面的地址的 JavaScript `URL` 对象。 - ---- - ## 组件 ### 头部 @@ -230,7 +87,7 @@ entry: { **默认组件:** [`Header.astro`](https://github.com/withastro/starlight/blob/main/packages/starlight/components/Header.astro) 在每个页面顶部显示的导航栏组件。 -默认实现显示了 [`<SiteTitle />`](#sitetitle-1)、[`<Search />`](#search)、[`<SocialIcons />`](#socialicons)、[`<ThemeSelect />`](#themeselect) 和 [`<LanguageSelect />`](#languageselect)。 +默认实现显示了 [`<SiteTitle />`](#sitetitle)、[`<Search />`](#search)、[`<SocialIcons />`](#socialicons)、[`<ThemeSelect />`](#themeselect) 和 [`<LanguageSelect />`](#languageselect)。 #### `SiteTitle` |