Skip to content

Commit

Permalink
Adds a RedirectBody key to control the body of meta-refresh redirect …
Browse files Browse the repository at this point in the history
…files (#153)
  • Loading branch information
daveaglick committed Dec 1, 2020
1 parent 0f5c57e commit d108bda
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# 1.0.0-beta.31

- Added some additional retry policies to file operations to avoid file lock exceptions under certain conditions (#151).
- Added ability to customize the body of meta-refresh redirect HTML files using the `RedirectBody` key (#153).

# 1.0.0-beta.30

Expand Down
6 changes: 6 additions & 0 deletions src/core/Statiq.Common/Meta/Keys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,12 @@ public static class Keys
/// <type><see cref="NormalizedPath"/></type>
public const string RedirectFrom = nameof(RedirectFrom);

/// <summary>
/// Replaces the default body of meta-refresh redirect HTML files with the specified body
/// (it will be included raw so don't escape HTML).
/// </summary>
public const string RedirectBody = nameof(RedirectBody);

/// <summary>
/// Entirely disables cache modules, both during runtime and the persistent cache.
/// </summary>
Expand Down
5 changes: 3 additions & 2 deletions src/core/Statiq.Core/Modules/Content/GenerateRedirects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,19 @@ async IAsyncEnumerable<IDocument> GetOutputsAsync(IDocument input)

if (_metaRefreshPages)
{
string body = input.GetString(Keys.RedirectBody, $@"<p>This page has moved to a <a href=""{url}"">{url}</a></p>");
yield return context.CreateDocument(
outputPath,
await context.GetContentProviderAsync(
$@"
<!doctype html>
< !doctype html>
<html>
<head>
<title>Redirected</title>
<meta http-equiv=""refresh"" content=""0;url='{url}'"" />
</head>
<body>
<p>This page has moved to a <a href=""{url}"">{url}</a></p>
{body}
</body>
</html>",
MediaTypes.Html));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,23 @@ public async Task WithAdditionalOutputWithoutMetaRefresh()
// Then
results.Select(x => x.Destination.FullPath).ShouldBe(new[] { "a/b" });
}

[Test]
public async Task DifferentBody()
{
// Given
TestDocument redirected = new TestDocument(new MetadataItems
{
{ Keys.RedirectFrom, new List<NormalizedPath> { new NormalizedPath("foo.html") } },
{ Keys.RedirectBody, "<p>FOOBAR</p>" }
});
GenerateRedirects redirect = new GenerateRedirects();

// When
IReadOnlyList<TestDocument> results = await ExecuteAsync(new[] { redirected }, redirect);

// Then
results.Single().Content.ShouldContain("<p>FOOBAR</p>");
}
}
}

0 comments on commit d108bda

Please sign in to comment.