Skip to content

Insert added keys into the header instead of rebuilding it [patch] - #212

Merged
matt-edmondson merged 5 commits into
mainfrom
fix/170-add-keeps-header-text
Oct 25, 2026
Merged

matt-edmondson merged 5 commits into
mainfrom
fix/170-add-keeps-header-text

Conversation

@matt-edmondson

@matt-edmondson matt-edmondson commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #170

What was wrong

When a document already had a header, AddFrontmatter parsed it into a dictionary, added the new keys, and serialized the whole thing again through ReplaceFrontmatter. Every YAML comment was dropped and flow collections such as tags: [a, b] came back as block lists, even though the caller only added a key.

Change

Tests

New AddKeepsHeaderTextTests:

  • The issue's repro in LF, CRLF and CR: both comments and tags: [a, b] are kept, and author: Jane is added.
  • Existing keys keep their value and only the new ones are inserted. A list value is inserted as YAML and reads back. A | scalar before the last key is kept. A BOM and trailing space on the opener are normalized while the comments stay. A ... closer is kept.
  • After Keep the blank line that sets a body rule apart from the header [patch] #211 merged: the body is trimmed as a rebuilt header trims it, and a commented header plus a body rule set off by a blank line keeps both.
  • Controls: a flow-mapping header is rebuilt and still parses, and stacked blocks are still folded.

main (with #211) is merged in. The full suite passes, 364/364 locally on net10.0. Before the follow-up commit, the merged code failed #211's control test and the new body-trim test (4 cases). That disagreement is what the follow-up commit fixes.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Q4e8DaJpCfoS8XViLjQzcD

AddFrontmatter on a document with a header parsed the header into a
dictionary and serialized it again, so every comment was dropped and flow
collections were rewritten as block ones, even when it only added a key.

When the document has a single header that already holds properties,
AddFrontmatter now writes just the new keys in front of the closing
delimiter and leaves the rest of the document as written. The result is
read back, and used only when it holds exactly the combined properties
and the original body; otherwise, as for a flow-mapping header, the
header is rebuilt as before. Stacked blocks and empty placeholder headers
keep the rebuild path.

Fixes #170

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q4e8DaJpCfoS8XViLjQzcD
Comment thread Frontmatter/Frontmatter.cs Fixed
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q4e8DaJpCfoS8XViLjQzcD
Merging #211 showed the fast path left the body exactly as written,
while AddFrontmatter has always re-trimmed it (the #211 control test
pins this). The fast path now keeps the header text as written but joins
the body through the same JoinHeaderAndBody that WithHeader uses, so the
body is trimmed as before and a body rule set off by a blank line keeps
that line (#210).

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

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 0c7b4ec into main Oct 25, 2026
15 checks passed
@matt-edmondson
matt-edmondson deleted the fix/170-add-keeps-header-text branch October 25, 2026 05:08
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 deletes every YAML comment in the existing header and rewrites its formatting, even when it only adds a new key

2 participants