Skip to content

AddFrontmatter deletes every YAML comment in the existing header and rewrites its formatting, even when it only adds a new key #170

Description

@matt-edmondson

What's wrong

When a document already has frontmatter, AddFrontmatter parses it into a Dictionary<string, object>, adds the new keys, and serializes the whole dictionary again through ReplaceFrontmatter. Anything in the header that isn't a key/value pair is lost: comments disappear and the original formatting is rewritten, even though the caller only asked to add a property.

Repro

string doc = "---\n# Draft: do not publish until legal signs off\ntitle: Launch # working title\ntags: [a, b]\n---\nBody\n";
Console.Write(Frontmatter.AddFrontmatter(doc, new() { ["author"] = "Jane" }));

Observed:

---
title: Launch
tags:
- a
- b
author: Jane
---
Body
  • The whole-line comment is gone.
  • The inline # working title comment is gone.
  • The flow list [a, b] has been turned into a block list.

Why it matters

Stamping a property such as a date, author or id onto many files is the main batch use of AddFrontmatter. Running it over a content repository silently removes editorial notes and commented-out settings from every file, and produces a large, noisy diff.

Suggested fix / acceptance criteria

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions