Skip to content

Commit 72a2cb6

Browse files
authored
Merge pull request #10 from ahmadalfy/feature/ignore-extension
Feature/ignore extension
2 parents f55259c + c232f5c commit 72a2cb6

5 files changed

Lines changed: 41 additions & 3 deletions

File tree

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,14 @@ Example: `image.jpg => image.webp (instead of image.jpg.webp)`
5959
Type: `Array<string>`
6060
Default: `[]`
6161
Description: *list of classes for which the transformation will be ignored*
62-
Example: `image.jpg => image.webp (instead of image.jpg.webp)`
62+
Example: `classIgnore: ['ignore-webp']` will ignore transformation for images with the class `ignore-web`
63+
64+
#### `extensionIgnore`
65+
66+
Type: `Array<string>`
67+
Default: `[]`
68+
Description: *list of extension for which the transformation will be ignored*
69+
Example: `extensionIgnore: ['svg']` will ignore transformation for images with the `svg` extension
6370

6471
### License [MIT](LICENSE)
6572

lib/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ module.exports = function (options) {
55
options = {}
66
}
77

8-
if (!options.classIgnore) {
8+
if (options.classIgnore === undefined) {
99
options.classIgnore = []
1010
}
1111

12+
if (options.extensionIgnore === undefined) {
13+
options.extensionIgnore = []
14+
}
15+
1216
if (options.replaceExtension === undefined) {
1317
options.replaceExtension = false
1418
}
@@ -17,7 +21,10 @@ module.exports = function (options) {
1721
tree.match([{ tag: 'img' }, { tag: 'amp-img' }], function (imgNode) {
1822
if (imgNode.skip) return imgNode
1923
var classes = imgNode.attrs && imgNode.attrs.class && (imgNode.attrs.class.split(' ') || [])
20-
var isIgnore = options.classIgnore.filter(className => classes.includes(className)).length > 0
24+
var extension = imgNode.attrs.src.split('.').pop()
25+
var isIgnoredByClass = options.classIgnore.filter(className => classes.includes(className)).length > 0
26+
var isIgnoredByExtension = options.extensionIgnore.filter(fileExtension => fileExtension === extension).length > 0
27+
var isIgnore = isIgnoredByClass || isIgnoredByExtension
2128
if (isIgnore) return imgNode
2229
switch (imgNode.tag) {
2330
case 'amp-img':
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!doctype html>
2+
<html>
3+
<body>
4+
<img src="photo.svg">
5+
<img src="photo.gif">
6+
<picture><source type="image/webp" srcset="photo.jpg.webp"><img src="photo.jpg"></picture>
7+
<picture><source type="image/webp" srcset="photo.png.webp"><img src="photo.png"></picture>
8+
</body>
9+
</html>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!doctype html>
2+
<html>
3+
<body>
4+
<img src="photo.svg">
5+
<img src="photo.gif">
6+
<img src="photo.jpg">
7+
<img src="photo.png">
8+
</body>
9+
</html>

test/test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ test('Class ignore', (t) => {
3939
})
4040
})
4141

42+
test('Extension ignore', (t) => {
43+
return compare(t, 'ignore-extension', {
44+
extensionIgnore: ['gif', 'svg']
45+
})
46+
})
47+
4248
function compare (t, name, options) {
4349
const html = readFileSync(path.join(fixtures, `${name}.html`), 'utf8')
4450
const expected = readFileSync(path.join(fixtures, `${name}.expected.html`), 'utf8')

0 commit comments

Comments
 (0)