Skip to content

Commit

Permalink
Readme + control optimization from settings
Browse files Browse the repository at this point in the history
  • Loading branch information
alanta committed Nov 20, 2020
1 parent 6610647 commit 4996aca
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 15 deletions.
1 change: 1 addition & 0 deletions Models/SiteSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ public class SiteSettings{
public string DisqusId { get; set; }
public string ContactFormUrl { get; set; }
public string? GoogleAnalytics { get; set; }
public bool OptimizeOutput { get; set; } = true;
}
}
61 changes: 53 additions & 8 deletions Modules/HtmlOptimizer.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,76 @@
using NUglify;
using NUglify.Html;
using NUglify.JavaScript;
using Statiq.Common;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace MemoirsTheme.Modules
{
/// <summary>
/// Optimize HTML using NUglify.
/// </summary>
public class OptimizeHtml : Module
{
private readonly bool _enabled;
private readonly HtmlSettings _settings = new HtmlSettings();

/// <summary>
/// Optimize HTML in the content of the Document.
/// </summary>
public OptimizeHtml() : this(true)
{

}

/// <summary>
/// Optimize HTML in the content of the Document.
/// </summary>
/// <param name="enabled">Set to false to skip optimization. For example for debug builds.</param>
public OptimizeHtml(bool enabled)
{
_enabled = enabled;
}

/// <summary>
/// Configure the NUglify settings
/// </summary>
/// <param name="settings">An action to tweak the settings.</param>
/// <returns></returns>
public OptimizeHtml WithSettings(Action<HtmlSettings> settings)
{
settings?.Invoke(_settings);
return this;
}

protected override async Task<IEnumerable<IDocument>> ExecuteInputAsync(IDocument input, IExecutionContext context)
{
var settings = new HtmlSettings
{
MinifyJs = false
};
var original = await input.GetContentStringAsync();
var minifiedContent = Uglify.Html(original, settings);
if (string.IsNullOrWhiteSpace(original))
{
return (input).Yield(); // nothing to do, return original document
}

var minifiedContent = Uglify.Html(original, _settings);

if (minifiedContent.HasErrors)
{
context.LogError(input, $"Minification failed");
foreach (var error in minifiedContent.Errors)
{
context.LogError(input, $"{error.Message} {error.File} line ({error.StartLine},{error.StartColumn}) through ({error.EndLine},{error.EndColumn})");
if (error.IsError)
{
context.LogError(input, error.ToString());
}
else
{
context.LogWarning(input, error.ToString());
}
}
}

if (string.IsNullOrWhiteSpace(minifiedContent.Code))
{
context.LogWarning(input, $"Content minification skipped.");
return (input).Yield(); // return original document
}

Expand Down
8 changes: 7 additions & 1 deletion Pipelines/Posts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ public Posts(IDeliveryClient deliveryClient, SiteSettings site)
.WithViewData("SiteMetadata", site )
.WithModel(KontentConfig.As<Post>()),
new KontentImageProcessor(),
new OptimizeHtml()
new OptimizeHtml(site.OptimizeOutput)
.WithSettings(settings =>
{
// conflicts with ratings
settings.RemoveScriptStyleTypeAttribute = false;
settings.MinifyJs = false;
})
};

OutputModules = new ModuleList {
Expand Down
12 changes: 9 additions & 3 deletions Pipelines/StyleSheets.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Kentico.Kontent.Statiq.Memoirs.Models;
using Statiq.Common;
using Statiq.Core;
using Statiq.Sass;
Expand All @@ -6,17 +7,22 @@ namespace Kentico.Kontent.Statiq.Lumen.Pipelines
{
public class StyleSheets : Pipeline
{
public StyleSheets()
public StyleSheets(SiteSettings site)
{
InputModules = new ModuleList
{
new ReadFiles("_sass/**/{!_,}*.scss"),
new CompileSass()
.WithCompactOutputStyle(),
CompileSass(site.OptimizeOutput),
new SetDestination(Config.FromDocument((doc, ctx) =>
new NormalizedPath($"assets/css/{doc.Source.FileNameWithoutExtension}.css"))),
new WriteFiles()
};
}

private CompileSass CompileSass(bool optimize)
{
var module = new CompileSass();
return optimize ? module.WithCompressedOutputStyle() : module.WithCompactOutputStyle();
}
}
}
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
# Memoirs Statiq Kontent Theme

This theme was adopted from the Memoirs Jekyl theme by [Wowthemes](https://www.wowthemes.net). Please [buy them coffee](https://www.wowthemes.net/donate/)!
This is a full featured theme for [Kontent](https://kontent.ai/) and [Statiq](https://statiq.dev/) using [Kontent.Statiq](https://github.com/alanta/Kontent.Statiq).

# TODO
- [Live Demo](https://wowthemesnet.github.io/jekyll-theme-memoirs/) | [Docs & Download](https://bootstrapstarter.com/bootstrap-templates/jekyll-theme-memoirs/) | [Buy me a coffee](https://www.wowthemes.net/donate/)
## Features

* Posts & Pages with
* Quotes, Spoilers (blurring)
* GitHub Gists
* Code snippets with [Prism.js](https://prismjs.com/)
* Related pages, Tags and Categories
* Ratings
* Contact form using [Formspree](https://formspree.io/)
* Comments with [Disqus](https://disqus.com/)
* Table of contents generated from HTML _TODO_
* SEO support _TODO_
* Google Analytics
* Search with pre-compiled Lunr index
* HTML minification with [NUglify](https://github.com/trullock/NUglify)

## Credits

This theme was adopted from the Memoirs Jekyl theme by [Wowthemes](https://bootstrapstarter.com/bootstrap-templates/jekyll-theme-memoirs/). Please [buy them coffee](https://www.wowthemes.net/donate/)!

![memoirs](https://bootstrapstarter.com/assets/img/themes/memoirs-jekyll.jpg)

Expand Down

0 comments on commit 4996aca

Please sign in to comment.