Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
inetbiz opened this issue Apr 16, 2022 · 0 comments
Open

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

inetbiz opened this issue Apr 16, 2022 · 0 comments

Comments

@inetbiz
Copy link

inetbiz commented Apr 16, 2022

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.

@inetbiz inetbiz changed the title Getting image path from 11tydata in a folder Getting image path from 11tydata in a folder for 11ty image plugin Apr 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants