aboutsummaryrefslogtreecommitdiff
path: root/_posts/2023-11-05-add-lazy-loading-to-jekyll-posts.md
blob: 8293a4d5b5c51a43ebb1ff856b8c8a86b8d24e21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
---
title: "Add lazy loading of images in Jekyll posts"
permalink: /add-lazy-loading-to-jekyll-posts.html
date: 2023-11-05T09:04:28+02:00
layout: post
type: note
draft: false
---

Normally you define images with `![]()` in markdown files. But jekyll also
provides a way to adding custom attributes to tags with `{:attr="value"}`.

If you have lots of posts this command will append `{:loading="lazy"}` to all
images in all your markdown files.

```md
![image-title](/path/to/your/image.jpg)
```

will become

```md
![image-title](/path/to/your/image.jpg){:loading="lazy"}
```

Shell line bellow. Go into the folder where your posts are (probably `_posts`).

```sh
find . -type f -name "*.md" -exec sed -i -E 's/(\!\[.*\]\((.*?)\))$/\1{:loading="lazy"}/' {} \;
```

Under the hood this adds `loading="lazy"` to HTML `img` nodes.

That is about it.