Skip to content

Commit

Permalink
feat: imgs don't clash anymore now that the url paths are different
Browse files Browse the repository at this point in the history
  • Loading branch information
TrebledJ committed Nov 4, 2023
1 parent eed90a5 commit 2682d9c
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions eleventy/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,30 @@ module.exports = function (eleventyConfig) {
const ext = extDefault ?? 'webp';
const animated = file.endsWith('gif');

// Construct a unique path so that images with the same fileslug don't clash.
let paths = file.split(path.sep);
paths.pop(); // Discard
if (file.endsWith('.mp4')) {
// Skip mp4s, as they're copied directly via passthrough copy.
paths = [];
} else if (file.startsWith('http')) {
// Also skip web imgs.
paths = [];
} else if (paths.includes('content')) {
paths = paths.slice(paths.indexOf('content') + 1); // Use inputPath after `content` and onwards.
// console.log('appending unique', paths, 'to image output dir');
} else if (file.includes('assets/img/')) {
paths = []; // Added through passthrough copy.
}

// console.log('outputting to', path.join(eleventyConfig.dir.output, 'img', ...paths));

// Full list of formats here: https://www.11ty.dev/docs/plugins/image/#output-formats
const options = {
widths: imageWidths,
formats: [ext],
outputDir: path.join(eleventyConfig.dir.output, 'img'),
outputDir: path.join(eleventyConfig.dir.output, 'img', ...paths),
urlPath: `/${path.join('img', ...paths)}`,
sharpOptions: {
animated,
},
Expand Down Expand Up @@ -162,7 +181,9 @@ module.exports = function (eleventyConfig) {

const { page } = post;
const src = resolveResourcePath(page, post.data.thumbnail_src);
const altText = post.data.title;

const removeTagsRegex = /(<\w+>)|(<\/\w+>)/g;
const altText = post.data.title.replace(removeTagsRegex, '');

const { ext, options } = getOptions(src);
eleventyImage(src, options);
Expand Down

0 comments on commit 2682d9c

Please sign in to comment.