A markdown-it plugin and Nunjucks async filter to make working with Cloudinary in Eleventy easier.

Overview

Cloudinary Eleventy Helpers

This is a collection of Eleventy Cloudinary helpers. It currently includes:

  • A markdown-it plugin that converts local images in your Markdown files to Cloudinary URLs.
  • An async Nunjucks filter that allows converting local images in frontmatter to Cloudinary URLs.

Why is this useful?

Cloudinary offers a generous free tier, and it allows for automatica optimization, format conversion, and resizing. These utilities take advantage of this to increase your site's performance with very little effort on your part.

NOTE: If you don't already have a Cloudinary account, you can sign up for free here. If you use this link to sign up, Cloudinary sends me a few dollars without any effect on the account you create, so consider this a kind of tip jar.

Performance benefits of these utilities

  • Automatic format conversion — Cloudinary will automatically serve modern formats like WebP instead of JPEGs when the browser supports them. This cuts down on bandwidth, loading time, and file size.

  • Automatic srcSet — images in Markdown will have a srcset added, which has huge performance benefits, especially on smaller viewports.

  • Lazy loading — images in Markdown have loading="lazy" added, which improves initial page load times on supported browsers.

  • Automatic optimization — Cloudinary will automatically optimize your images, which reduces the file size without impacting quality.

These add up to a pretty drastic impact on your site's performance without much manual effort to get it up and running.

Installation

npm i @jlengstorf/cloudinary-11ty-helpers markdown-it

In your .eleventy.js:

const cloudinary = require('@jlengstorf/cloudinary-11ty-helpers')({
  // find this at https://cloudinary.com/console
  cloud_name: 'YOUR_CLOUD_NAME',
  api_key: 'YOUR_API_KEY',
  api_secret: 'YOUR_API_SECRET',
});

module.exports = function (eleventyConfig) {
  /*
   * Modifying Markdown requires adding your own instance of the markdown-it
   * library so we can add plugins.
   */
  const markdown = require('markdown-it');

  const mdLib = markdown({ html: true }).use(cloudinary.mdPlugin);

  eleventyConfig.setLibrary('md', mdLib);

  /*
   * Adding the async Nunjucks filter is nice and straightforward.
   */
  eleventyConfig.addNunjucksAsyncFilter('cloudinary', cloudinary.asyncFilter);
};

Usage

Markdown plugin

Once the Markdown plugin is installed, local images in Markdown files will be converted to Cloudinary URLs with no additional work on your part.

Non-local images are ignored.

NOTE: Currently GIFs are ignored. GIFs can consume a ton of Cloudinary bandwidth, so it's recommended to convert them to videos or to manually upload them to Cloudinary if you're sure you want to host GIFs on Cloudinary.

Async Nunjucks filter

To use the filter, add it to any template variable that contains a relative image path:

{% if image %}
<meta name="image" content="{{ image | cloudinary(page.inputPath, 1200) }}" />
{% else %}

NOTE: page.inputPath is the path to the file that is being rendered. 11ty supplies this, so the value should be available in any Nunjucks template.

The above example assumes that the page being rendered has fronmatter with an image property. Assuming the file is at src/my-page.md and the image is at src/images/my-image.jpg, it should look something like this:

---
image: ./images/my-image.jpg
---

Configuration

The initialization function accepts a configuration object. Only the cloud_name, api_key, and api_secret are required for the utilities to work.

type CloudinaryHelperOptions = {
  /**
   * Find yours at https://cloudinary.com/console
   */
  cloud_name: string;

  /**
   * This can also be set via env var as `CLOUDINARY_API_KEY`
   */
  api_key?: string;

  /**
   * This can also be set via env var as `CLOUDINARY_API_SECRET`
   */
  api_secret?: string;

  /**
   * Defaults to `https://res.cloudinary.com/`
   */
  base_url?: string;

  /**
   * If set, images will be placed into this folder in your Cloudinary account
   */
  folder?: string;

  /**
   * Transformations to apply to all images
   */
  base_transformation?: string;

  /**
   * Ideally, set this to the same width as your content area
   */
  image_width?: number;
};

NOTE: You can also set your API key and secret via env vars as CLOUDINARY_API_KEY, and CLOUDINARY_API_SECRET. If you do, you can omit the api_key and api_secret options.

You might also like...

A weather app done with React. Allows you to make a search for the current weather by simply typing "common knowledge" cities and save them to favourites on local storage.

A weather app done with React. Allows you to make a search for the current weather by simply typing

Simpliest Weather React-App A weather app done with React. Allows you to make a search for the current weather by simply typing "common knowledge" cit

Aug 24, 2022

we are make our components in story book. So if we add some components you can find document and example of useage in storybook.

we are make our components in story book. So if we add some components you can find document and example of useage in storybook.

we are make our components in story book. So if we add some components you can find document and example of useage in storybook.

Aug 12, 2022

Math Magicians! This is a website for all fans of mathematics. It is a Single Page App (SPA) that allows users to make basic calculations and read a random math-related quote.

Math Magicians! This is a website for all fans of mathematics. It is a Single Page App (SPA) that allows users to make basic calculations and read a random math-related quote.

Math Magicians Math Magicians is the first React Project I'm building. The main objective of this is to understand React features with a project-based

Feb 26, 2022

A Single Page App (SPA) that allows users to: make simple calculations and read a random math-related quote

A Single Page App (SPA) that allows users to: make simple calculations and read a random math-related quote

Math magicians is a website for all fans of mathematics. It is a Single Page App (SPA) that allows users to: make simple calculations and read a random math-related quote.

May 31, 2022

"Math magician is a website for all fans of mathematics. It is a Single Page Application(SPA) that allows users to make simple math calculations and read some math related quotes."

Math Magicians "Math magicians" is a website for all fans of mathematics. It is a Single Page App (SPA) that allows users to make simple calculations

Mar 29, 2022

An awesome tool to make Images and GIFs of your code!

An awesome tool to make Images and GIFs of your code!

Recoded An awesome tool to make Images and GIFs of your code! View Demo · Report Bug · Request Feature About The Project There are plenty of Apps like

Dec 18, 2022

"Math magicians" is a website for all fans of mathematics. You can Make simple calculations and Read a random math-related quote.

Math-Magicians This project was bootstrapped with Create React App. "Math magicians" is a website for all fans of mathematics. You can Make simple cal

Jun 23, 2022

İnstagram Clone.You can sign in, sign up, upload image, make comment 📷 Used ReactJS, Material UI, Firebase, Firestore Database

 İnstagram Clone.You can sign in, sign up, upload image, make comment 📷 Used ReactJS, Material UI, Firebase, Firestore Database

🟡 instagram-clone This is a instagram clone. You can sign in, sign up, upload image, make comment 📷 ⚡ LIVE To check out the live demo of this app AB

Apr 20, 2022

Juka Official Website built on top of Docusaurus/React Framework. Help us make it better!

Juka Programming Language Juka Programming Language website is built on top of Docusaurus 2. Feel free to contribute to our website! Any help is appre

Dec 24, 2022
Owner
Jason Lengstorf
💻 Developer / software architect. 📢 Frequent speaker. 🎨 Occasional designer. 🍸 Mediocre bartender.
Jason Lengstorf
Challenge [Frontend Mentor] - In this challenge, JavaScript was used to filter jobs based on the selected categories. Technologies used: HTML5, CSS3 and React.

Frontend Mentor - Job listings with filtering Front-end challenge focused on javascript logic, where a list of fictitious vacancies is presented and t

Rui Neto 11 Apr 13, 2022
Edvora App is a web application based on an external API, showing data about different types of products and the user can filter these data by choosing a specific state, city or product name. Build with React.js

Edvora App is a web application based on an external API, showing data about different types of products and the user can filter these data by choosing a specific state, city or product name. Build with React.js

Kyrillos Hany 5 Mar 11, 2022
This is a custom recipe app called chefMaster. which contains a lot of interesting such as many apis requests, filter, search , add posts. Property design

In the project directory, you can run: npm start Runs the app in the development mode. Open http://localhost:3000 to view it in the browser. The page

Andrew Smal 0 Nov 9, 2022
Mirrors the functionality of Apollo client's useQuery hook, but with a "query" being any async function rather than GQL statement.

useAsyncQuery Mirrors the functionality of Apollo client's useQuery hook, but with a "query" being any async function rather than GQL statement. Usage

Alasdair McLeay 7 Nov 16, 2022
Read Blog. This is also a practical project. Working with APIs more and Routing and many new things.

React blog This is a React blog that created with a custom API. We serve the API in our system and run blog with it. Goals of this project In this pro

AmirHossein Mohammadi 7 Jul 25, 2022
React + Redux starter kit / boilerplate with Babel, hot reloading, testing, linting and a working example app built in

A comprehensive starter kit for rapid application development using React. Why Slingshot? One command to get started - Type npm start to start develop

Cory House 9.8k Dec 22, 2022
A collection of framework specific Auth utilities for working with Supabase.

A collection of framework specific Auth utilities for working with Supabase.

Supabase Community 507 Jan 2, 2023
Markdown component for React

react-markdown Markdown component for React using remark. Learn markdown here and check out the demo here. Install npm: npm install react-markdown Why

remark 9.7k Jan 4, 2023
Purple haze is a TypeScript, Markdown, JS generative bundler that works in headless browser

Purple haze Inspired by generative programming and weed :). So I was learning Elm language at home usually in the evening and now I am missing all thi

Aexol 6 Nov 13, 2022
This compress library was made with Brotli and Gzip help, for React users who want to make website more performance and reduce JS bundle code

React-compress This compress library was made with Brotli and Gzip help, for React users who want to make website more performance and reduce JS bundl

Koma Human 30 Jan 6, 2023