Skip to content

Commit 1bdf285

Browse files
committed
Fixed a bug in ExtractFrontMatter to ensure front matter is extracted when the delimiter is on the last line of the file
1 parent 80d048e commit 1bdf285

File tree

3 files changed

+126
-1
lines changed

3 files changed

+126
-1
lines changed

RELEASE.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.0.0-beta.66
2+
3+
- Fixed a bug in `ExtractFrontMatter` to ensure front matter is extracted when the delimiter is on the last line of the file.
4+
15
# 1.0.0-beta.65
26

37
- 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.

src/core/Statiq.Core/Modules/Control/ExtractFrontMatter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ public static string GetDelimiterRegex(
348348
{
349349
regexBuilder.Append("+");
350350
}
351-
regexBuilder.Append(@"[^\S\n]*$\r?\n)");
351+
regexBuilder.Append(@"[^\S\n]*$(\r?\n)?)");
352352

353353
return regexBuilder.ToString();
354354
}

tests/core/Statiq.Core.Tests/Modules/Control/ExtractFrontMatterFixture.cs

+121
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,127 @@ public async Task EmptyFirstLineWithoutDelimiterTreatsAsFrontMatter()
121121
Content2");
122122
}
123123

124+
[Test]
125+
public async Task DelimiterWithNewLineTreatsAsFrontMatter()
126+
{
127+
// Given
128+
TestExecutionContext context = new TestExecutionContext();
129+
TestDocument[] inputs =
130+
{
131+
new TestDocument(@"FM1
132+
FM2
133+
---
134+
")
135+
};
136+
string frontMatterContent = null;
137+
ExtractFrontMatter frontMatter = new ExtractFrontMatter(new ExecuteConfig(Config.FromDocument(async x =>
138+
{
139+
frontMatterContent = await x.GetContentStringAsync();
140+
return new[] { x };
141+
})));
142+
143+
// When
144+
IEnumerable<IDocument> documents = await ExecuteAsync(inputs, context, frontMatter);
145+
146+
// Then
147+
documents.Count().ShouldBe(1);
148+
frontMatterContent.ShouldBe(
149+
@"FM1
150+
FM2
151+
");
152+
(await documents.First().GetContentStringAsync()).ShouldBeEmpty();
153+
}
154+
155+
[Test]
156+
public async Task DelimiterOnLastLineTreatsAsFrontMatter()
157+
{
158+
// Given
159+
TestExecutionContext context = new TestExecutionContext();
160+
TestDocument[] inputs =
161+
{
162+
new TestDocument(@"FM1
163+
FM2
164+
---")
165+
};
166+
string frontMatterContent = null;
167+
ExtractFrontMatter frontMatter = new ExtractFrontMatter(new ExecuteConfig(Config.FromDocument(async x =>
168+
{
169+
frontMatterContent = await x.GetContentStringAsync();
170+
return new[] { x };
171+
})));
172+
173+
// When
174+
IEnumerable<IDocument> documents = await ExecuteAsync(inputs, context, frontMatter);
175+
176+
// Then
177+
documents.Count().ShouldBe(1);
178+
frontMatterContent.ShouldBe(
179+
@"FM1
180+
FM2
181+
");
182+
(await documents.First().GetContentStringAsync()).ShouldBeEmpty();
183+
}
184+
185+
[Test]
186+
public async Task DelimiterFollowedByJunkIsNotFrontMatter()
187+
{
188+
// Given
189+
TestExecutionContext context = new TestExecutionContext();
190+
TestDocument[] inputs =
191+
{
192+
new TestDocument(@"FM1
193+
FM2
194+
---foo")
195+
};
196+
string frontMatterContent = null;
197+
ExtractFrontMatter frontMatter = new ExtractFrontMatter(new ExecuteConfig(Config.FromDocument(async x =>
198+
{
199+
frontMatterContent = await x.GetContentStringAsync();
200+
return new[] { x };
201+
})));
202+
203+
// When
204+
IEnumerable<IDocument> documents = await ExecuteAsync(inputs, context, frontMatter);
205+
206+
// Then
207+
documents.Count().ShouldBe(1);
208+
frontMatterContent.ShouldBeNull();
209+
(await documents.First().GetContentStringAsync()).ShouldBe(
210+
@"FM1
211+
FM2
212+
---foo");
213+
}
214+
215+
[Test]
216+
public async Task DelimiterPrecededByJunkIsNotFrontMatter()
217+
{
218+
// Given
219+
TestExecutionContext context = new TestExecutionContext();
220+
TestDocument[] inputs =
221+
{
222+
new TestDocument(@"FM1
223+
FM2
224+
foo---")
225+
};
226+
string frontMatterContent = null;
227+
ExtractFrontMatter frontMatter = new ExtractFrontMatter(new ExecuteConfig(Config.FromDocument(async x =>
228+
{
229+
frontMatterContent = await x.GetContentStringAsync();
230+
return new[] { x };
231+
})));
232+
233+
// When
234+
IEnumerable<IDocument> documents = await ExecuteAsync(inputs, context, frontMatter);
235+
236+
// Then
237+
documents.Count().ShouldBe(1);
238+
frontMatterContent.ShouldBeNull();
239+
(await documents.First().GetContentStringAsync()).ShouldBe(
240+
@"FM1
241+
FM2
242+
foo---");
243+
}
244+
124245
[Test]
125246
public async Task DashStringDoesNotSplitAtNonmatchingDashes()
126247
{

0 commit comments

Comments
 (0)