npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

eleventy-shortcode-image

v1.7.2

Published

Shortcode for image transformation, optimization and building its HTML representation.

Downloads

12

Readme

eleventy-shortcode-image 🖼

code style: prettier

Optimize your raster and vector images 👨‍🎨

Intention

This world needs SVG! Optimal SVGs ☝️ And we bring it, while also do not forget about beloved raster images 🙂

Get started

Installation

At first do:

npm i -D eleventy-shortcode-image

and then you can include it into .eleventy.js:

const { createImageShortcode } = require('eleventy-shortcode-image');

module.exports = (eleventyConfig) => {
  eleventyConfig.addShortcode(
    'image',
    createImageShortcode({
      /* Options */
    }),
  );
};

Options

Package exports factory-function createImageShortcode that returns shortcode function.

This function accepts optional options:

interface ImageShortCodeOptions {
  /**
   * Path to directory where all images live.
   *
   * Should start from the _current working directory_.
   *
   * By default it is `src/assets/images`.
   */
  inputDirectory?: string;
  /**
   * Path to directory for optimized and transformed images.
   * First part of the path is meant to be the _output_ directory.
   *
   * Should start from the _current working directory_.
   *
   * By default it is `_site/images`.
   */
  outputDirectory?: string;
  /**
   * Options for [svgo](https://github.com/svg/svgo) package.
   * for subtle configuration of SVGs optimizations.
   */
  svgoOptions?: OptimizeOptions & {
    readonly toHTML?: boolean;
    readonly shouldDeleteViewBox?: boolean;
    readonly shouldDeleteDimensions?: boolean;
  };
  /**
   * Options for [@11ty/eleventy-img](https://www.11ty.dev/docs/plugins/image/) package.
   * Is is used for optiomizations of raster images.
   * For more info see its documentation.
   */
  rasterOptions?: Record<string, any>;
}

Example:

const options = {
  // Do not add leading and trailing `/`
  inputDirectory: 'src/images',
  // Do not add leading and trailing `/`
  outputDirectory: '_site/images',
  svgoOptions: {
    /* ... */
  },
  rasterOptions: {
    /* ... */
  },
};

Use

What is shortcode and how to use it?

This shortcode accepts two arguments:

interface ImageProperties {
  /** Inserts SVG into HTML. **Only for SVG**. */
  readonly toHTML?: boolean;
  /** **Only for SVG**. */
  readonly shouldDeleteViewBox?: boolean;
  /** **Only for SVG**. */
  readonly shouldDeleteDimensions?: boolean;
  /** Class names for <img>. */
  readonly classes?: string | ReadonlyArray<string>;
  /**
   * Defines that image should be loaded lazily.
   * Notifies plugin that _src_ and _srcset_ should not
   * be set directly, but to other custom attributes.
   */
  readonly lazy?: boolean;
  /** Class names for <img>. */
  readonly classes?: string | ReadonlyArray<string>;
  /** Name of the custom _src_ attribute for lazy loaded image. */
  readonly srcName?: string;
  /** Name of the custom _srcset_ attribute for lazy loaded image. */
  readonly srcsetName?: string;

  // And any other valid attribute.
}

// This is a signature of the actual shortcode.
async function image(
  src: string,
  properties?: ImageProperties,
): Promise<string>;
  • src is the path to image from inputDirectory that are passed to factory-function in .eleventy.js.

    // .eleventy.js
    eleventyConfig.addShortcode(
      'image',
      createImageShortcode({
        inputDirectory: 'src/images',
      }),
    );
    
    // your_template.11ty.js
    module.exports = async function () {
      // Shortcode will assume that path to image is 'src/images/some_image.png'
      return `${await this.image('some_image.png')}`;
    };
  • attributes is a couple of attributes that can be added to image.

    Note that for SVG that is inserted into HTML is applicable only classes property.

    module.exports = async function () {
      return `${this.image('foo.png', {
        alt: 'baz',
        classes: 'my-image',
      })}`;
    };

lazy property signals that image should not be rendered instantly. When this property is true then plugin does not set src attribute for <img> (or srcset for <source> element). This behavior allows third-party plugins lazily load images when they are needed. By default, lazy equals to false.

Also, you can customize names of custom src and srcset attributes. For that to be done, provide srcName and srcsetName properties of ImageProperties. By default, srcName equals to data-src and srcsetName - data-srcset.

Debug

Package uses debug package to display some errors. They can be visible in EleventyShortcodeImage namespace. More detail on debug's page.

What's special

  • This shortcode is configured to optimize images without its resizing. So all assets save its original width/height size.
  • It optimizes and includes SVG into HTML and not just copy it do build directory. Also shortcode allows to pass your classes to SVG 😱 Yeah, we mean it 😏
  • Allows third-party lazy load plugins integration. You will love it! ❤️

Internally shortcode uses SVGO and @11ty/eleventy-img packages. You can configure them through according options. See above about it ☝️ .

Note that shortcode has default options for these packages, but if you will add additional options, then some options may be overwritten. Default options for SVG optimizer is here and for raster optimizer - here.

By default, SVGs are not inserted into HTML directly, but through <img> element. If you want to insert SVG into HTML provide toHTML property to svgoOptions. This property tells that insertion will be done globally. You can override it by toHTML property of ImageProperties object.

Word from author

Have fun! ✌️