Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 10, 2018
1 parent 72bc2b6 commit e9f44d1
Show file tree
Hide file tree
Showing 15 changed files with 390 additions and 333 deletions.
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage
rehype.js
rehype.min.js
18 changes: 13 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"lerna": "^2.0.0",
"mdast-zone": "^3.0.1",
"nyc": "^12.0.0",
"prettier": "^1.13.5",
"remark-cli": "^5.0.0",
"remark-preset-wooorm": "^4.0.0",
"tape": "^4.5.1",
Expand All @@ -32,23 +33,30 @@
},
"scripts": {
"postinstall": "lerna bootstrap",
"build-md": "remark . -qfo",
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
"build-bundle": "browserify packages/rehype -s rehype -p browser-pack-flat/plugin -p [ common-shakeify -v ] -p bundle-collapser/plugin -g [ babelify --presets [ \"babel-preset-env\" ] ] > rehype.js",
"build-mangle": "esmangle rehype.js > rehype.min.js",
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
"lint": "xo",
"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,
"ignores": [
"rehype.js"
Expand Down
19 changes: 7 additions & 12 deletions packages/rehype-cli/cli.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env node
'use strict';
'use strict'

var start = require('unified-args');
var processor = require('rehype');
var proc = require('rehype/package.json');
var cli = require('./package.json');
var start = require('unified-args')
var processor = require('rehype')
var proc = require('rehype/package.json')
var cli = require('./package.json')

start({
processor: processor,
Expand All @@ -18,10 +18,5 @@ start({
packageField: proc.name,
rcName: '.' + proc.name + 'rc',
ignoreName: '.' + proc.name + 'ignore',
extensions: [
'html',
'htm',
'xht',
'xhtml'
]
});
extensions: ['html', 'htm', 'xht', 'xhtml']
})
93 changes: 53 additions & 40 deletions packages/rehype-parse/index.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,93 @@
'use strict';
'use strict'

var fromParse5 = require('hast-util-from-parse5');
var Parser5 = require('parse5/lib/parser');
var xtend = require('xtend');
var errors = require('./errors.json');
var fromParse5 = require('hast-util-from-parse5')
var Parser5 = require('parse5/lib/parser')
var xtend = require('xtend')
var errors = require('./errors.json')

var base = 'https://html.spec.whatwg.org/multipage/parsing.html#parse-error-';
var base = 'https://html.spec.whatwg.org/multipage/parsing.html#parse-error-'

var fatalities = {2: true, 1: false, 0: null};
var fatalities = {2: true, 1: false, 0: null}

module.exports = parse;
module.exports = parse

function parse(options) {
var settings = xtend(options, this.data('settings'));
var position = typeof settings.position === 'boolean' ? settings.position : true;
var settings = xtend(options, this.data('settings'))
var position = settings.position

this.Parser = parser;
position = typeof position === 'boolean' ? position : true

this.Parser = parser

function parser(doc, file) {
var fn = settings.fragment ? 'parseFragment' : 'parse';
var onParseError = settings.emitParseErrors ? onerror : null;
var fn = settings.fragment ? 'parseFragment' : 'parse'
var onParseError = settings.emitParseErrors ? onerror : null
var parse5 = new Parser5({
sourceCodeLocationInfo: position,
onParseError: onParseError,
scriptingEnabled: false
});
})

return fromParse5(parse5[fn](doc), {
file: file,
verbose: settings.verbose
});
})

function onerror(err) {
var code = err.code;
var name = camelcase(code);
var setting = settings[name];
var config = setting === undefined || setting === null ? true : setting;
var level = typeof config === 'number' ? config : config ? 1 : 0;
var start = {line: err.startLine, column: err.startCol, offset: err.startOffset};
var end = {line: err.endLine, column: err.endCol, offset: err.endOffset};
var info;
var message;
var code = err.code
var name = camelcase(code)
var setting = settings[name]
var config = setting === undefined || setting === null ? true : setting
var level = typeof config === 'number' ? config : config ? 1 : 0
var start = {
line: err.startLine,
column: err.startCol,
offset: err.startOffset
}
var end = {line: err.endLine, column: err.endCol, offset: err.endOffset}
var info
var message

if (level) {
info = errors[name] || /* istanbul ignore next */ {reason: '', description: ''};
message = file.message(format(info.reason), {start: start, end: end});
message.source = 'parse-error';
message.ruleId = code;
message.fatal = fatalities[level];
message.note = format(info.description);
message.url = info.url === false ? null : base + code;
info = errors[name] || /* istanbul ignore next */ {
reason: '',
description: ''
}

message = file.message(format(info.reason), {start: start, end: end})
message.source = 'parse-error'
message.ruleId = code
message.fatal = fatalities[level]
message.note = format(info.description)
message.url = info.url === false ? null : base + code
}

function format(value) {
return value
.replace(/%c(?:-(\d+))?/g, char)
.replace(/%x/g, encodedChar);
return value.replace(/%c(?:-(\d+))?/g, char).replace(/%x/g, encodedChar)
}

function char($0, $1) {
var offset = $1 ? -parseInt($1, 10) : 0;
var char = doc.charAt(err.startOffset + offset);
return char === '`' ? '` ` `' : char;
var offset = $1 ? -parseInt($1, 10) : 0
var char = doc.charAt(err.startOffset + offset)
return char === '`' ? '` ` `' : char
}

function encodedChar() {
return '0x' + doc.charCodeAt(err.startOffset).toString(16).toUpperCase();
var char = doc
.charCodeAt(err.startOffset)
.toString(16)
.toUpperCase()

return '0x' + char
}
}
}
}

function camelcase(value) {
return value.replace(/-[a-z]/g, replacer);
return value.replace(/-[a-z]/g, replacer)
}

function replacer($0) {
return $0.charAt(1).toUpperCase();
return $0.charAt(1).toUpperCase()
}
18 changes: 10 additions & 8 deletions packages/rehype-parse/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ npm install rehype-parse
## Usage

```js
var unified = require('unified');
var createStream = require('unified-stream');
var parse = require('rehype-parse');
var stringify = require('rehype-stringify');

process.stdin
.pipe(createStream(unified().use(parse).use(stringify)))
.pipe(process.stdout);
var unified = require('unified')
var createStream = require('unified-stream')
var parse = require('rehype-parse')
var stringify = require('rehype-stringify')

var processor = unified()
.use(parse)
.use(stringify)

process.stdin.pipe(createStream(processor)).pipe(process.stdout)
```

## API
Expand Down
14 changes: 7 additions & 7 deletions packages/rehype-stringify/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
'use strict';
'use strict'

var xtend = require('xtend');
var toHTML = require('hast-util-to-html');
var xtend = require('xtend')
var toHTML = require('hast-util-to-html')

module.exports = stringify;
module.exports = stringify

function stringify(config) {
var settings = xtend(config, this.data('settings'));
var settings = xtend(config, this.data('settings'))

this.Compiler = compiler;
this.Compiler = compiler

function compiler(tree) {
return toHTML(tree, settings);
return toHTML(tree, settings)
}
}
12 changes: 5 additions & 7 deletions packages/rehype-stringify/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ npm install rehype-stringify
## Usage

```js
var unified = require('unified');
var createStream = require('unified-stream');
var parse = require('rehype-parse');
var stringify = require('rehype-stringify');
var unified = require('unified')
var createStream = require('unified-stream')
var parse = require('rehype-parse')
var stringify = require('rehype-stringify')

var processor = unified()
.use(parse)
Expand All @@ -29,9 +29,7 @@ var processor = unified()
entities: {useShortestReferences: true}
})

process.stdin
.pipe(createStream(processor))
.pipe(process.stdout);
process.stdin.pipe(createStream(processor)).pipe(process.stdout)
```

## API
Expand Down
10 changes: 5 additions & 5 deletions packages/rehype/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';
'use strict'

var unified = require('unified');
var parse = require('rehype-parse');
var stringify = require('rehype-stringify');
var unified = require('unified')
var parse = require('rehype-parse')
var stringify = require('rehype-stringify')

module.exports = unified()
.use(parse)
.use(stringify)
.freeze();
.freeze()
12 changes: 6 additions & 6 deletions packages/rehype/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ npm install rehype
## Usage

```js
var rehype = require('rehype');
var report = require('vfile-reporter');
var rehype = require('rehype')
var report = require('vfile-reporter')

rehype().process('<title>Hi</title><h2>Hello world!', function (err, file) {
console.log(report(err || file));
console.log(String(file));
});
rehype().process('<title>Hi</title><h2>Hello world!', function(err, file) {
console.log(report(err || file))
console.log(String(file))
})
```

Yields:
Expand Down
43 changes: 20 additions & 23 deletions script/parse-error.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,40 @@
'use strict';
'use strict'

var u = require('unist-builder');
var zone = require('mdast-zone');
var errors = require('../packages/rehype-parse/errors.json');
var u = require('unist-builder')
var zone = require('mdast-zone')
var errors = require('../packages/rehype-parse/errors.json')

var base = 'https://github.com/' + require('../package.json').repository + '/blob/master';
var repo = require('../package.json').repository

var whatwg = 'https://html.spec.whatwg.org/multipage/parsing.html#parse-error-';
var whatwg = 'https://html.spec.whatwg.org/multipage/parsing.html#parse-error-'
var base = 'https://github.com/' + repo + '/blob/master'

var ignoreFixture = {
surrogateInInputStream: true
};
}

module.exports = parseErrors;
module.exports = parseErrors

function parseErrors() {
return transform;
return transform
}

function transform(tree) {
zone(tree, 'parse-error', visit);
zone(tree, 'parse-error', visit)
}

function visit(start, nodes, end) {
return [
start,
u('list', {ordered: false}, Object.keys(errors).map(map)),
end
];
return [start, u('list', {ordered: false}, Object.keys(errors).map(map)), end]

function map(name) {
var info = errors[name];
var kebab = name.replace(/[A-Z]/g, replacer);
var reason = info.reason.charAt(0).toLowerCase() + info.reason.slice(1);
var head = u('inlineCode', name);
var info = errors[name]
var kebab = name.replace(/[A-Z]/g, replacer)
var reason = info.reason.charAt(0).toLowerCase() + info.reason.slice(1)
var head = u('inlineCode', name)
var fields = [
info.url === false ? head : u('link', {url: whatwg + kebab}, [head]),
u('text', ' — ' + reason)
];
]

if (!ignoreFixture[name]) {
fields.push(
Expand All @@ -46,13 +43,13 @@ function visit(start, nodes, end) {
u('text', 'example')
]),
u('text', ')')
);
)
}

return u('listItem', [u('paragraph', fields)]);
return u('listItem', [u('paragraph', fields)])
}

function replacer($0) {
return '-' + $0.toLowerCase();
return '-' + $0.toLowerCase()
}
}
Loading

0 comments on commit e9f44d1

Please sign in to comment.