Skip to content

Commit

Permalink
Added MakeLinksAbsolute and MakeLinksRootRelative settings
Browse files Browse the repository at this point in the history
  • Loading branch information
daveaglick committed Jul 29, 2020
1 parent 3e2aa31 commit 3830e2f
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
<!-- Controls whether references are local projects or NuGet packages -->
<LocalReferences>false</LocalReferences>
<!-- The NuGet version of Statiq that should be referenced if LocalReferences is false -->
<StatiqFrameworkVersion>1.0.0-beta.17</StatiqFrameworkVersion>
<StatiqFrameworkVersion>1.0.0-beta.18</StatiqFrameworkVersion>
</PropertyGroup>

<PropertyGroup>
<Version>1.0.0-alpha.22</Version>
<Version>1.0.0-beta.1</Version>
<InformationalVersion>$(Version)</InformationalVersion>
<AssemblyVersion>$(Version.Split('-')[0])</AssemblyVersion>
<FileVersion>$(Version.Split('-')[0])</FileVersion>
Expand Down
2 changes: 2 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
This version of Statiq Framework contains breaking changes which Statiq Web will inherit.
- **Breaking change:** The `Content` pipeline no longer nests output documents and instead all documents are now output.
`IEnumerable<IDocument>.FilterDestinations("*.html")` or `Outputs["*.html"]` can be used to get "root" documents.
- Added a `MakeLinksAbsolute` setting to rewrite relative links to be absolute.
- Added a `MakeLinksRootRelative` setting to rewrite relative links to be root-relative.
- Suppressed archive output when there's no documents to archive.
- Added the `CacheDocuments` module to additional pipelines for faster rebuild times.
- Added an `ArchiveKeyComparer` metadata that allows specifying a specific comparer for use with archive groups (usually with script metadata).
Expand Down
10 changes: 9 additions & 1 deletion src/Statiq.Web/Modules/RenderPostProcessTemplates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@ public RenderPostProcessTemplates(Templates templates)
{
new MirrorResources()
},
new ResolveXrefs()
new ResolveXrefs(),
new ExecuteIf(Config.FromSetting<bool>(WebKeys.MakeLinksAbsolute))
{
new MakeLinksAbsolute()
},
new ExecuteIf(Config.FromSetting<bool>(WebKeys.MakeLinksRootRelative))
{
new MakeLinksRootRelative()
}
})
.ToArray())
{
Expand Down
10 changes: 10 additions & 0 deletions src/Statiq.Web/WebKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ public static class WebKeys
/// </summary>
public const string GatherHeadingsLevel = nameof(GatherHeadingsLevel);

/// <summary>
/// Converts relative links to absolute links.
/// </summary>
public const string MakeLinksAbsolute = nameof(MakeLinksAbsolute);

/// <summary>
/// Converts relative links to root-relative links.
/// </summary>
public const string MakeLinksRootRelative = nameof(MakeLinksRootRelative);

////////// Document

public const string Title = nameof(Title);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
using Shouldly;
using Statiq.Common;
using Statiq.Testing;
using Statiq.Web.Modules;

namespace Statiq.Web.Tests.Modules
{
[TestFixture]
public class RenderPostProcessTemplatesFixture : BaseFixture
{
public class ExecuteTests : RenderPostProcessTemplatesFixture
{
[Test]
public async Task MakesRelativeLinksAbsolute()
{
// Given
TestExecutionContext context = new TestExecutionContext();
context.Services.AddSingleton<IReadOnlyFileSystem>(context.FileSystem);
context.Settings[WebKeys.MakeLinksAbsolute] = true;
context.Settings[Keys.Host] = "site.com";
context.Settings[Keys.LinksUseHttps] = true;
TestDocument document = new TestDocument(
new NormalizedPath("/a/b/c.html"),
new NormalizedPath("a/b/c.html"),
@"<html>
<body>
Foo <a href=""../d"">FizzBuzz</a>
</body>
</html>");
Templates templates = new Templates();
RenderPostProcessTemplates module = new RenderPostProcessTemplates(templates);

// When
TestDocument result = await ExecuteAsync(document, context, module).SingleAsync();

// Then
result.Content.ShouldBe(
@"<html><head></head><body>
Foo <a href=""https://site.com/a/d"">FizzBuzz</a>
</body></html>",
StringCompareShould.IgnoreLineEndings);
}

[Test]
public async Task MakesRelativeLinksRootRelative()
{
// Given
TestExecutionContext context = new TestExecutionContext();
context.Services.AddSingleton<IReadOnlyFileSystem>(context.FileSystem);
context.Settings[WebKeys.MakeLinksRootRelative] = true;
context.Settings[Keys.Host] = "site.com";
context.Settings[Keys.LinksUseHttps] = true;
TestDocument document = new TestDocument(
new NormalizedPath("/a/b/c.html"),
new NormalizedPath("a/b/c.html"),
@"<html>
<body>
Foo <a href=""../d"">FizzBuzz</a>
</body>
</html>");
Templates templates = new Templates();
RenderPostProcessTemplates module = new RenderPostProcessTemplates(templates);

// When
TestDocument result = await ExecuteAsync(document, context, module).SingleAsync();

// Then
result.Content.ShouldBe(
@"<html><head></head><body>
Foo <a href=""/a/d"">FizzBuzz</a>
</body></html>",
StringCompareShould.IgnoreLineEndings);
}
}
}
}

0 comments on commit 3830e2f

Please sign in to comment.