Skip to content

fix(web): show number and boolean custom metadata as pills - #839

Open
wanjinhao1 wants to merge 3 commits into
cooklang:mainfrom
wanjinhao1:agent/fix-838-numeric-pills
Open

wanjinhao1 wants to merge 3 commits into
cooklang:mainfrom
wanjinhao1:agent/fix-838-numeric-pills

Conversation

@wanjinhao1

Copy link
Copy Markdown

Problem

The recipe page shows custom front-matter keys as key: value pills only when the value is text. A key whose YAML value reads as a number or true/false is left out with no warning, while cook recipe prints all of them (issue #838). The menu page has the same gap.

Fixes #838

Changes

Tests

  • New regression test custom_metadata_pills_cover_numbers_and_booleans covers rating: 4, tested: true, precision: 4.5 and the text control rated: 4/5, asserting both the model data and the rendered HTML.
  • cargo test --lib: 407 passed, 0 failed.
  • cargo fmt --check clean; cargo clippy --lib --tests -- -D warnings clean.

AI assistance

This change was implemented with the help of an AI coding agent operated by the account owner (wanjinhao1), who reviewed and tested the result.

)

The recipe and menu pages built custom metadata pills from string values
only, so a key whose YAML value read as a number or a boolean was silently
dropped while `cook recipe` printed it. Reuse the string/number/boolean
conversion the standard fields already use, with floats formatted by
format_number as before. Lists and mappings remain cooklang#459.
@wanjinhao1

Copy link
Copy Markdown
Author

No CI checks have run on this PR yet — likely awaiting first-time-contributor workflow approval. Could a maintainer click "Approve and run" so the workflows execute? Thanks!

@dubadub dubadub left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking up #838. The direction is right, but a few things need changing before this can merge:

  1. Numbers render as fractions. Custom values go through format_number, so precision: 4.5 shows as 4 1/2 (the new test asserts that). #838 asks for numbers "as 4, 4.5", and fractions read oddly for things like rating: 4.5 or abv: 5.5. format_number makes sense for servings, but custom pills should show the plain number.
  2. One helper, not two copies. value_to_text is a nested fn inside build_recipe_template, and the menu path repeats the same str/i64/bool/f64 chain inline. Please move it to module level and use it for both pages' custom pills (and the menu's get_field, so both pages treat standard fields the same way).
  3. Menu page test. The regression test only covers the recipe page. Please add a .menu case so the second change is covered too.
  4. Heads-up on a conflict. #853 changes the same menu custom-metadata chain in src/web/builders.rs (it adds a source. filter). Whichever merges second will need a small rebase.

@dubadub

dubadub commented Nov 25, 2026

Copy link
Copy Markdown
Member

I merged main into this branch to resolve the conflict with #853 (point 4 above): the menu chain keeps both the new source. filter and your conversion. Tests pass. Points 1–3 of the review still stand: plain numbers rather than fractions, one module-level helper for both pages, and a .menu test.

@wanjinhao1

Copy link
Copy Markdown
Author

Thanks for the merge and the review. Points 1–3 addressed in 48ea469:

  1. Custom pills now show plain numbers: value_to_text renders f64 via its own Display (4.5, not 4 1/2); the recipe test asserts precision: 4.5.
  2. value_to_text moved to module level and is used for both pages' custom pills and both pages' get_field, so standard fields are treated the same way. The menu's custom fields now come from a serde_yaml serialisation of the metadata so the same helper covers them too, instead of the inline str/i64/bool/f64 chain.
  3. Added menu_custom_metadata_pills_cover_numbers_and_booleans, a .menu regression test on the menu page.

Verified locally: cargo test --lib web::builders (31 passed), cargo fmt --check and cargo clippy clean.

@LeMyst

LeMyst commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the quick turnaround on points 1–3. The fix itself works: I served a recipe and a menu with rating: 4, year: 2022, oven temperature: 180, tested: true, precision: 4.5 and whole: 4.0, and every one now gets a key: value pill on both pages (whole reads 4). The serde_yaml round trip on the menu is sound too, since cooklang-find's Metadata is a flattened HashMap<String, serde_yaml::Value>. Before this merges, there are two things to fix and one to tidy:

  1. The recipe cards from feat(server): tag pages, so a recipe can belong to several collections #795 still use fractions. feat(server): tag pages, so a recipe can belong to several collections #795 was merged after the last merge from main. It added CardFacts::of in src/web/builders.rs, a third copy of the str/i64/f64 chain, and that copy still calls format_number. 48ea469 moves the recipe page's standard fields to plain numbers, which follows from point 2. So after the merge, a recipe with servings: 1.5, time: 1.5 and difficulty: 2.5 shows this:

    time servings difficulty
    card in the recipe list ⏱️ 1 1/2 👥 1 1/2 📊 2 1/2
    recipe page ⏱️ 1.5 👥 1.5 servings 📊 2.5

    This branch merges into main without a conflict, so nothing will flag the mismatch. Please merge main again and make CardFacts::of call value_to_text, keeping its trim() and empty check. Then cards, the recipe page and the menu page all follow one rule. Plain numbers are also what the servings stepper already prints (chosen.to_string()).

  2. A numeric preptime / cooktime now shows twice on the recipe page. get_field reads both aliases, but cooklang doesn't count them as standard keys, so map_filtered() keeps them and they get a custom pill as well: ⏱️ Prep Time: 20 and preptime: 20. A text value (cooktime: 30 min) did this before, but this PR makes it happen for the usual case, a number of minutes. Skipping the two aliases in the custom loop fixes it:

    if key_str.starts_with("source.")
        || key_str.starts_with("time.")
        // aliases `get_field` reads that cooklang's `StdKey` does not know
        || matches!(key_str, "preptime" | "cooktime")
    {
        continue;
    }

    The menu page doesn't need this. menu.html shows only time, so a menu's preptime appears once, as its custom pill.

  3. The PR description is out of date. It still says "Floats still go through format_number, so 4.5 renders as 4 1/2 exactly like standard fields". 48ea469 does the opposite. The first commit's message says the same, so please update the description before a squash merge reuses it.

Optional:

  • No test pins the standard-field formatting yet. Once CardFacts uses the helper, one assertion that difficulty: 2.5 reads 2.5 on both the card and the page would keep the two from drifting apart again.
  • For the record, cook recipe prints whole: 4.0 and leaves tested: true out, because cooklang's as_str_like has no boolean branch. So the page now shows a little more than the CLI. Nothing to change here.

What I ran:

  • At 48ea469: cargo fmt --check and cargo clippy --all-targets -- -D warnings are clean, and web::builders passes 31/31.
  • Merged with today's main (a0ce37a): it builds, clippy is clean, and web::builders passes 38/38. The whole workspace passes: 2024 tests, 0 failures.
  • I checked that the snippet in point 2 compiles, passes rustfmt, and removes the duplicate pill.
  • The card/page table and the duplicate pill come from cook server runs on those builds.

🤖 Analysis and recommendations made by Claude (Anthropic's AI assistant), posted on behalf of @LeMyst.

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.

Server: custom metadata with a number or true/false value is not shown on the recipe page

3 participants