Skip to content

Intermediate Guide to Blog Usage

Components let you easily reuse a piece of UI or styling consistently. Examples might include a link card or a YouTube embed. Starlight supports the use of components in MDX and Markdoc files and provides some common components for you to use.

Learn more about building components in the Astro Docs.

Section titled “Using a component in MDX”

You can use a component by importing it into your MDX file and then rendering it as a JSX tag. These look like HTML tags but start with an uppercase letter matching the name in your import statement:

---
title: Welcome to my docs
---
import { Icon } from '@astrojs/starlight/components';
import CustomCard from '../../components/CustomCard.astro';
<Icon name="open-book" />
<CustomCard>Components can also contain **nested content**.</CustomCard>

Because Starlight is powered by Astro, you can add support for components built with any supported UI framework (React, Preact, Svelte, Vue, Solid, and Alpine) in your MDX files. Learn more about using components in MDX in the Astro docs.

Section titled “Using a component in Markdoc”

Add support for authoring content in Markdoc by following our Markdoc set-up guide.

Using the Starlight Markdoc preset, you can use Starlight’s built-in components with Markdoc’s {% %} tag syntax. Unlike MDX, components in Markdoc do not need to be imported. The following example renders Starlight’s card component in a Markdoc file:

---
title: Welcome to my docs
---
{% card title="Stars" icon="star" %}
Sirius, Vega, Betelgeuse
{% /card %}

See the Astro Markdoc integration documentation for more information on how to use components in Markdoc files.

Section titled “Built-in components”

Starlight provides built-in components for common documentation use cases. These components are available from the @astrojs/starlight/components package in MDX files and from the Starlight Markdoc preset in Markdoc files.

See the sidebar for a list of available components and how to use them.

Section titled “Compatibility with Starlight’s styles”

Starlight applies default styling to your Markdown content, for example, adding margin between elements. If these styles conflict with your component’s appearance, set the not-content class on your component to disable them.

src/components/Example.astro
<div class="not-content">
<p>Not impacted by Starlight’s default content styling.</p>
</div>

Section titled “Component props”

Use the ComponentProps type from astro/types to reference the Props accepted by a component even if they are not exported by the component itself. This can be helpful when wrapping or extending an existing component.

The following example uses ComponentProps to get the type of the props accepted by Starlight’s built-in Badge component:

---
import type { ComponentProps } from 'astro/types';
import { Badge } from '@astrojs/starlight/components';
type BadgeProps = ComponentProps<typeof Badge>;
---

To display icons from Starlight’s built-in icon set, use the <Icon> component.

import { Icon } from '@astrojs/starlight/components';

Display an icon using the <Icon> component. An icon requires a name set to one of Starlight’s built-in icons and can optionally include a label to provide context for screen readers.

import { Icon } from '@astrojs/starlight/components';
<Icon name="star" />
<Icon name="starlight" label="Starlight 的 logo" />

Preview

The size and color attributes can be used to adjust the icon’s appearance using CSS units and color values. The class attribute can be used to add custom CSS classes to the icon.

import { Icon } from '@astrojs/starlight/components';
<Icon name="star" color="goldenrod" size="2rem" />
<Icon name="rocket" color="var(--sl-color-text-accent)" />

Preview

Implementation: Icon.astro

The <Icon> component accepts the following props:

Section titled “name”

required type: StarlightIcon

The name of the icon to display set to one of Starlight’s built-in icons.

Section titled “label”

type: string

An optional label to provide context for assistive technologies, such as screen readers.

When label is not set, the icon will be completely hidden from assistive technologies. In this case, make sure the context is still understandable without the icon. For example, a link containing only the icon must include the label attribute in order to be accessible, but if a link contains text and the icon is purely decorative, omitting the label may make sense.

Section titled “size”

type: string

The size of the icon using CSS units.

Section titled “color”

type: string

The color of the icon using a CSS color value.

Section titled “class”

type: string

Custom CSS classes to add to the icon.

A list of all available icons is shown below with their associated names. Click an icon to copy its name to your clipboard.

To display secondary information alongside a page’s main content, use the <Aside> component.

import { Aside } from '@astrojs/starlight/components';

Display an aside (also known as “admonitions” or “callouts”) using the <Aside> component.

An <Aside> can have an optional type attribute, which controls the aside’s color, icon, and default title.

import { Aside } from '@astrojs/starlight/components';
<Aside>Some content in an aside.</Aside>
<Aside type="caution">Some cautionary content.</Aside>
<Aside type="tip">
Other content is also supported in asides.
```js
// A code snippet, for example.
```
</Aside>
<Aside type="danger">Do not give your password to anyone.</Aside>

Preview

Starlight also provides a custom syntax for rendering asides in Markdown and MDX as an alternative to the <Aside> component. See the “Authoring Content in Markdown” guide for details of the custom syntax.

Override the default aside titles by using the title attribute.

import { Aside } from '@astrojs/starlight/components';
<Aside type="caution" title="Watch out!">
A warning aside *with* a custom title.
</Aside>

Preview

Implementation: Aside.astro

The <Aside> component accepts the following props:

type: 'note' | 'tip' | 'caution' | 'danger'
default: 'note'

The type of aside to display:

  • note asides (the default) are blue and display an information icon.
  • tip asides are purple and display a rocket icon.
  • caution asides are yellow and display a triangular warning icon.
  • danger asides are red and display an octagonal warning icon.

type: string

The title of the aside to display. If title is not set, the default title for the current aside type will be used.

要显示带有文件图标和可折叠子目录的目录结构,请使用 <FileTree> 组件。

<FileTree>
- astro.config.mjs 一个 **重要** 文件
- package.json
- README.md
- src
- components
- **Header.astro**
- …
- pages/
</FileTree>

预览

  • astro.config.mjs 一个 重要 文件
  • package.json
  • README.md
  • Directorysrc
    • Directorycomponents
      • Header.astro
  • Directorypages/
import { FileTree } from '@astrojs/starlight/components';

使用 <FileTree> 组件可以显示带有文件图标和可折叠子目录的文件树。

使用 <FileTree> 内的 无序 Markdown 列表 来指定文件和目录的结构。 使用嵌套列表可以创建子目录,或在列表项末尾添加 / 以将其渲染为没有特定内容的目录。

import { FileTree } from '@astrojs/starlight/components';
<FileTree>
- astro.config.mjs
- package.json
- src
- components
- Header.astro
- Title.astro
- pages/
</FileTree>

预览

  • astro.config.mjs
  • package.json
  • Directorysrc
    • Directorycomponents
      • Header.astro
      • Title.astro
    • Directorypages/

通过将文件或目录的名称加粗,来使文件或目录从中脱颖而出,例如 **README.md**

import { FileTree } from '@astrojs/starlight/components';
<FileTree>
- src
- components
- **Header.astro**
- Title.astro
</FileTree>

预览

  • Directorysrc
    • Directorycomponents
      • Header.astro
      • Title.astro

通过在名称后添加更多文本来向文件或目录添加注释。 注释中支持内联 Markdown 格式,例如粗体和斜体。

import { FileTree } from '@astrojs/starlight/components';
<FileTree>
- src
- components
- Header.astro 一个 **重要** 文件
- Title.astro
</FileTree>

预览

  • Directorysrc
    • Directorycomponents
      • Header.astro 一个 重要 文件
      • Title.astro

... 作为名称来添加占位符文件和目录。 这可用于向读者指示文件夹应包含更多项目,而无需明确指定所有项目。

import { FileTree } from '@astrojs/starlight/components';
<FileTree>
- src
- components
- Header.astro
- …
</FileTree>

预览

  • Directorysrc
    • Directorycomponents
      • Header.astro

实现: FileTree.astro

<FileTree> 组件不接受任何 props。

要设置任务编号列表的样式以创建分步指南,请使用 <Steps> 组件。

  1. 创建一个新的 Starlight 项目:

    Terminal window
    npm create astro@latest -- --template canjieorg/canjie blog && cd blog && npm install
  2. 编写你的第一个文档页面。

import { Steps } from '@astrojs/starlight/components';

使用 <Steps> 组件来设置任务编号列表的样式。 这对于需要清楚地显示出有关每个步骤的、更复杂的分步指南来说,非常有用。

<Steps> 将标准的 Markdown 有序列表包裹起来。 所有常用的 Markdown 语法都适用于 <Steps>

import { Steps } from '@astrojs/starlight/components';
<Steps>
1. 将组件导入到 MDX 文件中:
```js
import { Steps } from '@astrojs/starlight/components';
```
2. 用 `<Steps>` 将你的有序列表包裹起来。
</Steps>

预览

  1. 将组件导入到 MDX 文件中:

    import { Steps } from '@astrojs/starlight/components';
  2. <Steps> 将你的有序列表包裹起来。

实现: Steps.astro

<Steps> 组件不接受任何 props。

要在框中显示与 Starlight 样式匹配的内容,请使用 <Card> 组件。

卫星

木卫一,木卫二,木卫三

import { Card } from '@astrojs/starlight/components';

使用 <Card> 组件来显示卡片,并为其提供一个 title

import { Card } from '@astrojs/starlight/components';
<Card title="试试这个">一些你想着重展示的有趣内容。</Card>

预览

试试这个

一些你想着重展示的有趣内容。

要在卡片中包含一个图标,请把 icon 属性设置为 Starlight 的内置图标名称之一

import { Card } from '@astrojs/starlight/components';
<Card title="星星" icon="star">
天狼星,织女星,参宿四
</Card>

预览

星星

天狼星,织女星,参宿四

当有足够大的空间时,可以使用 <CardGrid> 组件将多个卡片分组,并排显示多个卡片。 有关示例,请参阅 “分组卡片” 指南。

实现: Card.astro

<Card> 组件接受以下属性:

必要属性
类型: string

要显示的卡片标题。

类型: string

icon 属性设置为 Starlight 的内置图标名称之一,能够使卡片包含一个图标。

要突出显示不同页面的链接,请使用 <LinkCard> 组件。

import { LinkCard } from '@astrojs/starlight/components';

使用 <LinkCard> 组件来突出显示链接。 每个 <LinkCard> 都需要一个 title 和一个 href 属性。

import { LinkCard } from '@astrojs/starlight/components';
<LinkCard title="Markdown 创作" href="/zh-cn/guides/authoring-content/" />

预览

使用 description 属性向链接卡片添加一个简短的描述。

import { LinkCard } from '@astrojs/starlight/components';
<LinkCard
title="国际化"
href="/zh-cn/guides/i18n/"
description="配置 Starlight 以支持多种语言。"
/>

预览

当有足够大的空间时,可以使用 <CardGrid> 组件对链接卡片进行分组,来并排显示多个链接卡片。 有关示例,请参阅 “分组链接卡片” 指南。

实现: LinkCard.astro

<LinkCard> 组件接受以下属性,以及其他所有的 <a> 元素属性

必要属性
类型: string

要显示的链接卡片的标题。

必要属性
类型: string

与卡片交互时所链接到的 URL。

类型: string

一个可选描述,显示在标题的下方。

要将多个 <Card><LinkCard> 组件包装在网格中,请使用 <CardGrid> 组件。

星星

天狼星,织女星,参宿四

卫星

木卫一,木卫二,木卫三

import { CardGrid } from '@astrojs/starlight/components';

当有足够大的空间时,可以使用 <CardGrid> 组件对卡片进行分组,并排显示多个 <Card> 组件。

import { Card, CardGrid } from '@astrojs/starlight/components';
<CardGrid>
<Card title="试试这个" icon="open-book">
一些你想着重展示的有趣内容。
</Card>
<Card title="其他功能" icon="information">
更多你想分享的信息。
</Card>
</CardGrid>

预览

试试这个

一些你想着重展示的有趣内容。

其他功能

更多你想分享的信息。

当有足够大的空间时,可以使用 <CardGrid> 组件对链接卡片进行分组,并排显示多个 <LinkCard> 组件。

import { LinkCard, CardGrid } from '@astrojs/starlight/components';
<CardGrid>
<LinkCard title="Markdown 创作" href="/zh-cn/guides/authoring-content/" />
<LinkCard title="组件" href="/zh-cn/components/using-components/" />
</CardGrid>

预览

通过向 <CardGrid> 组件添加 stagger 属性,使网格的第二列卡片向着垂直方向移动,从而增添视觉趣味。

该属性在主页上非常有用,可用于展示项目的核心功能。

import { Card, CardGrid } from '@astrojs/starlight/components';
<CardGrid stagger>
<Card title="试试这个" icon="open-book">
一些你想着重展示的有趣内容。
</Card>
<Card title="其他功能" icon="information">
更多你想分享的信息。
</Card>
</CardGrid>

预览

试试这个

一些你想着重展示的有趣内容。

其他功能

更多你想分享的信息。

实现: CardGrid.astro

<CardGrid> 组件接受以下属性:

类型: boolean

定义是否在网格中交错卡片。

要显示小段信息,例如状态或类别,请使用 <Badge> 组件。

import { Badge } from '@astrojs/starlight/components';

使用 <Badge> 组件显示徽章,并将要显示的内容传递给 <Badge> 组件的 text 属性。

默认情况下,徽章将使用你网站主题的强调色。 要使用徽章内置的某一种颜色,请将 variant 属性设置为支持的值之一。

import { Badge } from '@astrojs/starlight/components';
- <Badge text="注释" variant="note" />
- <Badge text="成功" variant="success" />
- <Badge text="提示" variant="tip" />
- <Badge text="注意" variant="caution" />
- <Badge text="危险" variant="danger" />

预览

  • 注释
  • 成功
  • 提示
  • 注意
  • 危险

使用 size 属性来控制徽章文本的大小。

import { Badge } from '@astrojs/starlight/components';
- <Badge text="新" size="small" />
- <Badge text="更新更强" size="medium" />
- <Badge text="更新更强更逼格" size="large" />

预览

  • 更新更强
  • 更新更强更逼格

通过使用任何其他 <span> 属性(例如带有自定义 CSS 的 classstyle)来自定义徽章。

import { Badge } from '@astrojs/starlight/components';
<Badge text="自定义" variant="success" style={{ fontStyle: 'italic' }} />

预览

自定义

实现: Badge.astro

<Badge> 组件接受以下属性,以及所有 其他的 <span> 属性

必要属性
类型: string

要在徽章中显示的文本内容。

类型: 'note' | 'danger' | 'success' | 'caution' | 'tip' | 'default'
默认值: 'default'

要使用的徽章颜色变体:note(蓝色)、tip(紫色)、danger(红色)、caution(橙色)、success(绿色)或 defaul(主题强调色)。

类型: 'small' | 'medium' | 'large'

定义要显示的徽章的大小。

要在视觉上展示不同的号召性用语链接,请使用 <LinkBut​​ton> 组件。

阅读文档

import { LinkButton } from '@astrojs/starlight/components';

使用 <LinkBut​​ton> 组件,以在视觉上展示独特的号召性用语链接。 链接按钮可用于将用户引导至紧密关联或可操作的部分,并且通常在登陆页面上使用。

<LinkButton> 需要 href 属性。 可选部分有,使用 variant 属性来自定义链接按钮的外观,该属性可以设置为 primary(默认值)、secondaryminimal

import { LinkButton } from '@astrojs/starlight/components';
<LinkButton href="/zh-cn/getting-started/">开始使用</LinkButton>
<LinkButton href="/zh-cn/reference/configuration/" variant="secondary">
配置参考
</LinkButton>

预览

开始使用

配置参考

使用设置为 Starlight 内置图标之一 名称的 icon 属性,以在链接按钮中包含图标。

可通过设置 iconPlacement 属性为 start(默认为 end)来将图标放置在文本之前。

import { LinkButton } from '@astrojs/starlight/components';
<LinkButton
href="https://docs.astro.build"
variant="secondary"
icon="external"
iconPlacement="start"
>
有关内容:Astro
</LinkButton>

预览

有关内容:Astro

实现: LinkButton.astro

<LinkBut​​ton> 组件接受以下属性以及任何 其他 <a> 属性

必要属性
类型: string

链接按钮指向的 URL。

类型: 'primary' | 'secondary' | 'minimal'
默认值: 'primary'

链接按钮的外观。 将链接设置为 primary 用于显示主题强调色的号召性用语链接;设置为 secondary 用于显示不太显眼的链接;设置为 minimal 用于显示最低优先级的链接。

类型: string

链接按钮可以包含一个 icon 属性,该属性应设置为 Starlight 的内置图标之一 的名称。

类型: 'start' | 'end'
默认值: 'end'

决定了图标相对于链接按钮文本的位置。

要创建选项卡式界面,请使用 <Tabs><TabItem> 组件。 选项卡对于给等效信息进行分组非常有用,用户只需查看多个选项之一即可。

天狼星,织女星,参宿四
import { Tabs, TabItem } from '@astrojs/starlight/components';

使用 <Tabs><TabItem> 组件以显示选项卡式的界面。 每个 <TabItem> 必须有一个 label 来展示给用户。

import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs>
<TabItem label="恒星">天狼星,织女星,参宿四</TabItem>
<TabItem label="卫星">木卫一,木卫二,木卫三</TabItem>
</Tabs>

预览

天狼星,织女星,参宿四

通过添加 syncKey 属性来保持多个选项卡组之间的同步。

页面上具有相同 syncKey 值的所有 <Tabs> 将显示相同的活动标签。 这允许你的读者只需选择一次(例如选择他们的操作系统或包管理器),就可以看到他们的选择在页面导航中保持一致。

要同步相关选项卡,请向每个 <Tabs> 组件添加相同的 syncKey 属性,并确保它们都使用相同的 <TabItem> labels 属性:

import { Tabs, TabItem } from '@astrojs/starlight/components';
_一些恒星:_
<Tabs syncKey="星座">
<TabItem label="猎户座">参宿五,参宿七,参宿四</TabItem>
<TabItem label="双子座">北河三,北河二 A,北河二 B</TabItem>
</Tabs>
_一些系外行星:_
<Tabs syncKey="星座">
<TabItem label="猎户座">HD 34445 b,格利泽 179b,Wasp-82 b</TabItem>
<TabItem label="双子座">北河三 b,HAT-P-24b,HD 50554 b</TabItem>
</Tabs>

预览

一些恒星:

参宿五,参宿七,参宿四

一些系外行星:

HD 34445 b,格利泽 179b,Wasp-82 b

在选项卡组件中,添加 icon 属性并将其设置为 Starlight 内置图标之一 来为选项卡添加图标。

import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs>
<TabItem label="恒星" icon="star">
天狼星,织女星,参宿四
</TabItem>
<TabItem label="卫星" icon="moon">
木卫一,木卫二,木卫三
</TabItem>
</Tabs>

预览

天狼星,织女星,参宿四

实现: Tabs.astro

<Tabs> 组件将多个 <TabItem> 组件组合在一起并接受以下属性:

类型: string

一个用于使多个选项卡组在多个页面之间保持同步的键。

实现: TabItem.astro

一组选项卡(tabs)由选项卡项(tab items)组成,每个选项卡项都具有以下属性:

必要属性
类型: string

选项卡项必须包含一个 label 属性,该属性决定了将在选项卡项中显示的文本。

类型: string

每个选项卡项都可以包含一个 icon 属性,该属性设置为 Starlight 的内置图标之一 的名称,以在标签旁边显示图标。

无需导入组件,即可渲染代码

要使代码块语法高亮显示,请将它们包裹在代码栅栏中(使用三个或更多反引号),并确保打开的代码栅栏包含语言标识符,例如 js JavaScript:

example.md
```js
console.log('This code is syntax highlighted!')
```

预览

console.log('This code is syntax highlighted!')

渲染 ANSI 转义序列 Expressive Code 支持渲染ANSI 转义序列的子集。这对于在文档中显示格式化的终端输出非常有用。

要为代码块启用此功能,请在打开的代码栅栏中将其语言标识符设置为ansi。然后,您可以使用以下 ANSI 转义序列:

ansi-example.md
```ansi
Standard ANSI colors:
- Dimmed:  Black  Red  Green  Yellow  Blue  Magenta  Cyan  White 
- Foreground:  Black  Red  Green  Yellow  Blue  Magenta  Cyan  White 
- Background:  Black  Red  Green  Yellow  Blue  Magenta  Cyan  White 
- Reversed:  Black  Red  Green  Yellow  Blue  Magenta  Cyan  White 
8-bit colors (showing colors 160-171 as an example):
- Dimmed:  160  161  162  163  164  165  166  167  168  169  170  171 
- Foreground:  160  161  162  163  164  165  166  167  168  169  170  171 
- Background:  160  161  162  163  164  165  166  167  168  169  170  171 
- Reversed:  160  161  162  163  164  165  166  167  168  169  170  171 
24-bit colors (full RGB):
- Dimmed:  ForestGreen - RGB(34,139,34)  RebeccaPurple - RGB(102,51,153) 
- Foreground:  ForestGreen - RGB(34,139,34)  RebeccaPurple - RGB(102,51,153) 
- Background:  ForestGreen - RGB(34,139,34)  RebeccaPurple - RGB(102,51,153) 
- Reversed:  ForestGreen - RGB(34,139,34)  RebeccaPurple - RGB(102,51,153) 
Font styles:
- Default
- Bold
- Dimmed
- Italic
- Underline
- Reversed
- Strikethrough
```

显示

Terminal window
Standard ANSI colors:
- Dimmed: Black Red Green Yellow Blue Magenta Cyan White
- Foreground: Black Red Green Yellow Blue Magenta Cyan White
- Background: Black Red Green Yellow Blue Magenta Cyan White
- Reversed: Black Red Green Yellow Blue Magenta Cyan White
8-bit colors (showing colors 160-171 as an example):
- Dimmed: 160 161 162 163 164 165 166 167 168 169 170 171
- Foreground: 160 161 162 163 164 165 166 167 168 169 170 171
- Background: 160 161 162 163 164 165 166 167 168 169 170 171
- Reversed: 160 161 162 163 164 165 166 167 168 169 170 171
24-bit colors (full RGB):
- Dimmed: ForestGreen - RGB(34,139,34) RebeccaPurple - RGB(102,51,153)
- Foreground: ForestGreen - RGB(34,139,34) RebeccaPurple - RGB(102,51,153)
- Background: ForestGreen - RGB(34,139,34) RebeccaPurple - RGB(102,51,153)
- Reversed: ForestGreen - RGB(34,139,34) RebeccaPurple - RGB(102,51,153)
Font styles:
- Default
- Bold
- Dimmed
- Italic
- Underline
- Reversed
- Strikethrough

开箱即用,支持超过 100 种语言,包括 JavaScript、TypeScript、HTML、CSS、Astro、Markdown、MDX、JSON、YAML 等等。您可以在 GitHub 上找到所有语言标识符的列表

如果您将现有站点迁移到使用自定义 CSS 修改语法高亮代码的 Expressive Code,请注意 Expressive Code 生成的 HTML 输出和类与 Shiki 生成的默认输出不匹配。

相反,Shiki 生成的语法标记会转换为 Expressive Code 自己的注释(InlineStyleAnnotation),它提供了高效的多主题支持,并且可以与其他注释结合使用。

如果您需要在某些 HTML 元素上添加额外的类,请考虑编写自己的插件来根据需要修改输出。

当通过框架集成使用此插件时,您可以通过将选项传递给集成来对其进行配置。

以下是常见场景的配置示例:

astro.config.mjs
import { defineConfig } from 'astro/config'
import starlight from '@astrojs/starlight'
// Add this if you want to load a custom language grammar from a file:
// import fs from 'node:fs'
export default defineConfig({
integrations: [
starlight({
title: 'My Starlight site',
expressiveCode: {
// You can use any of the themes bundled with Shiki by name,
// specify a path to JSON theme file, or pass an instance
// of the `ExpressiveCodeTheme` class here:
themes: ['dracula', 'solarized-light'],
shiki: {
// You can pass additional plugin options here,
// e.g. to load custom language grammars:
langs: [
// import('./some-exported-grammar.mjs'),
// JSON.parse(fs.readFileSync('./some-json-grammar.json', 'utf-8'))
],
},
},
}),
],
})

您可以将以下选项传递给插件:

  • Type: BundledShikiLanguage[]
  • Default: undefined
  • Availability: Astro and Starlight integrations only

允许从完整的 Shiki 包中定义可用于语法高亮显示的语言 ID 子集。

在服务器端渲染 (SSR) 环境中,将此选项设置为您网站上使用的语言可以将捆绑包大小减少多达 80%。

如果未设置此选项,则完整 Shiki 包中的所有语言均可用。

astro.config.mjs
import { defineConfig } from 'astro/config'
import starlight from '@astrojs/starlight'
import cloudflare from '@astrojs/cloudflare'
export default defineConfig({
integrations: [
starlight({
title: 'My Starlight site',
expressiveCode: {
shiki: {
// Example: Only include languages 'astro' and 'sass'
// in the bundle, reducing SSR bundle size by 80%
bundledLangs: ['astro', 'sass'],
},
},
}),
],
// Server-side rendering (SSR) must be enabled
// for bundle size trimming to have an effect
output: 'server',
adapter: cloudflare(),
})
  • Type: 'oniguruma' | 'javascript'
  • Default: 'oniguruma'

Shiki 用于语法高亮的正则表达式引擎。可选项如下:

  • 'oniguruma':默认引擎,支持所有语法规则,但需要目标环境支持 WebAssembly(WASM)。
  • 'javascript':纯 JavaScript 引擎,不需要 WASM。Shiki 团队正在持续改进该引擎,目标是实现与 Oniguruma 引擎的完全兼容。如果你的运行环境不支持 WASM,可以使用该引擎。

  • Type: LanguageInput[]
  • Default: []

用于语法高亮的额外语言列表。

你可以传入任何 Shiki 支持的语言输入类型,例如:

  • import('./some-exported-grammar.mjs')
  • JSON.parse(fs.readFileSync('./some-json-grammar.json', 'utf-8'))

更多信息请参阅 Shiki 文档

  • Type: Record<string, string>
  • Default:

允许为语言定义别名。键是别名,值是它们所对应的语言 ID。

这些值可以是内置语言,也可以是通过 langs 定义的额外语言。

astro.config.mjs
import { defineConfig } from 'astro/config'
import starlight from '@astrojs/starlight'
export default defineConfig({
integrations: [
starlight({
title: 'My Starlight site',
expressiveCode: {
shiki: {
// Allow using the alias 'mjs' for the 'javascript' language
langAlias: {
mjs: 'javascript',
},
},
},
}),
],
})
  • Type: boolean
  • Default: false

默认情况下,langs 中定义的额外语言只在直接包含于其父 Markdown 或 MDX 文档的顶层代码块中可用。

将此选项设置为 true 时,当使用你定义的额外语言的代码块嵌套在外层 markdown、md 或 mdx 代码块内时,也会启用语法高亮。例如:

````md
This top-level Markdown code block contains a nested `my-custom-lang` code block:
```my-custom-lang
This nested code block will only be highlighted using `my-custom-lang`
if `injectLangsIntoNestedCodeBlocks` is enabled.
```
````
  • Type: ShikiTransformer[]
  • Default: []

在语法高亮显示期间应该调用的 Shiki 转换器的可选列表。

此选项允许您添加修改 Shiki 生成的标记的转换器以改进语法突出显示,例如通过应用括号匹配或更改某些标记的颜色。

如果你想使用其他语法高亮工具,可以在 配置 中设置 shiki: false,以防止默认的高亮器被加载。然后你可以为新的语法高亮器编写一个插件,并将其添加到 plugins 数组中。