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';
|
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 (!sections[i].content) sections[i].content = [];
|
||||||
|
let steps = [];
|
||||||
|
|
||||||
if (config.outputHtml) {
|
step.value.items.forEach(item => {
|
||||||
return `<span class="recipe--${type}">${tagContent}</span>`;
|
if (item.type === 'text') {
|
||||||
} else {
|
steps.push({ type: "text", content: item.value });
|
||||||
return `${tagContent}`;
|
} 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') {
|
||||||
recipe.steps.forEach((stepTokens, i) => {
|
let cookware = recipe.recipe.cookware[item.index];
|
||||||
if (!steps[i]) steps[i] = [];
|
steps.push({ type: "cookware", content: cookware.name, ...cookware});
|
||||||
|
} else if (item.type === 'timer') {
|
||||||
stepTokens.forEach((token) => {
|
let timer = recipe.recipe.timers[item.index];
|
||||||
if (token.type == "ingredient") {
|
steps.push({ type: "timer", content: timer.quantity.value.value.value + " " + timer.quantity.unit, ...timer });
|
||||||
let { name, quantity, units } = token;
|
} else if (item.type === 'inlineQuantity') {
|
||||||
|
let inlineQuantity = recipe.recipe.inline_quantities[item.index];
|
||||||
if (
|
steps.push({ type: "inlineQuantity", content: inlineQuantity.value.value.value + " " + inlineQuantity.unit, ...inlineQuantity });
|
||||||
config.limitIngredientDecimals &&
|
} else {
|
||||||
!isNaN(config.limitIngredientDecimals)
|
console.log("Unknown type: ", item.type)
|
||||||
) {
|
|
||||||
const decimalPlaces = parseInt(config.limitIngredientDecimals);
|
|
||||||
// Parsing float twice removes any trailing 0s
|
|
||||||
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) => {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue