Skip to content

Instantly share code, notes, and snippets.

@robdodson
Created February 10, 2020 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robdodson/fa563e1a0fa87c48a3f699d2f4fa91be to your computer and use it in GitHub Desktop.
Save robdodson/fa563e1a0fa87c48a3f699d2f4fa91be to your computer and use it in GitHub Desktop.
module.exports = (collection) => {
return collection
.slice(0, 3);
};
@zachleat
Copy link

zachleat commented Feb 10, 2020

would something like this work?

module.exports = function(eleventyConfig) {
	if(process.env.TEST_ENV) {
		eleventyConfig.addCollection("recent", function(collectionApi) {
			return collectionApi.getFilteredByGlob(["post1.md", "post2.md", "post3.md"]);
		});
	}
};

This will override collections.recent no matter how many posts have this tag assigned.

@robdodson
Copy link
Author

I was thinking something like this, but it feels pretty dangerous:

if (process.env.TEST_ENV) {
  eleventyConfig.addCollection('mocks', (collection) => {
    // This seems like a bad idea 😈 Creates a fake collection to mutate the collectionApi
    collection.items = collection.items.filter((item) => MOCK_POST_SLUGS.includes(item.fileSlug));
    // Have to return something so just return collection.getAll
    return collection.getAll();
  });
}

// ... now all other custom collections start with only the mock posts available to them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment