Skip to content

fix(format): stop hard wrapping markdown output - #512

Merged
dubadub merged 1 commit into
mainfrom
fix/markdown-hard-wrapping
Sep 17, 2026
Merged

dubadub merged 1 commit into
mainfrom
fix/markdown-hard-wrapping

Conversation

@dubadub

@dubadub dubadub commented Sep 17, 2026

Copy link
Copy Markdown
Member

Fixes #497.

Problem

The Markdown writer piped steps and the description through textwrap at termwidth().min(80). Terminal-driven hard wrapping in a file format has three consequences:

  • a line break lands mid-sentence, as reported in Markdown output format introduces EOL characters mid text. #497 (...San Marzano tomato\nsauce on it...);
  • the output depends on the width of whatever terminal produced it, so cook recipe -f markdown is not reproducible — that also made the recipe_markdown_output snapshot test width-dependent;
  • the continuation lines sit at column 0, which ends the ordered list for a CommonMark parser instead of continuing the item.

Fix

print_wrapped/print_wrapped_with_options are replaced by write_block(w, text, first, rest), which emits only the line breaks the recipe itself has:

  • Steps: "5. " prefixes the first line; any later line is indented to the marker width (" "), the CommonMark content column. This also corrects the - bullets a step can contain, which previously broke out of their list item.
  • Description: each line gets "> " (blockquote style) or nothing (heading style). The prefix is trimmed off blank lines so no stray trailing whitespace turns into a hard break.

seed/Neapolitan Pizza.cook step 5 now renders as a single line, as the issue asks.

Tests

Three new unit tests in crates/format/src/markdown.rs, written before the fix and watched fail:

  • step_text_is_not_hard_wrapped
  • description_is_not_hard_wrapped
  • a_list_inside_a_step_is_indented_under_the_number

snapshot_test__recipe_markdown_output is re-recorded; its only diff is the unwrapped step.

The cooklang output format wraps the same way, but is left alone: the parser joins soft-wrapped lines back into one step, and a round trip preserves the step count. textwrap is still used by the human and cooklang writers.

cargo test, cargo fmt --check and cargo clippy --all-targets pass cleanly.

The Markdown writer ran steps and the description through `textwrap` at
`termwidth().min(80)`, which put a line break in the middle of a sentence
and made the output depend on the width of the terminal that produced it.
The continuation lines also sat at column zero, which ends the ordered
list for a CommonMark parser.

Emit only the line breaks the recipe itself has, indenting them to the
list item's content column. This also fixes the `-` bullets a step can
contain, which used to break out of the step they belong to.

Fixes #497
@claude

claude Bot commented Sep 17, 2026

Copy link
Copy Markdown

Review of #512 — stop hard wrapping markdown output

Nice, focused fix. Root cause (piping recipe text through textwrap at terminal width) is correctly identified, and the fix is scoped tightly to the markdown writer — the cooklang and human writers keep using textwrap deliberately, which the PR description explains well (round-trip parsing joins soft-wrapped cooklang lines back together, so that format isn't affected by the bug).

Code quality

  • write_block(w, text, first, rest) is a clean, small replacement for print_wrapped/print_wrapped_with_options. Good use of .chars().count() (not .len()) for the indent width, and using split('\n') rather than .lines() so an empty step still emits its marker is a nice touch called out in a comment.
  • Doc comments on write_block and the fixture explain the why (non-obvious CommonMark indentation requirement, trailing-whitespace-as-hard-break gotcha) rather than restating the code — matches the repo's comment conventions.
  • Splitting w_step's single string into marker + step_str makes the function easier to follow than the original single-string-with-suffix approach.

Potential bugs

  • None found. I traced both call sites (Blockquote/Heading description styles and w_step) and the multi-digit step case (marker.chars().count() correctly accounts for 10. being 4 columns wide, which the old code never had to worry about since it never indented continuations at all).
  • One behavior change worth confirming is intentional: previously textwrap::wrap collapsed all whitespace (including blank lines) in a multi-paragraph description into one flowed paragraph; write_block now preserves embedded blank lines as their own quote-marker lines. That's arguably more correct, but it's a user-visible change beyond stopping the 80-column wrap and isn't covered by the new tests (both fixtures are single-paragraph). Might be worth a quick test/mention if multi-paragraph descriptions are a supported case.

Performance

  • Non-issue. write_block allocates one String per line via format!(...).trim_end(), slightly more allocation than necessary, but recipe steps/descriptions are short and this isn't a hot path.

Security

  • None applicable — pure text formatting of already-trusted recipe content, no new I/O, injection, or trust-boundary changes.

Test coverage

  • Good: the three new unit tests target exactly the three symptoms from the linked issue (mid-sentence wrap, terminal-width dependence via a fixture longer than 80 columns, and a list bullet inside a step breaking out of its list item), and the snapshot was re-recorded with a minimal, expected diff (the previously-wrapped line collapses to one).
  • I wasn't able to execute cargo test/clippy myself in this review sandbox to independently confirm, but the logic checks out under static review and the PR description states the full check suite (fmt, clippy, test) passes.
  • Nit: consider also asserting on a multi-digit step (e.g. step 10+) indenting correctly, since that's the one case the existing tests don't exercise and where an off-by-one in the new indent math would be easy to introduce later without anyone noticing.

Overall this looks correct, minimal, and well-tested — happy with this as-is; the notes above are non-blocking suggestions.

@dubadub
dubadub merged commit 831eec8 into main Sep 17, 2026
6 checks passed
@dubadub
dubadub deleted the fix/markdown-hard-wrapping branch September 17, 2026 15:40
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.

Markdown output format introduces EOL characters mid text.

1 participant