Eleventy - Merge data with an existing collection
There are times when you would like to merge some data with an existing collection in eleventy. For example, I wanted to create an archive of my tech writing. I wanted to merge posts I wrote for other websites such as CSS Tricks with the posts I wrote on my personal blog. Everything in a single collection.
I couldn’t find an answer in the eleventy docs on how to include external data in an another collection. Here is how I did it.
TLDR
The simplest approach is to make a markdown file for each external post, the same as you do for your local posts, but provide the requisite data in the frontmatter.
My preference is to create a global data JSON file for the external posts. Having a clear separation should make it easier to manage.
Let’s name our file externalPosts.json and populate it with an array of posts. You can reference it in your eleventy configuration file by drilling down into the all
collection as below:
There probably should be a function in the collection API to retrieve the collections created from data files directly!
My use case in full
I wanted to create a posts
collection for all of my writing that was published on my blog and other websites. For example, I wrote 2 articles on CSS Tricks. There is no data source for that article metadata, so I need to manually create it.
Rather than make a markdown file for each external post, I would prefer to create a global data JSON file and merge it with the posts
collection.
This is my project structure:
My “local” posts live in content/posts
. I added the externalPosts.json
file to hold the metadata for my external posts, they are available in page templates as an externalPosts
variable. The problem is that you cannot concatenate this array with another array in a page template in a simple way. For example, Nunjucks does not have a merge filter or similar. You could create this filter, but I would prefer to do it in the eleventy configuration file in any case.
Here is an excerpt of externalPosts.json
:
I structured it similar to the collection item object that Eleventy uses internally. This cuts out the need to transform the data later. The date
field is a string because JSON does not have a Date
data type.
In the eleventy configuration file, I did the following to merge this data with my “local” posts:
You will notice that I have a transformExternalPosts
function that converts the date
field from a string to a Date
object for the external posts.
Now, you can use the posts
collection in your page templates. My blog page now includes the 2 posts from CSS Tricks.
Source code
The source code is available in this GitHub repo – https://github.com/robole/eleventy-tutorials. You can find it as an independent project in the external-posts folder.
Can you give the repo a star to indicate that this was a worthwhile tutorial please? 🌟