File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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+
3263function getPicture ( imgNode , options ) {
3364 imgNode . skip = true
3465
Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 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 >
You can’t perform that action at this time.
0 commit comments