-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
110 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
coverage/ | ||
rehype-format.js | ||
rehype-format.min.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,140 +1,143 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
var minify = require('rehype-minify-whitespace')({newlines: true}); | ||
var phrasing = require('hast-util-phrasing'); | ||
var sensitive = require('html-whitespace-sensitive-tag-names'); | ||
var repeat = require('repeat-string'); | ||
var visit = require('unist-util-visit-parents'); | ||
var minify = require('rehype-minify-whitespace')({newlines: true}) | ||
var phrasing = require('hast-util-phrasing') | ||
var sensitive = require('html-whitespace-sensitive-tag-names') | ||
var repeat = require('repeat-string') | ||
var visit = require('unist-util-visit-parents') | ||
|
||
module.exports = format; | ||
module.exports = format | ||
|
||
/* Constants. */ | ||
var double = '\n\n'; | ||
var single = '\n'; | ||
var re = /\n/g; | ||
var double = '\n\n' | ||
var single = '\n' | ||
var re = /\n/g | ||
|
||
/* Format white-space. */ | ||
function format(options) { | ||
var settings = options || {}; | ||
var indent = settings.indent || 2; | ||
var indentInitial = settings.indentInitial; | ||
var blanks = settings.blanks || []; | ||
var settings = options || {} | ||
var indent = settings.indent || 2 | ||
var indentInitial = settings.indentInitial | ||
var blanks = settings.blanks || [] | ||
|
||
if (typeof indent === 'number') { | ||
indent = repeat(' ', indent); | ||
indent = repeat(' ', indent) | ||
} | ||
|
||
/* Default to indenting the initial level. */ | ||
if (indentInitial === null || indentInitial === undefined) { | ||
indentInitial = true; | ||
indentInitial = true | ||
} | ||
|
||
return transform; | ||
return transform | ||
|
||
function transform(tree) { | ||
var root = minify(tree); | ||
var head = false; | ||
var root = minify(tree) | ||
var head = false | ||
|
||
visit(root, visitor); | ||
visit(root, visitor) | ||
|
||
return root; | ||
return root | ||
|
||
function visitor(node, parents) { | ||
var children = node.children || []; | ||
var length = children.length; | ||
var level = parents.length; | ||
var index = -1; | ||
var result; | ||
var prev; | ||
var child; | ||
var newline; | ||
var children = node.children || [] | ||
var length = children.length | ||
var level = parents.length | ||
var index = -1 | ||
var result | ||
var prev | ||
var child | ||
var newline | ||
|
||
if (node.type === 'element' && node.tagName === 'head') { | ||
head = true; | ||
head = true | ||
} | ||
|
||
if (head && node.type === 'element' && node.tagName === 'body') { | ||
head = false; | ||
head = false | ||
} | ||
|
||
/* Don’t indent content of whitespace-sensitive nodes / inlines. */ | ||
if (!length || !padding(node, head) || ignore(parents.concat(node))) { | ||
return; | ||
return | ||
} | ||
|
||
if (!indentInitial) { | ||
level--; | ||
level-- | ||
} | ||
|
||
/* Indent newlines in `text`. */ | ||
while (++index < length) { | ||
child = children[index]; | ||
child = children[index] | ||
|
||
if (child.type === 'text') { | ||
if (child.value.indexOf('\n') !== -1) { | ||
newline = true; | ||
newline = true | ||
} | ||
|
||
child.value = child.value.replace(re, '$&' + repeat(indent, level)); | ||
child.value = child.value.replace(re, '$&' + repeat(indent, level)) | ||
} | ||
} | ||
|
||
result = []; | ||
index = -1; | ||
result = [] | ||
index = -1 | ||
|
||
node.children = result; | ||
node.children = result | ||
|
||
while (++index < length) { | ||
child = children[index]; | ||
child = children[index] | ||
|
||
if (padding(child, head) || (newline && index === 0)) { | ||
result.push({ | ||
type: 'text', | ||
value: ((prev && blank(prev) && blank(child)) ? double : single) + | ||
value: | ||
(prev && blank(prev) && blank(child) ? double : single) + | ||
repeat(indent, level) | ||
}); | ||
}) | ||
} | ||
|
||
prev = child; | ||
result.push(child); | ||
prev = child | ||
result.push(child) | ||
} | ||
|
||
if (newline || padding(prev, head)) { | ||
result.push({ | ||
type: 'text', | ||
value: single + repeat(indent, level - 1) | ||
}); | ||
}) | ||
} | ||
} | ||
} | ||
|
||
function blank(node) { | ||
return node.type === 'element' && | ||
return ( | ||
node.type === 'element' && | ||
blanks.length !== 0 && | ||
blanks.indexOf(node.tagName) !== -1; | ||
blanks.indexOf(node.tagName) !== -1 | ||
) | ||
} | ||
} | ||
|
||
function padding(node, head) { | ||
if (node.type === 'root') { | ||
return true; | ||
return true | ||
} | ||
|
||
if (node.type === 'element') { | ||
return node.tagName === 'script' || !phrasing(node) || head; | ||
return node.tagName === 'script' || !phrasing(node) || head | ||
} | ||
|
||
return false; | ||
return false | ||
} | ||
|
||
function ignore(nodes) { | ||
var index = nodes.length; | ||
var index = nodes.length | ||
|
||
while (index--) { | ||
if (sensitive.indexOf(nodes[index].tagName) !== -1) { | ||
return true; | ||
return true | ||
} | ||
} | ||
|
||
return false; | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,46 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
var fs = require('fs'); | ||
var path = require('path'); | ||
var bail = require('bail'); | ||
var test = require('tape'); | ||
var rehype = require('rehype'); | ||
var vfile = require('to-vfile'); | ||
var negate = require('negate'); | ||
var hidden = require('is-hidden'); | ||
var fmt = require('..'); | ||
var fs = require('fs') | ||
var path = require('path') | ||
var bail = require('bail') | ||
var test = require('tape') | ||
var rehype = require('rehype') | ||
var vfile = require('to-vfile') | ||
var negate = require('negate') | ||
var hidden = require('is-hidden') | ||
var fmt = require('..') | ||
|
||
test('format', function (t) { | ||
var root = path.join(__dirname, 'fixtures'); | ||
test('format', function(t) { | ||
var root = path.join(__dirname, 'fixtures') | ||
|
||
fs.readdir(root, function (err, files) { | ||
bail(err); | ||
files = files.filter(negate(hidden)); | ||
t.plan(files.length); | ||
fs.readdir(root, function(err, files) { | ||
bail(err) | ||
files = files.filter(negate(hidden)) | ||
t.plan(files.length) | ||
|
||
files.forEach(one); | ||
}); | ||
files.forEach(one) | ||
}) | ||
|
||
function one(fixture) { | ||
var base = path.join(root, fixture); | ||
var input = vfile.readSync(path.join(base, 'input.html')); | ||
var output = vfile.readSync(path.join(base, 'output.html')); | ||
var config; | ||
var proc; | ||
var base = path.join(root, fixture) | ||
var input = vfile.readSync(path.join(base, 'input.html')) | ||
var output = vfile.readSync(path.join(base, 'output.html')) | ||
var config | ||
var proc | ||
|
||
try { | ||
config = JSON.parse(fs.readFileSync(path.join(base, 'config.json'))); | ||
config = JSON.parse(fs.readFileSync(path.join(base, 'config.json'))) | ||
} catch (err) {} | ||
|
||
proc = rehype().use(fmt, config); | ||
proc = rehype().use(fmt, config) | ||
|
||
proc.process(input, function (err) { | ||
t.test(fixture, function (st) { | ||
st.plan(3); | ||
st.ifErr(err, 'shouldn’t throw'); | ||
st.equal(input.messages.length, 0, 'shouldn’t warn'); | ||
st.equal(String(input), String(output), 'should match'); | ||
}); | ||
}); | ||
proc.process(input, function(err) { | ||
t.test(fixture, function(st) { | ||
st.plan(3) | ||
st.ifErr(err, 'shouldn’t throw') | ||
st.equal(input.messages.length, 0, 'shouldn’t warn') | ||
st.equal(String(input), String(output), 'should match') | ||
}) | ||
}) | ||
} | ||
}); | ||
}) |