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 19, 2018
1 parent 481bada commit 99ffd36
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 50 deletions.
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage/
rehype-sanitize.js
rehype-sanitize.min.js
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict';
'use strict'

var clean = require('hast-util-sanitize');
var clean = require('hast-util-sanitize')

module.exports = sanitize;
module.exports = sanitize

function sanitize(options) {
return transformer;
return transformer
function transformer(tree) {
return clean(tree, options);
return clean(tree, options)
}
}
20 changes: 14 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,38 @@
"deepmerge": "^2.0.0",
"esmangle": "^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",
"tape": "^4.0.0",
"xo": "^0.21.0"
},
"scripts": {
"build-md": "remark . -qfo",
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
"build-bundle": "browserify index.js --bare -s rehypeSanitize > rehype-sanitize.js",
"build-mangle": "esmangle rehype-sanitize.js > rehype-sanitize.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.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-sanitize.js"
Expand Down
20 changes: 10 additions & 10 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@ require('child_process').spawn('rm', ['-r', '-f', process.env.HOME]);
And our script, `example.js`, looks as follows:

```javascript
var fs = require('fs');
var rehype = require('rehype');
var merge = require('deepmerge');
var gh = require('hast-util-sanitize/lib/github');
var sanitize = require('rehype-sanitize');
var fs = require('fs')
var rehype = require('rehype')
var merge = require('deepmerge')
var gh = require('hast-util-sanitize/lib/github')
var sanitize = require('rehype-sanitize')

var schema = merge(gh, {tagNames: ['math', 'mi']});
var schema = merge(gh, {tagNames: ['math', 'mi']})

rehype()
.data('settings', {fragment: true})
.use(sanitize, schema)
.process(fs.readFileSync('index.html'), function (err, file) {
if (err) throw err;
console.log(String(file));
});
.process(fs.readFileSync('index.html'), function(err, file) {
if (err) throw err
console.log(String(file))
})
```

Now, running `node example` yields:
Expand Down
59 changes: 30 additions & 29 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
'use strict';
'use strict'

var test = require('tape');
var rehype = require('rehype');
var merge = require('deepmerge');
var gh = require('hast-util-sanitize/lib/github');
var sanitize = require('.');
var test = require('tape')
var rehype = require('rehype')
var merge = require('deepmerge')
var gh = require('hast-util-sanitize/lib/github')
var sanitize = require('.')

test('sanitize', function (t) {
t.plan(2);
test('sanitize', function(t) {
t.plan(2)

t.test('should work', function (st) {
var input = '<img onmouseover="alert(\'alpha\')">';
var output = '<img>';
t.test('should work', function(st) {
var input = '<img onmouseover="alert(\'alpha\')">'
var output = '<img>'

st.plan(3);
st.plan(3)

rehype()
.use(sanitize)
.process(input, function (err, file) {
st.ifErr(err, 'shouldn’t fail');
st.equal(file.messages.length, 0, 'shouldn’t warn');
st.equal(String(file), String(output), 'should match');
});
});
.process(input, function(err, file) {
st.ifErr(err, 'shouldn’t fail')
st.equal(file.messages.length, 0, 'shouldn’t warn')
st.equal(String(file), String(output), 'should match')
})
})

t.test('options', function (st) {
var input = '<math><mi xlink:href="data:x,<script>alert(\'echo\')</script>"></mi></math>';
var output = '<math><mi></mi></math>';
t.test('options', function(st) {
var input =
'<math><mi xlink:href="data:x,<script>alert(\'echo\')</script>"></mi></math>'
var output = '<math><mi></mi></math>'

st.plan(3);
st.plan(3)

rehype()
.use(sanitize, merge(gh, {tagNames: ['math', 'mi']}))
.process(input, function (err, file) {
st.ifErr(err, 'shouldn’t fail');
st.equal(file.messages.length, 0, 'shouldn’t warn');
st.equal(String(file), String(output), 'should match');
});
});
});
.process(input, function(err, file) {
st.ifErr(err, 'shouldn’t fail')
st.equal(file.messages.length, 0, 'shouldn’t warn')
st.equal(String(file), String(output), 'should match')
})
})
})

0 comments on commit 99ffd36

Please sign in to comment.