分類法

本頁內容

分類法是用於整理內容的分類系統。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刪除術語

下一步