import { Parser } from '@cooklang/cooklang'; import fs from 'fs'; let config = {} export default function (eleventyConfig, pluginOptions) { config = pluginOptions; eleventyConfig.addTemplateFormats('cook'); eleventyConfig.addExtension("cook", cookExtension); }; const cookExtension = { getData: async function (inputPath) { const content = fs.readFileSync(inputPath, "utf-8"); // Parse recipe using cooklang const parser = new Parser(); const recipe = parser.parse(content); let sections = []; let ingredients = recipe.recipe.ingredients || []; let cookware = recipe.recipe.cookware || []; let timers = recipe.recipe.timers || []; const metadata = recipe?.metadata || []; recipe.recipe.sections.forEach((section, i) => { if (!sections[i]) sections[i] = {}; if (section.name != null) { sections[i].title = { type: "title", content: section.name }; } section.content.forEach((step, stepI) => { if (!sections[i].content) sections[i].content = []; let steps = []; step.value.items.forEach(item => { if (item.type === 'text') { steps.push({ type: "text", content: item.value }); } else if (item.type === 'ingredient') { let ingredient = recipe.recipe.ingredients[item.index]; steps.push({ type: "ingredient", content: ingredient.name, ...ingredient }); } else if (item.type === 'cookware') { let cookware = recipe.recipe.cookware[item.index]; steps.push({ type: "cookware", content: cookware.name, ...cookware}); } else if (item.type === 'timer') { let timer = recipe.recipe.timers[item.index]; steps.push({ type: "timer", content: timer.quantity.value.value.value + " " + timer.quantity.unit, ...timer }); } else if (item.type === 'inlineQuantity') { let inlineQuantity = recipe.recipe.inline_quantities[item.index]; steps.push({ type: "inlineQuantity", content: inlineQuantity.value.value.value + " " + inlineQuantity.unit, ...inlineQuantity }); } else { console.log("Unknown type: ", item.type) } }); sections[i].content.push(steps); }); }); // const {value, error} = parser.parse_render( // content // ); // const html = value; // TODO Sections name in ingredients list return { recipe, sections, ingredients, cookware, timers, metadata }; }, compile: async (inputContent) => { // We probably don't need the raw content but it's here if we want return async () => { return inputContent; }; }, };