vike-vue-content

queryCollection 系列 API,用于查询单页、导航树、上下页与所有路径。

内容查询

vike-vue-content/query 暴露一组查询 API,用来在服务端(data 钩子、prerender 钩子等)读取内容数据。DocsPage 的内置数据加载器就是基于它们实现的;当你要做自定义页面时,可以直接使用。

import {
  queryCollection,
  queryCollectionNavigation,
  queryCollectionItemSurroundings,
  queryCollectionPaths,
} from 'vike-vue-content/query'

queryCollection

查询某个 collection 的内容条目,返回一个可链式调用的查询构造器。

// 查询单页
const page = await queryCollection('docs')
  .path('/docs/getting-started')
  .first()

// 查询并排序
const all = await queryCollection('docs')
  .order('title', 'ASC')
  .all()

链式方法

方法返回说明
.path(value)QueryBuilder按路径过滤。
.order(field, direction?)QueryBuilder按字段排序,direction'ASC'(默认)或 'DESC'
.first()Promise<ContentEntry | null>取第一条。
.all()Promise<ContentEntry[]>取全部。

queryCollectionNavigation

返回 collection 的导航树(侧边栏数据)。返回值本身是一个 Promise,同时支持 .order()

const navigation = await queryCollectionNavigation('docs')

// 也可以排序
const sorted = await queryCollectionNavigation('docs').order('title', 'DESC')

导航树会应用 .config.yml / frontmatter 里的 hiddenflatten 等规则。

queryCollectionItemSurroundings

返回某个路径的上一页和下一页,用于上下页导航:

const [prev, next] = await queryCollectionItemSurroundings(
  'docs',
  '/docs/getting-started',
)

没有相邻项时对应位置为 null

queryCollectionPaths

枚举 collection 下所有内容路径(去掉排序前缀后的最终公开路径),主要用于预渲染:

const paths = await queryCollectionPaths('docs')
// → ['/docs', '/docs/getting-started', '/docs/guide/routing', …]

查询选项

所有查询函数都接收一个可选的 options 参数:

type QueryOptions = {
  cwd?: string         // 工作目录,默认 process.cwd()
  contentDir?: string  // 内容目录,默认 'content'
}

核心类型

查询返回的 ContentEntry 主要字段:

type ContentEntry = {
  id: string
  collection: string
  path: string
  stem: string
  title?: string
  description?: string
  toc?: ContentTocLink[]
  navigation?: boolean | ContentNavigationConfig
  body: unknown            // 内容 AST,传给 <ContentRenderer>
  rawbody: string
  frontmatter: Record<string, unknown>
  meta: Record<string, unknown>
}

完整类型从 vike-vue-content/shared/types 导出。