Files
Epicure/apps/web/lib/markdown/recipe.ts
T
Arnaud ba1614073b feat: adapt recipe surfaces for batch-cook sessions
- Recipes page action buttons reordered/restyled to push AI generation
  and batch cooking ahead of manual recipe creation.
- Recipe detail page hides meal/drink pairing (doesn't make sense for
  a multi-dish batch session).
- Print pages force light mode regardless of app theme (dark bg +
  hardcoded dark text was unreadable) and render sectioned steps +
  dishes/storage for batch-cook recipes.
- Markdown export mirrors the same sectioned structure.
- Cooking mode tags each step with which dish(es) it belongs to.
- Meal planner: mealPlanEntries.batchDishId lets a slot point at one
  specific dish within a batch session; picking a batch-cook recipe
  now prompts for which dish, and the grid shows the dish name.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-12 02:32:57 +02:00

89 lines
2.9 KiB
TypeScript

type RecipeMarkdownInput = {
title: string;
description: string | null;
baseServings: number;
prepMins: number | null;
cookMins: number | null;
difficulty: "easy" | "medium" | "hard" | null;
sourceUrl: string | null;
ingredients: Array<{ rawName: string; quantity: string | null; unit: string | null; note: string | null }>;
steps: Array<{ instruction: string; timerSeconds: number | null; appliesTo?: string[] }>;
isBatchCook?: boolean;
batchDishes?: Array<{
name: string;
fridgeDays: number;
freezerFriendly: boolean;
freezerNote: string | null;
dayOfInstructions: string;
}>;
};
function formatQuantity(quantity: string | null, unit: string | null): string {
return [quantity, unit].filter(Boolean).join(" ");
}
export function recipeToMarkdown(recipe: RecipeMarkdownInput): string {
const lines: string[] = [`# ${recipe.title}`, ""];
if (recipe.description) {
lines.push(recipe.description, "");
}
const meta: string[] = [`Servings: ${recipe.baseServings}`];
if (recipe.prepMins) meta.push(`Prep: ${recipe.prepMins} min`);
if (recipe.cookMins) meta.push(`Cook: ${recipe.cookMins} min`);
if (recipe.difficulty) meta.push(`Difficulty: ${recipe.difficulty}`);
lines.push(meta.join(" · "), "");
if (recipe.ingredients.length > 0) {
lines.push("## Ingredients", "");
for (const ing of recipe.ingredients) {
const qty = formatQuantity(ing.quantity, ing.unit);
const note = ing.note ? ` (${ing.note})` : "";
lines.push(`- ${qty ? `${qty} ` : ""}${ing.rawName}${note}`);
}
lines.push("");
}
if (recipe.steps.length > 0) {
lines.push("## Instructions", "");
if (recipe.isBatchCook) {
let lastLabel = "";
let n = 0;
for (const step of recipe.steps) {
const label = !step.appliesTo || step.appliesTo.length === 0 ? "Prep" : step.appliesTo.join(" + ");
if (label !== lastLabel) {
if (lastLabel) lines.push("");
lines.push(`### ${label}`, "");
lastLabel = label;
n = 0;
}
n += 1;
lines.push(`${n}. ${step.instruction}`);
}
} else {
recipe.steps.forEach((step, i) => {
const timer = step.timerSeconds ? ` (${Math.round(step.timerSeconds / 60)} min)` : "";
lines.push(`${i + 1}. ${step.instruction}${timer}`);
});
}
lines.push("");
}
if (recipe.isBatchCook && recipe.batchDishes && recipe.batchDishes.length > 0) {
lines.push("## Dishes & storage", "");
for (const dish of recipe.batchDishes) {
const storage = [`Fridge: ${dish.fridgeDays}d`];
if (dish.freezerFriendly) storage.push("Freezer-friendly");
lines.push(`**${dish.name}**`, storage.join(" · ") + (dish.freezerNote ? ` — ${dish.freezerNote}` : ""));
lines.push(`On the day: ${dish.dayOfInstructions}`, "");
}
}
if (recipe.sourceUrl) {
lines.push(`Source: ${recipe.sourceUrl}`);
}
return lines.join("\n").trim() + "\n";
}