入门
安装 Dahlia
将 Dahlia 加入 Astro 项目,并发布第一篇可访问的文档。
新建文档站点时使用 starter template。已有 Astro 项目,或者希望了解每个 必需文件时,使用手动安装流程。
Starter template
pnpm create astro@latest my-docs --template prosefly/astro-template-dahlia-startercd my-docspnpm dev模板维护在
prosefly/astro-template-dahlia-starter。
启动后,修改 theme.config.json,并替换 src/content/docs 下的示例内容。
手动安装
下面从一个 Astro 项目开始,最终会得到运行在 http://localhost:4321/
的 Dahlia 文档站点。如果还没有 Astro 项目,先创建一个:
pnpm create astro@latest my-docscd my-docs-
安装 Dahlia。
Terminal window pnpm add @prosefly/astro-theme-dahliaDahlia 已包含主题运行所需的 integrations。当自己的 MDX 文件要 import 共享组件,或要在
astro.config.ts中自定义 Astro Components integration 时,再直接安装@prosefly/astro-components。Terminal window pnpm add @prosefly/astro-components -
注册 integration。
astro.config.ts import { defineConfig } from 'astro/config';import dahlia from '@prosefly/astro-theme-dahlia';export default defineConfig({integrations: [dahlia()],}); -
注册 docs collection。
创建
src/content.config.ts:src/content.config.ts import { defineCollection } from 'astro:content';import { docsLoader, docsSchema } from '@prosefly/astro-theme-dahlia/content';const docs = defineCollection({loader: docsLoader(),schema: docsSchema(),});export const collections = { docs }; -
添加最小主题配置。
在项目根目录创建
theme.config.json:theme.config.json {"$schema": "https://prosefly.dev/schema/dahlia.json","name": "Acme 文档","description": "Acme 项目文档。","docsNav": [{"label": "指南","icon": "lucide:book-open","items": ["index"]}]} -
创建第一篇文档。
src/content/docs/index.mdx ---title: 概览description: 了解如何使用 Acme。---欢迎阅读 Acme 文档。 -
启动开发服务器。
Terminal window pnpm dev打开
http://localhost:4321/。页面中应显示“概览”内容和“指南”侧边栏。
使用与 CI 相同的检查
构建前运行 pnpm check,检查 Astro、TypeScript、content schema 和 MDX。
添加另一篇文档
创建 src/content/docs/getting-started.mdx:
---title: 快速开始sidebar: icon: "lucide:rocket"---
在这里编写第一个任务。然后把 content entry ID 加入 docsNav:
{ "label": "指南", "items": ["index", "getting-started"]}Content entry ID 是 src/content/docs 下的相对路径,不包含 .mdx 扩展名。
嵌套文件使用 configuration/authentication 这样的斜杠路径。
将文档放到 /docs
最小配置会把文档放在 /。如果产品首页和文档需要共存,设置 docsBase
并更新顶部导航:
{ "name": "Acme", "docsBase": "/docs", "siteNav": [ { "label": "首页", "href": "/" }, { "label": "文档", "href": "/docs/" } ], "docsNav": [ { "label": "指南", "items": ["index", "getting-started"] } ]}内容仍保存在 src/content/docs,只有公开 URL 改变。Index 页面会渲染到
/docs/。