Skip to content

Commit 895d0cb

Browse files
Add support for Short-hand ingredient preparations (cooklang#28)
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.
1 parent 3187661 commit 895d0cb

File tree

5 files changed

+17
-1
lines changed

5 files changed

+17
-1
lines changed

src/Parser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ export default class Parser {
122122
parseQuantity(groups.mIngredientQuantity) ??
123123
this.defaultIngredientAmount,
124124
units: parseUnits(groups.mIngredientUnits) ?? this.defaultUnits,
125+
...(groups.mIngredientPreparation ? { preparation: groups.mIngredientPreparation } : null),
125126
};
126127

127128
if (this.includeStepNumber) ingredient.step = stepNumber;

src/Recipe.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export default class Recipe {
5555
stepStr += '{';
5656
if (item.quantity) stepStr += item.quantity;
5757
if ('units' in item && item.units) stepStr += '%' + item.units;
58+
if ('preparation' in item && item.preparation) stepStr += `(${item.preparation})`
5859
stepStr += '}';
5960
}
6061
}

src/cooklang.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface Ingredient {
88
name: string;
99
quantity: string | number;
1010
units: string;
11+
preparation?: string;
1112
step?: number;
1213
}
1314

src/tokens.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const metadata = /^>>\s*(?<key>.+?):\s*(?<value>.+)/;
22

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

66
const multiwordCookware = /#(?<mCookwareName>[^@#~[]+?)\{(?<mCookwareQuantity>.*?)\}/;

tests/custom.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,16 @@ tests:
198198
value: "step"
199199
metadata:
200200
"source": "example.com"
201+
202+
testShortHandPreparation:
203+
source: |
204+
@onion{1}(peeled and finely chopped)
205+
result:
206+
steps:
207+
-
208+
- type: ingredient
209+
quantity: 1
210+
units: ""
211+
name: "onion"
212+
preparation: "peeled and finely chopped"
213+
metadata: []

0 commit comments

Comments
 (0)