Skip to content

Frontmatter whose lines are uniformly indented is treated as unreadable, so AddFrontmatter silently drops the new properties #161

Description

@matt-edmondson

What's wrong

ExtractFrontmatterObjects (Frontmatter/Frontmatter.cs:318) calls block.Trim() before it parses each block. Trim() removes the indentation from the first line only. The remaining lines keep their indentation, so a uniformly indented block that is valid YAML becomes invalid YAML:

  title: Hello      ->  "title: Hello\n  tags: [a]"
  tags: [a]             (the second line is now read as a continuation of `title`'s value)

Parsing fails. ExtractFrontmatter returns null. AddFrontmatter treats the block as unreadable and returns the document unchanged, so the caller's new properties are lost without any error. CombineFrontmatter also leaves the document unchanged.

Repro

string d1 = "---\n  title: Hello\n  tags: [a]\n---\nBody\n";

Frontmatter.ExtractFrontmatter(d1);                             // null, expected { title, tags }
YamlSerializer.TryParseYamlObject("  title: Hello\n  tags: [a]", out var r); // true: the YAML itself is valid
Frontmatter.AddFrontmatter(d1, new() { ["author"] = "X" });     // returns d1 unchanged; "author" is lost

Suggested fix

Don't strip the leading indentation of the first line. Use block.TrimEnd(), dropping only leading blank lines, or pass the block to the parser unchanged, since YamlDotNet already handles uniform indentation and leading blank lines. Keep the IsNullOrWhiteSpace skip.

Acceptance criteria

  • ExtractFrontmatter on a uniformly indented block returns its properties.
  • AddFrontmatter on such a document keeps the existing properties and adds the new ones.

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