eleventy-img icon indicating copy to clipboard operation
eleventy-img copied to clipboard

Use this plugin with markdown images

Open Brixy opened this issue 3 years ago • 5 comments

Thank you for this plugin.

It would be great if it was possible to use this plugin with markdown images.

![Alt text](/img/image.jpg "Optional title text -> besomes image caption")

![Alt text](/img/image.svg "Optional title text -> besomes image caption")

Some ideas:

  • Only use this plugin if image wider than a defined width (e.g. 500px).
  • Use optional markdown title attribute as image caption (figcaption).

Thanks!

Brixy avatar Apr 18 '21 09:04 Brixy

Some more context here: https://github.com/markdown-it/markdown-it/blob/master/docs/development.md#i-need-async-rule-how-to-do-it

fwiw it doesn’t look like markdown-it handles async plugins

zachleat avatar Apr 23 '21 15:04 zachleat

All the code is put in this gist.

1. Transform images in markdown

I am using transforms to achieve this.

Since eleventy-img provides Image.statsSync function, as long as you have null in the widths option of eleventy-img, you can get the width of the original image. You can then compare its width to your defined width to decide if you want to transform the image.

It can cover all images in your website including the ones from markdown files, as long as you set a proper query selector. (i.e., article img, main img)

To be noted, I use the variable index to add loading=lazy and decoding=async attributes to all images except the first image on every page. Also I use process.env.ELEVENTY_ENV to only transform images in the production build. Please adjust these and the options for eleventy-img according to your use case.

2. Use figcaption

To use figcaption, I wrote a markdown-it plugin which is also included in the gist. Originally, an image element will be rendered as:

<p>
  <img src="" alt="" title="" />
</p>

With this plugin, it will be rendered as:

// if title is not defined
<figure>
  <img src="" alt="" title="" />
</figure>

// if title is defined
<figure>
  <img src="" alt="" title="title" />
  <figcaption>title</figcaption>
</figure>

To use this plugin, you can configure markdown-it in your .eleventy.js as following:

const md = require('markdown-it')()
  .use(require('./figure')) // the plugin in the gist, adjust this depending on where you put the file

eleventyConfig.setLibrary('md', md)

Alexs7zzh avatar Apr 24 '21 05:04 Alexs7zzh

@Alexs7zzh good approach but I don't understand the reason to use 2 stages. All sync/async image transformation can be done in transform filter.

mahnunchik avatar Dec 08 '21 12:12 mahnunchik

Hi @Brixy, I know this is a pretty old discussion and you might already have found a solution or maybe it's not relevant anymore. Anyhow, to answer this:

It would be great if it was possible to use this plugin with markdown images.

I just published a markdown-it plugin that uses eleventy-img to process images. Check it out here. If you end up trying it, let me know what you think.

solution-loisir avatar Jul 28 '22 04:07 solution-loisir

Hi @Brixy, I know this is a pretty old discussion and you might already have found a solution or maybe it's not relevant anymore. Anyhow, to answer this:

It would be great if it was possible to use this plugin with markdown images.

I just published a markdown-it plugin that uses eleventy-img to process images. Check it out here. If you end up trying it, let me know what you think.

this looks really good! thanks! I'm still so new to Eleventy but I'll give it a try later.

shirooo39 avatar Sep 11 '22 07:09 shirooo39

Great! Let me know what you think. :smile:

solution-loisir avatar Oct 07 '22 00:10 solution-loisir

Related: #208

zachleat avatar Feb 12 '24 14:02 zachleat

The docs have been updated to recommend #208 moving forward for synchronous use cases like Markdown. I think we can consider this one resolved with Eleventy v3.0.0-canary.5 and Eleventy Image v4.0.1.

https://www.11ty.dev/docs/plugins/image/#synchronous-shortcode

Thanks y’all!

zachleat avatar Feb 14 '24 16:02 zachleat