Skip to content

Commit

Permalink
Fixed some regressions with layout templates and HTML fragments (#934)
Browse files Browse the repository at this point in the history
  • Loading branch information
daveaglick committed Dec 3, 2020
1 parent f59fb61 commit 602b597
Show file tree
Hide file tree
Showing 4 changed files with 388 additions and 1 deletion.
5 changes: 5 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# 1.0.0-beta.17

- Fixed a regression in how layouts are applied to Markdown files (#934).
- Changed behavior introduced in 1.0.0-beta.16 regarding HTML files and layouts, now layouts are applied if the HTML file does not contain a `<html>` tag, and are not applied if it does (#934).
- Added a `SetDefaultLayoutTemplate()` bootstrapper extension to change the layout engine applied to HTML fragments to an existing one (if the default of Razor is not wanted).
- Added a `SetDefaultLayoutModule()` bootstrapper extension to change the layout engine applied to HTML fragments to a new module (if the default of Razor is not wanted).

# 1.0.0-beta.16

- Updated Statiq Framework to version [1.0.0-beta.31](https://github.com/statiqdev/Statiq.Framework/releases/tag/v1.0.0-beta.31).
Expand Down
30 changes: 30 additions & 0 deletions src/Statiq.Web/Bootstrapper/BootstrapperTemplateExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,35 @@ public static TBootstrapper ModifyTemplate<TBootstrapper>(
throw new Exception($"Template for media type {mediaType} not found");
}
});

public static TBootstrapper SetDefaultLayoutTemplate<TBootstrapper>(
this TBootstrapper bootstrapper,
string mediaType)
where TBootstrapper : IBootstrapper =>
bootstrapper.ConfigureTemplates(templates =>
{
if (!templates.TryGetValue(mediaType, out Template template))
{
throw new Exception($"Template for media type {mediaType} not found");
}
if (template.ContentType != ContentType.Content)
{
throw new Exception($"Template for media type {mediaType} is not a {ContentType.Content} template");
}
if (template.Phase != Phase.PostProcess)
{
throw new Exception($"Template for media type {mediaType} is not a {Phase.PostProcess} template");
}
templates[MediaTypes.HtmlFragment].Module = template.Module;
});

public static TBootstrapper SetDefaultLayoutModule<TBootstrapper>(
this TBootstrapper bootstrapper,
IModule module)
where TBootstrapper : IBootstrapper =>
bootstrapper.ConfigureTemplates(templates =>
{
templates[MediaTypes.HtmlFragment].Module = module;
});
}
}
14 changes: 13 additions & 1 deletion src/Statiq.Web/Templates/Templates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,19 @@ internal Templates()
}
return null; // If no layout metadata, revert to default behavior
}))));
Add(MediaTypes.HtmlFragment, this[MediaTypes.Razor]); // Set Razor as the default for HTML fragment files, don't process full HTML files as part of a template, they're assumed to be complete

// Set Razor as the default for HTML and HTML fragment files, but if HTML make sure we don't already have a full <html> before applying layouts
// By changing the module of MediaTypes.HtmlFragment we can change the default layout template
Add(MediaTypes.HtmlFragment, this[MediaTypes.Razor]);
Add(
MediaTypes.Html,
new Template(
ContentType.Content,
Phase.PostProcess,
new ExecuteIf(Config.FromDocument(async doc => !(await doc.GetContentStringAsync()).Replace(" ", string.Empty).Contains("<html")))
{
new ExecuteConfig(Config.FromContext(_ => this[MediaTypes.HtmlFragment].Module))
}));
}

/// <summary>
Expand Down
Loading

0 comments on commit 602b597

Please sign in to comment.