Files
hextra/layouts/_markup/render-image.html
MioYi Sama a90429d991 fix(render-image): use page resources for relative images in i18n page bundles (#938)
* fix(layout): Images missing (404) in multilingual (i18n) Page Bundles due to Markdown render hook

* fix(layout): Changed to .PageInner based on PR review to support included shortcodes correctly

* refactor(render-image): simplify i18n page bundle resource lookup

Use single PathUnescape + TrimPrefix expression instead of six
Get attempts; keep same behavior for bundle images and fallback.

* refactor: simplify resource path handling in render-image.html

* Add comment for multilingual permalink handling

Added comment for multilingual permalink resolution in render-image.html

---------

Co-authored-by: Xin <5097752+imfing@users.noreply.github.com>
2026-02-23 22:42:09 +00:00

60 lines
2.5 KiB
HTML

{{- $alt := .PlainText | safeHTML -}}
{{- $lazyLoading := .Page.Site.Params.enableImageLazyLoading | default true -}}
{{- $enableImageZoom := .Page.Site.Params.imageZoom.enable | default false -}}
{{- if not (eq .Page.Params.imageZoom nil) -}}
{{- $enableImageZoom = .Page.Params.imageZoom -}}
{{- end -}}
{{- $dest := .Destination -}}
{{- $url := urls.Parse $dest -}}
{{- $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 -}}
{{- 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 -}}
{{- else -}}
{{/* Resolve page bundle resource for multilingual permalink */}}
{{- with .PageInner.Resources.Get (strings.TrimPrefix "./" $url.Path) -}}
{{- $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 -}}
{{- if and $isPage (not $startsWithRelative) -}}
{{ $dest = (printf "../%s" $dest) }}
{{- end -}}
{{- end -}}
{{- 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 -}}
{{- if $enableImageZoom -}}
{{- .Page.Store.Set "hasImageZoom" true -}}
{{- end -}}
{{- with .Title -}}
<figure>
<img src="{{ $dest | safeURL }}" title="{{ . }}" alt="{{ $alt }}" {{ $attributes | safeHTMLAttr }}{{ if $enableImageZoom }} data-zoomable{{ end }}{{ if $lazyLoading }} loading="lazy"{{ end }} />
<figcaption>{{ . }}</figcaption>
</figure>
{{- else -}}
<img src="{{ $dest | safeURL }}" alt="{{ $alt }}" {{ $attributes | safeHTMLAttr }}{{ if $enableImageZoom }} data-zoomable{{ end }}{{ if $lazyLoading }} loading="lazy"{{ end }} />
{{- end -}}