콘텐츠로 이동

분류 체계

분류체계는 콘텐츠를 조직화하기 위한 분류 시스템입니다. EmDash는 기본 제공되는 카테고리와 태그를 포함하며, 특수한 분류 요구를 위한 사용자 정의 분류체계를 지원합니다.

EmDash는 두 가지 기본 분류체계를 제공합니다:

분류체계유형설명
카테고리계층적부모-자식 관계를 가진 중첩 분류
태그평면형계층 구조 없이 단순한 라벨

둘 다 기본적으로 게시물 컬렉션에 사용 가능합니다.

  1. 분류체계 페이지로 이동합니다 (예: /_emdash/admin/taxonomies/category)

  2. 새로 추가 양식에 용어 이름을 입력합니다

  3. 선택적으로 설정합니다:

    • 슬러그 - URL 식별자 (이름에서 자동 생성됨)
    • 상위 - 계층적 분류체계용
    • 설명 - 용어 설명
  4. 추가를 클릭합니다

  1. 분류체계 용어 페이지로 이동합니다

  2. 용어 옆의 편집을 클릭합니다

  3. 이름, 슬러그, 상위 또는 설명을 업데이트합니다

  4. 저장을 클릭합니다

  1. 분류체계 용어 페이지로 이동합니다

  2. 용어 옆의 삭제를 클릭합니다

  3. 삭제를 확인합니다

EmDash는 분류체계 용어를 쿼리하고 용어별로 콘텐츠를 필터링하는 기능을 제공합니다.

분류체계의 모든 용어를 검색합니다:

import { getTaxonomyTerms } from "emdash";
// 모든 카테고리 가져오기 (트리 구조 반환)
const categories = await getTaxonomyTerms("category");
// 모든 태그 가져오기 (평면 목록 반환)
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
}
import { getTerm } from "emdash";
const category = await getTerm("category", "news");
// TaxonomyTerm 또는 null 반환
import { getEntryTerms } from "emdash";
// 게시물의 모든 카테고리 가져오기
const categories = await getEntryTerms("posts", "post-123", "category");
// 게시물의 모든 태그 가져오기
const tags = await getEntryTerms("posts", "post-123", "tag");

where 필터와 함께 getEmDashCollection을 사용합니다:

import { getEmDashCollection } from "emdash";
// "news" 카테고리의 게시물
const { entries: newsPosts } = await getEmDashCollection("posts", {
status: "published",
where: { category: "news" },
});
// "javascript" 태그가 있는 게시물
const { entries: jsPosts } = await getEmDashCollection("posts", {
status: "published",
where: { tag: "javascript" },
});

또는 편의 함수를 사용합니다:

import { getEntriesByTerm } from "emdash";
const newsPosts = await getEntriesByTerm("posts", "category", "news");

카테고리 내 게시물을 나열하는 페이지를 생성합니다:

astro title="src/pages/category/[slug].astro"
---
import { getTaxonomyTerms, getTerm, 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} posts</p>
<ul>
{posts.map((post) => (
<li>
<a href={`/blog/${post.data.slug}`}>{post.data.title}</a>
</li>
))}
</ul>
</Base>

태그가 지정된 게시물을 나열하는 페이지를 생성합니다:

astro title="src/pages/tag/[slug].astro"
---
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>

게시물 수와 함께 카테고리 목록을 표시합니다:

astro title="src/components/CategoryList.astro"
---
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>

사용량에 따라 크기가 다른 태그를 표시합니다:

astro title="src/components/TagCloud.astro"
---
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) {
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>

게시물에 카테고리와 태그를 표시합니다:

astro title="src/components/PostTerms.astro"
---
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>

카테고리와 태그를 넘어 특수한 요구사항을 위한 분류 체계를 생성하세요.

관리자 API를 사용하여 분류 체계를 생성하세요:

Terminal window
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";
// 모든 장르 가져오기
const genres = await getTaxonomyTerms("genre");
// 특정 장르의 책 가져오기
const { entries: sciFiBooks } = await getEmDashCollection("books", {
where: { genre: "science-fiction" },
});

분류 체계는 적용할 컬렉션을 지정합니다:

{
"name": "difficulty",
"label": "Difficulty Levels",
"hierarchical": false,
"collections": ["recipes", "tutorials"]
}
엔드포인트메서드설명
/_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용어 삭제
Terminal window
POST /_emdash/api/content/posts/post-123/terms/category
Content-Type: application/json
Authorization: Bearer YOUR_API_TOKEN
{
"termIds": ["term_news", "term_featured"]
}