From 28c06ef8ae29d615b7c5c2ba6ebe81400b123a96 Mon Sep 17 00:00:00 2001 From: Jimmy Cai Date: Thu, 10 Sep 2020 18:53:36 +0200 Subject: [PATCH 1/8] feat(helper/image): new image helper (WIP) --- layouts/partials/article-list/compact.html | 18 ++++++---- layouts/partials/article-list/default.html | 29 +++++++++------- layouts/partials/article-list/tile.html | 23 ++++++++----- .../partials/article/components/details.html | 11 +++---- .../partials/article/components/header.html | 23 +++++++------ layouts/partials/head/opengraph.html | 12 ++++--- layouts/partials/helper/image.html | 33 +++++++++++++++++-- 7 files changed, 100 insertions(+), 49 deletions(-) diff --git a/layouts/partials/article-list/compact.html b/layouts/partials/article-list/compact.html index 3988b83..dfcc276 100644 --- a/layouts/partials/article-list/compact.html +++ b/layouts/partials/article-list/compact.html @@ -12,13 +12,17 @@ - {{ if .Params.image }} - {{- $image := partial "helper/image" . -}} - {{- $thumbnail := $image.Fill "120x120" -}} + {{ $image := partial "helper/image" . }} -
- -
+ {{ if $image.exists }} +
+ {{ if $image.local }} + {{- $thumbnail := $image.src.Fill "120x120" -}} + + {{ else }} + Featured image of post {{ .Title }} + {{ end }} +
{{ end }} \ No newline at end of file diff --git a/layouts/partials/article-list/default.html b/layouts/partials/article-list/default.html index 25fcbc2..45f00d1 100644 --- a/layouts/partials/article-list/default.html +++ b/layouts/partials/article-list/default.html @@ -1,17 +1,22 @@
- {{ if .Params.image }} - {{- $image := partial "helper/image" . -}} - {{- $thumbnail := $image.Fill "800x250" -}} - {{- $thumbnailRetina := $image.Fill "1600x500" -}} + {{ $image := partial "helper/image" . }} -
- - Featured image of post {{ .Title }} - -
+ {{ if $image.exists }} +
+ + {{ if $image.local }} + {{- $thumbnail := $image.src.Fill "800x250" -}} + {{- $thumbnailRetina := $image.src.Fill "1600x500" -}} + + Featured image of post {{ .Title }} + {{ else }} + Featured image of post {{ .Title }} + {{ end }} + +
{{ end }} {{ partial "article/components/details" . }} diff --git a/layouts/partials/article-list/tile.html b/layouts/partials/article-list/tile.html index 9a58d4b..6b4375a 100644 --- a/layouts/partials/article-list/tile.html +++ b/layouts/partials/article-list/tile.html @@ -1,12 +1,19 @@ -
+{{ $image := partial "helper/image" .context }} +
- {{ if .context.Params.image }} - {{- $image := partial "helper/image" .context | resources.Fingerprint "md5" -}} - {{- $thumbnail := $image.Fill .size -}} -
- -
+ + {{ if $image.exists }} +
+ {{ if $image.local }} + {{- $imageRaw := $image.src | resources.Fingerprint "md5" -}} + {{- $thumbnail := $imageRaw.Fill .size -}} + + + {{ else }} + + {{ end }} +
{{ end }}
diff --git a/layouts/partials/article/components/details.html b/layouts/partials/article/components/details.html index 4349d61..0e3ff50 100644 --- a/layouts/partials/article/components/details.html +++ b/layouts/partials/article/components/details.html @@ -1,15 +1,14 @@ -{{ $i := .Params.image }} +{{ $image := partial "helper/image" . }} {{ $context := . }} -
{{ with $categories := .Params.categories }}
- {{ if .Params.image }} - {{- $image := partial "helper/image" . -}} - {{- $thumbnail := $image.Fill "120x120" -}} + {{ $image := partial "helper/image" . }} + {{ if $image.exists }}
- + {{ if $image.local }} + {{- $thumbnail := $image.src.Fill "120x120" -}} + + {{ else }} + + {{ end }}
{{ end }}
From 3e18e165e0dbc10ea1e81c745a04ea7ae82c6e47 Mon Sep 17 00:00:00 2001 From: Jimmy Cai Date: Thu, 10 Sep 2020 23:24:19 +0200 Subject: [PATCH 3/8] feat(helper/image): better detection of external image By using strings.HasPrefix, it matches any images which name starts with http --- layouts/partials/helper/image.html | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/layouts/partials/helper/image.html b/layouts/partials/helper/image.html index 4136e67..c8371b4 100644 --- a/layouts/partials/helper/image.html +++ b/layouts/partials/helper/image.html @@ -4,7 +4,8 @@ {{ $result = merge $result (dict "exists" true) }} - {{ if strings.HasPrefix $imageField "http" }} + {{ $url := urls.Parse $imageField }} + {{ if or (eq $url.Scheme "http") (eq $url.Scheme "https") }} {{ $result = merge $result (dict "src" $imageField) }} {{ else }} From cfd4cdb731750786f787b7aaea6a9f411caa5af2 Mon Sep 17 00:00:00 2001 From: Jimmy Cai Date: Fri, 11 Sep 2020 16:01:01 +0200 Subject: [PATCH 4/8] feat(helper/image): new return format --- layouts/_default/term.html | 6 +++--- layouts/partials/article-list/compact.html | 6 +++--- layouts/partials/article-list/default.html | 13 ++++++------- layouts/partials/article-list/tile.html | 6 +++--- layouts/partials/article/components/details.html | 4 ++-- layouts/partials/article/components/header.html | 10 +++++----- layouts/partials/head/opengraph.html | 8 ++------ layouts/partials/helper/image.html | 12 ++++++------ 8 files changed, 30 insertions(+), 35 deletions(-) diff --git a/layouts/_default/term.html b/layouts/_default/term.html index d0f9471..4ca37da 100644 --- a/layouts/_default/term.html +++ b/layouts/_default/term.html @@ -18,12 +18,12 @@ {{ $image := partial "helper/image" . }} {{ if $image.exists }}
- {{ if $image.local }} - {{- $thumbnail := $image.src.Fill "120x120" -}} + {{ if $image.resource }} + {{- $thumbnail := $image.resource.Fill "120x120" -}} {{ else }} - + {{ end }}
{{ end }} diff --git a/layouts/partials/article-list/compact.html b/layouts/partials/article-list/compact.html index dfcc276..bebb573 100644 --- a/layouts/partials/article-list/compact.html +++ b/layouts/partials/article-list/compact.html @@ -16,12 +16,12 @@ {{ if $image.exists }}
- {{ if $image.local }} - {{- $thumbnail := $image.src.Fill "120x120" -}} + {{ if $image.resource }} + {{- $thumbnail := $image.resource.Fill "120x120" -}} {{ else }} - Featured image of post {{ .Title }} + Featured image of post {{ .Title }} {{ end }}
{{ end }} diff --git a/layouts/partials/article-list/default.html b/layouts/partials/article-list/default.html index 45f00d1..0beaeca 100644 --- a/layouts/partials/article-list/default.html +++ b/layouts/partials/article-list/default.html @@ -1,19 +1,18 @@ -
- {{ $image := partial "helper/image" . }} - +{{ $image := partial "helper/image" . }} +
{{ if $image.exists }}
- {{ if $image.local }} - {{- $thumbnail := $image.src.Fill "800x250" -}} - {{- $thumbnailRetina := $image.src.Fill "1600x500" -}} + {{ if $image.resource }} + {{- $thumbnail := $image.resource.Fill "800x250" -}} + {{- $thumbnailRetina := $image.resource.Fill "1600x500" -}} Featured image of post {{ .Title }} {{ else }} - Featured image of post {{ .Title }} + Featured image of post {{ .Title }} {{ end }}
diff --git a/layouts/partials/article-list/tile.html b/layouts/partials/article-list/tile.html index 6b4375a..a6a29a0 100644 --- a/layouts/partials/article-list/tile.html +++ b/layouts/partials/article-list/tile.html @@ -4,14 +4,14 @@ {{ if $image.exists }}
- {{ if $image.local }} - {{- $imageRaw := $image.src | resources.Fingerprint "md5" -}} + {{ if $image.resource }} + {{- $imageRaw := $image.resource | resources.Fingerprint "md5" -}} {{- $thumbnail := $imageRaw.Fill .size -}} {{ else }} - + {{ end }}
{{ end }} diff --git a/layouts/partials/article/components/details.html b/layouts/partials/article/components/details.html index 0e3ff50..9ba972c 100644 --- a/layouts/partials/article/components/details.html +++ b/layouts/partials/article/components/details.html @@ -4,8 +4,8 @@ {{ with $categories := .Params.categories }}