cooklang-parse 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +185 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11538 -0
- package/dist/semantics.d.ts +6 -0
- package/dist/semantics.d.ts.map +1 -0
- package/dist/types.d.ts +46 -0
- package/dist/types.d.ts.map +1 -0
- package/grammars/cooklang.ohm +164 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Brian Sunter
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
# cooklang-parse
|
|
2
|
+
|
|
3
|
+
> A simple, type-safe [Cooklang](https://cooklang.org) parser built with [Ohm.js](https://ohmjs.org)
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/cooklang-parse)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- Full Cooklang spec support including ingredients, cookware, timers, metadata, sections, notes, and YAML frontmatter
|
|
11
|
+
- Written in TypeScript with exported type definitions
|
|
12
|
+
- Single function API — `parseCooklang(source)` returns a structured recipe
|
|
13
|
+
- 190 tests with canonical parity against the [Rust reference implementation](https://github.com/cooklang/cooklang-rs)
|
|
14
|
+
- Source position tracking and parse error reporting
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install cooklang-parse
|
|
20
|
+
# or
|
|
21
|
+
bun add cooklang-parse
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Quick Start
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { parseCooklang } from "cooklang-parse"
|
|
28
|
+
|
|
29
|
+
const recipe = parseCooklang(`
|
|
30
|
+
>> servings: 4
|
|
31
|
+
|
|
32
|
+
Preheat #oven to 180C.
|
|
33
|
+
Mix @flour{250%g} and @eggs{3} in a #bowl{}.
|
|
34
|
+
Bake for ~{20%minutes}.
|
|
35
|
+
`)
|
|
36
|
+
|
|
37
|
+
recipe.metadata // { servings: 4 }
|
|
38
|
+
recipe.ingredients // [{ type: "ingredient", name: "flour", quantity: 250, units: "g", fixed: false }, ...]
|
|
39
|
+
recipe.cookware // [{ type: "cookware", name: "oven", quantity: 1, units: "" }, ...]
|
|
40
|
+
recipe.timers // [{ type: "timer", name: "", quantity: 20, units: "minutes" }]
|
|
41
|
+
recipe.steps // [[{ type: "text", value: "Preheat " }, { type: "cookware", ... }, ...], ...]
|
|
42
|
+
recipe.errors // [] (parse errors and warnings)
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Cooklang Syntax
|
|
46
|
+
|
|
47
|
+
| Syntax | Description | Example |
|
|
48
|
+
|--------|-------------|---------|
|
|
49
|
+
| `@name{qty%unit}` | Ingredient with quantity and unit | `@flour{250%g}` |
|
|
50
|
+
| `@name{qty}` | Ingredient with quantity only | `@eggs{3}` |
|
|
51
|
+
| `@name` | Ingredient (implicit "some") | `@salt` |
|
|
52
|
+
| `@name{}` | Multi-word ingredient | `@olive oil{}` |
|
|
53
|
+
| `#name{}` | Cookware | `#cast iron skillet{}` |
|
|
54
|
+
| `~name{qty%unit}` | Named timer | `~resting{30%minutes}` |
|
|
55
|
+
| `~{qty%unit}` | Anonymous timer | `~{20%minutes}` |
|
|
56
|
+
| `-- comment` | Inline comment (space required after `--`) | `-- note to self` |
|
|
57
|
+
| `[- text -]` | Block comment | `[- Chef's tip -]` |
|
|
58
|
+
| `> text` | Note | `> Serve immediately` |
|
|
59
|
+
| `== Title ==` | Section header | `== For the sauce ==` |
|
|
60
|
+
| `>> key: value` | Metadata directive | `>> servings: 4` |
|
|
61
|
+
| `---` | YAML frontmatter block | See below |
|
|
62
|
+
| `=@name{qty}` | Fixed quantity (won't scale) | `=@salt{1%tsp}` |
|
|
63
|
+
| `@name{=qty}` | Fixed quantity (alternate) | `@salt{=1%tsp}` |
|
|
64
|
+
| `@name{}(prep)` | Ingredient with preparation | `@flour{100%g}(sifted)` |
|
|
65
|
+
| `@name\|alias{}` | Pipe alias syntax | `@ground beef\|beef{}` |
|
|
66
|
+
|
|
67
|
+
## API
|
|
68
|
+
|
|
69
|
+
### `parseCooklang(source: string): CooklangRecipe`
|
|
70
|
+
|
|
71
|
+
Parses a Cooklang source string into a structured recipe object.
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
interface CooklangRecipe {
|
|
75
|
+
metadata: Record<string, unknown>
|
|
76
|
+
steps: RecipeStepItem[][]
|
|
77
|
+
ingredients: RecipeIngredient[]
|
|
78
|
+
cookware: RecipeCookware[]
|
|
79
|
+
timers: RecipeTimer[]
|
|
80
|
+
sections: string[]
|
|
81
|
+
notes: string[]
|
|
82
|
+
errors: ParseError[]
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
**`steps`** is an array of steps, where each step is an array of items:
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
type RecipeStepItem =
|
|
90
|
+
| { type: "text"; value: string }
|
|
91
|
+
| RecipeIngredient
|
|
92
|
+
| RecipeCookware
|
|
93
|
+
| RecipeTimer
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
**`ingredients`**, **`cookware`**, and **`timers`** are deduplicated across all steps.
|
|
97
|
+
|
|
98
|
+
### Types
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
interface RecipeIngredient {
|
|
102
|
+
type: "ingredient"
|
|
103
|
+
name: string
|
|
104
|
+
quantity: number | string
|
|
105
|
+
units: string
|
|
106
|
+
fixed: boolean
|
|
107
|
+
preparation?: string
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
interface RecipeCookware {
|
|
111
|
+
type: "cookware"
|
|
112
|
+
name: string
|
|
113
|
+
quantity: number | string
|
|
114
|
+
units: string
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
interface RecipeTimer {
|
|
118
|
+
type: "timer"
|
|
119
|
+
name: string
|
|
120
|
+
quantity: number | string
|
|
121
|
+
units: string
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
interface ParseError {
|
|
125
|
+
message: string
|
|
126
|
+
shortMessage?: string
|
|
127
|
+
position: { line: number; column: number; offset: number }
|
|
128
|
+
severity: "error" | "warning"
|
|
129
|
+
}
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### Grammar Access
|
|
133
|
+
|
|
134
|
+
The underlying Ohm.js grammar is exported for advanced use cases:
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
import { grammar } from "cooklang-parse"
|
|
138
|
+
|
|
139
|
+
const match = grammar.match(source)
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Example: Recipe with Frontmatter and Sections
|
|
143
|
+
|
|
144
|
+
```typescript
|
|
145
|
+
const recipe = parseCooklang(`
|
|
146
|
+
---
|
|
147
|
+
title: Sourdough Bread
|
|
148
|
+
source: My grandmother
|
|
149
|
+
---
|
|
150
|
+
|
|
151
|
+
>> servings: 2
|
|
152
|
+
|
|
153
|
+
== Starter ==
|
|
154
|
+
Mix @starter{100%g} with @water{100%g}
|
|
155
|
+
Let ferment for ~{8%hours}
|
|
156
|
+
|
|
157
|
+
== Dough ==
|
|
158
|
+
Combine @flour{500%g} and @water{325%g}
|
|
159
|
+
Add @starter{200%g} and @salt{10%g}
|
|
160
|
+
Knead in #mixing bowl{} for ~kneading{10%minutes}
|
|
161
|
+
`)
|
|
162
|
+
|
|
163
|
+
recipe.metadata
|
|
164
|
+
// { title: "Sourdough Bread", source: "My grandmother", servings: 2 }
|
|
165
|
+
|
|
166
|
+
recipe.sections
|
|
167
|
+
// ["Starter", "Dough"]
|
|
168
|
+
|
|
169
|
+
recipe.ingredients.map(i => `${i.quantity} ${i.units} ${i.name}`.trim())
|
|
170
|
+
// ["100 g starter", "100 g water", "500 g flour", ...]
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
## Development
|
|
174
|
+
|
|
175
|
+
```bash
|
|
176
|
+
bun install # Install dependencies
|
|
177
|
+
bun test # Run all 190 tests
|
|
178
|
+
bun run build # Bundle + emit declarations
|
|
179
|
+
bun run typecheck # Type-check without emitting
|
|
180
|
+
bun run lint # Lint with Biome
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## License
|
|
184
|
+
|
|
185
|
+
[MIT](LICENSE)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AACpD,mBAAmB,SAAS,CAAA"}
|