|
1 | 1 | {{- $alt := .PlainText | safeHTML -}}
|
2 | 2 | {{- $lazyLoading := .Page.Site.Params.enableImageLazyLoading | default true -}}
|
3 | 3 | {{- $dest := .Destination -}}
|
| 4 | +{{- $url := urls.Parse $dest -}} |
4 | 5 |
|
5 |
| -{{- $isLocal := not (urls.Parse $dest).Scheme -}} |
| 6 | +{{- $isLocal := not $url.Scheme -}} |
6 | 7 | {{- $isPage := and (eq .Page.Kind "page") (not .Page.BundleType) -}}
|
7 | 8 | {{- $startsWithSlash := hasPrefix $dest "/" -}}
|
8 | 9 | {{- $startsWithRelative := hasPrefix $dest "../" -}}
|
9 | 10 |
|
10 | 11 | {{- if and $dest $isLocal -}}
|
11 | 12 | {{- if $startsWithSlash -}}
|
12 |
| - {{/* Images under static directory */}} |
13 |
| - {{- $dest = (relURL (strings.TrimPrefix "/" $dest)) -}} |
| 13 | + {{- with or (.PageInner.Resources.Get $url.Path) (resources.Get $url.Path) -}} |
| 14 | + {{/* Images under assets directory */}} |
| 15 | + {{- $query := cond $url.RawQuery (printf "?%s" $url.RawQuery) "" -}} |
| 16 | + {{- $fragment := cond $url.Fragment (printf "?%s" $url.Fragment) "" -}} |
| 17 | + {{- $dest = printf "%s%s%s" .RelPermalink $query $fragment -}} |
| 18 | + {{- else -}} |
| 19 | + {{/* Images under static directory */}} |
| 20 | + {{- $dest = (relURL (strings.TrimPrefix "/" $dest)) -}} |
| 21 | + {{- end -}} |
14 | 22 | {{- else if and $isPage (not $startsWithRelative) -}}
|
15 | 23 | {{/* Images that are sibling to the individual page file */}}
|
16 | 24 | {{ $dest = (printf "../%s" $dest) }}
|
17 | 25 | {{- end -}}
|
18 | 26 | {{- end -}}
|
19 | 27 |
|
| 28 | +{{- $attributes := "" -}} |
| 29 | +{{- range $key, $value := .Attributes -}} |
| 30 | + {{- if $value -}} |
| 31 | + {{- $pair := printf "%s=%q" $key ($value | transform.HTMLEscape) -}} |
| 32 | + {{- $attributes = printf "%s %s" $attributes $pair -}} |
| 33 | + {{- end -}} |
| 34 | +{{- end -}} |
| 35 | + |
20 | 36 | {{- with .Title -}}
|
21 | 37 | <figure>
|
22 |
| - <img src="{{ $dest | safeURL }}" title="{{ . }}" alt="{{ $alt }}" {{ if $lazyLoading }}loading="lazy"{{ end }} /> |
| 38 | + <img src="{{ $dest | safeURL }}" title="{{ . }}" alt="{{ $alt }}" {{ $attributes | safeHTMLAttr }} {{ if $lazyLoading }}loading="lazy"{{ end }} /> |
23 | 39 | <figcaption>{{ . }}</figcaption>
|
24 | 40 | </figure>
|
25 | 41 | {{- else -}}
|
26 |
| - <img src="{{ $dest | safeURL }}" alt="{{ $alt }}" {{ if $lazyLoading }}loading="lazy"{{ end }} /> |
| 42 | + <img src="{{ $dest | safeURL }}" alt="{{ $alt }}" {{ $attributes | safeHTMLAttr }} {{ if $lazyLoading }}loading="lazy"{{ end }} /> |
27 | 43 | {{- end -}}
|
0 commit comments