rehype-format
Advanced tools
Comparing version 2.1.0 to 2.2.0
25
index.js
'use strict'; | ||
/* Dependencies. */ | ||
var minify = require('rehype-minify-whitespace')({newlines: true}); | ||
@@ -10,3 +9,2 @@ var phrasing = require('hast-util-phrasing'); | ||
/* Expose. */ | ||
module.exports = format; | ||
@@ -39,2 +37,3 @@ | ||
var root = minify(tree); | ||
var head = false; | ||
@@ -55,4 +54,12 @@ visit(root, visitor); | ||
if (node.type === 'element' && node.tagName === 'head') { | ||
head = true; | ||
} | ||
if (head && node.type === 'element' && node.tagName === 'body') { | ||
head = false; | ||
} | ||
/* Don’t indent content of whitespace-sensitive nodes / inlines. */ | ||
if (!length || !padding(node) || ignore(parents.concat(node))) { | ||
if (!length || !padding(node, head) || ignore(parents.concat(node))) { | ||
return; | ||
@@ -78,9 +85,11 @@ } | ||
node.children = result = []; | ||
result = []; | ||
index = -1; | ||
node.children = result; | ||
while (++index < length) { | ||
child = children[index]; | ||
if (padding(child) || (newline && index === 0)) { | ||
if (padding(child, head) || (newline && index === 0)) { | ||
result.push({ | ||
@@ -97,3 +106,3 @@ type: 'text', | ||
if (newline || padding(prev)) { | ||
if (newline || padding(prev, head)) { | ||
result.push({ | ||
@@ -114,3 +123,3 @@ type: 'text', | ||
function padding(node) { | ||
function padding(node, head) { | ||
if (node.type === 'root') { | ||
@@ -121,3 +130,3 @@ return true; | ||
if (node.type === 'element') { | ||
return node.tagName === 'script' || !phrasing(node); | ||
return node.tagName === 'script' || !phrasing(node) || head; | ||
} | ||
@@ -124,0 +133,0 @@ |
{ | ||
"name": "rehype-format", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "Format HTML with rehype", | ||
@@ -35,9 +35,9 @@ "license": "MIT", | ||
"negate": "^1.0.0", | ||
"nyc": "^10.0.0", | ||
"rehype": "^4.0.0", | ||
"nyc": "^11.0.0", | ||
"rehype": "^5.0.0", | ||
"remark-cli": "^3.0.0", | ||
"remark-preset-wooorm": "^2.0.0", | ||
"remark-preset-wooorm": "^3.0.0", | ||
"tape": "^4.0.0", | ||
"to-vfile": "^2.1.1", | ||
"xo": "^0.17.0" | ||
"xo": "^0.18.0" | ||
}, | ||
@@ -62,2 +62,3 @@ "scripts": { | ||
"space": true, | ||
"esnext": false, | ||
"ignores": [ | ||
@@ -64,0 +65,0 @@ "rehype-format.js" |
@@ -67,6 +67,6 @@ # rehype-format [![Build Status][travis-badge]][travis] [![Coverage Status][codecov-badge]][codecov] | ||
* Collapse all white-space (to a single space or newline); | ||
* Remove unneeded white-space; | ||
* Inject needed newlines and indentation; | ||
* Indent previously collapsed newlines properly. | ||
* Collapse all white-space (to a single space or newline) | ||
* Remove unneeded white-space | ||
* Inject needed newlines and indentation | ||
* Indent previously collapsed newlines properly | ||
@@ -77,17 +77,28 @@ All superfluous white-space is removed. However, as newlines | ||
###### `options` | ||
##### `options` | ||
* `indent` (`number`, `string`, default: `2`) | ||
— Indentation per level. When number, uses that amount | ||
of spaces. When `string`, uses that per indentation | ||
level. | ||
* `indentInitial` (`boolean`, default: `true`) | ||
— Whether to indent the first level (usually, in the | ||
`html` element, thus not indenting `head` and `body`). | ||
* `blanks` (`Array.<string>`, default: `[]`) | ||
— List of tag-names, which, when next to each other, | ||
are joined by a blank line (`\n\n`). For example, | ||
when `['head', 'body']` is given, a blank line is added | ||
between these two. | ||
###### `options.indent` | ||
`number`, `string`, default: `2` | ||
— Indentation per level. When number, uses that amount of spaces. When | ||
`string`, uses that per indentation level. | ||
###### `options.indentInitial` | ||
`boolean`, default: `true` | ||
— Whether to indent the first level (usually, in the `html` element, thus | ||
not indenting `head` and `body`). | ||
###### `options.blanks` | ||
`Array.<string>`, default: `[]` | ||
— List of tag-names, which, when next to each other, are joined by a blank | ||
line (`\n\n`). For example, when `['head', 'body']` is given, a blank line | ||
is added between these two. | ||
## Related | ||
* [`rehype-minify`](https://github.com/wooorm/rehype-minify) | ||
— Minify HTML | ||
## License | ||
@@ -94,0 +105,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8681
109
123