Skip to content

Commit

Permalink
Merge pull request #943 from gethinode/image
Browse files Browse the repository at this point in the history
Refine CDN image handling
  • Loading branch information
markdumay authored May 20, 2024
2 parents 0660a4c + adbff26 commit 2433fa8
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 13 deletions.
4 changes: 2 additions & 2 deletions data/dimensions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
- 992x661
- 1200x800
- 1400x933
- 2800x1866
- 2800x1867
- ratio: 1x1
dimensions:
- 576x576
Expand All @@ -29,7 +29,7 @@
- 992x558
- 1200x675
- 1400x788
- 2800x1576
- 2800x1575
- ratio: 21x9
dimensions:
- 576x247
Expand Down
8 changes: 8 additions & 0 deletions exampleSite/config/_default/params.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@
[messages]
placement = "bottom-right"

[dam]
dimensions = "dimensions.yml"

[[dam.providers]]
name = "Cloudinary"
pattern = "cloudinary"
adapter = "assets/adapters/cloudinary.html"

[sharing]
enabled = true
sort = "weight"
Expand Down
2 changes: 1 addition & 1 deletion layouts/partials/assets/adapters/cloudinary.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
{{ $operation := "" }}
{{ if $format }}
{{ $operation = printf "%s,h_%d,w_%d" $transform $height $width }}
{{ $file = printf "%s.%s" (path.BaseName $file) $format }}
{{ $file = printf "%s.%s" (strings.TrimSuffix (path.Ext $file) $file) $format }}
{{ else }}
{{ $operation = printf "f_auto,%s,h_%d,w_%d" $transform $height $width }}
{{ end }}
Expand Down
20 changes: 16 additions & 4 deletions layouts/partials/assets/helpers/GetDimension.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,23 @@
-->

{{ $ratio := .ratio }}

{{ $dim := "" }}
{{ $matches := first 1 (where site.Data.dimensions "ratio" $ratio) }}
{{ if eq ($matches | len) 1 }}
{{ $dim = (index $matches 0).dimensions }}
{{ $default := dict "4x3" "1400x1050" "3x2" "1400x933" "1x1" "1400x1400" "16x9" "1400x788" "21x9" "1400x600" "auto" "1400" }}

<!-- Initialize dimensions data - stripping unsupported file extension -->
{{ $config := "dimensions" }}
{{ with index site.Params "dam" }}{{ with index . "dimensions" }}{{ $config = . }}{{ end }}{{ end }}
{{ $config = path.Join (path.Dir $config) (path.BaseName $config) }}

{{ with index site.Data $config }}
{{ $matches := first 1 (where . "ratio" $ratio) }}
{{ if eq ($matches | len) 1 }}
{{ $dim = (index $matches 0).dimensions }}
{{ end }}
{{ end }}

{{ if not $dim }}
{{ $dim = slice (index $default $ratio) }}
{{ end }}

{{ return $dim }}
8 changes: 5 additions & 3 deletions layouts/partials/assets/helpers/image-definition.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@
<!-- Derive image width and height -->
{{ $width := "" }}
{{ $height := "" }}
{{ range $dim := ($dims | last 1) }}
{{ $width = (int (index (split $dim "x") 0)) }}
{{ $height = (int (index (split $dim "x") 1)) }}
{{ with $dims }}
{{ range $dim := (. | last 1) }}
{{ $width = (int (index (split $dim "x") 0)) }}
{{ $height = (int (index (split $dim "x") 1)) }}
{{ end }}
{{ end }}

<!-- Generate image urls -->
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gethinode/hinode",
"version": "0.24.0-alpha2",
"version": "0.24.0-alpha3",
"description": "Hinode is a clean documentation and blog theme for Hugo, an open-source static site generator",
"keywords": [
"hugo",
Expand Down

0 comments on commit 2433fa8

Please sign in to comment.