Skip to content

Commit

Permalink
feat: enable lazy loading for custom img shortcode (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoxys authored Feb 21, 2021
1 parent 582d844 commit 0317ea1
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 8 deletions.
2 changes: 2 additions & 0 deletions exampleSite/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,5 @@ params:

geekblogLegalNotice: https://thegeeklab.de/legal-notice/#contact-information
geekblogPrivacyPolicy: https://thegeeklab.de/legal-notice/#privacy-policy

geekblogImageLazyLoading: true
8 changes: 8 additions & 0 deletions exampleSite/content/posts/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ enableGitInfo = true
# (Optional, default posts) Set the name of the default content section.
geekblogContentSection = "my-pages"

# (Optional, default true) Enable or disable image lazy loading for images rendered
# by the 'img' shortcode.
geekblogImageLazyLoading = true

# (Optional, default none) Adds a "Hosted on <provider>" line to the footer.
# Could be used if you want to give credits to your hosting provider.
[params.geekblogHostedOn]
Expand Down Expand Up @@ -355,6 +359,10 @@ params:
# (Optional, default posts) Set the name of the default content section.
geekblogContentSection: "my-pages"

# (Optional, default true) Enable or disable image lazy loading for images rendered
# by the 'img' shortcode.
geekblogImageLazyLoading: true

# (Optional, default none) Adds a "Hosted on <provider>" line to the footer.
# Could be used if you want to give credits to your hosting provider.
geekblogHostedOn:
Expand Down
35 changes: 32 additions & 3 deletions exampleSite/content/posts/post-with-images/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,37 @@ resources:
credits: "[David Pennington](https://unsplash.com/@dtpennington) on [Unsplash](https://unsplash.com/s/photos/test)"
---

Amalia id per in minimum facility, quid facet modifier ea ma. Ill um select ma ad, en ferric patine sentient vim. Per expendable foreordained interpretations cu, maxim sole pertinacity in ram. Dolor sit, sumo unique argument um no. Gracie nominal id xiv. Romanesque acclimates investiture. Ornateness bland it ex enc, est yeti am bongo detract re. Pro ad prompts feud gait, quid exercise emeritus bis e. In pro quints consequent, denim fastidious copious quo ad. Stet probates in duo.
If you need more flexibility for your embedded images, you could use the `img` shortcode. It is using Hugo's
[page resources](https://gohugo.io/content-management/page-resources/) and supports lazy loading of your images.

{{< img name="testimage" size="small" >}}
**Attributes:**

Dolor sit, sumo unique argument um no. Gracie nominal id xiv. Romanesque acclimates investiture. Ornateness bland it ex enc, est yeti am bongo detract re. Pro ad prompts feud gait, quid exercise emeritus bis e. In pro quints consequent, denim fastidious copious quo ad. Stet probates in duo.
| Name | Usage | default |
| ------------- | -------------------------------------------------------------------------------------------------------------- | ----------------- |
| name (string) | Name of the image resource defined in your front matter. | empty |
| alt (string) | Description for displayed image. | resource `.Title` |
| size (string) | Thumbnail size (tiny\|small\|medium\|large). | empty |
| lazy (bool) | Enable or disable image lazy loading. Can be controlled globally by site parameter `geekblogImageLazyLoading`. | true |

**Example:**

Define your resources in the page front matter, custom parameter `params.credits` is optional.

<!-- spellchecker-disable -->

```md
---
resources:
- name: testimage
src: "images/testimage.jpg"
title: This is a test image
params:
credits: "[David Pennington](https://unsplash.com/@dtpennington) on [Unsplash](https://unsplash.com/s/photos/test)"
---

{{</* img name="testimage" size="small" lazy=true */>}}
```

<!-- spellchecker-enable -->

{{< img name="testimage" size="small" lazy=true >}}
2 changes: 2 additions & 0 deletions layouts/shortcodes/img.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{{ $source := ($.Page.Resources.ByType "image").GetMatch (printf "*%s*" (.Get "name")) }}
{{ $customAlt := .Get "alt" }}
{{ $customSize := .Get "size" }}
{{ $lazyLoad := default (default true $.Site.Params.GeekblogImageLazyLoading) (.Get "lazy") }}

{{ with $source }}
{{ $caption := default .Title $customAlt }}
Expand All @@ -16,6 +17,7 @@
<figure class="gblog-post__figure">
<a class="gblog-post__link--raw" href="{{ .RelPermalink }}">
<img
{{ if $lazyLoad }}loading="lazy"{{ end }}
{{ with $customSize }}
src="{{ index $size $customSize }}" alt="{{ $caption }}"
{{ else }}
Expand Down
12 changes: 8 additions & 4 deletions src/sass/_base.scss
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ img {
min-height: 180px;
object-fit: cover;
overflow: hidden;
border-radius: 3px;
border-radius: $border-radius;
}

span {
Expand All @@ -283,7 +283,7 @@ img {
padding: $padding-4;
font-size: 0.8em;
color: $gray-200;
border-radius: 3px 0;
border-radius: $border-radius 0;
}

a:hover,
Expand All @@ -301,17 +301,21 @@ img {
}

&__figure {
padding: $padding-4 $padding-4 0 $padding-4;
padding: $padding-4;
margin: $padding-16 0;
background-color: $gray-300;
display: table;
border-top-left-radius: $border-radius;
border-top-right-radius: $border-radius;

figcaption {
display: table-caption;
caption-side: bottom;
background-color: $gray-300;
padding: $padding-4;
padding: 0 $padding-4 $padding-4;
text-align: center;
border-bottom-left-radius: $border-radius;
border-bottom-right-radius: $border-radius;
}

img {
Expand Down
2 changes: 1 addition & 1 deletion src/sass/_defaults.scss
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ $font-size-14: 0.875rem !default;
$font-size-16: 1rem !default;
$font-size-32: 2rem !default;

$border-radius: 0.3rem !default;
$border-radius: 0.15rem !default;

// Grayscale
$white: rgba(255, 255, 255, 1) !default;
Expand Down
1 change: 1 addition & 0 deletions src/sass/_markdown.scss
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

img {
max-width: 100%;
border-radius: $border-radius;
}

blockquote {
Expand Down

0 comments on commit 0317ea1

Please sign in to comment.