Skip to content

getTags

getTags()

Extracts a unique list of tags (or categories/keywords) from an Astro content collection based on a specified frontmatter key.

  • Iterates over a collection of CollectionEntry items.

  • Reads the array at entry.data[tagDataKey] for each item.

  • Flattens all the tag arrays and removes duplicates using a Set.

  • Useful for building tag/category filters, tag clouds, or dynamic routing.

@params collection CollectionEntry[]

@params tagDataKey string

@returns string[] — An array of unique tags found in the collection.

Usage:

import { getTags } from '@/utils';
const tags = getTags(posts, "tags");
// => ['javascript', 'astro', 'typescript', 'css']

Where each article has frontmatter like:

---
tags: ["notation", "musescore"]
// ...other keys
---