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 5e59f19 commit 06c4b89
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 63 deletions.
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage/
rehype-picture.js
rehype-picture.min.js
51 changes: 25 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,53 @@
'use strict';
'use strict'

var path = require('path');
var visit = require('unist-util-visit');
var is = require('hast-util-is-element');
var replaceExt = require('replace-ext');
var h = require('hastscript');
var path = require('path')
var visit = require('unist-util-visit')
var is = require('hast-util-is-element')
var replaceExt = require('replace-ext')
var h = require('hastscript')

module.exports = picture;
module.exports = picture

var own = {}.hasOwnProperty;
var own = {}.hasOwnProperty

function picture(options) {
var settings = options || {};
var settings = options || {}

return transformer;
return transformer

function transformer(tree) {
visit(tree, 'element', visitor);
visit(tree, 'element', visitor)
}

function visitor(node, index, parent) {
var src = node.properties.src;
var extension;
var map;
var src = node.properties.src
var extension
var map

if (!parent || !is(node, 'img') || !src) {
return;
return
}

extension = path.extname(src).slice(1);
extension = path.extname(src).slice(1)

if (!own.call(settings, extension)) {
return;
return
}

map = settings[extension];
parent.children[index] = h('picture', sources(src, map).concat(node));
map = settings[extension]
parent.children[index] = h('picture', sources(src, map).concat(node))
}

function sources(src, map) {
var nodes = [];
var key;
var nodes = []
var key

for (key in map) {
nodes.push(h('source', {
srcSet: replaceExt(src, '.' + key),
type: map[key]
}));
nodes.push(
h('source', {srcSet: replaceExt(src, '.' + key), type: map[key]})
)
}

return nodes;
return nodes
}
}
20 changes: 14 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"browserify": "^16.0.0",
"esmangle": "^1.0.1",
"nyc": "^11.0.0",
"prettier": "^1.13.7",
"rehype-parse": "^5.0.0",
"rehype-stringify": "^4.0.0",
"remark-cli": "^5.0.0",
Expand All @@ -38,23 +39,30 @@
"xo": "^0.21.0"
},
"scripts": {
"build-md": "remark . -qfo",
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
"build-bundle": "browserify index.js --bare -s rehypePicture > rehype-picture.js",
"build-mangle": "esmangle rehype-picture.js > rehype-picture.min.js",
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
"lint": "xo",
"test-api": "node test.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,
"rules": {
"guard-for-in": "off"
Expand Down
19 changes: 10 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ npm install rehype-picture
## Usage

```js
var unified = require('unified');
var parse = require('rehype-parse');
var stringify = require('rehype-stringify');
var picture = require('rehype-picture');
var unified = require('unified')
var report = require('vfile-reporter')
var parse = require('rehype-parse')
var stringify = require('rehype-stringify')
var picture = require('rehype-picture')

unified()
.use(parse, {fragment: true})
Expand All @@ -25,16 +26,16 @@ unified()
png: {svg: 'image/svg+xml'}
})
.use(stringify)
.process('<img src="cat.jpg">\n<img src="logo.png">', function (err, file) {
console.error(err);
console.log(String(file));
});
.process('<img src="cat.jpg">\n<img src="logo.png">', function(err, file) {
console.error(report(err || file))
console.log(String(file))
})
```

Yields:

```html
null
no issues found
<picture><source srcset="cat.webp" type="image/webp"><img src="cat.jpg"></picture>
<picture><source srcset="logo.svg" type="image/svg+xml"><img src="logo.png"></picture>
```
Expand Down
47 changes: 25 additions & 22 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,62 @@
'use strict';
'use strict'

var test = require('tape');
var unified = require('unified');
var parse = require('rehype-parse');
var stringify = require('rehype-stringify');
var picture = require('.');
var test = require('tape')
var unified = require('unified')
var parse = require('rehype-parse')
var stringify = require('rehype-stringify')
var picture = require('.')

test('rehype-picture', function (t) {
test('rehype-picture', function(t) {
unified()
.use(parse, {fragment: true})
.use(picture)
.use(stringify)
.process('<img src="cat.png">', function (err, file) {
.process('<img src="cat.png">', function(err, file) {
t.deepEqual(
[err, String(file)],
[null, '<img src="cat.png">'],
'should ignore non-matching images'
);
});
)
})

unified()
.use(parse, {fragment: true})
.use(picture, {})
.use(stringify)
.process('<img>', function (err, file) {
.process('<img>', function(err, file) {
t.deepEqual(
[err, String(file)],
[null, '<img>'],
'should ignore images without src'
);
});
)
})

unified()
.use(parse, {fragment: true})
.use(picture, {jpg: {}})
.use(stringify)
.process('<img src="cat.jpg">', function (err, file) {
.process('<img src="cat.jpg">', function(err, file) {
t.deepEqual(
[err, String(file)],
[null, '<picture><img src="cat.jpg"></picture>'],
'should work without replacement map'
);
});
)
})

unified()
.use(parse, {fragment: true})
.use(picture, {jpg: {webp: 'image/webp'}})
.use(stringify)
.process('<img src="cat.jpg">', function (err, file) {
.process('<img src="cat.jpg">', function(err, file) {
t.deepEqual(
[err, String(file)],
[null, '<picture><source srcset="cat.webp" type="image/webp"><img src="cat.jpg"></picture>'],
[
null,
'<picture><source srcset="cat.webp" type="image/webp"><img src="cat.jpg"></picture>'
],
'should add sources'
);
});
)
})

t.end();
});
t.end()
})

0 comments on commit 06c4b89

Please sign in to comment.