From 4d4b12eb618ef3a351f000b2b9d03aa1496b9fb2 Mon Sep 17 00:00:00 2001 From: abiwab Date: Tue, 17 Nov 2026 16:24:23 +0100 Subject: [PATCH 1/2] fix(kitchen): keep quantities written in words in shopping list and views --- .changeset/text-only-quantity.md | 6 ++++ .../analyzed.json | 3 ++ .../compiled.json | 3 ++ .../analyzed.json | 6 ++++ .../compiled.json | 6 ++++ packages/kitchen/src/section.ts | 4 ++- packages/kitchen/src/shopping.ts | 14 ++++---- packages/renderer/src/shared/quantity.ts | 6 +++- .../renderer/tests/text-only-quantity.test.ts | 36 +++++++++++++++++++ 9 files changed, 76 insertions(+), 8 deletions(-) create mode 100644 .changeset/text-only-quantity.md create mode 100644 packages/renderer/tests/text-only-quantity.test.ts diff --git a/.changeset/text-only-quantity.md b/.changeset/text-only-quantity.md new file mode 100644 index 00000000..e8079f55 --- /dev/null +++ b/.changeset/text-only-quantity.md @@ -0,0 +1,6 @@ +--- +"@gram-lang/kitchen": patch +"@gram-lang/renderer": patch +--- + +**Kitchen & Rendering**: Fixed a quantity written in words (`@sel{une pincée}`, `@pepper{to taste}`) disappearing: it now shows as written in the step, in the section's ingredients and in the shopping list, where the compiled entry lists it in `variable_entries`. diff --git a/conformance/cases/026-fraction-zero-denominator/analyzed.json b/conformance/cases/026-fraction-zero-denominator/analyzed.json index bfce6566..eb47e2ca 100644 --- a/conformance/cases/026-fraction-zero-denominator/analyzed.json +++ b/conformance/cases/026-fraction-zero-denominator/analyzed.json @@ -20,6 +20,9 @@ "name": "flour", "_usageIds": [ "1" + ], + "variable_entries": [ + "cups" ] } ], diff --git a/conformance/cases/026-fraction-zero-denominator/compiled.json b/conformance/cases/026-fraction-zero-denominator/compiled.json index fd8cdfd6..3c197f7a 100644 --- a/conformance/cases/026-fraction-zero-denominator/compiled.json +++ b/conformance/cases/026-fraction-zero-denominator/compiled.json @@ -20,6 +20,9 @@ "name": "flour", "_usageIds": [ "1" + ], + "variable_entries": [ + "cups" ] } ], diff --git a/conformance/cases/warn-018-invalid-unit-hidden-quantity/analyzed.json b/conformance/cases/warn-018-invalid-unit-hidden-quantity/analyzed.json index 80297185..8eabdbc8 100644 --- a/conformance/cases/warn-018-invalid-unit-hidden-quantity/analyzed.json +++ b/conformance/cases/warn-018-invalid-unit-hidden-quantity/analyzed.json @@ -21,6 +21,9 @@ "name": "farine", "_usageIds": [ "1" + ], + "variable_entries": [ + "environ 200g" ] }, { @@ -28,6 +31,9 @@ "name": "sel", "_usageIds": [ "2" + ], + "variable_entries": [ + "une pincée" ] } ], diff --git a/conformance/cases/warn-018-invalid-unit-hidden-quantity/compiled.json b/conformance/cases/warn-018-invalid-unit-hidden-quantity/compiled.json index a6b01de4..1f7a74ac 100644 --- a/conformance/cases/warn-018-invalid-unit-hidden-quantity/compiled.json +++ b/conformance/cases/warn-018-invalid-unit-hidden-quantity/compiled.json @@ -21,6 +21,9 @@ "name": "farine", "_usageIds": [ "1" + ], + "variable_entries": [ + "environ 200g" ] }, { @@ -28,6 +31,9 @@ "name": "sel", "_usageIds": [ "2" + ], + "variable_entries": [ + "une pincée" ] } ], diff --git a/packages/kitchen/src/section.ts b/packages/kitchen/src/section.ts index 2dbb412d..b6f9d868 100644 --- a/packages/kitchen/src/section.ts +++ b/packages/kitchen/src/section.ts @@ -111,7 +111,9 @@ export function aggregateSectionIngredients( } const name = ing.name; - const hasMeasuredQty = ing.qty != null || !!ing.parts?.length; + // A quantity in words (`{une pincée}`) has no number, only a unit: it + // is listed as written, like a measured one. + const hasMeasuredQty = ing.qty != null || !!ing.parts?.length || !!ing.unit; const key = ing.preparation ? `${ing.id}::${ing.preparation}` : ing.id; if (hasMeasuredQty) { diff --git a/packages/kitchen/src/shopping.ts b/packages/kitchen/src/shopping.ts index 8c80c214..a3884811 100644 --- a/packages/kitchen/src/shopping.ts +++ b/packages/kitchen/src/shopping.ts @@ -294,12 +294,14 @@ export function generateShoppingList( const entryTerms = termsByKey.get(key); if (entryTerms) entryTerms.push(...terms); else termsByKey.set(key, [...terms]); - } else { - if (item.qty) { - const qStr = formatQuantity(item.qty); - const uStr = unit ? ` ${unit}` : ""; - existing.variableParts!.push(`${qStr}${uStr}`); - } + } else if (item.qty) { + const qStr = formatQuantity(item.qty); + const uStr = unit ? ` ${unit}` : ""; + existing.variableParts!.push(`${qStr}${uStr}`); + } else if (unit && !item.parts?.length) { + // A quantity in words (`{une pincée}`, `{to taste}`): no number, + // so the whole text is its unit. Bought as written. + existing.variableParts!.push(unit); } if (!isGhost && item.isCircular) { diff --git a/packages/renderer/src/shared/quantity.ts b/packages/renderer/src/shared/quantity.ts index 1a7db26d..87ef7e44 100644 --- a/packages/renderer/src/shared/quantity.ts +++ b/packages/renderer/src/shared/quantity.ts @@ -119,10 +119,14 @@ export function formatUsageQuantity( } else if (!Array.isArray(summed)) { // A text (`{une pincée}`) or a relative quantity (`{10%}`). const qty = getQty(item, ctx); + const unit = typeof item.unit === "string" ? item.unit : ""; if (qty) { - const unit = typeof item.unit === "string" ? item.unit : ""; const value = qty.text || formatDecimalToFraction(qty.value, unit, ctx); text = unit ? `${value} ${unit}` : value; + } else if (unit && !parts?.length) { + // A quantity in words (`{une pincée}`): no number, the whole text + // is its unit. Written as is, never scaled. + text = unit; } } return joinAmounts([text, ...extra]); diff --git a/packages/renderer/tests/text-only-quantity.test.ts b/packages/renderer/tests/text-only-quantity.test.ts new file mode 100644 index 00000000..12a93e7b --- /dev/null +++ b/packages/renderer/tests/text-only-quantity.test.ts @@ -0,0 +1,36 @@ +import { describe, expect, it } from "bun:test"; +import { compile } from "@gram-lang/kitchen"; +import { getAST } from "@gram-lang/parser"; +import { toMarkdown, toText } from "../src/index"; + +const SOURCE = + "## Pâte\n\nMélanger @farine{200 g} et @sel{une pincée}.\n\nAjouter @poivre{selon le goût} puis @sel{1 g}.\n"; + +const build = (scaleFactor?: number) => + compile(getAST(SOURCE), scaleFactor ? { scaleFactor } : {}); + +describe("a quantity in words (`{une pincée}`)", () => { + it("lists it in the compiled shopping list, as written", () => { + const entries = build().shopping_list.filter( + (e) => e.id === "sel" || e.id === "poivre", + ); + expect(entries.flatMap((e) => e.variable_entries ?? [])).toEqual([ + "une pincée", + "selon le goût", + ]); + }); + + it("shows it in the step, the section and the shopping list", () => { + const md = toMarkdown(build()); + expect(md).toContain("**sel** (une pincée)."); + expect(md).toContain("**poivre** (selon le goût)"); + expect(md).toContain("- **sel** (une pincée + 1 g)"); + expect(md).toContain("- **sel** (une pincée)\n"); + }); + + it("keeps it as written in a scaled recipe", () => { + const text = toText(build(2)); + expect(text).toContain("une pincée"); + expect(text).toContain("selon le goût"); + }); +}); -- 2.54.0 From 055d0acb8a7f8919013e5752694650080b955116 Mon Sep 17 00:00:00 2001 From: abiwab Date: Tue, 17 Nov 2026 16:51:12 +0100 Subject: [PATCH 2/2] test(conformance): accept the shopping entry of a unit with no number in the 1.3.0 digest of 026 --- conformance/legacy-1.3.0.json | 2 +- conformance/legacy-1.3.0.test.ts | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/conformance/legacy-1.3.0.json b/conformance/legacy-1.3.0.json index ecf2af3a..1177a404 100644 --- a/conformance/legacy-1.3.0.json +++ b/conformance/legacy-1.3.0.json @@ -24,7 +24,7 @@ "023-composite-bare-child": "def43938708f3ca8bef5651b9fbb3f3dad8e766398dcb4e6246870c3738916a5", "024-composite-parent-preparation": "ab5e0f76bb8234678d70333b1df482744fb8f8b5b85ae37a7374755c35a64343", "025-fraction-decimal-numerator": "9266b7592c8b9ee20c3d55ed83d97cc481aabe37b47cc88cd980893971e8e5b2", - "026-fraction-zero-denominator": "37950f4df0768746efca8c75d381741d055e4dbd000aa9834dfb603e5ed1fa41", + "026-fraction-zero-denominator": "affe9663aad839d3372dc6917e9a4bb659a35e985ad69473c04ef24782855541", "027-empty-section-scheduling": "68c66a2ca525c04f2c72c4d42f15d32add28f255668a3bd5d7ab9ab66cd3f58f", "028-comment-only-section-scheduling": "f8ef2bc128e41100a522ae0cd651d5eb85c79b6e5400ae06b04a2399c05915b9", "029-frontmatter-proto-key-inert": "0485f304bcfb20aa5d27e704e224428e7b02e226268ec85babf58ec59994ed53", diff --git a/conformance/legacy-1.3.0.test.ts b/conformance/legacy-1.3.0.test.ts index 2a9cdb9d..7b59071f 100644 --- a/conformance/legacy-1.3.0.test.ts +++ b/conformance/legacy-1.3.0.test.ts @@ -17,7 +17,10 @@ import { fileURLToPath } from "node:url"; * intermediates' overhead) is the only legitimate reason to regenerate a digest, * and the case must be named in the pull request. 1.5 did too: a section * anchor's warnings point at the anchor itself, no longer at the whole section - * or `@use` line (`loc` only, in 021, 022, mod-016 and mod-017). + * or `@use` line (`loc` only, in 021, 022, mod-016 and mod-017). And a unit + * written with no number is no longer lost from the shopping list: its entry + * lists it in `variable_entries` (026, `{1/0 cups}`, whose fraction has no + * value). * * The digests come from the tag's goldens: `canonical(json)` below, sha256, hex. */ -- 2.54.0