Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 28 additions & 25 deletions _11ty/img-dim.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

const { JSDOM } = require("jsdom");
const { parse } = require("node-html-parser");
const { promisify } = require("util");
const sizeOf = promisify(require("image-size"));
const blurryPlaceholder = require("./blurry-placeholder");
Expand Down Expand Up @@ -69,21 +69,22 @@ const processImage = async (img, outputPath) => {
}
if (inputType == "gif") {
const videoSrc = await gif2mp4(src);
const video = img.ownerDocument.createElement(
/AMP/i.test(img.tagName) ? "amp-video" : "video"
);
[...img.attributes].map(({ name, value }) => {
const tagName = /AMP/i.test(img.tagName) ? "amp-video" : "video";
const video = parse(`<${tagName}></${tagName}>`).firstChild;

Object.entries(img.attributes).forEach(([name, value]) => {
video.setAttribute(name, value);
});
video.src = videoSrc;

video.setAttribute("src", videoSrc);
video.setAttribute("autoplay", "");
video.setAttribute("muted", "");
video.setAttribute("loop", "");
if (!video.getAttribute("aria-label")) {
video.setAttribute("aria-label", img.getAttribute("alt"));
video.removeAttribute("alt");
}
img.parentElement.replaceChild(video, img);
img.replaceWith(video);
return;
}
// When the input is a PNG, we keep the fallback image a PNG because JPEG does
Expand All @@ -98,28 +99,30 @@ const processImage = async (img, outputPath) => {
`background-size:cover;` +
`background-image:url("${await blurryPlaceholder(src)}")`
);
const doc = img.ownerDocument;
const picture = doc.createElement("picture");
const avif = doc.createElement("source");
const webp = doc.createElement("source");
const jpeg = doc.createElement("source");
const fallback = await setSrcset(jpeg, src, fallbackType);

const picture = parse("<picture></picture>").firstChild;
const avifNode = parse("<source>").firstChild;
const webpNode = parse("<source>").firstChild;
const jpegNode = parse("<source>").firstChild;

const fallback = await setSrcset(jpegNode, src, fallbackType);
if (!fallback) {
return;
}
const avifFallback = await setSrcset(avif, src, "avif");
const avifFallback = await setSrcset(avifNode, src, "avif");
if (avifFallback) {
avif.setAttribute("type", "image/avif");
picture.appendChild(avif);
avifNode.setAttribute("type", "image/avif");
picture.appendChild(avifNode);
}
const webpFallback = await setSrcset(webp, src, "webp");
const webpFallback = await setSrcset(webpNode, src, "webp");
if (webpFallback) {
webp.setAttribute("type", "image/webp");
picture.appendChild(webp);
webpNode.setAttribute("type", "image/webp");
picture.appendChild(webpNode);
}
jpeg.setAttribute("type", `image/${fallbackType}`);
picture.appendChild(jpeg);
img.parentElement.replaceChild(picture, img);
jpegNode.setAttribute("type", `image/${fallbackType}`);
picture.appendChild(jpegNode);

img.replaceWith(picture);
picture.appendChild(img);
img.setAttribute("src", fallback);
} else if (!img.getAttribute("srcset")) {
Expand Down Expand Up @@ -149,12 +152,12 @@ const dimImages = async (rawContent, outputPath) => {
let content = rawContent;

if (outputPath && outputPath.endsWith(".html")) {
const dom = new JSDOM(content);
const images = [...dom.window.document.querySelectorAll("img,amp-img")];
const root = parse(content);
const images = root.querySelectorAll("img,amp-img");

if (images.length > 0) {
await Promise.all(images.map((i) => processImage(i, outputPath)));
content = dom.serialize();
content = root.toString();
}
}

Expand Down
85 changes: 82 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"image-size": "^0.8.3",
"lru-cache": "^5.1.1",
"mocha": "^10.1.0",
"node-html-parser": "^7.0.2",
"phin": "^3.5.0",
"purge-from-html": "^1.0.3",
"purgecss": "^4.0.3",
Expand Down