API 参考
AR Docs 提供了一组 API 接口,用于获取文档数据和配置信息。
文档 API
获取文档列表
获取所有文档的元数据列表。
import { getCollection } from 'astro:content';
// 获取所有文档
const docs = await getCollection('docs');
// 按分类筛选
const tutorials = docs.filter(doc => doc.data.category === 'tutorial');获取单个文档
import { getEntry } from 'astro:content';
// 通过 ID 获取
const doc = await getEntry('docs', 'getting-started');
// 渲染文档
const { Content, headings } = await render(doc);内容集合 Schema
文档的 frontmatter 结构:
interface DocFrontmatter {
title: string; // 文档标题
description?: string; // 描述
category?: string; // 分类
order?: number; // 排序权重
pubDate?: string; // 发布日期
tags?: string[]; // 标签
}配置 API
获取站点配置
// 在 Astro 组件中
const siteConfig = {
title: 'AR Docs',
description: '现代化文档站点',
lang: 'zh',
};获取导航配置
// src/config/docs.ts
export const docsConfig = {
sidebar: [
{
title: '入门',
items: [
{ title: '快速开始', href: '/docs/getting-started' },
{ title: '安装', href: '/docs/installation' },
],
},
],
};搜索 API
客户端搜索(开发模式)
import { search } from '../plugins/client-search';
const results = await search('关键词');Pagefind 搜索(生产模式)
// 自动在构建时生成索引
const pagefind = await import('/pagefind/pagefind.js');
await pagefind.init();
const search = await pagefind.search('关键词');渲染 API
render() 函数
import { render } from 'astro:content';
export async function getStaticPaths() {
const docs = await getCollection('docs');
return docs.map((doc) => ({
params: { slug: doc.id },
props: { doc },
}));
}
const { doc } = Astro.props;
const { Content, headings } = await render(doc);提示
Astro v6 的
render()API 已从doc.render()改为render(doc)。