Update cooklang plugin for new parser version
This commit is contained in:
parent
6f508915f3
commit
d786f02c9a
|
|
@ -1,4 +1,4 @@
|
|||
import { Recipe, Parser, getImageURL } from '@cooklang/cooklang-ts';
|
||||
import { Parser } from '@cooklang/cooklang';
|
||||
import fs from 'fs';
|
||||
|
||||
let config = {}
|
||||
|
|
@ -10,66 +10,65 @@ export default function (eleventyConfig, pluginOptions) {
|
|||
|
||||
const cookExtension = {
|
||||
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
|
||||
const recipe = new Recipe(content);
|
||||
// Parse recipe using cooklang
|
||||
const parser = new Parser();
|
||||
const recipe = parser.parse(content);
|
||||
|
||||
let steps = [];
|
||||
let ingredients = [];
|
||||
let cookware = [];
|
||||
const recipeTags = recipe?.metadata?.tags?.split(",") || [];
|
||||
let sections = [];
|
||||
let ingredients = recipe.recipe.ingredients || [];
|
||||
let cookware = recipe.recipe.cookware || [];
|
||||
let timers = recipe.recipe.timers || [];
|
||||
|
||||
function getStepTokenHTML(token) {
|
||||
const { quantity, units, name, value, type } = token;
|
||||
let tagContent = "";
|
||||
const metadata = recipe?.metadata || [];
|
||||
|
||||
if (token.type == "timer") {
|
||||
tagContent = `${quantity} ${units}`;
|
||||
} else {
|
||||
tagContent = token.name || token.value;
|
||||
recipe.recipe.sections.forEach((section, i) => {
|
||||
if (!sections[i]) sections[i] = {};
|
||||
if (section.name != null) {
|
||||
sections[i].title = { type: "title", content: section.name };
|
||||
}
|
||||
|
||||
if (config.outputHtml) {
|
||||
return `<span class="recipe--${type}">${tagContent}</span>`;
|
||||
} else {
|
||||
return `${tagContent}`;
|
||||
}
|
||||
}
|
||||
|
||||
recipe.steps.forEach((stepTokens, i) => {
|
||||
if (!steps[i]) steps[i] = [];
|
||||
|
||||
stepTokens.forEach((token) => {
|
||||
if (token.type == "ingredient") {
|
||||
let { name, quantity, units } = token;
|
||||
|
||||
if (
|
||||
config.limitIngredientDecimals &&
|
||||
!isNaN(config.limitIngredientDecimals)
|
||||
) {
|
||||
const decimalPlaces = parseInt(config.limitIngredientDecimals);
|
||||
// Parsing float twice removes any trailing 0s
|
||||
quantity = parseFloat(parseFloat(quantity).toFixed(decimalPlaces));
|
||||
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)
|
||||
}
|
||||
ingredients.push({ name, quantity, units });
|
||||
}
|
||||
|
||||
if (token.type == "cookware") {
|
||||
const { name } = token;
|
||||
cookware.push({ name });
|
||||
}
|
||||
|
||||
steps[i].push(getStepTokenHTML(token));
|
||||
});
|
||||
sections[i].content.push(steps);
|
||||
});
|
||||
});
|
||||
|
||||
// const {value, error} = parser.parse_render(
|
||||
// content
|
||||
// );
|
||||
// const html = value;
|
||||
|
||||
// TODO Sections name in ingredients list
|
||||
|
||||
return {
|
||||
recipe,
|
||||
steps,
|
||||
sections,
|
||||
ingredients,
|
||||
cookware,
|
||||
recipeTags,
|
||||
timers,
|
||||
metadata
|
||||
};
|
||||
},
|
||||
compile: async (inputContent) => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue