Skip to content

Commit

Permalink
Fix html minification
Browse files Browse the repository at this point in the history
Closes #1292
  • Loading branch information
Keats committed Jan 7, 2021
1 parent aa03a7f commit 1a36c20
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion components/site/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
24 changes: 2 additions & 22 deletions components/site/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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;

Expand Down Expand Up @@ -490,26 +490,6 @@ impl Site {
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
pub fn copy_static_directories(&self) -> Result<()> {
// The user files will overwrite the theme files
Expand Down Expand Up @@ -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),
}
Expand Down
1 change: 1 addition & 0 deletions components/utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }

Expand Down
1 change: 1 addition & 0 deletions components/utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod de;
pub mod fs;
pub mod minify;
pub mod net;
pub mod site;
pub mod slugs;
Expand Down
50 changes: 50 additions & 0 deletions components/utils/src/minify.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 1a36c20

Please sign in to comment.