Overview
The ContentTags component displays a collection of tags as clickable badges. Each tag links to a dedicated tag page that shows all content associated with that tag, creating a taxonomy system for organizing posts, projects, and other content.Implementation
Location:src/components/ContentTags.astro
The component maps over an array of tag strings and renders each as a linked Badge component.
Key Features
- Tag linking - Each tag links to
/tags/{tagName} - Visual consistency - Uses Badge component from UI library
- Flexible layout - Wraps tags responsively with flexbox
- Truncation - Long tag names are truncated to prevent overflow
Props
Array of tag strings to display as badges
Usage
In MDX Layout
Tags are commonly displayed above content in theMDXLayout:
src/layouts/MDXLayout.astro
In Index Pages
Tags can also be displayed in list views like the books index:src/pages/books/index.astro
In Content Frontmatter
Tags are defined in the frontmatter of MDX/Markdown files:posts/my-post.mdx
Tag System Integration
The ContentTags component is part of a larger tag taxonomy system:Tag Pages
Each tag has a dedicated page at/tags/{tagName} that displays all content with that tag. This is generated using Astro’s dynamic routing:
src/pages/tags/[tag].astro
Tag Utility Function
ThegetAndGroupUniqueTags() function collects all unique tags across content collections and groups content by tag. This enables:
- Tag pages - Showing all content for a specific tag
- Tag index - Listing all available tags
- Related content - Finding content with similar tags
Badge Component
ContentTags uses the Badge component from the UI library:src/components/ui/badge.tsx
Badge Variants
default- Primary color background (used by ContentTags)secondary- Secondary color backgrounddestructive- Destructive/error coloroutline- Outlined badge with no background
Badge Styling
The Badge uses these default styles:rounded-full- Fully rounded cornerspx-2.5 py-0.5- Compact paddingtext-xs- Small text sizefont-semibold- Bold font weighthover:bg-primary/80- Subtle hover effect
Styling
The ContentTags wrapper uses:flex- Flexbox layoutflex-0- No flex grow/shrinkgap-1- Small gap between badgesflex-wrap- Wraps tags to multiple lines
Tag Links
Each tag is wrapped in a link:truncate class ensures long tag names don’t break the layout.
Customization
Change Badge Variant
Add Tag Count
Custom Tag Styling
Filter Tags
Show only certain tags:Use Cases
Blog Posts
Projects
Books
Experiences
Accessibility
- Uses semantic links for tag navigation
- Badge component includes focus states
- Truncation prevents layout overflow
- Color contrast meets WCAG standards
- Keyboard navigable tag links
Content Collections
Tags work across all content collections:- Posts - Blog posts and articles
- Projects - Portfolio projects
- Books - Reading list
- Experiences - Work history
tags field:
Best Practices
- Consistent naming - Use consistent tag names across content (e.g., “TypeScript” not “typescript” or “TS”)
- Limited tags - Keep 3-7 tags per content item for focus
- Hierarchical tags - Use broader categories (“Web Development”) alongside specific ones (“React”)
- Tag reuse - Reuse existing tags before creating new ones
- Meaningful names - Use descriptive tag names that users understand