分類體系是用於組織內容的分類系統。EmDash包含內建的分類和標籤,並支援用於專業分類需求的自訂分類體系。
內建分類體系
EmDash提供兩種預設分類體系:
| 分類體系 | 類型 | 描述 |
|---|---|---|
| 分類 | 階層型 | 具有父子關係的巢狀分類 |
| 標籤 | 扁平型 | 無階層結構的簡單標籤 |
預設情況下,兩者都可用於文章集合。
管理術語
建立術語
Admin Dashboard
-
前往分類體系頁面(例如:
/_emdash/admin/taxonomies/category) -
在新增項目表單中輸入術語名稱
-
可選設定:
- Slug - URL識別碼(從名稱自動產生)
- 父層級 - 用於階層型分類體系
- 描述 - 術語描述
-
點擊新增
Content Editor
-
在編輯器中開啟內容條目
-
在側邊欄中找到分類體系面板
-
對於分類,勾選適用術語的核取方塊,或點擊**+ 新增項目**
-
對於標籤,輸入用逗號分隔的標籤名稱
-
儲存內容
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"
} 編輯術語
-
前往分類體系術語頁面
-
點擊術語旁邊的編輯
-
更新名稱、slug、父層級或描述
-
點擊儲存
刪除術語
-
前往分類體系術語頁面
-
點擊術語旁邊的刪除
-
確認刪除
查詢分類體系
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
}
取得單個術語
以下範例透過分類體系和slug取得一個術語:
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/taxonomies | GET | 列出分類體系定義 |
/_emdash/api/taxonomies | POST | 建立分類體系 |
/_emdash/api/taxonomies/:name/terms | GET | 列出術語 |
/_emdash/api/taxonomies/:name/terms | POST | 建立術語 |
/_emdash/api/taxonomies/:name/terms/:slug | GET | 取得術語 |
/_emdash/api/taxonomies/:name/terms/:slug | PUT | 更新術語 |
/_emdash/api/taxonomies/:name/terms/:slug | DELETE | 刪除術語 |
向內容指派術語
以下請求向文章指派分類術語:
POST /_emdash/api/content/posts/post-123/terms/category
Content-Type: application/json
Authorization: Bearer YOUR_API_TOKEN
{
"termIds": ["term_news", "term_featured"]
}