Skip to content

Commit

Permalink
fix(eleventy): set-image-dimensions トランスフォームが <template> にも適用されるよ…
Browse files Browse the repository at this point in the history
…うにする
  • Loading branch information
chalkygames123 committed Mar 28, 2023
1 parent cfe818d commit d5e3f77
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions 11ty/transforms/set-image-dimensions.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ const { parse: parseSrcset } = require('srcset');

const config = require('../../config.cjs');

const templateQuerySelectorAll = (selector, document) => [
...document.querySelectorAll(selector),
...[...document.querySelectorAll('template')].flatMap((element) => [
...element.content.querySelectorAll(selector),
]),
];

const isValidSourceUrl = (sourceUrl) => {
if (!sourceUrl) return false;

Expand Down Expand Up @@ -87,7 +94,7 @@ const setImageDimensions = async function (content) {
} = dom;

await Promise.all([
...[...document.images]
...templateQuerySelectorAll('img', document)
.filter((element) => isValidSourceUrl(element.src))
.map(async (element) => {
const metadata = await getMetadata(
Expand All @@ -96,7 +103,7 @@ const setImageDimensions = async function (content) {

setDimensions(element, metadata.width, metadata.height);
}),
...[...document.querySelectorAll('picture > source')]
...templateQuerySelectorAll('picture > source', document)
.filter((element) => isValidSourceUrl(parseSrcset(element.srcset)[0].url))
.map(async (element) => {
const metadata = await getMetadata(
Expand Down

0 comments on commit d5e3f77

Please sign in to comment.