fix: make recipe crawling work
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import recipeSchema from "@lib/recipeSchema.ts";
|
||||
import { parseIngredients } from "@lib/parseIngredient.ts";
|
||||
import recipeSchema, { Recipe } from "@lib/recipeSchema.ts";
|
||||
|
||||
export function parseJsonLdToRecipeSchema(jsonLdContent: string) {
|
||||
try {
|
||||
@@ -19,12 +18,7 @@ export function parseJsonLdToRecipeSchema(jsonLdContent: string) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Map and parse ingredients into the new schema
|
||||
const ingredients = parseIngredients(
|
||||
data?.recipeIngredient?.join("\n") || "",
|
||||
);
|
||||
|
||||
const instructions = Array.isArray(data.recipeInstructions)
|
||||
const recipeInstructions = Array.isArray(data.recipeInstructions)
|
||||
? data.recipeInstructions.map((instr: unknown) => {
|
||||
if (!instr) return "";
|
||||
if (typeof instr === "string") return instr;
|
||||
@@ -36,43 +30,41 @@ export function parseJsonLdToRecipeSchema(jsonLdContent: string) {
|
||||
: [];
|
||||
|
||||
// Parse servings
|
||||
const servings = parseServings(data.recipeYield);
|
||||
const recipeYield = parseServings(data.recipeYield);
|
||||
|
||||
// Parse times
|
||||
const prepTime = parseDuration(data.prepTime);
|
||||
const cookTime = parseDuration(data.cookTime);
|
||||
const totalTime = parseDuration(data.totalTime);
|
||||
|
||||
// Extract tags
|
||||
const tags = data.keywords
|
||||
const keywords = data.keywords
|
||||
? Array.isArray(data.keywords)
|
||||
? data.keywords
|
||||
: data.keywords.split(",").map((tag: string) => tag.trim())
|
||||
: [];
|
||||
|
||||
// Build the recipe object
|
||||
const recipe = {
|
||||
const recipe: Recipe = {
|
||||
_type: "Recipe",
|
||||
title: data.name || "Unnamed Recipe",
|
||||
name: data.name || "Unnamed Recipe",
|
||||
image: pickImage(image || data.image || ""),
|
||||
author: Array.isArray(data.author)
|
||||
? data.author.map((a: { name: string }) => a.name).join(", ")
|
||||
: data.author?.name || "",
|
||||
author: {
|
||||
"_type": "Person",
|
||||
name: Array.isArray(data.author)
|
||||
? data.author.map((a: { name: string }) => a.name).join(", ")
|
||||
: data.author?.name || "",
|
||||
},
|
||||
description: data.description || "",
|
||||
ingredients,
|
||||
instructions,
|
||||
servings,
|
||||
prepTime,
|
||||
cookTime,
|
||||
recipeIngredient: data.recipeIngredient,
|
||||
recipeInstructions,
|
||||
recipeYield,
|
||||
totalTime,
|
||||
tags,
|
||||
notes: data.notes || [],
|
||||
keywords,
|
||||
};
|
||||
|
||||
// Validate against the schema
|
||||
return recipeSchema.parse(recipe);
|
||||
} catch (error) {
|
||||
console.error("Invalid JSON-LD content or parsing error:", error);
|
||||
console.log("Invalid JSON-LD content or parsing error:", error);
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user