feat: copy/export as Markdown wherever print exists
Added a shared ExportMarkdownButton (copy to clipboard / download .md) next to every existing print button: recipe, shopping list, collection, meal plan, pantry. Each surface gets a small serializer in lib/markdown/ built from data already in scope at that page — no new queries except pantry, where items now thread through as a prop to PantryPageHeader instead of being fetched only for PantryManager. Also fixes an unrelated bug hit while verifying the collection export: RecipeCard called the client-only useTranslations() hook without "use client", so it rendered fine everywhere it happened to run inside an already-client tree but 500'd — "Couldn't find next-intl config file" — when Next tried to run it as a Server Component, which only happens on the collection detail page (its only caller). Collections with recipes in them were completely broken. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
type CollectionMarkdownInput = {
|
||||
name: string;
|
||||
description: string | null;
|
||||
recipes: Array<{
|
||||
title: string;
|
||||
description: string | null;
|
||||
baseServings: number;
|
||||
prepMins: number | null;
|
||||
cookMins: number | null;
|
||||
difficulty: "easy" | "medium" | "hard" | null;
|
||||
}>;
|
||||
};
|
||||
|
||||
export function collectionToMarkdown(collection: CollectionMarkdownInput): string {
|
||||
const lines: string[] = [`# ${collection.name}`, ""];
|
||||
|
||||
if (collection.description) {
|
||||
lines.push(collection.description, "");
|
||||
}
|
||||
|
||||
for (const recipe of collection.recipes) {
|
||||
lines.push(`## ${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(" · "), "");
|
||||
}
|
||||
|
||||
return lines.join("\n").trim() + "\n";
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
type MealPlanMarkdownInput = {
|
||||
label: string;
|
||||
entries: Array<{
|
||||
day: string;
|
||||
mealType: string;
|
||||
servings: number;
|
||||
note: string | null;
|
||||
recipe: { title: string } | null;
|
||||
}>;
|
||||
};
|
||||
|
||||
export function mealPlanToMarkdown(plan: MealPlanMarkdownInput): string {
|
||||
const lines: string[] = [`# Meal Plan — ${plan.label}`, ""];
|
||||
|
||||
const byDay = new Map<string, typeof plan.entries>();
|
||||
for (const entry of plan.entries) {
|
||||
const group = byDay.get(entry.day) ?? [];
|
||||
group.push(entry);
|
||||
byDay.set(entry.day, group);
|
||||
}
|
||||
|
||||
for (const [day, entries] of byDay) {
|
||||
lines.push(`## ${day}`, "");
|
||||
for (const entry of entries) {
|
||||
const title = entry.recipe?.title ?? "(no recipe)";
|
||||
const note = entry.note ? ` — ${entry.note}` : "";
|
||||
lines.push(`- **${entry.mealType}**: ${title} (${entry.servings} servings)${note}`);
|
||||
}
|
||||
lines.push("");
|
||||
}
|
||||
|
||||
return lines.join("\n").trim() + "\n";
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
type PantryMarkdownInput = {
|
||||
items: Array<{ rawName: string; quantity: string | null; unit: string | null; expiresAt: string | null }>;
|
||||
};
|
||||
|
||||
export function pantryToMarkdown(pantry: PantryMarkdownInput): string {
|
||||
const lines: string[] = ["# Pantry", ""];
|
||||
|
||||
for (const item of pantry.items) {
|
||||
const qty = [item.quantity, item.unit].filter(Boolean).join(" ");
|
||||
const expiry = item.expiresAt ? ` (expires ${new Date(item.expiresAt).toLocaleDateString()})` : "";
|
||||
lines.push(`- ${qty ? `${qty} ` : ""}${item.rawName}${expiry}`);
|
||||
}
|
||||
|
||||
return lines.join("\n").trim() + "\n";
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
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 }>;
|
||||
};
|
||||
|
||||
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", "");
|
||||
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.sourceUrl) {
|
||||
lines.push(`Source: ${recipe.sourceUrl}`);
|
||||
}
|
||||
|
||||
return lines.join("\n").trim() + "\n";
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
type ShoppingListMarkdownInput = {
|
||||
name: string;
|
||||
items: Array<{ rawName: string; quantity: string | null; unit: string | null; aisle: string | null; checked: boolean }>;
|
||||
};
|
||||
|
||||
export function shoppingListToMarkdown(list: ShoppingListMarkdownInput): string {
|
||||
const lines: string[] = [`# ${list.name}`, ""];
|
||||
|
||||
const byAisle = new Map<string, typeof list.items>();
|
||||
for (const item of list.items) {
|
||||
const aisle = item.aisle ?? "Other";
|
||||
const group = byAisle.get(aisle) ?? [];
|
||||
group.push(item);
|
||||
byAisle.set(aisle, group);
|
||||
}
|
||||
|
||||
for (const [aisle, items] of byAisle) {
|
||||
lines.push(`## ${aisle}`, "");
|
||||
for (const item of items) {
|
||||
const qty = [item.quantity, item.unit].filter(Boolean).join(" ");
|
||||
lines.push(`- [${item.checked ? "x" : " "}] ${qty ? `${qty} ` : ""}${item.rawName}`);
|
||||
}
|
||||
lines.push("");
|
||||
}
|
||||
|
||||
return lines.join("\n").trim() + "\n";
|
||||
}
|
||||
Reference in New Issue
Block a user