Skip to content

@tmlmt/cooklang-parser / Recipe

Class: Recipe

Recipe parser.

Usage

You can either directly provide the recipe string when creating the instance e.g. const recipe = new Recipe('Add @eggs{3}'), or create it first and then pass the recipe string to the parse() method.

Look at the properties to see how the recipe's properties are parsed.

Example

typescript
import { Recipe } from "@tmlmt/cooklang-parser";

const recipeString = `
---
title: Pancakes
tags: [breakfast, easy]
---
Crack the @eggs{3} with @flour{100%g} and @milk{200%mL}

Melt some @butter{50%g} in a #pan on medium heat.

Cook for ~{5%minutes} on each side.
`
const recipe = new Recipe(recipeString);

Constructors

Constructor

new Recipe(content?): Recipe

Creates a new Recipe instance.

Parameters

content?

string

The recipe content to parse.

Returns

Recipe

Properties

cookware

cookware: Cookware[] = []

The parsed recipe cookware.


ingredients

ingredients: Ingredient[] = []

The parsed recipe ingredients.


metadata

metadata: Metadata = {}

The parsed recipe metadata.


sections

sections: Section[] = []

The parsed recipe sections.


servings?

optional servings: number

The parsed recipe servings. Used for scaling. Parsed from one of Metadata.servings, Metadata.yield or Metadata.serves metadata fields.

See

scaleBy() and scaleTo() methods


timers

timers: Timer[] = []

The parsed recipe timers.

Methods

clone()

clone(): Recipe

Clones the recipe.

Returns

Recipe

A new Recipe instance with the same properties.


parse()

parse(content): void

Parses a recipe from a string.

Parameters

content

string

The recipe content to parse.

Returns

void


scaleBy()

scaleBy(factor): Recipe

Scales the recipe by a factor.

Parameters

factor

number

The factor to scale the recipe by.

Returns

Recipe

A new Recipe instance with the scaled ingredients.


scaleTo()

scaleTo(newServings): Recipe

Scales the recipe to a new number of servings. In practice, it calls scaleBy with a factor corresponding to the ratio between newServings and the recipe's servings value.

Parameters

newServings

number

The new number of servings.

Returns

Recipe

A new Recipe instance with the scaled ingredients.

Throws

Error if the recipe does not contains an initial servings value