文档内容管理和主题定制
内容与主题分离:把文档内容放在 Markdown/MDX,把样式/组件封装在主题层,便于复用和升级。
内容组织
- 使用 Content Collections 定义 frontmatter schema(标题、描述、tags、日期)。
- 目录按模块/版本/语言分层,文件名语义化。
- 通过
getCollection生成导航与侧边栏,避免硬编码。
主题设计
- 布局组件:
DocsLayout负责头部/侧栏/页脚/主题切换。 - UI 组件:Alert、Badge、Callout、CodeTabs、TOC、Breadcrumb 等集中管理。
- 颜色与字体:使用 CSS 变量,支持暗黑模式;在
:root与.dark设置主题值。
MDX 与组件映射
- 在文档中
export const components = { h2: HeadingWithIconH2, pre: CodeBlock, ... }。 - 让 Markdown 标题自动应用主题样式;代码块定制行号、复制按钮。
- 为提示块、API 表格、步骤列表提供专用组件。
多语言与版本
- 路径分语种
/zh-cn//en/;或通过 frontmatter locale 构建导航。 - 版本化可按
v1/v2/目录或在 frontmatter 中标记版本,侧栏按版本分组。
可访问性与可维护性
- 标题层级不跳级,确保 TOC 正常。
- 图像提供 alt;表格列头明确。
- 链接使用绝对/相对路径一致性,避免大小写问题。
主题发布/复用
- 主题组件与样式放独立包或子目录,暴露最小 API。
- 通过 npm 包或 git 子模块复用;版本更新时注意破坏性变更。***
内容管理系统
1. 内容集合
Astro 的内容集合系统让文档管理变得简单高效:
// src/content/config.ts
import HeadingWithIconH2 from '@/components/mdx/HeadingWithIconH2.astro';
export const components = {
h2: HeadingWithIconH2,
};
import { defineCollection, z } from 'astro:content';
const docs = defineCollection({
schema: z.object({
title: z.string(),
description: z.string(),
pubDate: z.date().optional(),
updatedDate: z.date().optional(),
tags: z.array(z.string()).optional(),
}),
});
export const collections = { docs };
2. 文档组织
推荐的文档目录结构:
src/content/docs/
├── getting-started/
│ ├── installation.md
│ └── configuration.md
├── guides/
│ ├── content-management.md
│ └── deployment.md
└── api/
├── endpoints.md
└── authentication.md
3. 元数据管理
使用 frontmatter 管理文档元数据:
---
title: API 端点
description: API 端点完整参考
pubDate: 2024-01-15
tags: ['api', 'reference']
---
主题定制
1. 自定义样式
基础样式覆盖
/* src/styles/custom.css */
:root {
--sl-color-accent: #0077CC;
--sl-color-text: #2C3E50;
--sl-font-system: system-ui, -apple-system, sans-serif;
}
/* 自定义组件样式 */
.custom-card {
background: var(--sl-color-gray-1);
border-radius: 8px;
padding: 1.5rem;
margin: 1rem 0;
}
主题变体
// astro.config.mjs
starlight({
customCss: [
'./src/styles/custom.css',
'./src/styles/theme-dark.css',
],
});
2. 组件系统
创建可重用组件
---
// src/components/ApiEndpoint.astro
interface Props {
method: 'GET' | 'POST' | 'PUT' | 'DELETE';
path: string;
auth?: boolean;
}
const { method, path, auth = false } = Astro.props;
---
<div class="api-endpoint">
<span class={`method ${method.toLowerCase()}`}>{method}</span>
<code class="path">{path}</code>
{auth && <span class="auth">🔒 需要认证</span>}
</div>
<style>
.api-endpoint {
display: flex;
align-items: center;
gap: 1rem;
padding: 1rem;
background: var(--sl-color-gray-1);
border-radius: 6px;
}
.method {
padding: 0.25rem 0.5rem;
border-radius: 4px;
font-weight: bold;
}
.method.get { background: #61affe; }
.method.post { background: #49cc90; }
.method.put { background: #fca130; }
.method.delete { background: #f93e3e; }
</style>
使用自定义组件
---
title: API 文档
---
# API 端点
<ApiEndpoint method="GET" path="/api/users" />
## 用户认证
<ApiEndpoint method="POST" path="/api/auth/login" auth={true} />
3. 导航定制
自定义侧边栏
// astro.config.mjs
starlight({
sidebar: [
{
label: '入门指南',
items: [
{ label: '快速开始', link: '/getting-started' },
{
label: '基础教程',
collapsed: true,
items: [
{ label: '安装', link: '/tutorials/installation' },
{ label: '配置', link: '/tutorials/configuration' },
],
},
],
},
{
label: 'API 参考',
autogenerate: { directory: 'api' },
},
],
});
自定义页头
---
// src/components/CustomHeader.astro
---
<div class="custom-header">
<div class="version-selector">
<select>
<option value="v1">v1.0.0</option>
<option value="v2">v2.0.0</option>
</select>
</div>
<div class="header-links">
<a href="/support">支持</a>
<a href="/github">GitHub</a>
</div>
</div>
<style>
.custom-header {
display: flex;
justify-content: space-between;
padding: 1rem;
border-bottom: 1px solid var(--sl-color-gray-5);
}
</style>
高级功能
1. 国际化支持
// astro.config.mjs
starlight({
defaultLocale: 'zh-cn',
locales: {
'zh-cn': {
label: '简体中文',
lang: 'zh-CN',
},
'en': {
label: 'English',
lang: 'en',
},
},
});
2. 插件集成
集成评论系统
// src/components/Comments.astro
import Giscus from '@giscus/react';
<Giscus
repo="username/repo"
repoId="R_xxx"
category="Announcements"
categoryId="xxx"
mapping="pathname"
reactionsEnabled="1"
emitMetadata="0"
theme="light"
/>
添加搜索增强
// astro.config.mjs
starlight({
search: {
provider: 'algolia',
config: {
appId: 'YOUR_APP_ID',
apiKey: 'YOUR_SEARCH_API_KEY',
indexName: 'YOUR_INDEX_NAME',
},
},
});
最佳实践
-
主题定制
- 保持品牌一致性
- 确保良好的暗色模式支持
- 优化移动端显示
-
内容组织
- 使用清晰的文件命名规范
- 保持合理的目录深度
- 定期更新文档内容
-
组件开发
- 组件职责单一
- 保持可复用性
- 提供完整的类型定义
下一步
完成内容管理和主题定制后,你可以:
- 优化构建流程
- 配置自动部署
- 设置性能监控
- 添加用户分析
在下一章中,我们将详细介绍如何部署和优化你的文档网站。