feat: Implement auto-parsing of markdown in WriteRecipePage with error handling
This commit is contained in:
@@ -26,6 +26,11 @@ export abstract class RecipeParser {
|
||||
|
||||
/**
|
||||
* Hjälpfunktion: parsa ingrediens-rad
|
||||
* Hanterar format som:
|
||||
* - "3 ägg"
|
||||
* - "150 g lax"
|
||||
* - "1 msk senap"
|
||||
* - "salt och peppar"
|
||||
*/
|
||||
protected parseIngredientLine(line: string): {
|
||||
quantity: number;
|
||||
@@ -35,7 +40,17 @@ export abstract class RecipeParser {
|
||||
const cleaned = line.replace(/<[^>]+>/g, '').trim();
|
||||
if (!cleaned) return null;
|
||||
|
||||
const match = cleaned.match(/^([\d.,]+)?\s*([a-zåäö]*)\s*(.+)$/i);
|
||||
// Kända enheter
|
||||
const knownUnits = [
|
||||
'g', 'kg', 'hg', 'ml', 'dl', 'l',
|
||||
'st', 'tsk', 'msk', 'krm', 'matsked', 'tesked',
|
||||
'pris', 'portion', 'burk', 'förp', 'paket',
|
||||
];
|
||||
|
||||
// Försök extrahera: [quantity] [unit?] [productName]
|
||||
// Regex: början med valfri mängd, sedan valfritt ord (potentiell enhet), sedan resten
|
||||
const match = cleaned.match(/^([\d.,]+)?\s*([a-zåäö]*)\s*(.*)$/i);
|
||||
|
||||
if (!match) {
|
||||
return {
|
||||
quantity: 0,
|
||||
@@ -44,10 +59,31 @@ export abstract class RecipeParser {
|
||||
};
|
||||
}
|
||||
|
||||
let quantity = match[1] ? parseFloat(match[1].replace(',', '.')) : 0;
|
||||
let potentialUnit = match[2]?.toLowerCase().trim() || '';
|
||||
let productName = match[3]?.trim() || '';
|
||||
|
||||
// Om potentialUnit är inte en känd enhet, lägg det tillbaka i produktnamnet
|
||||
if (potentialUnit && !knownUnits.includes(potentialUnit)) {
|
||||
productName = potentialUnit + (productName ? ' ' + productName : '');
|
||||
potentialUnit = '';
|
||||
}
|
||||
|
||||
// Om inget produktnamn men vi hade potentialUnit, denna är faktiskt produktnamnet
|
||||
if (!productName && potentialUnit) {
|
||||
productName = potentialUnit;
|
||||
potentialUnit = '';
|
||||
}
|
||||
|
||||
// Fallback: om vi har mängd men inget annat, vara produktnamn
|
||||
if (quantity > 0 && !potentialUnit && !productName) {
|
||||
return null; // Ogiltigt
|
||||
}
|
||||
|
||||
return {
|
||||
quantity: match[1] ? parseFloat(match[1].replace(',', '.')) : 0,
|
||||
unit: (match[2] || 'st').toLowerCase().trim(),
|
||||
name: match[3].trim(),
|
||||
quantity,
|
||||
unit: potentialUnit || 'st',
|
||||
name: productName,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,11 +72,46 @@ export default function WriteRecipePage() {
|
||||
if (prefilledMarkdown) {
|
||||
setMarkdown(prefilledMarkdown);
|
||||
sessionStorage.removeItem('prefilled_markdown');
|
||||
// Auto-parse om Markdown finns
|
||||
setTimeout(() => {
|
||||
// Markeringen för auto-parse görs via en flag
|
||||
sessionStorage.setItem('auto_parse_markdown', 'true');
|
||||
}, 100);
|
||||
// Auto-parse markdown från snabbimport
|
||||
setIsParsing(true);
|
||||
(async () => {
|
||||
try {
|
||||
const res = await fetch('/api/parse-markdown-proxy', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ markdown: prefilledMarkdown }),
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const errorText = await res.text();
|
||||
throw new Error(errorText || 'Kunde inte tolka recept');
|
||||
}
|
||||
|
||||
const data = await res.json();
|
||||
|
||||
const rows: ParsedIngredientRow[] = data.ingredients.map(
|
||||
(ing: Omit<ParsedIngredientRow, 'selectedProductId' | 'editedQuantity' | 'editedUnit' | 'editedNote'>) => ({
|
||||
...ing,
|
||||
selectedProductId: ing.suggestions[0]?.productId ?? 0,
|
||||
editedQuantity: String(ing.quantity),
|
||||
editedUnit: ing.unit,
|
||||
editedNote: ing.note ?? '',
|
||||
}),
|
||||
);
|
||||
|
||||
setParsed(data);
|
||||
setEditedName(data.name);
|
||||
setEditedDescription(data.description ?? '');
|
||||
setEditedInstructions(data.instructions ?? '');
|
||||
setIngredients(rows);
|
||||
setStep('review');
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : 'Något gick fel vid tolkning.';
|
||||
setError(message);
|
||||
} finally {
|
||||
setIsParsing(false);
|
||||
}
|
||||
})();
|
||||
}
|
||||
}, []);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user