Skip to content

ReplaceFrontmatter/RemoveFrontmatter/AddFrontmatter/ExtractBody strip the first body line's indentation, turning a leading indented code block into prose #146

Description

@matt-edmondson

What's wrong

These methods trim the body with string.Trim(). That removes the leading blank lines, but it also removes the leading spaces or tabs on the first content line:

  • Frontmatter/Frontmatter.cs:189: AddFrontmatter, for a document with no frontmatter (input.Trim())
  • Frontmatter/Frontmatter.cs:211: ReplaceFrontmatter (body.Trim()). AddFrontmatter also reaches this line when the document already has frontmatter.
  • Frontmatter/Frontmatter.cs:230: RemoveFrontmatter (body.Trim())
  • Frontmatter/Frontmatter.cs:244: ExtractBody (body.Trim())

CombineFrontmatter keeps the body as it is, so the public APIs handle the same body differently.

Failure scenario

Input:

---
title: T
---
    int x = 1;
    int y = 2;

Text

ReplaceFrontmatter(doc, { title: "U" }), RemoveFrontmatter(doc), ExtractBody(doc) and AddFrontmatter(doc, { date: 2024 }) all return a body that starts like this:

int x = 1;
    int y = 2;

AddFrontmatter(" code\n\nText\n", { title: "U" }) returns the body code\n\nText.

CombineFrontmatter on the same input keeps int x = 1; unchanged.

In Markdown, int x = 1; now starts a paragraph and the next indented line continues it, so the whole code block renders as prose. A body whose first line is indented with a tab breaks the same way. Nested list content and poetry are affected too.

I reproduced this with MSTest tests against the current main.

Suggested fix

  • Trim only the leading blank lines: remove leading \r/\n, or skip whole lines that contain only whitespace. Then TrimEnd() the body.
  • Leave the first content line's indentation alone.
  • Add tests that run each of the four methods on a body starting with an indented code block, and assert that the leading spaces survive.

Activity

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

Metadata

Metadata

Labels

readyFully specified; implement as written

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions