@small-tools/html 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ type MinifyHtml = (html: string) => string;
2
+ declare const minifyHtml: MinifyHtml;
3
+ export { minifyHtml };
4
+ export type { MinifyHtml };
@@ -0,0 +1,25 @@
1
+ const minifyHtml = (html) => {
2
+ const preservedTagContentRegExp = /<(pre|code|textarea|script|style)(?:[\s\S]*?)<\/\1>/gi;
3
+ const preservedAttributeContentRegExp = /\s+(?:title|alt|placeholder|value|style|srcdoc|data-.+?|aria-.+?|on.+?)\s*=\s*(?:"(?:[\s\S]*?)")/gi;
4
+ const preservedTagContents = [];
5
+ const preservedAttributesContents = [];
6
+ return html
7
+ .replaceAll(preservedTagContentRegExp, (match) => {
8
+ preservedTagContents.push(match);
9
+ return `___PRESERVED_TAG_${preservedTagContents.length - 1}___`;
10
+ }) // Preserve tag contents
11
+ .replaceAll(preservedAttributeContentRegExp, (match) => {
12
+ preservedAttributesContents.push(match.trim());
13
+ return `___PRESERVED_ATTRIBUTE_${preservedAttributesContents.length - 1}___`;
14
+ }) // Preserve attribute contents
15
+ .replaceAll(/<!--.*?-->/gs, "") // Remove comments
16
+ .replaceAll(/\n|\r|\t/g, "") // Remove tabs and new lines
17
+ .replaceAll(/\s{2,}/g, " ") // Remove multiple spaces
18
+ .replaceAll(/>\s+</g, "><") // Remove spaces between tags
19
+ .replaceAll(/\s+>/g, ">") // Remove spaces before closing tags
20
+ .replaceAll(/<\s+/g, "<") // Remove spaces before opening tags
21
+ .replaceAll(/\s+=\s+/g, "=") // Remove spaces around =
22
+ .replaceAll(/___PRESERVED_TAG_(\d+)___/g, (match, index) => preservedTagContents[index] ?? match)
23
+ .replaceAll(/___PRESERVED_ATTRIBUTE_(\d+)___/g, (_match, index) => ` ${preservedAttributesContents[index]}`);
24
+ };
25
+ export { minifyHtml };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from "./api/classnames.ts";
2
2
  export * from "./api/css.ts";
3
3
  export * from "./api/html.ts";
4
+ export * from "./api/minify-html.ts";
4
5
  export * from "./api/sanitize-html.ts";
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from "./api/classnames.js";
2
2
  export * from "./api/css.js";
3
3
  export * from "./api/html.js";
4
+ export * from "./api/minify-html.js";
4
5
  export * from "./api/sanitize-html.js";
package/package.json CHANGED
@@ -18,5 +18,5 @@
18
18
  "lint": "biome check",
19
19
  "build": "tsc --build"
20
20
  },
21
- "version": "1.2.0"
21
+ "version": "1.3.0"
22
22
  }