Taxonomías

En esta página

Las taxonomías clasifican contenido. EmDash incluye categorías y etiquetas y admite taxonomías propias.

Taxonomías integradas

TaxonomíaTipoDescripción
CategoríasJerárquicaClasificación anidada padre-hijo
EtiquetasPlanaEtiquetas sin jerarquía

Disponibles por defecto en la colección posts.

Gestionar términos

Crear término

Admin Dashboard

  1. Abra la página de taxonomía (p. ej. /_emdash/admin/taxonomies/category)

  2. Nombre en Add New

  3. Opcional: Slug, Parent, Description

  4. Add

Content Editor

  1. Abra una entrada

  2. Panel de taxonomía en la barra lateral

  3. Categorías: casillas o + Add New

  4. Etiquetas: nombres separados por comas

  5. Guarde

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

Editar / eliminar

Edite desde la lista de términos con Edit / Save. Elimine con Delete y confirme.

Consultar taxonomías

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");

Páginas de archivo

Archivo por categoría

---
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} publicaciones</p>
  <ul>
    {posts.map((post) => (
      <li>
        <a href={`/blog/${post.data.slug}`}>{post.data.title}</a>
      </li>
    ))}
  </ul>
</Base>

Archivo por etiqueta

---
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={`Entradas etiquetadas «${tag.label}»`}>
  <h1>#{tag.label}</h1>

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

Lista de categorías

---
import { getTaxonomyTerms } from "emdash";
const categories = await getTaxonomyTerms("category");
---

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

Nube de etiquetas

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

Mostrar términos en el contenido

---
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>Publicado en:</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>

Taxonomías personalizadas

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" },
});

Referencia REST

EndpointMétodoDescripción
/_emdash/api/taxonomiesGETListar definiciones
/_emdash/api/taxonomiesPOSTCrear taxonomía
/_emdash/api/taxonomies/:name/termsGET/POSTListar / crear términos
/_emdash/api/taxonomies/:name/terms/:slugGET/PUT/DELETEVer / actualizar / borrar
POST /_emdash/api/content/posts/post-123/terms/category
Content-Type: application/json
Authorization: Bearer YOUR_API_TOKEN

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

Próximos pasos