タクソノミー

このページ

タクソノミーは、コンテンツを整理するための分類システムです。EmDashには、カテゴリーとタグが組み込まれており、専門的な分類ニーズに対応するカスタムタクソノミーをサポートしています。

組み込みタクソノミー

EmDashは2つのデフォルトタクソノミーを提供しています:

タクソノミータイプ説明
カテゴリー階層型親子関係を持つネストされた分類
タグフラット型階層なしのシンプルなラベル

どちらもデフォルトで投稿コレクションで利用できます。

用語の管理

用語を作成する

Admin Dashboard

  1. タクソノミーページにアクセスします(例: /_emdash/admin/taxonomies/category

  2. 新規追加フォームに用語名を入力します

  3. オプションで設定:

    • スラッグ - URL識別子(名前から自動生成)
    • - 階層型タクソノミー用
    • 説明 - 用語の説明
  4. 追加をクリック

Content Editor

  1. エディターでコンテンツエントリーを開きます

  2. サイドバーのタクソノミーパネルを探します

  3. カテゴリーの場合、該当する用語のチェックボックスをオンにするか、+ 新規追加をクリックします

  4. タグの場合、カンマで区切ってタグ名を入力します

  5. コンテンツを保存します

API

以下のリクエストは、categoryタクソノミーに用語を作成します:

POST /_emdash/api/taxonomies/category/terms
Content-Type: application/json
Authorization: Bearer YOUR_API_TOKEN

{
  "slug": "tutorials",
  "label": "Tutorials",
  "parentId": "term_abc",
  "description": "How-to guides and tutorials"
}

用語を編集する

  1. タクソノミー用語ページにアクセスします

  2. 用語の横にある編集をクリックします

  3. 名前、スラッグ、親、または説明を更新します

  4. 保存をクリックします

用語を削除する

  1. タクソノミー用語ページにアクセスします

  2. 用語の横にある削除をクリックします

  3. 削除を確認します

タクソノミーのクエリ

EmDashは、タクソノミー用語をクエリし、用語でコンテンツをフィルタリングする関数を提供します。

すべての用語を取得する

タクソノミーのすべての用語を取得:

import { getTaxonomyTerms } from "emdash";

// Get all categories (returns tree structure)
const categories = await getTaxonomyTerms("category");

// Get all tags (returns flat list)
const tags = await getTaxonomyTerms("tag");

階層型タクソノミーの場合、用語にはchildren配列が含まれます:

interface TaxonomyTerm {
	id: string;
	name: string; // Taxonomy name ("category")
	slug: string; // Term slug ("news")
	label: string; // Display label ("News")
	parentId?: string;
	description?: string;
	children: TaxonomyTerm[];
	count?: number; // Number of entries with this term
}

単一の用語を取得する

以下の例は、タクソノミーとスラッグで1つの用語を取得します:

import { getTerm } from "emdash";

const category = await getTerm("category", "news");
// Returns TaxonomyTerm or null

エントリーの用語を取得する

以下の例は、単一のエントリーに割り当てられたカテゴリーとタグを取得します:

import { getEntryTerms } from "emdash";

// Get all categories for a post
const categories = await getEntryTerms("posts", "post-123", "category");

// Get all tags for a post
const tags = await getEntryTerms("posts", "post-123", "tag");

用語でコンテンツをフィルタリングする

whereフィルターでgetEmDashCollectionを使用:

import { getEmDashCollection } from "emdash";

// Posts in the "news" category
const { entries: newsPosts } = await getEmDashCollection("posts", {
	status: "published",
	where: { category: "news" },
});

// Posts with the "javascript" tag
const { entries: jsPosts } = await getEmDashCollection("posts", {
	status: "published",
	where: { tag: "javascript" },
});

または便利な関数を使用:

import { getEntriesByTerm } from "emdash";

const newsPosts = await getEntriesByTerm("posts", "category", "news");

タクソノミーページの構築

カテゴリーアーカイブ

カテゴリー内の投稿をリストするページを作成:

---
import { getTaxonomyTerms, getTerm, getEmDashCollection } from "emdash";
import Base from "../../layouts/Base.astro";

export async function getStaticPaths() {
  const categories = await getTaxonomyTerms("category");

  // Flatten hierarchical tree for routing
  function flatten(terms) {
    return terms.flatMap((term) => [term, ...flatten(term.children)]);
  }

  return flatten(categories).map((cat) => ({
    params: { slug: cat.slug },
    props: { category: cat },
  }));
}

const { category } = Astro.props;

const { entries: posts } = await getEmDashCollection("posts", {
  status: "published",
  where: { category: category.slug },
});
---

<Base title={category.label}>
  <h1>{category.label}</h1>
  {category.description && <p>{category.description}</p>}
  <p>{category.count} posts</p>

  <ul>
    {posts.map((post) => (
      <li>
        <a href={`/blog/${post.data.slug}`}>{post.data.title}</a>
      </li>
    ))}
  </ul>
</Base>

タグアーカイブ

タグ付きの投稿をリストするページを作成:

---
import { getTaxonomyTerms, getEmDashCollection } from "emdash";
import Base from "../../layouts/Base.astro";

export async function getStaticPaths() {
  const tags = await getTaxonomyTerms("tag");

  return tags.map((tag) => ({
    params: { slug: tag.slug },
    props: { tag },
  }));
}

const { tag } = Astro.props;

const { entries: posts } = await getEmDashCollection("posts", {
  status: "published",
  where: { tag: tag.slug },
});
---

<Base title={`Posts tagged "${tag.label}"`}>
  <h1>#{tag.label}</h1>

  <ul>
    {posts.map((post) => (
      <li>
        <a href={`/blog/${post.data.slug}`}>{post.data.title}</a>
      </li>
    ))}
  </ul>
</Base>

カテゴリーリストウィジェット

投稿数付きのカテゴリーリストを表示:

---
import { getTaxonomyTerms } from "emdash";

const categories = await getTaxonomyTerms("category");
---

<nav class="category-list">
  <h3>Categories</h3>
  <ul>
    {categories.map((cat) => (
      <li>
        <a href={`/category/${cat.slug}`}>
          {cat.label} ({cat.count})
        </a>
        {cat.children.length > 0 && (
          <ul>
            {cat.children.map((child) => (
              <li>
                <a href={`/category/${child.slug}`}>
                  {child.label} ({child.count})
                </a>
              </li>
            ))}
          </ul>
        )}
      </li>
    ))}
  </ul>
</nav>

タグクラウド

使用状況に基づいたサイズでタグを表示:

---
import { getTaxonomyTerms } from "emdash";

const tags = await getTaxonomyTerms("tag");

// Calculate font sizes based on count
const counts = tags.map((t) => t.count ?? 0);
const maxCount = Math.max(...counts, 1);
const minSize = 0.8;
const maxSize = 2;

function getSize(count: number) {
  const ratio = count / maxCount;
  return minSize + ratio * (maxSize - minSize);
}
---

<div class="tag-cloud">
  {tags.map((tag) => (
    <a
      href={`/tag/${tag.slug}`}
      style={`font-size: ${getSize(tag.count ?? 0)}rem`}
    >
      {tag.label}
    </a>
  ))}
</div>

コンテンツに用語を表示する

投稿にカテゴリーとタグを表示:

---
import { getEntryTerms } from "emdash";

interface Props {
  collection: string;
  entryId: string;
}

const { collection, entryId } = Astro.props;

const categories = await getEntryTerms(collection, entryId, "category");
const tags = await getEntryTerms(collection, entryId, "tag");
---

<div class="post-terms">
  {categories.length > 0 && (
    <div class="categories">
      <span>Posted in:</span>
      {categories.map((cat, i) => (
        <>
          {i > 0 && ", "}
          <a href={`/category/${cat.slug}`}>{cat.label}</a>
        </>
      ))}
    </div>
  )}

  {tags.length > 0 && (
    <div class="tags">
      {tags.map((tag) => (
        <a href={`/tag/${tag.slug}`} class="tag">
          #{tag.label}
        </a>
      ))}
    </div>
  )}
</div>

カスタムタクソノミー

専門的なニーズに応じて、カテゴリーとタグを超えたタクソノミーを作成します。

カスタムタクソノミーを作成する

管理APIを使用してタクソノミーを作成:

POST /_emdash/api/taxonomies
Content-Type: application/json
Authorization: Bearer YOUR_API_TOKEN

{
  "name": "genre",
  "label": "Genres",
  "labelSingular": "Genre",
  "hierarchical": true,
  "collections": ["books", "movies"]
}

カスタムタクソノミーを使用する

組み込みタクソノミーと同じ方法でカスタムタクソノミーをクエリして表示:

import { getTaxonomyTerms, getEmDashCollection } from "emdash";

// Get all genres
const genres = await getTaxonomyTerms("genre");

// Get books in a genre
const { entries: sciFiBooks } = await getEmDashCollection("books", {
	where: { genre: "science-fiction" },
});

コレクションに割り当てる

タクソノミーは、適用されるコレクションを指定します:

{
  "name": "difficulty",
  "label": "Difficulty Levels",
  "hierarchical": false,
  "collections": ["recipes", "tutorials"]
}

タクソノミーAPIリファレンス

RESTエンドポイント

エンドポイントメソッド説明
/_emdash/api/taxonomiesGETタクソノミー定義をリスト
/_emdash/api/taxonomiesPOSTタクソノミーを作成
/_emdash/api/taxonomies/:name/termsGET用語をリスト
/_emdash/api/taxonomies/:name/termsPOST用語を作成
/_emdash/api/taxonomies/:name/terms/:slugGET用語を取得
/_emdash/api/taxonomies/:name/terms/:slugPUT用語を更新
/_emdash/api/taxonomies/:name/terms/:slugDELETE用語を削除

コンテンツに用語を割り当てる

以下のリクエストは、投稿にカテゴリー用語を割り当てます:

POST /_emdash/api/content/posts/post-123/terms/category
Content-Type: application/json
Authorization: Bearer YOUR_API_TOKEN

{
  "termIds": ["term_news", "term_featured"]
}

次のステップ