refactor: simplify parse ingredients code

This commit is contained in:
2025-01-19 19:22:19 +01:00
parent f106460502
commit 78e94ccf82
14 changed files with 201 additions and 137 deletions

View File

@ -90,14 +90,8 @@ async function extractUsingAI(
const markdown = service.turndown(cleanDocument);
streamResponse.enqueue("extracting recipe with openai");
console.log("------- MARKDOWN ------");
console.log(markdown);
console.log("-----------------------");
const recipe = await openai.extractRecipe(markdown);
console.log("------- EXTRACTED ------");
console.log(JSON.stringify(recipe, null, 2));
console.log("-----------------------");
return recipe;
}
@ -142,7 +136,6 @@ async function processCreateRecipeFromUrl(
let recipe: z.infer<typeof recipeSchema> | undefined = undefined;
if (jsonLds.length > 0) {
for (const jsonLd of jsonLds) {
console.log({ content: jsonLd.textContent });
recipe = parseJsonLdToRecipeSchema(jsonLd.textContent || "");
if (recipe) break;
}
@ -152,7 +145,7 @@ async function processCreateRecipeFromUrl(
recipe = await extractUsingAI(url, document, streamResponse);
}
const id = (recipe?.title || title || "").replaceAll(" ", "-");
const id = (recipe?.title || title || "").replace(/--+/, "-");
if (!recipe) {
streamResponse.enqueue("failed to parse recipe");
@ -226,10 +219,6 @@ async function processCreateRecipeFromUrl(
streamResponse.enqueue("finished processing, creating file");
console.log("------- CREATING ------");
console.log(JSON.stringify(recipe, null, 2));
console.log("-----------------------");
await createRecipe(newRecipe.id, newRecipe);
streamResponse.enqueue("id: " + newRecipe.id);
@ -254,6 +243,7 @@ export const handler: Handlers = {
processCreateRecipeFromUrl({ fetchUrl, streamResponse }).then((article) => {
log.debug("created article from link", { article });
}).catch((err) => {
streamResponse.enqueue(`error creating article: ${err}`);
log.error(err);
}).finally(() => {
streamResponse.cancel();

View File

@ -1,5 +1,5 @@
import recipeSchema from "@lib/recipeSchema.ts";
import { parseIngredient } from "@lib/parseIngredient.ts";
import { parseIngredients } from "@lib/parseIngredient.ts";
export function parseJsonLdToRecipeSchema(jsonLdContent: string) {
try {
@ -20,8 +20,8 @@ export function parseJsonLdToRecipeSchema(jsonLdContent: string) {
}
// Map and parse ingredients into the new schema
const ingredients = (data.recipeIngredient || []).map(
parseIngredient,
const ingredients = parseIngredients(
data?.recipeIngredient?.join("\n") || "",
);
const instructions = Array.isArray(data.recipeInstructions)

View File

@ -46,17 +46,19 @@ function ValidRecipe({
portion={portion}
/>
<h3 class="text-3xl my-5">Preparation</h3>
<ol class="list-decimal grid gap-4">
{recipe.instructions && (recipe.instructions.map((instruction) => {
return (
<li
dangerouslySetInnerHTML={{
__html: renderMarkdown(instruction),
}}
/>
);
}))}
</ol>
<div class="pl-2">
<ol class="list-decimal grid gap-4">
{recipe.instructions && (recipe.instructions.map((instruction) => {
return (
<li
dangerouslySetInnerHTML={{
__html: renderMarkdown(instruction),
}}
/>
);
}))}
</ol>
</div>
</>
);
}