Fix html minification

Closes #1292
This commit is contained in:
Vincent Prouillet 2021-01-07 19:04:02 +01:00
parent aa03a7fec5
commit 1a36c20bd2
6 changed files with 55 additions and 24 deletions

2
Cargo.lock generated
View file

@ -2423,7 +2423,6 @@ dependencies = [
"lazy_static", "lazy_static",
"library", "library",
"link_checker", "link_checker",
"minify-html",
"rayon", "rayon",
"relative-path", "relative-path",
"sass-rs", "sass-rs",
@ -2976,6 +2975,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"errors", "errors",
"filetime", "filetime",
"minify-html",
"percent-encoding", "percent-encoding",
"serde", "serde",
"serde_derive", "serde_derive",

View file

@ -9,7 +9,6 @@ include = ["src/**/*"]
tera = "1" tera = "1"
glob = "0.3" glob = "0.3"
walkdir = "2" walkdir = "2"
minify-html = "0.3.8"
rayon = "1" rayon = "1"
serde = "1" serde = "1"
serde_derive = "1" serde_derive = "1"

View file

@ -10,7 +10,6 @@ use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, RwLock}; use std::sync::{Arc, Mutex, RwLock};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use minify_html::{with_friendly_error, Cfg};
use rayon::prelude::*; use rayon::prelude::*;
use tera::{Context, Tera}; use tera::{Context, Tera};
use walkdir::{DirEntry, WalkDir}; use walkdir::{DirEntry, WalkDir};
@ -25,6 +24,7 @@ use templates::render_redirect_template;
use utils::fs::{ use utils::fs::{
copy_directory, copy_file_if_needed, create_directory, create_file, ensure_directory_exists, copy_directory, copy_file_if_needed, create_directory, create_file, ensure_directory_exists,
}; };
use utils::minify;
use utils::net::get_available_port; use utils::net::get_available_port;
use utils::templates::render_template; use utils::templates::render_template;
@ -490,26 +490,6 @@ impl Site {
html html
} }
/// Minifies html content
fn minify(&self, html: String) -> Result<String> {
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 /// Copy the main `static` folder and the theme `static` folder if a theme is used
pub fn copy_static_directories(&self) -> Result<()> { pub fn copy_static_directories(&self) -> Result<()> {
// The user files will overwrite the theme files // 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 { let final_content = if !filename.ends_with("html") || !self.config.minify_html {
content content
} else { } else {
match self.minify(content) { match minify::html(content) {
Ok(minified_content) => minified_content, Ok(minified_content) => minified_content,
Err(error) => bail!(error), Err(error) => bail!(error),
} }

View file

@ -15,6 +15,7 @@ serde_derive = "1"
slug = "0.1" slug = "0.1"
percent-encoding = "2" percent-encoding = "2"
filetime = "0.2.12" filetime = "0.2.12"
minify-html = "0.3.8"
errors = { path = "../errors" } errors = { path = "../errors" }

View file

@ -1,5 +1,6 @@
pub mod de; pub mod de;
pub mod fs; pub mod fs;
pub mod minify;
pub mod net; pub mod net;
pub mod site; pub mod site;
pub mod slugs; pub mod slugs;

View file

@ -0,0 +1,50 @@
use errors::{bail, Result};
use minify_html::{with_friendly_error, Cfg};
pub fn html(html: String) -> Result<String> {
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#"
<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<p>Example blog post</p>
FOO BAR
</body>
</html>
"#;
let expected = r#"<!doctype html><html><head><meta charset=utf-8><body><p>Example blog post</p> FOO BAR"#;
let res = html(input.to_owned()).unwrap();
assert_eq!(res, expected);
}
}