Skip to content

feat(server): nutrition dashboard with EU, US and UK standards and explained nutrients - #857

Open
Tloxipeuhca wants to merge 13 commits into
cooklang:mainfrom
Tloxipeuhca:feat/nutrition-dashboard
Open

Tloxipeuhca wants to merge 13 commits into
cooklang:mainfrom
Tloxipeuhca:feat/nutrition-dashboard

Conversation

@Tloxipeuhca

@Tloxipeuhca Tloxipeuhca commented Nov 25, 2026 •

Copy link
Copy Markdown

Closes #459
Replaces #458

Everything described here is implemented. Section 5 states the choices made on the format, the subject of cooklang/spec#172.

Context: a nutrition dashboard for every recipe

CookCLI helps to choose, scale, shop for and cook a recipe, but its web interface does not say what ends up on the plate. This PR adds a nutrition dashboard: from the figures written in the recipe, it takes the cook from a glance to the detail, one question at a time.

The cook's question What the dashboard shows
Which recipe suits me? on each card of the recipe list, energy and the three macros per serving
What is on the plate? energy donut and macros for the servings chosen
Is it good for me? cards rated by a norm, with a legend that says which one and how
What does it bring me? the vitamins and minerals well present
What are all the figures? the full table: per 100 g, per serving, whole recipe
What does this nutrient mean? an explanation bubble, on the nutrient itself
According to whom? EU, US or UK norm, or an Essentials view, chosen by the reader

The nutrition dashboard on a recipe page

A list of numbers would not do this. "Vitamin K 420 µg" or "saturated fat 11 g" raises questions: what is it, is that a lot, and according to whom? The answers differ from one country to the next. So before building the display, I worked out how a nutrition label is read under three norms, which of those rules come from a published text, and what a reader needs in order to understand them.

1. Analysis: one recipe, three ways to read it

Question European Union United States United Kingdom
Where does fibre sit? next to carbohydrates inside carbohydrates as EU
Which does the label give? salt sodium salt
How is a nutrient rated? nutrition claims only, nothing is ever "bad" % of daily value per serving: 5 % or less is low, 20 % or more is high traffic lights per 100 g
When is a vitamin or mineral "well present"? 15 % ("source of") and 30 % ("rich in") per 100 g 20 % per serving as EU
Daily reference for calcium 800 mg 1300 mg as EU
Daily reference for vitamin D 5 µg 20 µg as EU

A single hard-coded table would therefore be wrong for most readers.

Sources, and how far each was verified

Norm Sources read Reference intakes Table structure Rating rules
EU Regulation (EU) 1169/2011 (Art. 30-31, Annexes I, XIII-XV), Regulation (EC) 1924/2006 (Annex); WHO, Healthy diet (2026), for what to limit or encourage verified (33) partly verified: the grouping of minerals and vitamins is mine verified (15 claim conditions)
US FDA, Daily Values and How to understand and use the Nutrition Facts label verified (35) to verify: the pages read lost their indentation verified
UK FSA traffic-light labelling, NHS pages, retained Annex XIII verified partly verified, as EU partly verified: the high salt threshold is not confirmed

The EU texts were read on the UK-retained copies on legislation.gov.uk, which keep the EU wording.

Each of these statuses is written in the standard's own data file, next to its source. A value that could not be confirmed is flagged there and listed above.

2. Helping the reader understand each nutrient

Hovering, focusing or tapping a nutrient (card or table row) opens an explanation bubble. It answers, in order:

  1. What it is
  2. What it is for
  3. Where to find it
  4. The reference intake under the chosen norm
  5. How that norm rates it
  6. What this recipe gives, with its share of the reference intake
  7. Where the text comes from

Pointing at a card of the summary: saturated fat

Pointing at a card of the summary: saturated fat

Pointing at a row of the table: vitamin D

Pointing at a row of the table: vitamin D

The rating explained under the UK norm: sugars

The rating explained under the UK norm: sugars

The same bubble follows the US norm: sodium

The same bubble follows the US norm: sodium

Pointing at a slice of the donut

Pointing at a slice of the donut

No sourced text yet: the bubble says so

No sourced text yet: the bubble says so

Editorial rules I followed:

  • Every explanation names its source (NHS, WHO, the regulations, the Oral Health Foundation): 46 of the 47 nutrients have one.
  • Nothing is invented to fill a gap. A section without sourced text is left out, and a nutrient without any source says "Source: to be documented" (choline today).
  • The figures follow the reader's norm, so the same bubble gives 5 µg of vitamin D under the EU and 20 µg under the US.
  • It works without a mouse: keyboard focus and touch open it, Escape closes it.
  • The texts exist in all ten interface languages.

3. From the analysis to the design

Finding Decision
Norms disagree on structure, references and ratings Each norm is a data file (crates/format/data/standards/). The code lays out what the file says. Adding a norm means adding a file, registering it and naming it in each language.
Readers want an answer at a glance, then detail Summary first (energy donut, macros, rated cards); vitamins, minerals and the full table behind More info.
Not everyone wants 40 rows An Essentials view: eight basic rows, flat.
A colour without a rule is an opinion The legend states which norm rates and on what basis. Without the data a rating needs (weight: for per 100 g), the card stays unrated.
Salt and sodium are one quantity The norm's own one is derived from the other (salt = sodium × 2.5).
A norm may have no place for a nutrient a recipe gives It stays visible under "Other data".
Recipes are read on phones in the kitchen One column at a time on small screens; no hover-only information.

Where to look in the code

Part Files
The three norms, with sources and statuses crates/format/data/standards/
Loading and applying a norm crates/format/src/reference.rs
Nutrient names, aliases and units crates/format/src/nutrition.rs
Building the dashboard src/web/family_renderers/panel.rs
Display and explanation bubbles templates/panels/nutrition.html
Documentation docs/server.md

4. What the reader gets

Summary, European Union

Summary, European Union

Traffic lights, United Kingdom

Traffic lights, United Kingdom

Full table with the "well present" vitamins and minerals

Full table with the "well present" vitamins and minerals

On a phone

On a phone

Values in the screenshots are illustrative.

  • Energy donut and macros for the servings chosen; clicking a slice or a legend colour highlights what belongs to it.
  • Per 100 g, per serving and whole recipe.
  • "Well present" vitamins and minerals, by the norm's rule.
  • A donut on recipe list cards.
  • Norm chosen above the table or in the preferences (EU by default).
  • schema.org NutritionInformation in the JSON-LD export.
More screenshots: US and UK norms, Essentials view, highlighting, recipe list, phone table, French

Summary, United States

Summary, United States

Full table, United States

Full table, United States

Summary, United Kingdom

Summary, United Kingdom

Essentials view

Essentials view

One macro highlighted

One macro highlighted

Recipe list cards

Recipe list cards

Table on a phone, one column at a time

Table on a phone, one column at a time

In French, with decimal commas

In French, with decimal commas

An explanation in French

An explanation in French

Also in this PR

  • Any other list or mapping in the front matter gets its own panel; file: / meta: shows created, modified and translated-from.
  • A key prefixed with . is hidden from the page.
  • The scroll position is kept when the servings change.

5. Choices on the format

Topic Choice Reason
Shape a nutrition: mapping; the value%unit list is still read a key names one nutrient, so it can be translated, scaled and rated
Key names the schema.org NutritionInformation names (fatContent, proteinContent...), plus salt, minerals and vitamins; short spellings (fat, protein...) are accepted as aliases the same names serve the page and the JSON-LD export; aliases read what other tools write
Units each key has one unit, and a bare number is in it; a written unit (20 g, 0.4 g, 1255 kJ) is converted before any calculation hand-written and imported recipes both work
Basis values per serving the whole recipe follows from servings; without a whole number of servings, everything stays per serving
Per 100 g computed from weight:, the weight of the whole recipe per-100 g ratings need it
Carbohydrates the figure includes fibre, which the EU and UK views subtract one meaning per key; a figure copied from an EU label needs the fibre added back
Parsing one nutrient table (crates/format/src/nutrition.rs) for the page, the cards and the export self-contained, so it can move to cooklang-rs (cooklang/cooklang-rs#92)

The reference form: schema.org names, bare numbers

nutrition:
  calories: 185
  fatContent: 1.54
  saturatedFatContent: 0.205
  unsaturatedFatContent: 0.722
  monounsaturatedFatContent: 0.269
  polyunsaturatedFatContent: 0.453
  carbohydrateContent: 36.7
  sugarContent: 1.27
  fiberContent: 5.85
  proteinContent: 7.77
  salt: 0.754
  calcium: 20.2
  phosphorus: 158

Also accepted: short spellings and written units, as an importer may write them

nutrition:
  calories: 1255 kJ
  fat: 9 g
  saturated fat: 3 g
  carbohydrates: 45 g
  sugar: 12 g
  fiber: 2 g
  protein: 8 g
  sodium: 0.4 g

The same recipe on the page: energy in kcal, sodium shown as salt

Test plan

  • cargo fmt --all --check and cargo clippy --workspace --all-targets -- -D warnings: clean.
  • cargo test --workspace: 2023 tests pass. Two pantry permission tests fail when cargo runs as root, and pass as a normal user.
  • New tests cover unit conversion, bare numbers, decimal separators and nutrients outside a norm's table.
  • Screenshots taken from builds of this branch.
  • Playwright (Chromium, run on Windows against a server in a Linux container): 335 passed, 5 skipped, 4 did not run, 8 failed:
    • 3 need a native cook binary, which that setup did not have;
    • 2 rely on file change events, which did not reach the container;
    • 1 is a clipboard check, where Windows line ends differ;
    • 1 accessibility check on the preferences page flags the "Manage aisles" and "Manage pantry" links, also with this PR's new card removed;
    • 1, moving a meal plan line with the keyboard, is unexplained. Menu pages render no panel from this PR.

🤖 Generated with Claude Code

Tloxipeuhca and others added 13 commits August 24, 2026 19:35
… pages

Any non-standard YAML frontmatter key whose value is a list (e.g. `nutrition:`)
is now shown as its own line below the tags on the recipe and menu pages, one
line per family. Nutrition entries get a matching icon (Tabler Icons, MIT)
based on keyword: kcal/energy, protein, lipid/fat, sugar, fiber. The recipe
list page shows a compact kcal badge next to tags when `nutrition:` is present.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
…-hiding

Extends the custom metadata family display added in the previous commit:

- Supports a mapping form (`nutrition: {kcal: 234, proteins: 9.8, ...}`)
  alongside the original list form, with per-field unit inference
  (kcal -> kcal, everything else -> g) and 3 new nutrient fields
  (saturated-fat, carbohydrates, salt) plus matching icons.
- Nutrient names and a new `file`/`meta` family (created-by/created-at/
  modified-by/modified-at, with person/calendar/pencil/history icons) are
  translated into the viewer's UI language via the existing Fluent i18n
  system, across all 7 supported locales.
- A YAML key prefixed with "." is hidden from the recipe page, at either
  the family level (`.file:`) or a single mapping entry (`.lipids:`).
- Refactors the per-family rendering into src/web/family_renderers/
  (mod.rs + generic.rs + nutrition.rs + file.rs): a small FamilyRenderer
  trait with a generic fallback, and an explicit renderer_for() registry
  so adding a future specific renderer only touches one match arm.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Nutrition values apply to a single serving, which wasn't stated anywhere
on the page. A hover-only tooltip was considered but rejected: the site
is explicitly mobile-friendly, and tooltips don't work on touch.

Instead, adds a small always-visible qualifier stacked under the
"NUTRITION" label (translated across all 7 locales), via a new `note()`
method on FamilyRenderer (default None, overridden by NutritionRenderer)
so the mechanism stays available to any future family that needs one.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
…ata-display

Brings the branch from 0.34 up to upstream 0.41.1 and carries the nutrition
and file panels over to the new design system.

Conflicts resolved: docs/server.md (both sections kept), src/web/builders.rs,
src/web/mod.rs and templates/recipes.html (upstream's recipe_card.html partial,
with the kcal badge and the donut ported into it).

Nutrition
- One table of nutrients (crates/format/src/nutrition.rs) shared by the page,
  the cards and the JSON-LD output. The canonical keys are the schema.org
  NutritionInformation properties; kcal, lipids, proteins... stay accepted as
  aliases. salt is exported as sodiumContent; mono- and polyunsaturated fat
  and starch are shown but not exported.
- Recipe page: a donut and the three macros for the servings chosen, small
  cards for the sub-nutrients coloured by how good they are for health per
  100 g (green, amber, red, blue for no threshold) with a legend, a click on a
  slice of the donut that highlights what belongs to it, and the full table
  (per 100 g, per serving, whole recipe) behind a button that stays open
  across a change of servings.
- Without a whole number of servings everything is per serving: no per-100 g
  column and no whole-recipe column.
- Recipe list: the donut and the three macros on a card, the kcal badge
  otherwise.

Metadata panels
- One panel per family, built by that family's renderer and drawn by its own
  template under templates/panels/, in the order of the front matter, each in a
  frame titled on its border.
- file: shows one chip per event (created, modified, translated from) and keeps
  a chip for any other key. A chip never grows wider than the screen, and a
  web link shows its host.
- .metadata-pill wraps instead of overflowing on a phone.

i18n: every label is translated in all ten locales.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
… a change of servings

Changing the servings reloads the page. The scroll position was only put back
on the load event, so the page showed its top for a frame and then jumped to
where the reader was. Restore it as soon as the content is parsed, and once
more after the load, since images can change the height above the reader.

Measured on a recipe with a photo and the nutrition table open: before, the
timeline was [14 ms, scrollY 0] then [54 ms, scrollY 420]; after, the first
frame is already at 420.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Lay out and rate the nutrition panel from per-standard YAML files
(crates/format/data/standards): tree, reference intakes, rating
thresholds, claims, cards and conventions. The standard is chosen in
the preferences (cook_standard cookie, eu by default).

- donut, macros with %, rated cards, always-visible legend, collapsible
  table with subtotals and % of reference intake, well-present tiles
- explanation bubbles, multi-select, donut slice tooltip
- list cards show macro percentages
- translations in all 10 locales, docs and CLAUDE.md rule

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
- Dropdown above the table to switch between EU/US/UK and the flat
  "Essentials" table; the same choice is offered on /preferences
  (cook_standard=essential, cards still rated by eu)
- Calmer, more readable table: neutral surface, quiet family headings,
  nutrient icons, minerals and vitamins folded by default, double-click
  to fold/unfold, tooltip only on the label
- Sourced explanations (what / use / where / source) for 29 vitamins and
  minerals in every locale
- Drop the tree-lines button and the standard/carbohydrate notes

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…ines

Recipe lists lost tags and nutrition when the front matter was longer
than 30 lines. Use the branch behind cooklang/cooklang-find#42 until it
is released; remove the patch then.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
- A value written with a unit (`20 g`, `0.4 g`, `1255 kJ`) is converted to
  the unit of its key before it is computed, shown or exported. Bare
  numbers are unchanged. Before, such values showed a doubled unit, dropped
  the table and left the JSON-LD without nutrition.
- Decimals of the nutrition panel and cards use a comma where the language
  does.
- A nutrient outside a standard's table is named like the other rows.
- Docs: units, carbohydrates including fibre, and two stale mentions.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
The release holds the fix for front matter over 30 lines, so the temporary
patch pointing at a fork goes.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…play

Conflicts were additions on both sides, all kept: the cooking-mode notes
next to the nutrition documentation and messages, and the source link
metadata next to the family panels in src/web/builders.rs.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
The figure shown is the total sugars, while the explanation defined the free
sugars. It now gives the definition of Annex I of Regulation (EU) 1169/2011,
then says which sugars the WHO limits.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Display custom recipe metadata (e.g. nutrition) on the recipe page

1 participant