Skip to content

Keep the header as written when AddFrontmatter adds no new key [patch] - #215

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/213-add-noop-keeps-header
Oct 28, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/213-add-noop-keeps-header

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #213

What changed

TryInsertIntoHeader returned false when every key passed to AddFrontmatter was already in the header. AddFrontmatter then fell back to ReplaceFrontmatter, which rebuilds the header and drops its comments and quoting (title: ~ → title: , 'title': a → title: a).

The early exit on added.Count == 0 is gone. With nothing to insert, the header lines are kept as written and joined to the body through JoinHeaderAndBody, the same way the insert path writes the body (#210/#211). The existing read-back check still applies, so anything that does not round-trip (a flow-mapping header, for example) still falls back to the rebuild. Stacked blocks still take the fold-and-rebuild path.

Tests

  • AddKeepsHeaderTextTests.AddFrontmatter_EveryKeyAlreadyPresent_KeepsTheHeaderAsWritten covers comments, a ~ null, a quoted key, and CRLF with a ... end marker.
  • AddFrontmatter_EveryKeyAlreadyPresent_WritesTheBodyAsARebuiltHeaderWritesIt pins the body normalization.
  • Without the fix, all 5 new cases fail. With it, the full suite passes (369/369, dotnet test on Linux).

Generated by Claude Code

TryInsertIntoHeader bailed out when every key passed in was already in
the header, so AddFrontmatter fell back to rebuilding it and dropped its
comments and quoting. With nothing to insert, the header text is now kept
as written and only the body is rejoined, as the insert path does.

Fixes #213

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW39LG7U7W2n8oFVbZp
@sonarqubecloud

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit cfc7c6e into main Oct 28, 2026
15 checks passed
@matt-edmondson
matt-edmondson deleted the fix/213-add-noop-keeps-header branch October 28, 2026 23:35
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.

AddFrontmatter still strips header comments when every key being added already exists

2 participants