-
-
Notifications
You must be signed in to change notification settings - Fork 2k
feat: add responsive image support #1283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1f5ec88
b222466
218f8da
8bee046
16df4ff
fc65337
542b829
8e0b538
9c8c2df
ce2d59c
9cb6ef4
b35add5
331f16f
c8354ae
9e2d807
2bed4cb
6b37520
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,8 +56,6 @@ | |
| width: max-content; | ||
|
|
||
| article { | ||
| width: 250px; | ||
| height: 150px; | ||
| margin-right: 20px; | ||
| flex-shrink: 0; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,33 @@ | ||||||
| {{- /* | ||||||
| Params: | ||||||
| Resource: Hugo image resource object (Optional, for processing) | ||||||
| Widths: Slice of widths to generate for srcset | ||||||
| Attributes: Map of HTML attributes for the <img> tag | ||||||
| */ -}} | ||||||
|
|
||||||
| {{- $resource := .Resource -}} | ||||||
| {{- $widths := .Widths -}} | ||||||
| {{- $attributes := .Attributes | default dict -}} | ||||||
|
|
||||||
| {{/* Generate srcset if Resource and Widths are provided */}} | ||||||
| {{- if and $widths $resource (reflect.IsImageResourceProcessable $resource) -}} | ||||||
| {{- $srcset := slice -}} | ||||||
| {{- range $widths -}} | ||||||
| {{- if lt . $resource.Width -}} | ||||||
| {{- $resized := $resource.Resize (printf "%dx" .) -}} | ||||||
| {{- $srcset = $srcset | append (printf "%s %dw" $resized.RelPermalink .) -}} | ||||||
| {{- end -}} | ||||||
| {{- end -}} | ||||||
|
|
||||||
| {{- if gt (len $srcset) 0 -}} | ||||||
| {{- $permalink := default $resource.RelPermalink (index $attributes "src") -}} | ||||||
|
||||||
| {{- $permalink := default $resource.RelPermalink (index $attributes "src") -}} | |
| {{- $permalink := $resource.RelPermalink -}} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| {{- /* | ||
| Params: | ||
| Resource: Hugo image resource object (Optional, for processing) | ||
| Width: Image width (Required) | ||
| Height: Image height (Required) | ||
| Resize: Whether to perform resize (Optional, default: false) | ||
| Attributes: Map of HTML attributes for the <img> tag | ||
| */ -}} | ||
|
|
||
| {{- $resource := .Resource -}} | ||
| {{- $width := .Width -}} | ||
| {{- $height := .Height -}} | ||
| {{- $resize := .Resize -}} | ||
| {{- $attributes := .Attributes | default dict -}} | ||
|
|
||
| {{- if and $resize $resource (reflect.IsImageResourceProcessable $resource) $width $height -}} | ||
| {{/* Create thumbnail with 1x/2x descriptors */}} | ||
| {{- $srcset := slice -}} | ||
| {{- range (slice 1 2) -}} | ||
| {{- $w := mul $width . -}} | ||
| {{- $h := mul $height . -}} | ||
| {{- if and (le $w $resource.Width) (le $h $resource.Height) -}} | ||
| {{- $resized := $resource.Fill (printf "%dx%d" $w $h) -}} | ||
| {{- $srcset = $srcset | append (printf "%s %dx" $resized.RelPermalink .) -}} | ||
|
|
||
| {{- if eq . 1 -}} | ||
| {{- $attributes = merge $attributes (dict "src" $resized.RelPermalink) -}} | ||
| {{- end -}} | ||
| {{- end -}} | ||
| {{- end -}} | ||
|
|
||
| {{- if gt (len $srcset) 0 -}} | ||
| {{- $attributes = merge $attributes (dict "width" $width "height" $height "srcset" (delimit $srcset ", ")) -}} | ||
| {{- end -}} | ||
| {{- end -}} | ||
|
|
||
| <img {{- range $k, $v := $attributes -}} | ||
| {{- if $v -}} | ||
| {{- printf " %s=%q" (lower $k) (printf "%v" $v) | safeHTMLAttr -}} | ||
| {{- end -}} | ||
| {{- end -}}> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
data-title-escapedattribute is no longer rendered on gallery images in the newresponsive-imagehelper, butgallery.tsstill reads this attribute (lines 77–78) to populate figcaptions viainnerHTML. Without it, the gallery caption falls back to thealtattribute text instead of the (possibly markdown-formatted) image title.In the old code, the
data-title-escapedattribute held the HTML-entity-escaped version of the markdownified title, which was required for safe assignment toinnerHTMLin JS. Now that neitherdata-title-escapednor any equivalent attribute is emitted, any image with a title in Markdown (e.g.) will silently lose its gallery caption.The fix would be to include a
"data-title-escaped"key in the attributes dict passed toresponsive-image, set to the HTML-escaped form of$titlewhen$titleis non-empty.