rehype-minify-whitespace
Advanced tools
+26
-5
| /** | ||
| * @fileoverview | ||
| * Collapse whitespace. | ||
| * | ||
| * Normally, collapses to a single space. If `newlines: true`, | ||
| * collapses white-space containing newlines to `'\n'` instead | ||
| * of `' '`. | ||
| * @example | ||
@@ -25,7 +29,11 @@ * <h1>Heading</h1> | ||
| function collapse() { | ||
| function collapse(proc, options) { | ||
| return transform; | ||
| function transform(tree) { | ||
| return minify(tree, options || {}); | ||
| } | ||
| } | ||
| function transform(tree) { | ||
| function minify(tree, options) { | ||
| var whitespace = options.newlines ? collapseToNewLines : collapseWhiteSpace; | ||
| var modifier = modify(visitor); | ||
@@ -51,11 +59,11 @@ var inside = false; | ||
| value = collapseWhiteSpace(node.value); | ||
| value = whitespace(node.value); | ||
| end = value.length; | ||
| start = 0; | ||
| if (value.charAt(0) === ' ' && viable(prev)) { | ||
| if (empty(value.charAt(0)) && viable(prev)) { | ||
| start++; | ||
| } | ||
| if (value.charAt(end - 1) === ' ' && viable(next)) { | ||
| if (empty(value.charAt(end - 1)) && viable(next)) { | ||
| end--; | ||
@@ -102,1 +110,14 @@ } | ||
| } | ||
| /* Collapse to spaces, or newlines if they’re in a run. */ | ||
| function collapseToNewLines(value) { | ||
| var result = String(value).replace(/\s+/g, function ($0) { | ||
| return $0.indexOf('\n') === -1 ? ' ' : '\n'; | ||
| }); | ||
| return result; | ||
| } | ||
| function empty(character) { | ||
| return character === ' ' || character === '\n'; | ||
| } |
+1
-1
| { | ||
| "name": "rehype-minify-whitespace", | ||
| "version": "1.0.0", | ||
| "version": "1.1.0", | ||
| "description": "Collapse whitespace with rehype", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
+4
-0
@@ -7,2 +7,6 @@ <!--This file is generated by `build-packages.js`--> | ||
| Normally, collapses to a single space. If `newlines: true`, | ||
| collapses white-space containing newlines to `'\n'` instead | ||
| of `' '`. | ||
| ## Install | ||
@@ -9,0 +13,0 @@ |
4571
19.88%134
15.52%35
12.9%