Skip to content

Commit a0cf52a

Browse files
authored
Merge pull request #3 from kevincolten/amp
[FEATURE] Google AMP amp-img support
2 parents 5d3901f + 37fec39 commit a0cf52a

6 files changed

Lines changed: 49 additions & 0 deletions

File tree

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@ After:
2222
</picture>
2323
```
2424

25+
Also supports [`<amp-img>`](https://amp.dev/documentation/components/amp-img/)
26+
27+
Before:
28+
```html
29+
<amp-img alt="photo" width="550" height="368" layout="responsive" src="photo.png"></amp-img>
30+
```
31+
32+
After:
33+
```html
34+
<amp-img alt="photo" width="550" height="368" layout="responsive" src="photo.png.webp">
35+
<amp-img alt="photo" width="550" height="368" layout="responsive" src="photo.png" fallback=""></amp-img>
36+
</amp-img>
37+
```
38+
2539
## Install
2640

2741
> npm i posthtml posthtml-webp

lib/index.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ module.exports = function (options) {
1515
return getPicture(imgNode, options)
1616
})
1717

18+
tree.match({ tag: 'amp-img' }, function (imgNode) {
19+
if (imgNode.skip) return imgNode
20+
return getAmpPicture(imgNode, options)
21+
})
22+
1823
return tree
1924
}
2025
}
@@ -29,6 +34,32 @@ function removeExtension (filename) {
2934
}
3035
}
3136

37+
function getAmpPicture (imgNode, options) {
38+
imgNode.skip = true
39+
40+
var src = imgNode.attrs.src
41+
if (options.replaceExtension) {
42+
src = removeExtension(src)
43+
}
44+
src += '.webp'
45+
return {
46+
tag: 'amp-img',
47+
attrs: {
48+
...imgNode.attrs,
49+
src
50+
},
51+
content: [
52+
{
53+
...imgNode,
54+
attrs: {
55+
...imgNode.attrs,
56+
fallback: ''
57+
}
58+
}
59+
]
60+
}
61+
}
62+
3263
function getPicture (imgNode, options) {
3364
imgNode.skip = true
3465

test/fixtures/extension.expected.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
<html>
33
<body>
44
<picture><source type="image/webp" srcset="photo.webp"><img src="photo.png"></picture>
5+
<amp-img alt="photo" width="550" height="368" layout="responsive" src="photo.webp"><amp-img alt="photo" width="550" height="368" layout="responsive" src="photo.png" fallback=""></amp-img></amp-img>
56
</body>
67
</html>

test/fixtures/extension.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
<html>
33
<body>
44
<img src="photo.png">
5+
<amp-img alt="photo" width="550" height="368" layout="responsive" src="photo.png"></amp-img>
56
</body>
67
</html>

test/fixtures/no-extension.expected.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
<html>
33
<body>
44
<picture><source type="image/webp" srcset="photo.png.webp"><img src="photo.png"></picture>
5+
<amp-img alt="photo" width="550" height="368" layout="responsive" src="photo.png.webp"><amp-img alt="photo" width="550" height="368" layout="responsive" src="photo.png" fallback=""></amp-img></amp-img>
56
</body>
67
</html>

test/fixtures/no-extension.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
<html>
33
<body>
44
<img src="photo.png">
5+
<amp-img alt="photo" width="550" height="368" layout="responsive" src="photo.png"></amp-img>
56
</body>
67
</html>

0 commit comments

Comments
 (0)