分类法

本页内容

分类法是用于组织内容的分类系统。EmDash 包含内置的分类和标签,并支持自定义分类法以满足专门的分类需求。

内置分类法

分类法类型描述
分类层级型具有父子关系的嵌套分类
标签扁平型无层级的简单标签

两者默认都可用于文章集合。

管理术语

创建术语

管理后台

  1. 进入分类法页面(例如 /_emdash/admin/taxonomies/category
  2. 新增表单中输入术语名称
  3. 可选设置:Slug父级描述
  4. 点击添加

内容编辑器

  1. 在编辑器中打开内容条目
  2. 在侧边栏找到分类法面板
  3. 对于分类,勾选适用的术语,或点击 + 新增
  4. 对于标签,输入用逗号分隔的标签名称
  5. 保存内容

API

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

{
  "slug": "tutorials",
  "label": "教程",
  "parentId": "term_abc",
  "description": "操作指南和教程"
}

编辑术语

  1. 进入分类法术语页面 → 编辑 → 更新 → 保存

删除术语

  1. 进入分类法术语页面 → 删除 → 确认

查询分类法

import { getTaxonomyTerms } from "emdash";
const categories = await getTaxonomyTerms("category");
const tags = await getTaxonomyTerms("tag");
const tags = await getTaxonomyTerms("tag", { includeCounts: false });
import { getTerm } from "emdash";
const category = await getTerm("category", "news");
import { getEntryTerms } from "emdash";
const categories = await getEntryTerms("posts", "post-123", "category");
import { getEmDashCollection } from "emdash";
const { entries: newsPosts } = await getEmDashCollection("posts", {
	status: "published",
	where: { category: "news" },
});

构建分类法页面

分类归档

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

export async function getStaticPaths() {
  const categories = await getTaxonomyTerms("category");
  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} 篇文章</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={`"${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>分类</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");
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) {
  return minSize + (count / maxCount) * (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>分类:</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>

自定义分类法

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

{
  "name": "genre",
  "label": "类型",
  "labelSingular": "类型",
  "hierarchical": true,
  "collections": ["books", "movies"]
}
const genres = await getTaxonomyTerms("genre");
const { entries: sciFiBooks } = await getEmDashCollection("books", {
	where: { genre: "science-fiction" },
});

分类法 API 参考

端点方法描述
/_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删除术语

下一步