-
-
Notifications
You must be signed in to change notification settings - Fork 3
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
5 changed files
with
77 additions
and
63 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-picture.js | ||
rehype-picture.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,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 | ||
} | ||
} |
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
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,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() | ||
}) |