Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions layouts/_default/_markup/render-image.html
Original file line number Diff line number Diff line change
@@ -1,27 +1,43 @@
{{- $alt := .PlainText | safeHTML -}}
{{- $lazyLoading := .Page.Site.Params.enableImageLazyLoading | default true -}}
{{- $dest := .Destination -}}
{{- $url := urls.Parse $dest -}}

{{- $isLocal := not (urls.Parse $dest).Scheme -}}
{{- $isLocal := not $url.Scheme -}}
{{- $isPage := and (eq .Page.Kind "page") (not .Page.BundleType) -}}
{{- $startsWithSlash := hasPrefix $dest "/" -}}
{{- $startsWithRelative := hasPrefix $dest "../" -}}

{{- if and $dest $isLocal -}}
{{- if $startsWithSlash -}}
{{/* Images under static directory */}}
{{- $dest = (relURL (strings.TrimPrefix "/" $dest)) -}}
{{- with or (.PageInner.Resources.Get $url.Path) (resources.Get $url.Path) -}}
{{/* Images under assets directory */}}
{{- $query := cond $url.RawQuery (printf "?%s" $url.RawQuery) "" -}}
{{- $fragment := cond $url.Fragment (printf "?%s" $url.Fragment) "" -}}
{{- $dest = printf "%s%s%s" .RelPermalink $query $fragment -}}
{{- else -}}
{{/* Images under static directory */}}
{{- $dest = (relURL (strings.TrimPrefix "/" $dest)) -}}
{{- end -}}
Comment on lines +13 to +21
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{{- else if and $isPage (not $startsWithRelative) -}}
{{/* Images that are sibling to the individual page file */}}
{{ $dest = (printf "../%s" $dest) }}
{{- end -}}
{{- end -}}

{{- $attributes := "" -}}
{{- range $key, $value := .Attributes -}}
{{- if $value -}}
{{- $pair := printf "%s=%q" $key ($value | transform.HTMLEscape) -}}
{{- $attributes = printf "%s %s" $attributes $pair -}}
{{- end -}}
{{- end -}}

{{- with .Title -}}
<figure>
<img src="{{ $dest | safeURL }}" title="{{ . }}" alt="{{ $alt }}" {{ if $lazyLoading }}loading="lazy"{{ end }} />
<img src="{{ $dest | safeURL }}" title="{{ . }}" alt="{{ $alt }}" {{ $attributes | safeHTMLAttr }} {{ if $lazyLoading }}loading="lazy"{{ end }} />
<figcaption>{{ . }}</figcaption>
</figure>
{{- else -}}
<img src="{{ $dest | safeURL }}" alt="{{ $alt }}" {{ if $lazyLoading }}loading="lazy"{{ end }} />
<img src="{{ $dest | safeURL }}" alt="{{ $alt }}" {{ $attributes | safeHTMLAttr }} {{ if $lazyLoading }}loading="lazy"{{ end }} />
Comment on lines +28 to +42
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for also adding the attributes 🙌

{{- end -}}