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:
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.
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()).AddFrontmatteralso reaches this line when the document already has frontmatter.Frontmatter/Frontmatter.cs:230:RemoveFrontmatter(body.Trim())Frontmatter/Frontmatter.cs:244:ExtractBody(body.Trim())CombineFrontmatterkeeps the body as it is, so the public APIs handle the same body differently.Failure scenario
Input:
ReplaceFrontmatter(doc, { title: "U" }),RemoveFrontmatter(doc),ExtractBody(doc)andAddFrontmatter(doc, { date: 2024 })all return a body that starts like this:AddFrontmatter(" code\n\nText\n", { title: "U" })returns the bodycode\n\nText.CombineFrontmatteron the same input keepsint 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
\r/\n, or skip whole lines that contain only whitespace. ThenTrimEnd()the body.