File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -59,7 +59,14 @@ Example: `image.jpg => image.webp (instead of image.jpg.webp)`
5959Type: ` Array<string> `
6060Default: ` [] `
6161Description: * 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
Original file line number Diff line number Diff 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' :
Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff 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+
4248function 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' )
You can’t perform that action at this time.
0 commit comments