Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Frontmatter.Test/DelimiterLineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,27 @@ public void AddFrontmatter_OpeningDelimiterHasTrailingWhitespaceOrBom_MergesInto
Assert.AreEqual($"---{Nl}title: A{Nl}author: B{Nl}---{Nl}Body{Nl}", result);
}

[TestMethod]
public void AddFrontmatter_BomPrefixedDocumentWithoutHeader_LeavesNoBomInTheBody()
{
string input = $"# Heading{Nl}{Nl}text{Nl}";

string result = Frontmatter.AddFrontmatter(input, new Dictionary<string, object> { ["title"] = "T" });

Assert.AreEqual($"---{Nl}title: T{Nl}---{Nl}# Heading{Nl}{Nl}text{Nl}", result);
Assert.AreEqual($"# Heading{Nl}{Nl}text", Frontmatter.ExtractBody(result));
}

[TestMethod]
public void AddFrontmatter_BomPrefixedDocumentOpeningWithBlankLines_LeavesNoBomInTheBody()
{
string input = $"{Nl}{Nl}# Heading{Nl}";

string result = Frontmatter.AddFrontmatter(input, new Dictionary<string, object> { ["title"] = "T" });

Assert.AreEqual($"---{Nl}title: T{Nl}---{Nl}# Heading{Nl}", result);
}

[TestMethod]
public void HasFrontmatter_DelimiterWithoutLineEnding_IsNotFrontmatter()
{
Expand Down
8 changes: 5 additions & 3 deletions Frontmatter/Frontmatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
}

List<Dictionary<string, object>> frontmatterObjects = ExtractFrontmatterObjects(input, out _);
return frontmatterObjects.Count > 0 ? frontmatterObjects.First() : null;

Check warning on line 140 in Frontmatter/Frontmatter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Indexing at 0 should be used instead of the "Enumerable" extension method "First"

Check warning on line 140 in Frontmatter/Frontmatter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Indexing at 0 should be used instead of the "Enumerable" extension method "First"
}

/// <summary>
Expand Down Expand Up @@ -284,7 +284,7 @@
}

// Then add any remaining properties that weren't in the standard order
foreach (KeyValuePair<string, object> property in frontmatter)

Check warning on line 287 in Frontmatter/Frontmatter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Loops should be simplified using the "Where" LINQ method

Check warning on line 287 in Frontmatter/Frontmatter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Loops should be simplified using the "Where" LINQ method
{
if (!sortedFrontmatter.ContainsKey(property.Key))
{
Expand Down Expand Up @@ -334,7 +334,7 @@
continue;
}

if (YamlSerializer.TryParseYamlObject(section, out Dictionary<string, object>? frontmatterObject) && frontmatterObject != null)

Check warning on line 337 in Frontmatter/Frontmatter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Change this condition so that it does not always evaluate to 'True'.

Check warning on line 337 in Frontmatter/Frontmatter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Change this condition so that it does not always evaluate to 'True'.
{
frontmatterObjects.Add(frontmatterObject);
}
Expand Down Expand Up @@ -482,7 +482,7 @@
lines.Add((start, i));
if (c == '\r' && i + 1 < input.Length && input[i + 1] == '\n')
{
i++;

Check warning on line 485 in Frontmatter/Frontmatter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Do not update the stop condition variable 'i' in the body of the for loop.

Check warning on line 485 in Frontmatter/Frontmatter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Do not update the stop condition variable 'i' in the body of the for loop.
}

start = i + 1;
Expand All @@ -496,13 +496,15 @@
/// Trims a document body for output, dropping the blank lines before its first content line and the
/// whitespace after its last. Unlike <see cref="string.Trim()"/>, it keeps the first content line's
/// indentation, so a body that opens with an indented code block or nested list content survives.
/// A leading byte order mark is dropped too: it is not whitespace, so it would otherwise be carried
/// past a newly written header and hide the first line from markdown renderers.
/// </summary>
/// <param name="body">The body to trim.</param>
/// <returns>The body without leading blank lines or trailing whitespace.</returns>
/// <returns>The body without a byte order mark, leading blank lines or trailing whitespace.</returns>
private static string TrimBody(string body)
{
int start = 0;
for (int i = 0; i < body.Length; i++)
int start = OpeningLineStart(body);
for (int i = start; i < body.Length; i++)
{
char c = body[i];
if (c == '\n')
Expand Down Expand Up @@ -552,7 +554,7 @@
}

// Then, add properties from dictionary b that don't exist in a
foreach (KeyValuePair<string, object> kvp in b)

Check warning on line 557 in Frontmatter/Frontmatter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Loops should be simplified using the "Where" LINQ method

Check warning on line 557 in Frontmatter/Frontmatter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Loops should be simplified using the "Where" LINQ method
{
if (!combinedFrontmatterObject.ContainsKey(kvp.Key))
{
Expand Down
Loading