コンテンツクエリ(Content Query)
一覧表示やプラグインのデータのために、タグ、フォルダー、日付、frontmatterのフィールドでコンテンツエントリを選んだり絞り込んだりする仕組みです。タクソノミー、シリーズ、関連記事で使われます。
クエリの方法
コンテンツクエリはcontent-queryプラグインを通じて解決され、次の条件で絞り込めます。
| フィルターの種類 | 説明 |
|---|---|
tag |
タグでエントリを照合する |
folder |
フォルダーのパスでエントリを照合する |
date |
公開日でエントリを照合する |
frontmatter field |
任意のfrontmatterのキーで照合する |
import { searchItems } from "@riebeckite/plugin-search";
const results = searchItems(entries, "tag:news");
content-queryプラグインの実行環境
content-queryプラグインは、エントリのHTML内のクエリブロックを解決する実行環境(packages/plugins/query/src/runtime.ts)を使います。この実行環境は次の処理を行います。
- エントリのHTML内で
query属性が付いたクエリブロックを見つける - YAMLでエンコードされたクエリの指定を解析する
queryContentEntriesでマニフェストのdiscoverableEntries(または指定されたプール)を絞り込む- ブロックを描画結果で置き換える
queryContentEntries関数(packages/core/src/content/content_query.ts)は、タグ、フォルダー、日付、frontmatterのフィールドによる絞り込みに対応し、並べ替え、ページネーション、グループ化を扱います。
グループ化とページネーション
コンテンツクエリは、タグ、フォルダー、日付、frontmatterのフィールドによるグループ化に対応し、offsetとlimitの指定によるページネーションにも対応します。groupContentEntries関数(packages/core/src/content/content_query.ts)は、グループのキーと所属するエントリを持つContentQueryGroup[]を返します。
// From packages/core/src/content/content_query.ts
export function groupContentEntries(
entries: readonly ContentManifestEntry[],
groupBy: ContentQueryGroupBy,
options: ContentQueryGroupOptions = {},
): ContentQueryGroup[];
関連用語
さらに詳しく
- [../plugins/query.ja.md]