Replies: 1 comment
-
Can’t say I suddenly smartened up and solved it like this—but used AI help to refactor the code. A case as mentioned can be handled like this: // Function to sort posts by (external) publication date
function sortPostsByDate(a, b) {
return Number(a.data.date_publication) > Number(b.data.date_publication) ? 1 : -1;
}
// Function to create collections
function createCollection(collectionApi, tag, limit) {
let results = collectionApi.getFilteredByTag(tag).sort(sortPostsByDate);
return limit ? results.slice(-limit) : results;
}
// Create collection sorted by (external) publication date
eleventyConfig.addCollection("postsByPubDate", function(collectionApi) {
return createCollection(collectionApi, "posts");
});
// Create collection of last 5,000 posts sorted by (external) publication date
eleventyConfig.addCollection("postsByPubDateHome", function(collectionApi) {
return createCollection(collectionApi, "posts", 5000);
});
// Create collection of last 250 posts sorted by (external) publication date
eleventyConfig.addCollection("postsByPubDateFeed", function(collectionApi) {
return createCollection(collectionApi, "posts", 250);
});
// Create (date-sorted) collection of 250 posts and all tools
eleventyConfig.addCollection("postsAndTools", function(collectionApi) {
let posts = createCollection(collectionApi, "posts", 250);
let tools = collectionApi.getFilteredByTag("tools");
return posts.concat(tools);
}); Always appreciating pointers to improve and learn, but closing this one accordingly. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I’m currently working with a hack of a solution (duplication) to limit the number of entries in one particular collection:
As far as I recall, the approach was needed because of an interplay of sorting in the places where I use these collections. Getting a limited set back solved those problems.
Ideally, I’d refer to the same collection but pass on how many entries it should return.
I’ve reviewed and tried things on and off, but as it’s not my strong suit and as I may miss something else entirely, I’d love to learn from you, if you see options to simplify or approach this differently. Thank you!
Beta Was this translation helpful? Give feedback.
All reactions