-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathimagekit.html
More file actions
82 lines (74 loc) · 2.92 KB
/
imagekit.html
File metadata and controls
82 lines (74 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
{{/*
Copyright © 2022 - 2025 The Hinode Team / Mark Dumay. All rights reserved.
Use of this source code is governed by The MIT License (MIT) that can be found in the LICENSE file.
Visit gethinode.com/license for more details.
*/}}
{{ $error := false }}
{{ $anchorMap := dict
"TopLeft" "top_left"
"Top" "top"
"TopRight" "top_right"
"Left" "left"
"Center" "center"
"Right" "right"
"BottomLeft" "bottom_left"
"Bottom" "bottom"
"BottomRight" "bottom_right"
"Smart" "auto"
}}
{{/* Initialize arguments */}}
{{ $args := partial "utilities/InitArgs.html" (dict "structure" "image-adapter" "args" . "group" "partial") }}
{{ if or $args.err $args.warnmsg }}
{{ partial (cond $args.err "utilities/LogErr.html" "utilities/LogWarn.html") (dict
"partial" "assets/adapter/imagekit.html"
"warnid" "warn-invalid-arguments"
"msg" "Invalid arguments"
"details" ($args.errmsg | append $args.warnmsg)
"file" page.File
)}}
{{ $error = $args.err }}
{{ end }}
{{/* Initialize arguments */}}
{{ $file := or $args.urlFile $args.file }}
{{ if not $file }}
{{ partial "utilities/LogErr.html" (dict
"partial" "assets/adapter/imagekit.html"
"msg" "Invalid arguments"
"details" slice "argument 'url-file': expected value"
"file" page.File
)}}
{{ $error = true }}
{{ end }}
{{ $dir := or $args.urlDir $args.dir }}
{{ $host := or $args.urlHost $args.host }}
{{ $format := $args.format }}
{{ $transform := $args.transform }}
{{ $anchor := "" }}
{{ with $args.anchor }}{{ $anchor = index $anchorMap . }}{{ end }}
{{ if eq $transform "fill" }}{{ $transform = "c-maintain_ratio" }}{{ else }}{{ $transform = "c-at_max" }}{{ end }}
{{ $element := "" }}
{{ $height := or $args.imageHeight $args.height -}}
{{ $width := or $args.imageWidth $args.width -}}
{{/* Validate image format is supported */}}
{{ if and $format (not (in (slice "jpg" "jpeg" "webp" "avif" "png") $format)) }}
{{ warnf "Image format not supported by ImageKit.io: %s.%s" (path.BaseName $file) $format }}
{{ $format = "jpg" }}
{{ end }}
{{/* Split path between end point and image path */}}
{{ $newdir := printf "/%s" (index (split $dir "/") 1) }}
{{ $file = partial "utilities/URLJoin.html" (dict "elements" (slice (strings.TrimPrefix $newdir $dir) $file)) }}
{{ $dir = $newdir }}
{{/* Generate image URL */}}
{{ if not $error }}
{{ $operation := "" }}
{{ if $format }}
{{ $operation = printf "tr:f-%s,%s,h-%d,w-%d" $format $transform $height $width }}
{{ else }}
{{ $operation = printf "tr:f-auto,%s,h-%d,w-%d" $transform $height $width }}
{{ end }}
{{ with $anchor }}
{{ $operation = printf "%s,fo-%s" $operation . }}
{{ end }}
{{- $element = partial "utilities/URLJoin.html" (dict "elements" (slice "https://" $host $dir $operation $file)) -}}
{{ end }}
{{ return $element }}