-
Notifications
You must be signed in to change notification settings - Fork 932
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #1292
- Loading branch information
Showing
6 changed files
with
55 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |