diff --git a/data/structures/image-adapter-rewrite.yml b/data/structures/image-adapter-rewrite.yml new file mode 100644 index 00000000..406cfaf1 --- /dev/null +++ b/data/structures/image-adapter-rewrite.yml @@ -0,0 +1,29 @@ +comment: >- + Rewrites an origin URL to a target URL as supported by a CDN image adapter. +arguments: + account: + type: string + optional: true + comment: Account name of the origin server. + container: + type: string + optional: true + comment: Container name of the origin server. + host: + type: string + optional: true + comment: >- + Host of an URL. For example, the host of the URL + 'https://example.com/first/second/third.webp' equals 'example.com'. + dir: + type: string + optional: true + comment: >- + All but the last element of an URL extension. For example, the dir of the + URL 'https://example.com/first/second/third.webp' equals '/first/second/'. + file: + type: string + optional: false + comment: >- + The last element of an URL extension. For example, the file of the + URL 'https://example.com/first/second/third.webp' equals 'third.webp'. diff --git a/layouts/partials/assets/adapters/imagekit-rewrite.html b/layouts/partials/assets/adapters/imagekit-rewrite.html new file mode 100644 index 00000000..963732d3 --- /dev/null +++ b/layouts/partials/assets/adapters/imagekit-rewrite.html @@ -0,0 +1,30 @@ + + +{{ $error := false }} + + +{{ if partial "utilities/IsInvalidArgs.html" (dict "structure" "image-adapter-rewrite" "args" . "group" "partial") }} + {{ errorf "partial [assets/adapter/imagekit-rewrite.html] - Invalid arguments" -}} + {{ $error = true }} +{{ end }} + + +{{ $account := .account }} +{{ $container := trim .container "/" }} +{{ $host := .host }} +{{ $dir := .dir }} +{{ $file := .file }} +{{ $element := "" }} +{{ with $container }}{{ $dir = strings.TrimPrefix (printf "/%s" .) $dir }}{{ end }} + +{{ if ne $host "ik.imagekit.io" }} + {{ $element = urls.JoinPath (slice "https://ik.imagekit.io" $account $dir $file) }} +{{ else }} + {{ $element = urls.JoinPath (slice "https://" $host $dir $file) }} +{{ end }} + +{{ return $element }} diff --git a/layouts/partials/assets/helpers/image-definition.html b/layouts/partials/assets/helpers/image-definition.html index 887e8ea9..5f19ca40 100644 --- a/layouts/partials/assets/helpers/image-definition.html +++ b/layouts/partials/assets/helpers/image-definition.html @@ -32,17 +32,28 @@ {{ $hook := "" }} +{{ $account := "" }} +{{ $container := "" }} +{{ $rewrite := false }} {{ range $provider, $val := site.Params.images }} {{ if not $hook }} {{ with index $val "host" }} {{ if (findRE . (urls.Parse $url).Hostname) }} {{ $hook = $provider }} + {{ with index $val "account" }}{{ $account = . }}{{ end }} + {{ with index $val "rewrite" }}{{ $rewrite = . }}{{ end }} + {{ with index $val "container" }}{{ $container = . }}{{ end }} {{ end }} {{ end }} {{ end }} {{ end }} {{ if not $hook }}{{ $hook = "hugo" }}{{ end }} + +{{ if $rewrite }} + {{ $url = partial "assets/helpers/image-rewrite.html" (dict "url" $url "account" $account "container" $container "hook" $hook) }} +{{ end }} + {{ $dims := slice }} {{- $res := "" -}} diff --git a/layouts/partials/assets/helpers/image-rewrite.html b/layouts/partials/assets/helpers/image-rewrite.html new file mode 100644 index 00000000..07f021c1 --- /dev/null +++ b/layouts/partials/assets/helpers/image-rewrite.html @@ -0,0 +1,29 @@ +{{- $url := .url -}} +{{- $account := .account -}} +{{- $container := .container -}} +{{ $hook := .hook }} + +{{ $host := (urls.Parse $url).Hostname }} +{{ $dir := (urls.Parse $url).Path }} +{{ $file := index ((split $dir "/") | collections.Reverse) 0 }} +{{ $dir = strings.TrimSuffix $file $dir }} +{{ $error := false }} + +{{ $adapter := (printf "assets/adapters/%s-rewrite.html" $hook) }} +{{ if not (fileExists (path.Join "/layouts/partials" $adapter)) }} + {{ warnf "Cannot find adapter: %s" (path.Join "/layouts/partials" $adapter) }} + {{ $error = true }} +{{ end }} + +{{ $result := $url }} +{{ if not $error }} + {{ $result = partial $adapter (dict + "account" $account + "container" $container + "host" $host + "dir" $dir + "file" $file + )}} +{{ end }} + +{{ return $result }} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index aff1a296..51f40f33 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@gethinode/hinode", - "version": "0.24.0-beta6", + "version": "0.24.0-beta7", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@gethinode/hinode", - "version": "0.24.0-beta6", + "version": "0.24.0-beta7", "license": "MIT", "devDependencies": { "@fullhuman/postcss-purgecss": "^6.0.0", diff --git a/package.json b/package.json index 2d959371..b897a266 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@gethinode/hinode", - "version": "0.24.0-beta6", + "version": "0.24.0-beta7", "description": "Hinode is a clean documentation and blog theme for Hugo, an open-source static site generator", "keywords": [ "hugo",