Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for recipeInstruction as string; added step formating #503

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions src/server/api/modules/recipes/service/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
import z from "zod"

export const RecipeStepSchema = z
.union([z.string(), z.object({ text: z.string() })])
.transform((input): string => {
if (typeof input === "string") return input
return input.text
})
type Step = {
"@context": string;
"@type": string;
text: string;
};

function beautifyInstructions(input: string): string {
const sections = input.split(/\n\n+/);

const formattedSections = sections.map((section, index) => {
const numberedSection = section.replace(/\n/g, '<br>');
return `${index + 1}. ${numberedSection}`;
});

return formattedSections.map(section => `<p>${section}</p>`).join('');
}

export function StepsSchema(steps: string | Step[]){
if (typeof steps === "string") return beautifyInstructions(steps);
return beautifyInstructions(steps.map(step => step.text).join('\n\n'));
}

const baseUrlSchema = z.union([z.string(), z.object({ url: z.string() })])
export const RecipeImageUrlSchema = z
Expand Down
18 changes: 9 additions & 9 deletions src/server/api/modules/recipes/service/scraper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
ExtractNumberSchema,
JsonLdRecipeSchema,
RecipeImageUrlSchema,
RecipeStepSchema,
StepsSchema,
} from "~/server/api/modules/recipes/service/schemas"
import { InsertIngredient, InsertRecipe } from "~/server/db/schema"
import { JSDOM } from "jsdom"
Expand Down Expand Up @@ -90,20 +90,20 @@ export async function hydrateRecipe(url: string) {

const recipeData = getSchemaRecipeFromNodeList(nodeList)

const steps = RecipeStepSchema.array().safeParse(
recipeData.recipeInstructions
)
let steps: string = ""
try {
steps = StepsSchema(recipeData.recipeInstructions)
}
catch(e){
throw new Error("Could not parse steps")
}

const ingredients: string[] = recipeData.recipeIngredient
.flat()
.map((ingredient: string) => ingredient.trim())

const image = RecipeImageUrlSchema.safeParse(recipeData.image)

if (!steps.success) {
throw new Error("Could not parse steps")
}

const ings: Pick<InsertIngredient, "scrapedName">[] = ingredients.map(
(a) => ({ scrapedName: a })
)
Expand All @@ -113,7 +113,7 @@ export async function hydrateRecipe(url: string) {
const recipe: InsertRecipe = {
name: recipeData.name,
url,
steps: steps.data.join("\n"),
steps: steps,
imageUrl: image.success ? image.data : undefined,
servings: servings.success ? servings.data : undefined,
}
Expand Down