65 lines
2.8 KiB
TypeScript
65 lines
2.8 KiB
TypeScript
import "server-only";
|
|
import type { AiSettings } from "./ai";
|
|
import { INPUT_UNITS, stepIngredient } from "./dishes";
|
|
import { standardizeSteps } from "./recipe-standardize";
|
|
import { withHousehold } from "./db";
|
|
import { allIngredients } from "./ingredients";
|
|
import { matchLines, type RawRecipe } from "./recipe-import";
|
|
import { toBase } from "./units";
|
|
|
|
// Ein gelesenes Rezept (JSON-LD, Textmodell oder Kochbuchfoto) → Entwurf für
|
|
// das Formular (components/dishes/dish-form.tsx). Gespeichert wird nichts.
|
|
export async function draftFromRecipe(
|
|
recipe: RawRecipe,
|
|
locale: string,
|
|
extra: { sourceUrl?: string | null; imageUrl?: string | null; ai?: AiSettings | null; householdId?: string } = {},
|
|
) {
|
|
// Mit Haushalt auch seine eigenen Zutaten (26.11.38).
|
|
const all = extra.householdId ? await withHousehold(extra.householdId, (c) => allIngredients(c)) : await allIngredients();
|
|
const lines = matchLines(recipe.ingredients, all, locale);
|
|
// Schritte in die Hausform (Abschnitte, eine Handlung je Schritt, Timer aus der Quelle).
|
|
const byId = new Map(all.map((e) => [e.id, e]));
|
|
const matched = lines.filter((l) => l.ingredient).map((l) => byId.get(l.ingredient!.id)!);
|
|
const styled = await standardizeSteps(
|
|
extra.ai ?? null,
|
|
locale,
|
|
recipe.title,
|
|
matched.map((e) => ({ id: e.id, name: e.names[locale] ?? e.names.en ?? "" })),
|
|
recipe.steps.map((text) => ({ text })),
|
|
matched.map(stepIngredient),
|
|
);
|
|
return {
|
|
title: recipe.title,
|
|
portions: recipe.yield ?? 4,
|
|
effortMinutes: recipe.totalMinutes,
|
|
sourceUrl: extra.sourceUrl ?? null,
|
|
imageUrl: extra.imageUrl ?? null,
|
|
ingredients: lines
|
|
.filter((l) => l.ingredient)
|
|
.map((l) => {
|
|
// Maße, die das Formular nicht anbietet (cl, Tasse), gleich in die Basiseinheit.
|
|
if (l.qty !== null && l.unit && !(INPUT_UNITS as readonly string[]).includes(l.unit)) {
|
|
return { ingredient: l.ingredient, qty: toBase(l.qty, l.unit, l.ingredient!.unit, l.ingredient!.packSize), unit: l.ingredient!.unit, note: l.note };
|
|
}
|
|
return { ingredient: l.ingredient, qty: l.qty, unit: l.unit, note: l.note };
|
|
}),
|
|
unmatched: lines.filter((l) => !l.ingredient).map((l) => l.raw),
|
|
steps: styled.steps.map((s) => ({ text: s.text, section: s.section, timerMinutes: s.timerSeconds ? Math.max(1, Math.round(s.timerSeconds / 60)) : null })),
|
|
styledByAi: styled.byAi,
|
|
};
|
|
}
|
|
|
|
/** Sichtbarer Text einer Seite für das Textmodell, gekürzt. */
|
|
export function pageText(html: string, max = 20_000): string {
|
|
return html
|
|
.replace(/<(script|style|noscript|svg|nav|footer|header)[\s\S]*?<\/\1>/gi, " ")
|
|
.replace(/<br\s*\/?>|<\/(p|li|h\d|div|tr)>/gi, "\n")
|
|
.replace(/<[^>]+>/g, " ")
|
|
.replace(/ /g, " ")
|
|
.replace(/&/g, "&")
|
|
.replace(/[ \t]+/g, " ")
|
|
.replace(/\n\s*\n+/g, "\n")
|
|
.trim()
|
|
.slice(0, max);
|
|
}
|