Description
In line with #209, another Hugo feature I like is its built-in taxonomies, which enable you to group your content based on document metadata. When you configure a taxonomy, such as tags
or categories
, you can then template that taxonomized content in any number of ways. So with a tags
taxonomy in place, for example, you can pretty easily template out a page at /blog/tags
and /blog/tags/fashion
would yield a templated page displaying all the blog posts that have the fashion
tag. And so on.
Now, I suspect that some of this is already possible with Contentlayer using logic à la allDocs.filter(...)
. But I do think it may be worth considering adding a specific feature along these lines. Maybe something like this:
export const Post = defineDocumentType(() => ({
name: 'Post',
filePathPattern: `**/*.md`,
fields: {
title: {
type: 'string',
description: 'The title of the post',
required: true,
},
},
taxonomies: {
tags: {
name: 'tag'
}
}
}));
With this config, Contentlayer would look for documents with a tags
field in the metadata and create a new sub-object for each value under tags
that it encounters. Then you could do things like:
// "Query" documents
import { allPosts, Post} from "contentlayer/generated";
const gettingStartedPosts: Post[] = allPosts.taxonomies.tags.find(["getting-started"]);
// Iterate through tags
const allTags: string[] = allPosts.taxonomies.tags.all();
allTags.forEach(tag => ...);
// Iterate through all available taxonomies
allPosts.taxonomies.forEach(taxonomy => {
console.log(taxonomy.name);
taxonomy.groups.forEach(group => {
console.log(group.name);
console.log(group.items.length);
});
});
This example is off the cuff but hopefully illustrates the kind of thing that I think would be nice to provide. Happy to help out with this if project leads think it's a good idea!