11ty / 11ty/image

Getting image path from 11tydata in a folder for 11ty image plugin

Open
#147 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

education
Dominant language
JavaScript
Stars
467
Forks
65
PR merge metrics
No merged PRs in 30d

Description

So, I'd like to keep images seperated by collectionGlob. Each collection has an image folder. When calling a nunjucks plugin to process the image, I'd like it to tell the folder path + src. That I would like to come from a folder data file either json or js. Just unsure how to go about doing it?

in _11ty/image.js

const Image = require("@11ty/eleventy-img");
const path = require("path");
const fs = require("fs");

module.exports = function(src, alt, widths, sizes, classattr) {
  // src input same as 'normal' site-relative path for convenience, so add base path:
  src = collectionpath + src;
  let ext = src.substr(src.lastIndexOf(".") + 1);
  let currentFormat = ext == "png" ? "png" : "jpeg";
  let srcset = [100, 320, 640, 1024, 1536, 2048];
  if (widths) {
    widthsArray = widths.split(",");
    srcset = widthsArray.map(n => Number(n));
  }
  let options = {
    urlPath: collectionpath,
    outputDir: collectionpath,

One of the collection folders has foldername.11tydata.js

const todaysDate = new Date();
const isDev = require("../_data/isdevelopment")();

function showDraft(data) {
  if (isDev) return true;
  const isDraft = "draft" in data && data.draft !== false;
  const isPostInFuture =
    "scheduled" in data ? data.scheduled > todaysDate : false;
  return !isDraft && !isPostInFuture;
}
const collectionpath = "/technical-seo/images";
module.exports = () => {
  return {
    eleventyComputed: {
      eleventyExcludeFromCollections: (data) =>
        showDraft(data) ? data.eleventyExcludeFromCollections : true,
        permalink: (data) => (showDraft(data) ? data.permalink : false),
    },
    collectionpath
  };
};

So, I need to tell image.js to LOOK in the folder and read the 11tydata and establish what the path variable will be to create the image. Haven't figured out cache yet.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading _11ty/image.js alongside the folder’s foldername.11tydata.js to understand how collectionpath is currently defined and consumed. Determine the intended behavior for resolving per-folder data and caching, with completion requiring the image plugin to process each collection’s images using that collection-specific path.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
build-system, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.