From 774e1411bbc1bcbe52466d564f84fd695d220881 Mon Sep 17 00:00:00 2001 From: Bauke Date: Mon, 9 Jan 2023 15:43:05 +0100 Subject: [PATCH] Add HTML minification. --- Cargo.toml | 1 + source/main.rs | 1 + source/minify.rs | 22 ++++++++++++++++++++++ source/templates/mod.rs | 2 +- source/video/mod.rs | 6 +++++- 5 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 source/minify.rs diff --git a/Cargo.toml b/Cargo.toml index 99a2ad6..5d784bd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ path = "source/main.rs" askama = "0.11.1" color-eyre = "0.6.2" comrak = "0.15.0" +minify-html = "0.10.7" rsass = "0.23.4" toml-frontmatter = "0.1.0" diff --git a/source/main.rs b/source/main.rs index 706a16b..ef22dcd 100644 --- a/source/main.rs +++ b/source/main.rs @@ -6,6 +6,7 @@ use std::{ use color_eyre::{install, Result}; mod copy; +mod minify; mod scss; mod templates; mod video; diff --git a/source/minify.rs b/source/minify.rs new file mode 100644 index 0000000..deb4d63 --- /dev/null +++ b/source/minify.rs @@ -0,0 +1,22 @@ +use color_eyre::Result; + +/** +Minify HTML using [`minify_html`]. +*/ +pub fn html(data: String) -> Result { + let minify_config = minify_html::Cfg { + do_not_minify_doctype: true, + ensure_spec_compliant_unquoted_attribute_values: true, + keep_closing_tags: true, + keep_comments: false, + keep_html_and_head_opening_tags: true, + keep_spaces_between_attributes: true, + minify_css: false, + minify_js: false, + remove_bangs: false, + remove_processing_instructions: false, + }; + + let minified_data = minify_html::minify(&data.into_bytes(), &minify_config); + String::from_utf8(minified_data).map_err(Into::into) +} diff --git a/source/templates/mod.rs b/source/templates/mod.rs index 7d594d0..f31097b 100644 --- a/source/templates/mod.rs +++ b/source/templates/mod.rs @@ -21,7 +21,7 @@ impl Index { page_title: "Bauke".to_string(), }; - write(destination, template.render()?)?; + write(destination, crate::minify::html(template.render()?)?)?; Ok(()) } diff --git a/source/video/mod.rs b/source/video/mod.rs index bb91a39..772130f 100644 --- a/source/video/mod.rs +++ b/source/video/mod.rs @@ -68,7 +68,11 @@ pub fn write_all(public_dir: &Path) -> Result<()> { speedrun: video_data.speedrun, video_id: video_data.id, }; - fs::write(video_dir.join("index.html"), template.render()?)?; + + fs::write( + video_dir.join("index.html"), + crate::minify::html(template.render()?)?, + )?; } Ok(())