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

Use this plugin with markdown images #90

Closed
Brixy opened this issue Apr 18, 2021 · 8 comments
Closed

Use this plugin with markdown images #90

Brixy opened this issue Apr 18, 2021 · 8 comments
Labels
enhancement New feature or request

Comments

@Brixy
Copy link

Brixy commented Apr 18, 2021

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!

@zachleat zachleat added the enhancement New feature or request label Apr 23, 2021
@zachleat
Copy link
Member

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

@Alexs7zzh
Copy link

Alexs7zzh commented Apr 24, 2021

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)

@mahnunchik
Copy link

@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.

@solution-loisir
Copy link
Contributor

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.

@shirooo39
Copy link

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.

@solution-loisir
Copy link
Contributor

Great! Let me know what you think. 😄

@zachleat
Copy link
Member

Related: #208

@zachleat
Copy link
Member

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants