Skip to content

Commit

Permalink
Add support for Short-hand ingredient preparations (#28)
Browse files Browse the repository at this point in the history
This is described in the specification under the advanced section.

For what it's worth, the official Cooklang playground does not properly support Short-hand ingredient preparations either.
  • Loading branch information
alexanderson1993 authored Dec 21, 2024
1 parent 3187661 commit 895d0cb
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export default class Parser {
parseQuantity(groups.mIngredientQuantity) ??
this.defaultIngredientAmount,
units: parseUnits(groups.mIngredientUnits) ?? this.defaultUnits,
...(groups.mIngredientPreparation ? { preparation: groups.mIngredientPreparation } : null),
};

if (this.includeStepNumber) ingredient.step = stepNumber;
Expand Down
1 change: 1 addition & 0 deletions src/Recipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export default class Recipe {
stepStr += '{';
if (item.quantity) stepStr += item.quantity;
if ('units' in item && item.units) stepStr += '%' + item.units;
if ('preparation' in item && item.preparation) stepStr += `(${item.preparation})`
stepStr += '}';
}
}
Expand Down
1 change: 1 addition & 0 deletions src/cooklang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface Ingredient {
name: string;
quantity: string | number;
units: string;
preparation?: string;
step?: number;
}

Expand Down
2 changes: 1 addition & 1 deletion src/tokens.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const metadata = /^>>\s*(?<key>.+?):\s*(?<value>.+)/;

const multiwordIngredient = /@(?<mIngredientName>[^@#~[]+?)\{(?<mIngredientQuantity>[^]*?)(?:%(?<mIngredientUnits>[^}]+?))?\}/;
const multiwordIngredient = /@(?<mIngredientName>[^@#~[]+?)\{(?<mIngredientQuantity>[^]*?)(?:%(?<mIngredientUnits>[^}]+?))?\}(\((?<mIngredientPreparation>[^]*?)\))?/;
const singleWordIngredient = /@(?<sIngredientName>[^\s\t\p{Zs}\p{P}]+)/;

const multiwordCookware = /#(?<mCookwareName>[^@#~[]+?)\{(?<mCookwareQuantity>.*?)\}/;
Expand Down
13 changes: 13 additions & 0 deletions tests/custom.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,16 @@ tests:
value: "step"
metadata:
"source": "example.com"

testShortHandPreparation:
source: |
@onion{1}(peeled and finely chopped)
result:
steps:
-
- type: ingredient
quantity: 1
units: ""
name: "onion"
preparation: "peeled and finely chopped"
metadata: []

0 comments on commit 895d0cb

Please sign in to comment.