I think of tag pages as wonderful archives for knowing what an author writes about — so they should include the number of articles in each tag. Otherwise, it is difficult for people to know how many articles there are.
Here’s an example:
Gathering tags
To make this happen, we need to gather the tags into an array that contains two things — the tags themselves, and the number of items within each tag.
const tags = [ { name: 'astro', items: 18 }, { name: 'css', items: 20 }]The only way to create this tags array is to loop through the content collection, look at the frontmatter for each post, and see what tags are available.
We can write a gatherTags function to do this.
---import { getCollection } from 'astro:content'const blog = await getCollection('blog')const tags = gatherTags(blog)
// Gathers tags into a usable arrayfunction gatherTags(files) { // ...}---Since we are looping through each post anyway, I think it’s even better to let the tags array have an array of posts within the tag. We can always use length to get the number of posts in the tag.
const tags = [ { name: 'astro', items: [{ /*...*/ }, { /*...*/ }, { /*...*/ }], }, { name: 'css', items: [{ /*...*/ }, { /*...*/ }, { /*...*/ }], },]With this in mind, I’ve created a gatherTags that looks like this:
// Gathers tags into a usable arrayfunction gatherTags(files) { const tags = {}
// Group files by tag files.forEach(file => { file.data.tags.forEach(tag => { if (!tags[tag]) tags[tag] = [] tags[tag].push(file) }) })
// Sort tags alphabetically and convert to an array for usage return Object.keys(tags) .sort() .map(tag => ({ name: tag, items: tags[tag] }))}gatherTags is added to Splendid Astro so you don’t have to write it yourself. Just use it!
---import { getCollection } from 'astro:content'import { gatherTags } from '@splendidlabz/astro/content'const blog = await getCollection('blog')const tags = gatherTags(blog)---Use processFiles first
I recommend always using processFiles before gatherTags because it ensures the content folder is processed according to the changes we’ve made in blogs made easy.
---// ...const blog = await getCollection('blog')const posts = await processFiles(blog)const tags = gatherTags(posts)---Creating tag pages
Once we have gathered tags into tags, creating tag pages is remarkably simple — we’ll use getStaticPaths just like we’ve created blog pages in the previous lessons.
---export const prerender = true // optional if output is static
export async function getStaticPaths() { const blog = await getCollection('blog') const posts = processFiles(blog) const tags = gatherTags(posts)
return tags.map(tag => ({ params: { tag: tag.name }, props: { posts: tag.items }, }))}
const { tag } = Astro.paramsconst { posts } = Astro.props---
<h1>{tag}</h1>{ posts.map(post => ( <article> <h2> <a href={`/blog/${post.data.slug}`}>{post.data.title}</a> </h2> <Markdown>{post.data.excerpt}</Markdown> </article> ))}Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson