From 1a36c20bd27d0847fa33e4f5ef5f82ed6f62358c Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Thu, 7 Jan 2021 19:04:02 +0100 Subject: [PATCH] Fix html minification Closes #1292 --- Cargo.lock | 2 +- components/site/Cargo.toml | 1 - components/site/src/lib.rs | 24 ++-------------- components/utils/Cargo.toml | 1 + components/utils/src/lib.rs | 1 + components/utils/src/minify.rs | 50 ++++++++++++++++++++++++++++++++++ 6 files changed, 55 insertions(+), 24 deletions(-) create mode 100644 components/utils/src/minify.rs diff --git a/Cargo.lock b/Cargo.lock index 0fc19c3c..56b643a0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2423,7 +2423,6 @@ dependencies = [ "lazy_static", "library", "link_checker", - "minify-html", "rayon", "relative-path", "sass-rs", @@ -2976,6 +2975,7 @@ version = "0.1.0" dependencies = [ "errors", "filetime", + "minify-html", "percent-encoding", "serde", "serde_derive", diff --git a/components/site/Cargo.toml b/components/site/Cargo.toml index 47c6a2fb..51fa4907 100644 --- a/components/site/Cargo.toml +++ b/components/site/Cargo.toml @@ -9,7 +9,6 @@ include = ["src/**/*"] tera = "1" glob = "0.3" walkdir = "2" -minify-html = "0.3.8" rayon = "1" serde = "1" serde_derive = "1" diff --git a/components/site/src/lib.rs b/components/site/src/lib.rs index 7347c23f..dd4b43a6 100644 --- a/components/site/src/lib.rs +++ b/components/site/src/lib.rs @@ -10,7 +10,6 @@ use std::path::{Path, PathBuf}; use std::sync::{Arc, Mutex, RwLock}; use lazy_static::lazy_static; -use minify_html::{with_friendly_error, Cfg}; use rayon::prelude::*; use tera::{Context, Tera}; use walkdir::{DirEntry, WalkDir}; @@ -25,6 +24,7 @@ use templates::render_redirect_template; use utils::fs::{ copy_directory, copy_file_if_needed, create_directory, create_file, ensure_directory_exists, }; +use utils::minify; use utils::net::get_available_port; use utils::templates::render_template; @@ -490,26 +490,6 @@ impl Site { html } - /// Minifies html content - fn minify(&self, html: String) -> Result { - let cfg = &Cfg { minify_js: false }; - let mut input_bytes = html.as_bytes().to_vec(); - match with_friendly_error(&mut input_bytes, cfg) { - Ok(_len) => match std::str::from_utf8(&input_bytes) { - Ok(result) => Ok(result.to_string()), - Err(err) => bail!("Failed to convert bytes to string : {}", err), - }, - Err(minify_error) => { - bail!( - "Failed to truncate html at character {}: {} \n {}", - minify_error.position, - minify_error.message, - minify_error.code_context - ); - } - } - } - /// Copy the main `static` folder and the theme `static` folder if a theme is used pub fn copy_static_directories(&self) -> Result<()> { // The user files will overwrite the theme files @@ -581,7 +561,7 @@ impl Site { let final_content = if !filename.ends_with("html") || !self.config.minify_html { content } else { - match self.minify(content) { + match minify::html(content) { Ok(minified_content) => minified_content, Err(error) => bail!(error), } diff --git a/components/utils/Cargo.toml b/components/utils/Cargo.toml index 9923f80f..b169e00a 100644 --- a/components/utils/Cargo.toml +++ b/components/utils/Cargo.toml @@ -15,6 +15,7 @@ serde_derive = "1" slug = "0.1" percent-encoding = "2" filetime = "0.2.12" +minify-html = "0.3.8" errors = { path = "../errors" } diff --git a/components/utils/src/lib.rs b/components/utils/src/lib.rs index 44ebf4ed..eac2325d 100644 --- a/components/utils/src/lib.rs +++ b/components/utils/src/lib.rs @@ -1,5 +1,6 @@ pub mod de; pub mod fs; +pub mod minify; pub mod net; pub mod site; pub mod slugs; diff --git a/components/utils/src/minify.rs b/components/utils/src/minify.rs new file mode 100644 index 00000000..f385d336 --- /dev/null +++ b/components/utils/src/minify.rs @@ -0,0 +1,50 @@ +use errors::{bail, Result}; +use minify_html::{with_friendly_error, Cfg}; + +pub fn html(html: String) -> Result { + let cfg = &Cfg { minify_js: false }; + let mut input_bytes = html.as_bytes().to_vec(); + + match with_friendly_error(&mut input_bytes, cfg) { + Ok(len) => match std::str::from_utf8(&input_bytes) { + Ok(result) => Ok(result[..len].to_string()), + Err(err) => bail!("Failed to convert bytes to string : {}", err), + }, + Err(minify_error) => { + bail!( + "Failed to truncate html at character {}: {} \n {}", + minify_error.position, + minify_error.message, + minify_error.code_context + ); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + // https://github.com/getzola/zola/issues/1292 + #[test] + fn can_minify_html() { + let input = r#" + + + + + + + + +

Example blog post

+ + FOO BAR + + +"#; + let expected = r#"

Example blog post

FOO BAR"#; + let res = html(input.to_owned()).unwrap(); + assert_eq!(res, expected); + } +}