fix(kitchen): keep quantities written in words in shopping list and views #175

Merged
abiwab merged 2 commits from fix/text-only-quantity into dev/1.5 2026-11-17 17:05:56 +01:00
11 changed files with 81 additions and 10 deletions

View file

@ -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`.

View file

@ -20,6 +20,9 @@
"name": "flour",
"_usageIds": [
"1"
],
"variable_entries": [
"cups"
]
}
],

View file

@ -20,6 +20,9 @@
"name": "flour",
"_usageIds": [
"1"
],
"variable_entries": [
"cups"
]
}
],

View file

@ -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"
]
}
],

View file

@ -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"
]
}
],

View file

@ -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",

View file

@ -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.
*/

View file

@ -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) {

View file

@ -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) {
} 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) {

View file

@ -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);
if (qty) {
const unit = typeof item.unit === "string" ? item.unit : "";
if (qty) {
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]);

View file

@ -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");
});
});