Repository navigation
Insert added keys into the header instead of rebuilding it [patch] - #212
Merged
Merged
Conversation
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
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Q4e8DaJpCfoS8XViLjQzcD
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
|
This was referenced Nov 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #170
What was wrong
When a document already had a header,
AddFrontmatterparsed it into a dictionary, added the new keys, and serialized the whole thing again throughReplaceFrontmatter. Every YAML comment was dropped and flow collections such astags: [a, b]came back as block lists, even though the caller only added a key.Change
TryInsertIntoHeaderinFrontmatter.cs. When the document has a single header that already holds properties, it writes only the keys the header lacks, serialized as before, in front of the closing delimiter. The rest of the header stays as written: comments, flow style and a...closer.JoinHeaderAndBody, factored out of Keep the blank line that sets a body rule apart from the header [patch] #211'sWithHeader, so it is trimmed as before and a body rule set off by a blank line keeps that line (AddFrontmatter/ReplaceFrontmatter delete the blank line under the header, so a---…---passage in the body becomes a second frontmatter block and that body text is lost #210).{title: A}), and a|scalar as the last key, whose parsed value would change (A|block scalar loses its trailing newline when it is the last key in the frontmatter (and CombineFrontmatter rewrites it as>-) #153).# TODO,~,{}) is still replaced, asEmptyYamlBlockTestspins (AddFrontmatter silently drops new properties when the existing header holds only a comment, ~, null or {} #206). Keys the header already has keep their existing value. The opening line is still written as a bare---, so a BOM or trailing whitespace on it is dropped, asDelimiterLineTestspins.Tests
New
AddKeepsHeaderTextTests:tags: [a, b]are kept, andauthor: Janeis added.|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.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