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
35 changes: 35 additions & 0 deletions Frontmatter.Test/LineEndingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,39 @@ public void RemoveFrontmatter_KeepsTheInputLineEnding(string nl)

Assert.AreEqual(Lines(nl, "line1", "line2"), result);
}

[TestMethod]
[DataRow("\r\n", DisplayName = "CRLF")]
[DataRow("\n", DisplayName = "LF")]
[DataRow("\r", DisplayName = "CR")]
public void ExtractBody_DropsBlankLinesAfterTheHeader_ForEveryLineEnding(string nl)
{
string input = Lines(nl, "---", "title: x", "---", string.Empty, string.Empty, "Body");

Assert.AreEqual("Body", Frontmatter.ExtractBody(input));
}

[TestMethod]
[DataRow("\r\n", DisplayName = "CRLF")]
[DataRow("\n", DisplayName = "LF")]
[DataRow("\r", DisplayName = "CR")]
public void RemoveFrontmatter_DropsBlankLinesAfterTheHeader_ForEveryLineEnding(string nl)
{
string input = Lines(nl, "---", "title: x", "---", string.Empty, string.Empty, "Body");

Assert.AreEqual(Lines(nl, "Body"), Frontmatter.RemoveFrontmatter(input));
}

[TestMethod]
[DataRow("\r\n", DisplayName = "CRLF")]
[DataRow("\n", DisplayName = "LF")]
[DataRow("\r", DisplayName = "CR")]
public void ReplaceFrontmatter_DropsBlankLinesAfterTheHeader_ForEveryLineEnding(string nl)
{
string input = Lines(nl, "---", "title: x", "---", string.Empty, string.Empty, "Body");

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

Assert.AreEqual(Lines(nl, "---", "title: y", "---", "Body"), result);
}
}
2 changes: 1 addition & 1 deletion Frontmatter/Frontmatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
}

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

Check warning on line 141 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 141 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 @@ -301,7 +301,7 @@
}

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

Check warning on line 304 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 304 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 @@ -351,7 +351,7 @@
continue;
}

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

Check warning on line 354 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 354 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 @@ -499,7 +499,7 @@
lines.Add((start, i));
if (c == '\r' && i + 1 < input.Length && input[i + 1] == '\n')
{
i++;

Check warning on line 502 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 502 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 @@ -524,7 +524,7 @@
for (int i = start; i < body.Length; i++)
{
char c = body[i];
if (c == '\n')
if (c is '\n' or '\r')
{
start = i + 1;
}
Expand Down Expand Up @@ -618,7 +618,7 @@
}

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

Check warning on line 621 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 621 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