分类体系是用于组织内容的分类系统。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"]
}