|
1 | 1 | 'use strict' |
2 | 2 |
|
3 | 3 | module.exports = function (options) { |
4 | | - if (!options) { |
5 | | - options = {} |
6 | | - } |
7 | | - |
8 | | - if (options.classIgnore === undefined) { |
9 | | - options.classIgnore = [] |
10 | | - } |
11 | | - |
12 | | - if (options.extensionIgnore === undefined) { |
13 | | - options.extensionIgnore = [] |
14 | | - } |
15 | | - |
16 | | - if (options.replaceExtension === undefined) { |
17 | | - options.replaceExtension = false |
18 | | - } |
19 | | - |
20 | | - if (options.lazySrc === undefined) { |
21 | | - options.lazySrc = 'data-src' |
22 | | - } |
23 | | - |
24 | | - if (options.lazySrcset === undefined) { |
25 | | - options.lazySrcset = 'data-srcset' |
26 | | - } |
27 | | - |
28 | | - return function posthtmlWebp (tree) { |
29 | | - tree.match([{ tag: 'img' }, { tag: 'amp-img' }], function (imgNode) { |
30 | | - if (imgNode.skip) return imgNode |
31 | | - var classes = (imgNode.attrs && imgNode.attrs.class && imgNode.attrs.class.split(' ')) || [] |
32 | | - // Extract extension from lazy loading attribute, because it always contains the right image. (`src` can contain "preview") |
33 | | - // Use `src` if there are no lazy loading attributes. |
34 | | - var extension = (imgNode.attrs[options.lazySrc] || imgNode.attrs[options.lazySrcset] || imgNode.attrs.src || imgNode.attrs.srcset).split('.').pop().split(/\s+/)[0] |
35 | | - var isIgnoredByClass = options.classIgnore.filter(className => classes.includes(className)).length > 0 |
36 | | - var isIgnoredByExtension = options.extensionIgnore.filter(fileExtension => fileExtension === extension).length > 0 |
37 | | - var isIgnore = isIgnoredByClass || isIgnoredByExtension |
38 | | - if (isIgnore) return imgNode |
39 | | - switch (imgNode.tag) { |
40 | | - case 'amp-img': |
41 | | - return getAmpPicture(imgNode, options) |
42 | | - default: |
43 | | - return getPicture(imgNode, options) |
44 | | - } |
45 | | - }) |
46 | | - |
47 | | - return tree |
48 | | - } |
| 4 | + if (!options) { |
| 5 | + options = {} |
| 6 | + } |
| 7 | + |
| 8 | + if (options.classIgnore === undefined) { |
| 9 | + options.classIgnore = [] |
| 10 | + } |
| 11 | + |
| 12 | + if (options.extensionIgnore === undefined) { |
| 13 | + options.extensionIgnore = [] |
| 14 | + } |
| 15 | + |
| 16 | + if (options.replaceExtension === undefined) { |
| 17 | + options.replaceExtension = false |
| 18 | + } |
| 19 | + |
| 20 | + if (options.lazySrc === undefined) { |
| 21 | + options.lazySrc = 'data-src' |
| 22 | + } |
| 23 | + |
| 24 | + if (options.lazySrcset === undefined) { |
| 25 | + options.lazySrcset = 'data-srcset' |
| 26 | + } |
| 27 | + |
| 28 | + if (options.extension === undefined) { |
| 29 | + options.extension = '.webp' |
| 30 | + } |
| 31 | + |
| 32 | + return function posthtmlWebp(tree) { |
| 33 | + tree.match([{ tag: 'img' }, { tag: 'amp-img' }], function (imgNode) { |
| 34 | + if (imgNode.skip) return imgNode |
| 35 | + var classes = (imgNode.attrs && imgNode.attrs.class && imgNode.attrs.class.split(' ')) || [] |
| 36 | + // Extract extension from lazy loading attribute, because it always contains the right image. (`src` can contain "preview") |
| 37 | + // Use `src` if there are no lazy loading attributes. |
| 38 | + var extension = (imgNode.attrs[options.lazySrc] || imgNode.attrs[options.lazySrcset] || imgNode.attrs.src || imgNode.attrs.srcset).split('.').pop().split(/\s+/)[0] |
| 39 | + var isIgnoredByClass = options.classIgnore.filter(className => classes.includes(className)).length > 0 |
| 40 | + var isIgnoredByExtension = options.extensionIgnore.filter(fileExtension => fileExtension === extension).length > 0 |
| 41 | + var isIgnore = isIgnoredByClass || isIgnoredByExtension |
| 42 | + if (isIgnore) return imgNode |
| 43 | + switch (imgNode.tag) { |
| 44 | + case 'amp-img': |
| 45 | + return getAmpPicture(imgNode, options) |
| 46 | + default: |
| 47 | + return getPicture(imgNode, options) |
| 48 | + } |
| 49 | + }) |
| 50 | + |
| 51 | + return tree |
| 52 | + } |
49 | 53 | } |
50 | 54 |
|
51 | | -function removeExtension (filename) { |
52 | | - var extIndex = filename.lastIndexOf('.') |
53 | | - if (extIndex === -1) { |
54 | | - // Filename has no extension |
55 | | - return filename |
56 | | - } else { |
57 | | - return filename.substring(0, extIndex) |
58 | | - } |
| 55 | +function removeExtension(filename) { |
| 56 | + var extIndex = filename.lastIndexOf('.') |
| 57 | + if (extIndex === -1) { |
| 58 | + // Filename has no extension |
| 59 | + return filename |
| 60 | + } else { |
| 61 | + return filename.substring(0, extIndex) |
| 62 | + } |
59 | 63 | } |
60 | 64 |
|
61 | | -function getAmpPicture (imgNode, options) { |
62 | | - imgNode.skip = true |
63 | | - |
64 | | - var src = imgNode.attrs.src |
65 | | - if (options.replaceExtension) { |
66 | | - src = removeExtension(src) |
67 | | - } |
68 | | - src += '.webp' |
69 | | - return { |
70 | | - tag: 'amp-img', |
71 | | - attrs: { |
72 | | - ...imgNode.attrs, |
73 | | - src |
74 | | - }, |
75 | | - content: [ |
76 | | - { |
77 | | - ...imgNode, |
| 65 | +function getAmpPicture(imgNode, options) { |
| 66 | + imgNode.skip = true |
| 67 | + |
| 68 | + var src = imgNode.attrs.src |
| 69 | + if (options.replaceExtension) { |
| 70 | + src = removeExtension(src) |
| 71 | + } |
| 72 | + src += options.extension |
| 73 | + return { |
| 74 | + tag: 'amp-img', |
78 | 75 | attrs: { |
79 | | - ...imgNode.attrs, |
80 | | - fallback: '' |
81 | | - } |
82 | | - } |
83 | | - ] |
84 | | - } |
| 76 | + ...imgNode.attrs, |
| 77 | + src |
| 78 | + }, |
| 79 | + content: [ |
| 80 | + { |
| 81 | + ...imgNode, |
| 82 | + attrs: { |
| 83 | + ...imgNode.attrs, |
| 84 | + fallback: '' |
| 85 | + } |
| 86 | + } |
| 87 | + ] |
| 88 | + } |
85 | 89 | } |
86 | 90 |
|
87 | | -function getPicture (imgNode, options) { |
88 | | - imgNode.skip = true |
89 | | - var srcset = (imgNode.attrs.srcset || imgNode.attrs[options.lazySrcset] || imgNode.attrs[options.lazySrc] || imgNode.attrs.src) |
90 | | - .split(',') |
91 | | - .filter(Boolean) |
92 | | - .map(value => { |
93 | | - value = value.trim().split(/\s/) |
94 | | - var path = options.replaceExtension ? removeExtension(value[0]) : value[0] |
95 | | - var size = value[1] |
96 | | - |
97 | | - return [path + '.webp', size].filter(Boolean).join(' ') |
98 | | - }) |
99 | | - .join(', ') |
100 | | - |
101 | | - var sourceAttrs = { |
102 | | - type: 'image/webp' |
103 | | - } |
104 | | - |
105 | | - sourceAttrs[imgNode.attrs[options.lazySrcset] || imgNode.attrs[options.lazySrc] ? options.lazySrcset : 'srcset'] = srcset |
106 | | - |
107 | | - return { |
108 | | - tag: 'picture', |
109 | | - content: [ |
110 | | - { |
111 | | - tag: 'source', |
112 | | - attrs: sourceAttrs |
113 | | - }, |
114 | | - imgNode |
115 | | - ] |
116 | | - } |
| 91 | +function getPicture(imgNode, options) { |
| 92 | + imgNode.skip = true |
| 93 | + var srcset = (imgNode.attrs.srcset || imgNode.attrs[options.lazySrcset] || imgNode.attrs[options.lazySrc] || imgNode.attrs.src) |
| 94 | + .split(',') |
| 95 | + .filter(Boolean) |
| 96 | + .map(value => { |
| 97 | + value = value.trim().split(/\s/) |
| 98 | + var path = options.replaceExtension ? removeExtension(value[0]) : value[0] |
| 99 | + var size = value[1] |
| 100 | + |
| 101 | + return [path + options.extension, size].filter(Boolean).join(' ') |
| 102 | + }) |
| 103 | + .join(', ') |
| 104 | + |
| 105 | + var sourceAttrs = { |
| 106 | + type: 'image/webp' |
| 107 | + } |
| 108 | + |
| 109 | + sourceAttrs[imgNode.attrs[options.lazySrcset] || imgNode.attrs[options.lazySrc] ? options.lazySrcset : 'srcset'] = srcset |
| 110 | + |
| 111 | + return { |
| 112 | + tag: 'picture', |
| 113 | + content: [ |
| 114 | + { |
| 115 | + tag: 'source', |
| 116 | + attrs: sourceAttrs |
| 117 | + }, |
| 118 | + imgNode |
| 119 | + ] |
| 120 | + } |
117 | 121 | } |
0 commit comments