feat: add minification

This commit is contained in:
thek4n 2026-07-17 15:10:14 +03:00
parent df4abd2ee6
commit 027b9517e1
9 changed files with 1289 additions and 29 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
notes/
target/
assets/
templates/*.min.html

View File

@ -6,6 +6,14 @@ Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to
[Semantic Versioning](https://semver.org/lang/en/).
## `v0.6.1` - 17.07.2026
### Added
- Add compile-time templates minificattion.
---
## `v0.6.0` - 17.07.2026
### Added
- Add text emoji support (`:smile:` for example).

1261
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
[package]
name = "mdpreview"
version = "0.6.0"
version = "0.6.1"
edition = "2024"
authors = ["Vladislav Kan <thek4n@yandex.ru>"]
build = "build.rs"
@ -14,6 +14,7 @@ strip = true
[build-dependencies]
reqwest = { version = "0.11", features = ["blocking"] }
minify-html = "0.18"
[dependencies]
pulldown-cmark = { version = "0.13.4", features = ["simd"] }

View File

@ -1,5 +1,7 @@
use minify_html::{Cfg, minify};
use std::env;
use std::fs;
use std::io;
use std::path::Path;
const HIGHLIGHT_STYLE_URL_TEMPLATE: &str =
@ -12,7 +14,12 @@ const MERMAID_SCRIPT_URL_TEMPLATE: &str =
fn main() {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=assets/");
println!("cargo:rerun-if-env-changed=FORCE_DEPS_DOWNLOAD");
println!("cargo:rerun-if-env-changed=RANDOM");
println!("cargo:rerun-if-changed=templates/");
minify_template_file("templates/base.html", "templates/base.min.html").unwrap();
minify_template_file("templates/dir.html", "templates/dir.min.html").unwrap();
minify_template_file("templates/file.html", "templates/file.min.html").unwrap();
let out_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let assets_path = Path::new(&out_dir).join("assets");
@ -79,3 +86,30 @@ fn should_download(path: &Path) -> bool {
true
}
fn minify_template_file<P: AsRef<str>>(input_path: P, output_path: P) -> io::Result<()> {
let html_content = fs::read_to_string(input_path.as_ref())?;
let minified = minify_template_content(&html_content)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
fs::write(output_path.as_ref(), minified)?;
Ok(())
}
#[allow(clippy::missing_errors_doc)]
pub fn minify_template_content(template: &str) -> Result<String, String> {
let mut cfg = Cfg::new();
cfg.allow_removing_spaces_between_attributes = true;
cfg.keep_comments = false;
cfg.minify_css = true;
cfg.minify_js = true;
cfg.keep_html_and_head_opening_tags = true;
cfg.keep_closing_tags = true;
cfg.remove_processing_instructions = true;
let minified_bytes = minify(template.as_bytes(), &cfg);
String::from_utf8(minified_bytes).map_err(|e| format!("Invalid UTF-8 in output: {e}"))
}

View File

@ -52,7 +52,7 @@ pub struct FileEntry {
}
#[derive(Template)]
#[template(path = "dir.html")]
#[template(path = "dir.min.html")]
pub struct DirectoryTemplate {
pub title_path: String,
pub files: Vec<FileEntry>,
@ -65,7 +65,7 @@ pub struct DirectoryTemplate {
}
#[derive(Template)]
#[template(path = "file.html")]
#[template(path = "file.min.html")]
pub struct NoteTemplate {
pub filename: String,
pub back_link: String,

View File

@ -276,6 +276,5 @@
hljs.highlightAll();
</script>
</body>
</html>

View File

@ -1,4 +1,4 @@
{% extends "base.html" %}
{% extends "base.min.html" %}
{% block title %}Directory: /{{ title_path }}{% endblock %}

View File

@ -1,4 +1,4 @@
{% extends "base.html" %}
{% extends "base.min.html" %}
{% block title %}{{ filename }}{% endblock %}