Working with images in markdown files and 11ty

Best practices for working with images from capturing or internet in markdown files and eleventy. This is the workflow I use for this website.
Last updated 23 days ago / Edit on Github

👉 Note: 11ty & Nunjucks.

What we want?

  • Capture the screen to the clipboard and then paste it into the editor. The captured image is automatically copied to a folder and can be viewed in the editor as well as on GitHub and on the 11ty website (after rendering).
  • The same function for online images (copy and paste the URL).

Tools

You will need to install the following tools. Depending on your working style, you can choose one or both of them.

Setting up your project structure

Below is an example of the folder structure on my website. Read this section to understand why this structure is important for all parts work together.

dinhanhthi.com
|- _built/ # The output folder (11ty renders markdown files to this)
|- .eleventy.js
|- notes/
|- blog/ # contains all markdown files
|- 2022-06-25-name-of-post.md
|- img_blog/ # contains all images
|- 2022-06-25-name-of-post/ # the same name as markdown file
|- figure-1.png
|- figure-2.png

Some remarks,

  • I put all the Markdown files and images in a folder called "notes", you do not have to do that. In other words, you can put the "blog" and "img_blog" folders in the root folder.
  • The folder "2022-06-25-name-of-post" inside "img_blog" is automatically created by Typora or VScode (that's why I use them). But you can also create it manually if you want.

Setting up .eleventy

You need to make 11ty recognize "blog" and "img_blog".

module.exports = function (eleventyConfig) {
// For images
eleventyConfig.addPassthroughCopy({ "notes/img_blog": "img_blog" });

return {
// other settings
dir: {
input: ".",
output: "_built",
},
};
}

Setting up VSCode

Make sure you have the Markdown All in One and Markdown Image extensions already installed.

Go to Makdown Image settings:

  • In Markdown-image > Local: Path: /img_blog/.
  • In Markdown-mage > Base: File Name Format: ${mdname}/${rand,100}. Whenever you paste an image (from clipboard or from an URL) to a file 2022-06-25-name-of-post.md, a new image file will be created at img_blog/2022-06-25-name-of-post/<random-number>.png
Warning icon

A weakness of Markdown Image and Typora is that if you make changes in VSCode or Typora, an image that is automatically created in the folder you want cannot be changed. For example, if you change your mind and delete the line ![](...), the image in the folder "img_blog" is still there, you have to delete it manually!

Setting up Typora

Go to Typora Preferences and make the same settings as below,

typora-image-setting

Why does it work?

Idea icon

Based on how an SSG works, you can use the same idea in this post to get the same workflow for other SSGs other than 11ty (Jekyll, Hugo, Gatsby, ...).

As you know, all rendered HTML files and images must be stored in an output folder, say _built/ in my example. What you see on the website (in the html file) is

<img alt="alt-of-image" src="../img_blog/2022-06-25-name-of-post/name-of-image.png">

and what you see in the Markdown editor is,

![alt-of-image](../img_blog/2022-06-25-name-of-post/name-of-image.png)

They look the same, but in fact they are not. Let's check the folder _built/,

dinhanhthi.com
|- _built/
|- name-of-post/
| |- index.html
|- img_blog/
|- 2022-06-25-name-of-post-name-of-post/
|- name-of-image.png

Because in the .eleventy.js, we have set addPassthroughCopy from notes/img_blog/ to img_blog/, we have _built/img_blog/ after rendering.

It works in markdown editors (VSCode, Typora, Github,...) because the relative path of the image to the markdown file is ../img_blog/.

Check again our project structure,

dinhanhthi.com
|- _built/
|- .eleventy.js
|- notes/
|- blog/
|- 2022-06-25-name-of-post.md
|- img_blog/
|- 2022-06-25-name-of-post/
|- name-of-image.png

It works in the HTML file because the relative path of the image to the index.html file is ../img_blog/ too.

In short, you have to make sure that the relative path of the image to the markdown file is the same as the relative path of the image to the (rendered) index.html file.

Support Thi Support Thi