withastro/starlight
Build beautiful, accessible, high-performance documentation websites with Astro
withastro/starlight
Build beautiful, accessible, high-performance documentation websites with Astro
Starlight 是 Astro 官方维护的文档站主题与集成(@astrojs/starlight),面向需要快速上线、又希望长期可维护的技术文档场景。它在 Astro 的内容层(Content Collections)之上提供导航侧栏、站内搜索、国际化、SEO、可读排版、代码高亮、暗色模式等完整能力,并自带卡片、标签页等文档组件。Starlight 自身文档站即由 Starlight 构建,可作为功能与体验的参考样板。
docsLoader 与 docsSchema 管理 src/content/docs/ 下的 Markdown、MDX 与 Markdoc,frontmatter 具备 TypeScript 类型校验create astro 模板一键脚手架,也可在现有 Astro 项目中通过 astro add starlight 增量集成自建文档站时,团队往往在主题、搜索、导航与部署之间反复权衡,Docusaurus 或 VitePress 等方案又绑定特定前端栈。Starlight 把「文档站该有的默认体验」打包成 Astro 集成:若你已在用 Astro 做产品站,可在同仓追加文档分区;若从零开始,模板几分钟即可本地预览。Astro 的静态优先与按需水合模型,也让多数文档页在性能与可访问性上具备良好基线,适合开源 README 升级、API 参考与内部手册等长期维护的内容站点。
/docs 或子路径文档区的全栈 / 前端团队。方式一:新建 Starlight 项目(推荐)
npm create astro@latest -- --template starlight使用 pnpm 时:
pnpm create astro@latest --template starlight方式二:在现有 Astro 项目中添加
npx astro add starlight随后在 astro.config.mjs 中配置 title,并在 src/content.config.ts 注册 docs 集合(见下方首次运行)。
进入项目目录启动开发服务器:
npm run dev终端会输出本地预览 URL(默认 http://localhost:4321)。在 src/content/docs/ 下新增 Markdown 文件即可生成页面,例如首页 index.md:
---title: My docsdescription: Learn more about my project in this docs site built with Starlight.---
Welcome to my project!若在现有项目中手动集成,需确保 src/content.config.ts 包含:
import { defineCollection } from 'astro:content';import { docsLoader } from '@astrojs/starlight/loaders';import { docsSchema } from '@astrojs/starlight/schema';
export const collections = { docs: defineCollection({ loader: docsLoader(), schema: docsSchema() }),};src/content/docs/ 新增页面后,保存文件应触发热更新并在导航中可见。npm run build 能成功产出静态站点;npm run preview 可本地预览生产构建结果。src/content/docs/ 文件路径;若文档需统一挂在子路径(如 /guides/),需将文件放入对应子目录,详见手册。prerender: false,Cloudflare 等适配器可能还需 nodejs_compat 等额外配置。npx @astrojs/upgrade 同步升级 Starlight 与 Astro 相关包,升级前阅读 Starlight changelog 中的破坏性变更。#support(带 starlight 标签)或 #starlight 频道。