forked from statiqdev/Statiq.Web
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4172905
commit 18f1195
Showing
5 changed files
with
174 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
using System.Linq; | ||
using Statiq.Common; | ||
using Statiq.Core; | ||
using Statiq.Html; | ||
|
||
namespace Statiq.Web.Pipelines | ||
{ | ||
public class Redirects : Pipeline | ||
{ | ||
public Redirects() | ||
{ | ||
Dependencies.Add(nameof(Content)); | ||
|
||
ProcessModules = new ModuleList | ||
{ | ||
new ReplaceDocuments(nameof(Content)), | ||
new FlattenTree(), | ||
new ExecuteConfig(Config.FromSettings(settings => | ||
{ | ||
GenerateRedirects generateRedirects = new GenerateRedirects() | ||
.WithMetaRefreshPages(settings.GetBool(WebKeys.MetaRefreshRedirects, true)); | ||
if (settings.GetBool(WebKeys.NetlifyRedirects, false)) | ||
{ | ||
generateRedirects = generateRedirects.WithAdditionalOutput( | ||
"_redirects", | ||
redirects => string.Join(Environment.NewLine, redirects.Select(r => $"/{r.Key} {r.Value}"))); | ||
} | ||
return generateRedirects; | ||
})) | ||
}; | ||
|
||
OutputModules = new ModuleList | ||
{ | ||
new WriteFiles() | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using NUnit.Framework; | ||
using Shouldly; | ||
using Statiq.App; | ||
using Statiq.Common; | ||
using Statiq.Testing; | ||
using Statiq.Web.Pipelines; | ||
|
||
namespace Statiq.Web.Tests.Pipelines | ||
{ | ||
[TestFixture] | ||
[NonParallelizable] | ||
public class RedirectsFixture : BaseFixture | ||
{ | ||
public class ExecuteTests : RedirectsFixture | ||
{ | ||
[Test] | ||
public async Task GeneratesClientRedirects() | ||
{ | ||
// Given | ||
Bootstrapper bootstrapper = Bootstrapper.Factory.CreateWeb(Array.Empty<string>()); | ||
TestFileProvider fileProvider = new TestFileProvider | ||
{ | ||
{ | ||
"/input/a/b/c.md", | ||
@"RedirectFrom: x/y | ||
--- | ||
Foo" | ||
}, | ||
{ | ||
"/input/d/e.md", | ||
"Bar" | ||
} | ||
}; | ||
|
||
// When | ||
BootstrapperTestResult result = await bootstrapper.RunTestAsync(fileProvider); | ||
|
||
// Then | ||
result.ExitCode.ShouldBe((int)ExitCode.Normal); | ||
IDocument document = result.Outputs[nameof(Redirects)][Phase.Process].ShouldHaveSingleItem(); | ||
document.Destination.ShouldBe("x/y.html"); | ||
(await document.GetContentStringAsync()).ShouldContain(@"<meta http-equiv=""refresh"" content=""0;url='/a/b/c'"" />"); | ||
} | ||
|
||
[Test] | ||
public async Task GeneratesNetlifyRedirects() | ||
{ | ||
// Given | ||
Bootstrapper bootstrapper = Bootstrapper.Factory | ||
.CreateWeb(Array.Empty<string>()) | ||
.AddSetting(WebKeys.NetlifyRedirects, true) | ||
.AddSetting(WebKeys.MetaRefreshRedirects, false); | ||
TestFileProvider fileProvider = new TestFileProvider | ||
{ | ||
{ | ||
"/input/a/b/c.md", | ||
@"RedirectFrom: x/y | ||
--- | ||
Foo" | ||
}, | ||
{ | ||
"/input/d/e.md", | ||
"Bar" | ||
} | ||
}; | ||
|
||
// When | ||
BootstrapperTestResult result = await bootstrapper.RunTestAsync(fileProvider); | ||
|
||
// Then | ||
result.ExitCode.ShouldBe((int)ExitCode.Normal); | ||
IDocument document = result.Outputs[nameof(Redirects)][Phase.Process].ShouldHaveSingleItem(); | ||
document.Destination.ShouldBe("_redirects"); | ||
(await document.GetContentStringAsync()).ShouldBe("/x/y /a/b/c"); | ||
} | ||
} | ||
} | ||
} |