Skip to content

Commit d08b077

Browse files
authored
feat: make image render hook aware of assets directory (#657)
1 parent 1f88cff commit d08b077

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

Diff for: layouts/_default/_markup/render-image.html

+21-5
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,43 @@
11
{{- $alt := .PlainText | safeHTML -}}
22
{{- $lazyLoading := .Page.Site.Params.enableImageLazyLoading | default true -}}
33
{{- $dest := .Destination -}}
4+
{{- $url := urls.Parse $dest -}}
45

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

1011
{{- if and $dest $isLocal -}}
1112
{{- 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 -}}
1422
{{- else if and $isPage (not $startsWithRelative) -}}
1523
{{/* Images that are sibling to the individual page file */}}
1624
{{ $dest = (printf "../%s" $dest) }}
1725
{{- end -}}
1826
{{- end -}}
1927

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+
2036
{{- with .Title -}}
2137
<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 }} />
2339
<figcaption>{{ . }}</figcaption>
2440
</figure>
2541
{{- 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 }} />
2743
{{- end -}}

0 commit comments

Comments
 (0)