{"id":21586125,"url":"https://github.com/magiclen/html-minifier","last_synced_at":"2025-04-10T20:20:23.425Z","repository":{"id":46975293,"uuid":"145802973","full_name":"magiclen/html-minifier","owner":"magiclen","description":"This library can help you generate and minify your HTML code at the same time. It also supports to minify JS and CSS in `\u003cstyle\u003e`, `\u003cscript\u003e` elements, and ignores the minification of `\u003cpre\u003e` elements.","archived":false,"fork":false,"pushed_at":"2023-12-15T03:00:06.000Z","size":102,"stargazers_count":20,"open_issues_count":1,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-05-03T04:47:48.217Z","etag":null,"topics":["minify-html","rust"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/magiclen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-08-23T05:03:09.000Z","updated_at":"2024-04-04T19:53:27.000Z","dependencies_parsed_at":"2024-11-24T18:00:38.850Z","dependency_job_id":null,"html_url":"https://github.com/magiclen/html-minifier","commit_stats":{"total_commits":85,"total_committers":4,"mean_commits":21.25,"dds":0.04705882352941182,"last_synced_commit":"6cac1c701c552ea09db4287ac22271396b6f22a0"},"previous_names":[],"tags_count":59,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magiclen%2Fhtml-minifier","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magiclen%2Fhtml-minifier/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magiclen%2Fhtml-minifier/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magiclen%2Fhtml-minifier/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/magiclen","download_url":"https://codeload.github.com/magiclen/html-minifier/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248289809,"owners_count":21078921,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["minify-html","rust"],"created_at":"2024-11-24T15:12:44.581Z","updated_at":"2025-04-10T20:20:23.400Z","avatar_url":"https://github.com/magiclen.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"HTML Minifier\n====================\n\n[![CI](https://github.com/magiclen/html-minifier/actions/workflows/ci.yml/badge.svg)](https://github.com/magiclen/html-minifier/actions/workflows/ci.yml)\n\nThis library can help you generate and minify your HTML code at the same time. It also supports to minify JS and CSS in `\u003cstyle\u003e`, `\u003cscript\u003e` elements, and ignores the minification of `\u003cpre\u003e`, `\u003ccode\u003e` and `\u003ctextarea\u003e` elements.\n\nHTML is minified by the following rules:\n\n* ASCII control characters (0x00-0x08, 0x11-0x1F, 0x7F) are always removed.\n* Comments can be optionally removed. (removed by default)\n* **Useless** whitespaces (spaces, tabs and newlines) are removed.\n* Whitespaces (spaces, tabs and newlines) are converted to a single `'\\x20'` or a single '\\n', if possible.\n* Empty attribute values are collapsed. (e.g `\u003cinput readonly=\"\"\u003e` =\u003e `\u003cinput readonly\u003e` )\n* The inner HTML of all elements is minified except for the following elements:\n  * `\u003cpre\u003e`\n  * `\u003ctextarea\u003e`\n  * `\u003ccode\u003e` (optionally, minified by default)\n  * `\u003cstyle\u003e` (if the `type` attribute is unsupported)\n  * `\u003cscript\u003e` (if the `type` attribute is unsupported)\n* JS code and CSS code in `\u003cscript\u003e` and `\u003cstyle\u003e` elements are minified by [minifier](https://crates.io/crates/minifier).\n\nThe original (non-minified) HTML doesn't need to be completely generated before using this library because this library doesn't do any deserialization to create DOMs.\n\n## Examples\n\n```rust\nuse html_minifier::HTMLMinifier;\n\nlet mut html_minifier = HTMLMinifier::new();\n\nhtml_minifier.digest(\"\u003c!DOCTYPE html\u003e   \u003chtml  \").unwrap();\nhtml_minifier.digest(\"lang=  en \u003e\").unwrap();\nhtml_minifier.digest(\"\n\u003chead\u003e\n    \u003cmeta name=viewport\u003e\n\u003c/head\u003e\n\").unwrap();\nhtml_minifier.digest(\"\n\u003cbody class=' container   bg-light '\u003e\n    \u003cinput type='text' value='123   456' readonly=''  /\u003e\n\n    123456\n    \u003cb\u003ebig\u003c/b\u003e 789\n    ab\n    c\n    中文\n    字\n\u003c/body\u003e\n\").unwrap();\nhtml_minifier.digest(\"\u003c/html  \u003e\").unwrap();\n\nassert_eq!(\"\u003c!DOCTYPE html\u003e \u003chtml lang=en\u003e\n\u003chead\u003e\n\u003cmeta name=viewport\u003e\n\u003c/head\u003e\n\u003cbody class='container bg-light'\u003e\n\u003cinput type='text' value='123   456' readonly/\u003e\n123456\n\u003cb\u003ebig\u003c/b\u003e 789\nab\nc\n中文\n字\n\u003c/body\u003e\n\u003c/html\u003e\".as_bytes(), html_minifier.get_html());\n```\n\n```rust\nuse html_minifier::HTMLMinifier;\n\nlet mut html_minifier = HTMLMinifier::new();\n\nhtml_minifier.digest(\"\u003cpre  \u003e   Hello  world!   \u003c/pre  \u003e\").unwrap();\n\nassert_eq!(b\"\u003cpre\u003e   Hello  world!   \u003c/pre\u003e\", html_minifier.get_html());\n```\n\n```rust\nuse html_minifier::HTMLMinifier;\n\nlet mut html_minifier = HTMLMinifier::new();\n\nhtml_minifier.digest(\"\u003cscript type='  application/javascript '\u003e   alert('Hello!')    ;   \u003c/script\u003e\").unwrap();\n\nassert_eq!(\"\u003cscript type='application/javascript'\u003ealert('Hello!')\u003c/script\u003e\".as_bytes(), html_minifier.get_html());\n```\n\n## Write HTML to a Writer\n\nIf you don't want to store your HTML in memory (e.g. writing to a file instead), you can use the `HTMLMinifierHelper` struct which provides a low-level API that allows you to pass your output instance when invoking the `digest` method.\n\n```rust\nuse html_minifier::HTMLMinifierHelper;\n\nuse std::fs::File;\nuse std::io::Read;\n\nlet mut input_file = File::open(\"tests/data/w3schools.com_tryhow_css_example_website.htm\").unwrap();\nlet mut output_file = File::create(\"tests/data/index.min.html\").unwrap();\n\nlet mut buffer = [0u8; 256];\n\nlet mut html_minifier_helper = HTMLMinifierHelper::new();\n\nloop {\n    let c = input_file.read(\u0026mut buffer).unwrap();\n\n    if c == 0 {\n        break;\n    }\n\n    html_minifier_helper.digest(\u0026buffer[..c], \u0026mut output_file).unwrap();\n}\n```\n\n## Crates.io\n\nhttps://crates.io/crates/html-minifier\n\n## Documentation\n\nhttps://docs.rs/html-minifier\n\n## License\n\n[MIT](LICENSE)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagiclen%2Fhtml-minifier","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmagiclen%2Fhtml-minifier","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagiclen%2Fhtml-minifier/lists"}