You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
AddFrontmatter still strips header comments when every key being added already exists #213
#212 (fixing #170) made AddFrontmatter insert new keys in front of the closing delimiter instead of rebuilding the header. That fix does not cover the case where every key passed in is already in the header. In that case nothing is added, yet the code still rebuilds the header from the parsed dictionary. The rebuild drops every comment and rewrites the formatting, which is exactly what #170 set out to stop.
Frontmatter/Frontmatter.cs:269-275: TryInsertIntoHeader returns false when added.Count == 0.
Frontmatter/Frontmatter.cs:226-231: when that returns false, AddFrontmatter falls back to ReplaceFrontmatter(input, combined).
Frontmatter.AddFrontmatter("---\n# keep me\ntitle: A # c\n---\nBody\n",new(){["title"]="X"});// returns "---\ntitle: A\n---\nBody\n" (both comments are gone)
The same no-op path also rewrites values:
title: ~ becomes title:
'title': a becomes title: a
Why it matters
A common use is stamping a property such as date or id across a content repository. The files that already carry the key gain nothing, yet they lose their comments and show up as noisy diffs.
Suggested fix
When there is a single readable block and added.Count == 0, keep the existing header text exactly as written. Join it to the body with JoinHeaderAndBody, so the body is written the same way a rebuild writes it and the #210/#211 behaviour is kept. Stacked blocks keep the fold-and-rebuild path.
Acceptance criteria
AddFrontmatter on a single-block header whose keys are all already present returns the header text unchanged, comments and quoting included.
A test next to AddKeepsHeaderTextTests.AddFrontmatter_SomeKeysAlreadyPresent_... covers the case where every key is already present.
Next step: for a single readable block with added.Count == 0, return the original header through JoinHeaderAndBody, and add the all-keys-present test beside AddKeepsHeaderTextTests.
Activity
matt-edmondson commentedon Oct 26, 2026
Triage
AddFrontmatterstrips comments and rewrites quoting and nulls.Frontmatter/Frontmatter.csAddFrontmatter/TryInsertIntoHeaderfallback toReplaceFrontmatteradded.Count == 0, return the original header throughJoinHeaderAndBody, and add the all-keys-present test besideAddKeepsHeaderTextTests.Generated by Claude Code