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
|
* Hjälpfunktion: parsa ingrediens-rad
|
||||||
|
* Hanterar format som:
|
||||||
|
* - "3 ägg"
|
||||||
|
* - "150 g lax"
|
||||||
|
* - "1 msk senap"
|
||||||
|
* - "salt och peppar"
|
||||||
*/
|
*/
|
||||||
protected parseIngredientLine(line: string): {
|
protected parseIngredientLine(line: string): {
|
||||||
quantity: number;
|
quantity: number;
|
||||||
@@ -35,7 +40,17 @@ export abstract class RecipeParser {
|
|||||||
const cleaned = line.replace(/<[^>]+>/g, '').trim();
|
const cleaned = line.replace(/<[^>]+>/g, '').trim();
|
||||||
if (!cleaned) return null;
|
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) {
|
if (!match) {
|
||||||
return {
|
return {
|
||||||
quantity: 0,
|
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 {
|
return {
|
||||||
quantity: match[1] ? parseFloat(match[1].replace(',', '.')) : 0,
|
quantity,
|
||||||
unit: (match[2] || 'st').toLowerCase().trim(),
|
unit: potentialUnit || 'st',
|
||||||
name: match[3].trim(),
|
name: productName,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -72,11 +72,46 @@ export default function WriteRecipePage() {
|
|||||||
if (prefilledMarkdown) {
|
if (prefilledMarkdown) {
|
||||||
setMarkdown(prefilledMarkdown);
|
setMarkdown(prefilledMarkdown);
|
||||||
sessionStorage.removeItem('prefilled_markdown');
|
sessionStorage.removeItem('prefilled_markdown');
|
||||||
// Auto-parse om Markdown finns
|
// Auto-parse markdown från snabbimport
|
||||||
setTimeout(() => {
|
setIsParsing(true);
|
||||||
// Markeringen för auto-parse görs via en flag
|
(async () => {
|
||||||
sessionStorage.setItem('auto_parse_markdown', 'true');
|
try {
|
||||||
}, 100);
|
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