Files
lunch/lib/recipe-draft.ts
T
kai e3efa97b44
release / which version (push) Successful in 2s
release / olares chart · sanity (push) Successful in 3s
release / app (push) Successful in 58s
release / tag and release (push) Successful in 2s
AI setting with vault, cookbook photo import, text model fallback (26.11.7) (#8)
2026-11-06 22:05:34 +00:00

44 lines
1.8 KiB
TypeScript

import "server-only";
import { INPUT_UNITS } from "./dishes";
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 } = {}) {
const lines = matchLines(recipe.ingredients, await allIngredients(), locale);
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), 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: recipe.steps.map((text) => ({ text, timerMinutes: null })),
};
}
/** 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(/&nbsp;/g, " ")
.replace(/&amp;/g, "&")
.replace(/[ \t]+/g, " ")
.replace(/\n\s*\n+/g, "\n")
.trim()
.slice(0, max);
}