Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jul 18, 2018
1 parent fa51e07 commit 8df91f4
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 93 deletions.
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage/
rehype-format.js
rehype-format.min.js
109 changes: 56 additions & 53 deletions index.js
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
}
23 changes: 17 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"is-hidden": "^1.0.1",
"negate": "^1.0.0",
"nyc": "^12.0.0",
"prettier": "^1.13.7",
"rehype": "^6.0.0",
"remark-cli": "^5.0.0",
"remark-preset-wooorm": "^4.0.0",
Expand All @@ -41,24 +42,34 @@
"xo": "^0.21.0"
},
"scripts": {
"build-md": "remark . -qfo",
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
"build-bundle": "browserify index.js --bare -s rehypeFormat > rehype-format.js",
"build-mangle": "esmangle rehype-format.js > rehype-format.min.js",
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
"lint": "xo",
"test-api": "node test/index.js",
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test/index.js",
"test": "npm run build && npm run lint && npm run test-coverage"
"test": "npm run format && npm run build && npm run test-coverage"
},
"nyc": {
"check-coverage": true,
"lines": 100,
"functions": 100,
"branches": 100
},
"prettier": {
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"bracketSpacing": false,
"semi": false,
"trailingComma": "none"
},
"xo": {
"space": true,
"prettier": true,
"esnext": false,
"rules": {
"complexity": "off"
},
"ignores": [
"rehype-format.js"
]
Expand Down
68 changes: 34 additions & 34 deletions test/index.js
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')
})
})
}
});
})

0 comments on commit 8df91f4

Please sign in to comment.