Taxonomien

Auf dieser Seite

Taxonomien klassifizieren Inhalte. EmDash bringt Kategorien und Tags mit und unterstützt eigene Taxonomien.

Eingebaute Taxonomien

TaxonomieTypBeschreibung
KategorienHierarchischVerschachtelung mit Eltern-Kind-Beziehungen
TagsFlachEinfache Labels ohne Hierarchie

Standardmäßig für die Collection posts verfügbar.

Begriffe verwalten

Begriff anlegen

Admin Dashboard

  1. Taxonomie-Seite öffnen (z. B. /_emdash/admin/taxonomies/category)

  2. Begriffsname im Formular Add New eintragen

  3. Optional:

    • Slug — URL-Kennung (oft automatisch)
    • Parent — bei hierarchischen Taxonomien
    • Description — Beschreibung
  4. Add klicken

Content Editor

  1. Eintrag im Editor öffnen

  2. Taxonomie-Panel in der Seitenleiste

  3. Kategorien: Checkboxen oder + Add New

  4. Tags: Namen kommagetrennt eingeben

  5. Speichern

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"
}

Begriff bearbeiten

  1. Taxonomie-Begriffsseite öffnen
  2. Edit neben dem Begriff
  3. Name, Slug, Parent oder Beschreibung anpassen
  4. Save

Begriff löschen

  1. Taxonomie-Begriffsseite
  2. Delete
  3. Bestätigen

Taxonomien abfragen

Alle Begriffe

import { getTaxonomyTerms } from "emdash";

const categories = await getTaxonomyTerms("category");
const tags = await getTaxonomyTerms("tag");

Hierarchisch: children-Array:

interface TaxonomyTerm {
	id: string;
	name: string;
	slug: string;
	label: string;
	parentId?: string;
	description?: string;
	children: TaxonomyTerm[];
	count?: number;
}

Einzelner Begriff

import { getTerm } from "emdash";

const category = await getTerm("category", "news");

Begriffe eines Eintrags

import { getEntryTerms } from "emdash";

const categories = await getEntryTerms("posts", "post-123", "category");
const tags = await getEntryTerms("posts", "post-123", "tag");

Inhalte nach Begriff filtern

import { getEmDashCollection } from "emdash";

const { entries: newsPosts } = await getEmDashCollection("posts", {
	status: "published",
	where: { category: "news" },
});

const { entries: jsPosts } = await getEmDashCollection("posts", {
	status: "published",
	where: { tag: "javascript" },
});

Oder:

import { getEntriesByTerm } from "emdash";

const newsPosts = await getEntriesByTerm("posts", "category", "news");

Taxonomie-Seiten

Kategorie-Archiv

---
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} Beiträge</p>

  <ul>
    {posts.map((post) => (
      <li>
        <a href={`/blog/${post.data.slug}`}>{post.data.title}</a>
      </li>
    ))}
  </ul>
</Base>

Tag-Archiv

---
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>

Kategorieliste

---
import { getTaxonomyTerms } from "emdash";

const categories = await getTaxonomyTerms("category");
---

<nav class="category-list">
  <h3>Kategorien</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>

Tag-Cloud

---
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>

Begriffe am Inhalt anzeigen

---
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>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>

Benutzerdefinierte Taxonomien

Anlegen

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

{
  "name": "genre",
  "label": "Genres",
  "labelSingular": "Genre",
  "hierarchical": true,
  "collections": ["books", "movies"]
}

Nutzung

import { getTaxonomyTerms, getEmDashCollection } from "emdash";

const genres = await getTaxonomyTerms("genre");

const { entries: sciFiBooks } = await getEmDashCollection("books", {
	where: { genre: "science-fiction" },
});

Collections zuordnen

{
  "name": "difficulty",
  "label": "Difficulty Levels",
  "hierarchical": false,
  "collections": ["recipes", "tutorials"]
}

Taxonomie-API (REST)

EndpointMethodeBeschreibung
/_emdash/api/taxonomiesGETTaxonomien auflisten
/_emdash/api/taxonomiesPOSTTaxonomie anlegen
/_emdash/api/taxonomies/:name/termsGETBegriffe auflisten
/_emdash/api/taxonomies/:name/termsPOSTBegriff anlegen
/_emdash/api/taxonomies/:name/terms/:slugGETBegriff abrufen
/_emdash/api/taxonomies/:name/terms/:slugPUTBegriff aktualisieren
/_emdash/api/taxonomies/:name/terms/:slugDELETEBegriff löschen

Begriffe zuweisen

POST /_emdash/api/content/posts/post-123/terms/category
Content-Type: application/json
Authorization: Bearer YOUR_API_TOKEN

{
  "termIds": ["term_news", "term_featured"]
}

Nächste Schritte