refactor: simplify parse ingredients code
This commit is contained in:
@ -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();
|
||||
|
@ -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)
|
||||
|
Reference in New Issue
Block a user