Skip to content

Commit 58e2a99

Browse files
authored
Merge pull request #6 from posthtml/milestone-1.3.0
Milestone 1.3.0
2 parents c586153 + b7c29c6 commit 58e2a99

8 files changed

Lines changed: 71 additions & 43 deletions

File tree

README.md

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,31 @@
77
[![Standard Code Style][style]][style-url]
88
[![Chat][chat]][chat-badge]
99

10-
This plugin add webp supporting in your html.
10+
This plugin add webp supporting in your html. Also supports [`<amp-img>`](https://amp.dev/documentation/components/amp-img/)
11+
12+
## Install
13+
```bash
14+
$ npm i posthtml posthtml-webp
15+
```
16+
17+
## Usage
18+
19+
``` js
20+
const fs = require('fs');
21+
const posthtml = require('posthtml');
22+
const posthtmlWebp = require('posthtml-webp');
23+
24+
posthtml()
25+
.use(posthtmlWebp(/* Plugin options */))
26+
.process(html/*, options */)
27+
.then(result => fs.writeFileSync('./after.html', result.html));
28+
```
29+
## Example
1130

1231
Before:
1332
``` html
1433
<img src="image.jpg">
34+
<amp-img alt="photo" width="550" height="368" layout="responsive" src="photo.png"></amp-img>
1535
```
1636

1737
After:
@@ -20,47 +40,19 @@ After:
2040
<source type="image/webp" srcset="image.jpg.webp">
2141
<img src="image.jpg">
2242
</picture>
23-
```
24-
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
3443
<amp-img alt="photo" width="550" height="368" layout="responsive" src="photo.png.webp">
3544
<amp-img alt="photo" width="550" height="368" layout="responsive" src="photo.png" fallback=""></amp-img>
3645
</amp-img>
3746
```
3847

39-
## Install
40-
41-
> npm i posthtml posthtml-webp
48+
## Options
4249

43-
## Plugin options
44-
`replaceExtension` (boolean)
45-
46-
**Default:** false
47-
48-
Replace the extension of the source image with .webp instead of appending .webp to the original filename
49-
50-
**Example**: image.jpg => image.webp (instead of image.jpg.webp)
51-
52-
## Usage
50+
#### `replaceExtension`
5351

54-
``` js
55-
const fs = require('fs');
56-
const posthtml = require('posthtml');
57-
const posthtmlWebp = require('posthtml-webp');
58-
59-
posthtml()
60-
.use(posthtmlWebp(/* Plugin options */))
61-
.process(html/*, options */)
62-
.then(result => fs.writeFileSync('./after.html', result.html));
63-
```
52+
Type: `boolean`
53+
Default: `false`
54+
Description: *Replace the extension of the source image with .webp instead of appending .webp to the original filename*
55+
Example: `image.jpg => image.webp (instead of image.jpg.webp)`
6456

6557
### License [MIT](LICENSE)
6658

changelog.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1+
## 1.3.0 (2019-09-26)
2+
3+
* feat: multi srcset, close #5 ([b31efc6](https://github.com/posthtml/posthtml-webp/commit/b31efc6)), closes [#5](https://github.com/posthtml/posthtml-webp/issues/5)
4+
* docs: simple update ([cc7079e](https://github.com/posthtml/posthtml-webp/commit/cc7079e))
5+
* test: for issue #5 ([9b63e67](https://github.com/posthtml/posthtml-webp/commit/9b63e67)), closes [#5](https://github.com/posthtml/posthtml-webp/issues/5)
6+
7+
8+
19
## 1.2.0 (2019-09-20)
210

11+
* build: bump to 1.2.0 ([e58bd0e](https://github.com/posthtml/posthtml-webp/commit/e58bd0e))
12+
* build: update changelog ([eee9697](https://github.com/posthtml/posthtml-webp/commit/eee9697))
313
* build: update depDev ([d3543fe](https://github.com/posthtml/posthtml-webp/commit/d3543fe))
414
* docs: fix typo cover ([d1c6dce](https://github.com/posthtml/posthtml-webp/commit/d1c6dce))
515
* docs: update badges ([5d3901f](https://github.com/posthtml/posthtml-webp/commit/5d3901f))

lib/index.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,18 @@ function getPicture (imgNode, options) {
6464
imgNode.skip = true
6565

6666
var src = imgNode.attrs.src
67-
if (options.replaceExtension) {
68-
src = removeExtension(src)
69-
}
70-
src += '.webp'
67+
var srcset = (imgNode.attrs.srcset || '')
68+
.split(',')
69+
.concat(src)
70+
.filter(Boolean)
71+
.map(value => {
72+
value = value.trim().split(/\s/)
73+
var path = options.replaceExtension ? removeExtension(value[0]) : value[0]
74+
var size = value[1]
75+
76+
return [path + '.webp', size].filter(Boolean).join(' ')
77+
})
78+
.join(', ')
7179

7280
return {
7381
tag: 'picture',
@@ -76,7 +84,7 @@ function getPicture (imgNode, options) {
7684
tag: 'source',
7785
attrs: {
7886
type: 'image/webp',
79-
srcset: src
87+
srcset
8088
}
8189
},
8290
imgNode

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "posthtml-webp",
33
"description": "Add webp supporting in your html",
4-
"version": "1.2.0",
4+
"version": "1.3.0",
55
"author": "seokirill",
66
"ava": {
77
"verbose": "true"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!doctype html>
2+
<html>
3+
<body>
4+
<picture><source type="image/webp" srcset="photo-lg.webp 1000w, photo-md.webp 500w, photo-sm.webp 250w, photo.webp"><img srcset="photo-lg.jpg 1000w, photo-md.jpg 500w, photo-sm.jpg 250w" src="photo.png"></picture>
5+
</body>
6+
</html>

test/fixtures/multisrcset.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!doctype html>
2+
<html>
3+
<body>
4+
<img srcset="photo-lg.jpg 1000w, photo-md.jpg 500w, photo-sm.jpg 250w" src="photo.png">
5+
</body>
6+
</html>

test/test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,17 @@ test('Default behaviour', (t) => {
2727
return compare(t, 'no-extension')
2828
})
2929

30+
test('Multi srcset', (t) => {
31+
return compare(t, 'multisrcset', {
32+
replaceExtension: true
33+
})
34+
})
35+
3036
function compare (t, name, options) {
3137
const html = readFileSync(path.join(fixtures, `${name}.html`), 'utf8')
3238
const expected = readFileSync(path.join(fixtures, `${name}.expected.html`), 'utf8')
3339

3440
return posthtml([plugin(options)])
3541
.process(html)
36-
.then((res) => t.truthy(res.html === expected))
42+
.then((res) => t.deepEqual(res.html, expected))
3743
}

0 commit comments

Comments
 (0)