Skip to content

Commit a390853

Browse files
committed
feat: add the ability to ignore by extension
1 parent f55259c commit a390853

4 files changed

Lines changed: 32 additions & 1 deletion

File tree

lib/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ module.exports = function (options) {
99
options.classIgnore = []
1010
}
1111

12+
if (!options.extensionIgnore) {
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 isIgnore =
26+
(options.classIgnore.filter(className => classes.includes(className)).length > 0) ||
27+
(options.extensionIgnore.filter(fileExtension => fileExtension === extension).length > 0)
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)