Update cooklang plugin for new parser version

This commit is contained in:
TheThomaas 2025-12-24 22:14:02 +01:00
parent 6f508915f3
commit d786f02c9a

View file

@ -1,4 +1,4 @@
import { Recipe, Parser, getImageURL } from '@cooklang/cooklang-ts'; import { Parser } from '@cooklang/cooklang';
import fs from 'fs'; import fs from 'fs';
let config = {} let config = {}
@ -10,66 +10,65 @@ export default function (eleventyConfig, pluginOptions) {
const cookExtension = { const cookExtension = {
getData: async function (inputPath) { getData: async function (inputPath) {
const content = fs.readFileSync(inputPath, "utf-8").split("---")[2]; const content = fs.readFileSync(inputPath, "utf-8");
// Parse recipe using cooklang-ts // Parse recipe using cooklang
const recipe = new Recipe(content); const parser = new Parser();
const recipe = parser.parse(content);
let steps = []; let sections = [];
let ingredients = []; let ingredients = recipe.recipe.ingredients || [];
let cookware = []; let cookware = recipe.recipe.cookware || [];
const recipeTags = recipe?.metadata?.tags?.split(",") || []; let timers = recipe.recipe.timers || [];
function getStepTokenHTML(token) { const metadata = recipe?.metadata || [];
const { quantity, units, name, value, type } = token;
let tagContent = "";
if (token.type == "timer") { recipe.recipe.sections.forEach((section, i) => {
tagContent = `${quantity} ${units}`; if (!sections[i]) sections[i] = {};
} else { if (section.name != null) {
tagContent = token.name || token.value; sections[i].title = { type: "title", content: section.name };
} }
section.content.forEach((step, stepI) => {
if (config.outputHtml) { if (!sections[i].content) sections[i].content = [];
return `<span class="recipe--${type}">${tagContent}</span>`; let steps = [];
} else {
return `${tagContent}`; step.value.items.forEach(item => {
} if (item.type === 'text') {
} steps.push({ type: "text", content: item.value });
} else if (item.type === 'ingredient') {
recipe.steps.forEach((stepTokens, i) => { let ingredient = recipe.recipe.ingredients[item.index];
if (!steps[i]) steps[i] = []; steps.push({ type: "ingredient", content: ingredient.name, ...ingredient });
} else if (item.type === 'cookware') {
stepTokens.forEach((token) => { let cookware = recipe.recipe.cookware[item.index];
if (token.type == "ingredient") { steps.push({ type: "cookware", content: cookware.name, ...cookware});
let { name, quantity, units } = token; } else if (item.type === 'timer') {
let timer = recipe.recipe.timers[item.index];
if ( steps.push({ type: "timer", content: timer.quantity.value.value.value + " " + timer.quantity.unit, ...timer });
config.limitIngredientDecimals && } else if (item.type === 'inlineQuantity') {
!isNaN(config.limitIngredientDecimals) let inlineQuantity = recipe.recipe.inline_quantities[item.index];
) { steps.push({ type: "inlineQuantity", content: inlineQuantity.value.value.value + " " + inlineQuantity.unit, ...inlineQuantity });
const decimalPlaces = parseInt(config.limitIngredientDecimals); } else {
// Parsing float twice removes any trailing 0s console.log("Unknown type: ", item.type)
quantity = parseFloat(parseFloat(quantity).toFixed(decimalPlaces));
} }
ingredients.push({ name, quantity, units }); });
} sections[i].content.push(steps);
if (token.type == "cookware") {
const { name } = token;
cookware.push({ name });
}
steps[i].push(getStepTokenHTML(token));
}); });
}); });
// const {value, error} = parser.parse_render(
// content
// );
// const html = value;
// TODO Sections name in ingredients list
return { return {
recipe, recipe,
steps, sections,
ingredients, ingredients,
cookware, cookware,
recipeTags, timers,
metadata
}; };
}, },
compile: async (inputContent) => { compile: async (inputContent) => {