Les taxonomies classifient le contenu. EmDash inclut des catégories et des étiquettes et prend en charge des taxonomies sur mesure.
Taxonomies intégrées
| Taxonomie | Type | Description |
|---|---|---|
| Catégories | Hiérarchique | Classification imbriquée parent-enfant |
| Étiquettes | Plate | Libellés sans hiérarchie |
Disponibles par défaut pour la collection posts.
Gérer les termes
Créer un terme
Admin Dashboard
-
Ouvrez la page de taxonomie (p. ex.
/_emdash/admin/taxonomies/category) -
Saisissez le nom dans Add New
-
Optionnel : Slug, Parent, Description
-
Cliquez sur Add
Content Editor
-
Ouvrez une entrée dans l’éditeur
-
Panneau de taxonomie dans la barre latérale
-
Catégories : cases à cocher ou + Add New
-
Étiquettes : noms séparés par des virgules
-
Enregistrez
API
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"
} Modifier / supprimer
Modifiez depuis la liste avec Edit / Save. Supprimez avec Delete et confirmez.
Interroger les taxonomies
import { getTaxonomyTerms } from "emdash";
const categories = await getTaxonomyTerms("category");
const tags = await getTaxonomyTerms("tag");
interface TaxonomyTerm {
id: string;
name: string;
slug: string;
label: string;
parentId?: string;
description?: string;
children: TaxonomyTerm[];
count?: number;
}
import { getTerm } from "emdash";
const category = await getTerm("category", "news");
import { getEntryTerms } from "emdash";
const categories = await getEntryTerms("posts", "post-123", "category");
const tags = await getEntryTerms("posts", "post-123", "tag");
import { getEmDashCollection } from "emdash";
const { entries: newsPosts } = await getEmDashCollection("posts", {
status: "published",
where: { category: "news" },
});
import { getEntriesByTerm } from "emdash";
const newsPosts = await getEntriesByTerm("posts", "category", "news");
Pages d’archive
Archive par catégorie
---
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} articles</p>
<ul>
{posts.map((post) => (
<li>
<a href={`/blog/${post.data.slug}`}>{post.data.title}</a>
</li>
))}
</ul>
</Base>
Archive par étiquette
---
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={`Articles avec l’étiquette « ${tag.label} »`}>
<h1>#{tag.label}</h1>
<ul>
{posts.map((post) => (
<li>
<a href={`/blog/${post.data.slug}`}>{post.data.title}</a>
</li>
))}
</ul>
</Base>
Liste des catégories
---
import { getTaxonomyTerms } from "emdash";
const categories = await getTaxonomyTerms("category");
---
<nav class="category-list">
<h3>Catégories</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>
Nuage d’étiquettes
---
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>
Afficher les termes sur le contenu
---
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>Publié dans :</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>
Taxonomies personnalisées
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" },
});
Référence REST
| Endpoint | Méthode | Description |
|---|---|---|
/_emdash/api/taxonomies | GET | Lister les définitions |
/_emdash/api/taxonomies | POST | Créer une taxonomie |
/_emdash/api/taxonomies/:name/terms | GET/POST | Lister / créer des termes |
/_emdash/api/taxonomies/:name/terms/:slug | GET/PUT/DELETE | Lire / mettre à jour / supprimer |
POST /_emdash/api/content/posts/post-123/terms/category
Content-Type: application/json
Authorization: Bearer YOUR_API_TOKEN
{
"termIds": ["term_news", "term_featured"]
}