Skip to content

Conversation

@StarryReverie
Copy link
Contributor

The image render hooks resolves image URLs in Markdown. So far the implementation in this theme simply assumes that those images are placed in /static or relative to the referencing page, while those in /assets are ignored. Because Hugo lazily copies files in /assets only when they are explicitly referenced (such as shortcodes), images located in /assets and dependent on pages using ![...](...) syntax will not be published, and the corresponding links will actually point to nothing.

This PR updates the hook, which now first tries to resolve a image whose link starts with / to a global resource (i.e. /assets), and fallbacks to /static when the image doesn't exist in /assets.

In addition, when the markdown attributes feature is enabled, attributes like {.class #id} should be preserved if exists. This PR also adds this functionality to make markdown attributes work with the image render hook.

@netlify
Copy link

netlify bot commented Apr 3, 2025

Deploy Preview for hugo-hextra ready!

Name Link
🔨 Latest commit 9730e1c
🔍 Latest deploy log https://app.netlify.com/sites/hugo-hextra/deploys/67ee5bf1d83e2b0008e0a246
😎 Deploy Preview https://deploy-preview-657--hugo-hextra.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Owner

@imfing imfing left a comment

Choose a reason for hiding this comment

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

thanks, great work!

Comment on lines +28 to +42
{{- $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 }} />
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 🙌

Comment on lines +13 to +21
{{- 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 -}}
Copy link
Owner

Choose a reason for hiding this comment

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

@imfing imfing merged commit d08b077 into imfing:main Apr 3, 2025
4 checks passed
@StarryReverie StarryReverie deleted the feat-image-render-hook branch April 4, 2025 03:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants