feat: created recipes structure

This commit is contained in:
Leyla Becker 2026-02-12 14:57:57 -06:00
parent a035c08249
commit f93207b4e3
7 changed files with 432 additions and 13 deletions

View file

@ -208,12 +208,6 @@ module.exports = (eleventyConfig) => {
return grouped;
});
eleventyConfig.addCollection("contentTags", (collectionApi) => {
const posts = collectionApi.getFilteredByGlob("posts/**/*.md").filter(isReleased);
return [...new Set(posts.flatMap(post => getPostTags(post, md)))].sort();
});
eleventyConfig.addCollection("postsByTag", (collectionApi) => {
const posts = collectionApi.getFilteredByGlob("posts/**/*.md").filter(isReleased);
const tagMap = {};
@ -224,6 +218,7 @@ module.exports = (eleventyConfig) => {
tagMap[tag] = {
name: tag,
posts: [post, ...(tagMap[tag]?.posts ?? [])],
recipes: tagMap[tag]?.recipes ?? [],
}
})
});
@ -239,6 +234,109 @@ module.exports = (eleventyConfig) => {
return tagMap;
});
// Recipe collections
eleventyConfig.addCollection("recipes", (collectionApi) => {
return collectionApi.getFilteredByGlob("recipes/**/*.md")
.filter(recipe => recipe.data.draft !== true);
});
eleventyConfig.addCollection("allRecipes", (collectionApi) => {
return collectionApi.getFilteredByGlob("recipes/**/*.md");
});
eleventyConfig.addCollection("recipesBySlug", (collectionApi) => {
const recipes = collectionApi.getFilteredByGlob("recipes/**/*.md");
// Group recipes by slug using reduce
const grouped = recipes.reduce((acc, recipe) => {
const slug = recipe.data.recipeSlug;
return {
...acc,
[slug]: [...(acc[slug] || []), recipe],
};
}, {});
// Transform grouped recipes into final structure with sorted versions and newest non-draft
return Object.entries(grouped).reduce((acc, [slug, recipeList]) => {
const versions = [...recipeList].sort((a, b) => a.data.recipeVersion - b.data.recipeVersion);
const newest = [...versions].reverse().find(r => r.data.draft !== true) || null;
return {
...acc,
[slug]: { versions, newest },
};
}, {});
});
// Get tags from recipes (only from newest non-draft versions)
const getRecipeTags = (recipe) => {
const filePath = recipe.inputPath;
try {
const content = fs.readFileSync(filePath, 'utf-8');
const tags = extractTags(content, md);
return tags.map(tag => tag.toLowerCase());
} catch (e) {
return [];
}
};
eleventyConfig.addCollection("contentTags", (collectionApi) => {
const posts = collectionApi.getFilteredByGlob("posts/**/*.md").filter(isReleased);
const recipes = collectionApi.getFilteredByGlob("recipes/**/*.md")
.filter(r => r.data.isNewestVersion && r.data.draft !== true);
const postTags = posts.flatMap(post => getPostTags(post, md));
const recipeTags = recipes.flatMap(recipe => getRecipeTags(recipe));
return [...new Set([...postTags, ...recipeTags])].sort();
});
eleventyConfig.addCollection("contentByTag", (collectionApi) => {
const posts = collectionApi.getFilteredByGlob("posts/**/*.md").filter(isReleased);
const recipes = collectionApi.getFilteredByGlob("recipes/**/*.md")
.filter(r => r.data.isNewestVersion && r.data.draft !== true);
const sortByDate = (a, b) => {
const aDate = a.data.createdAt || a.date;
const bDate = b.data.createdAt || b.date;
return aDate - bDate;
};
// Build tag map from posts
const postTagMap = posts.reduce((acc, post) => {
const tags = getPostTags(post, md);
return tags.reduce((innerAcc, tag) => ({
...innerAcc,
[tag]: {
name: tag,
posts: [...(innerAcc[tag]?.posts || []), post],
recipes: innerAcc[tag]?.recipes || [],
},
}), acc);
}, {});
// Merge recipe tags into the tag map
const tagMap = recipes.reduce((acc, recipe) => {
const tags = getRecipeTags(recipe);
return tags.reduce((innerAcc, tag) => ({
...innerAcc,
[tag]: {
name: tag,
posts: innerAcc[tag]?.posts || [],
recipes: [...(innerAcc[tag]?.recipes || []), recipe],
},
}), acc);
}, postTagMap);
// Return with sorted posts
return Object.entries(tagMap).reduce((acc, [tag, tagData]) => ({
...acc,
[tag]: {
...tagData,
posts: [...tagData.posts].sort(sortByDate),
},
}), {});
});
// Cache busting filter: returns hashed filename
eleventyConfig.addFilter("fileHash", (file, dir = "css") => {
const hash = getFileHash(file, dir);
@ -299,7 +397,6 @@ module.exports = (eleventyConfig) => {
return {
dir: {
input: ".",
includes: "_includes",
output: "_site"
},