Skip to content

Commit

Permalink
Fixed a bug in ExtractFrontMatter to ensure front matter is extract…
Browse files Browse the repository at this point in the history
…ed when the delimiter is on the last line of the file
  • Loading branch information
daveaglick committed Jan 24, 2023
1 parent 80d048e commit 1bdf285
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 1 deletion.
4 changes: 4 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.0.0-beta.66

- Fixed a bug in `ExtractFrontMatter` to ensure front matter is extracted when the delimiter is on the last line of the file.

# 1.0.0-beta.65

- Added a new pipeline `PostProcessHasDependencies` property that indicates the post-process phase of a pipeline should depend on the post-process phase(s) of the pipeline dependencies. This is helpful in certain situations where you need a pipeline to run after other post-process phases from dependencies.
Expand Down
2 changes: 1 addition & 1 deletion src/core/Statiq.Core/Modules/Control/ExtractFrontMatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ public static string GetDelimiterRegex(
{
regexBuilder.Append("+");
}
regexBuilder.Append(@"[^\S\n]*$\r?\n)");
regexBuilder.Append(@"[^\S\n]*$(\r?\n)?)");

return regexBuilder.ToString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,127 @@ public async Task EmptyFirstLineWithoutDelimiterTreatsAsFrontMatter()
Content2");
}

[Test]
public async Task DelimiterWithNewLineTreatsAsFrontMatter()
{
// Given
TestExecutionContext context = new TestExecutionContext();
TestDocument[] inputs =
{
new TestDocument(@"FM1
FM2
---
")
};
string frontMatterContent = null;
ExtractFrontMatter frontMatter = new ExtractFrontMatter(new ExecuteConfig(Config.FromDocument(async x =>
{
frontMatterContent = await x.GetContentStringAsync();
return new[] { x };
})));

// When
IEnumerable<IDocument> documents = await ExecuteAsync(inputs, context, frontMatter);

// Then
documents.Count().ShouldBe(1);
frontMatterContent.ShouldBe(
@"FM1
FM2
");
(await documents.First().GetContentStringAsync()).ShouldBeEmpty();
}

[Test]
public async Task DelimiterOnLastLineTreatsAsFrontMatter()
{
// Given
TestExecutionContext context = new TestExecutionContext();
TestDocument[] inputs =
{
new TestDocument(@"FM1
FM2
---")
};
string frontMatterContent = null;
ExtractFrontMatter frontMatter = new ExtractFrontMatter(new ExecuteConfig(Config.FromDocument(async x =>
{
frontMatterContent = await x.GetContentStringAsync();
return new[] { x };
})));

// When
IEnumerable<IDocument> documents = await ExecuteAsync(inputs, context, frontMatter);

// Then
documents.Count().ShouldBe(1);
frontMatterContent.ShouldBe(
@"FM1
FM2
");
(await documents.First().GetContentStringAsync()).ShouldBeEmpty();
}

[Test]
public async Task DelimiterFollowedByJunkIsNotFrontMatter()
{
// Given
TestExecutionContext context = new TestExecutionContext();
TestDocument[] inputs =
{
new TestDocument(@"FM1
FM2
---foo")
};
string frontMatterContent = null;
ExtractFrontMatter frontMatter = new ExtractFrontMatter(new ExecuteConfig(Config.FromDocument(async x =>
{
frontMatterContent = await x.GetContentStringAsync();
return new[] { x };
})));

// When
IEnumerable<IDocument> documents = await ExecuteAsync(inputs, context, frontMatter);

// Then
documents.Count().ShouldBe(1);
frontMatterContent.ShouldBeNull();
(await documents.First().GetContentStringAsync()).ShouldBe(
@"FM1
FM2
---foo");
}

[Test]
public async Task DelimiterPrecededByJunkIsNotFrontMatter()
{
// Given
TestExecutionContext context = new TestExecutionContext();
TestDocument[] inputs =
{
new TestDocument(@"FM1
FM2
foo---")
};
string frontMatterContent = null;
ExtractFrontMatter frontMatter = new ExtractFrontMatter(new ExecuteConfig(Config.FromDocument(async x =>
{
frontMatterContent = await x.GetContentStringAsync();
return new[] { x };
})));

// When
IEnumerable<IDocument> documents = await ExecuteAsync(inputs, context, frontMatter);

// Then
documents.Count().ShouldBe(1);
frontMatterContent.ShouldBeNull();
(await documents.First().GetContentStringAsync()).ShouldBe(
@"FM1
FM2
foo---");
}

[Test]
public async Task DashStringDoesNotSplitAtNonmatchingDashes()
{
Expand Down

0 comments on commit 1bdf285

Please sign in to comment.