44 lines
1.8 KiB
TypeScript
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(/ /g, " ")
|
|
.replace(/&/g, "&")
|
|
.replace(/[ \t]+/g, " ")
|
|
.replace(/\n\s*\n+/g, "\n")
|
|
.trim()
|
|
.slice(0, max);
|
|
}
|