Skip to content

ExtractFrontmatter returns only the first of stacked frontmatter blocks, while ExtractBody and RemoveFrontmatter strip all of them, so later blocks' properties are lost to readers #169

Description

@matt-edmondson

What's wrong

The library treats consecutive frontmatter blocks as one header. PR #166 (fixing #138) made AddFrontmatter and CombineFrontmatter fold all of the blocks together, and ExtractBody, RemoveFrontmatter and ReplaceFrontmatter also treat every block as header. ExtractFrontmatter, however, still returns frontmatterObjects.First() (Frontmatter/Frontmatter.cs:139). As a result, properties in the second and later blocks appear in neither the extracted frontmatter nor the extracted body.

Repro

string doc = "---\ntitle: Hello\n---\n---\nauthor: Jane\n---\n# Body\n";
Frontmatter.ExtractFrontmatter(doc);
Frontmatter.ExtractBody(doc);
Frontmatter.RemoveFrontmatter(doc);
Frontmatter.AddFrontmatter(doc, new() { ["x"] = "1" });

Observed:

ExtractFrontmatter:  { title: Hello }                      <- author missing
ExtractBody:         # Body                                <- author block stripped as header
RemoveFrontmatter:   # Body\n
AddFrontmatter(x=1): ---\ntitle: Hello\nauthor: Jane\nx: 1\n---\n# Body\n   <- author IS part of the header here

Why it matters

A static-site or indexing tool that reads a page with ExtractFrontmatter plus ExtractBody loses author: Jane entirely: it is in neither the metadata nor the body. The same library's AddFrontmatter and CombineFrontmatter do count it as frontmatter, so the API gives different answers depending on which method is called.

Suggested fix / acceptance criteria

  • ExtractFrontmatter returns CombineAllFrontmatterObjects(ExtractFrontmatterObjects(input, out _)), the same first-block-wins fold that AddFrontmatter uses. It returns null when the result is empty.
  • Test: for the document above, ExtractFrontmatter returns both title and author.
  • Test: when stacked blocks repeat a key, the first block's value wins, matching AddFrontmatter_StackedBlocksRepeatAKey_FirstBlockWins.

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