fix: card image not displaying correctly (#77)

* fix: use relative URL for card images

* chore: update card image processing

* chore: add width and height for process image

* docs: update cards docs
This commit is contained in:
Xin 2023-09-23 19:25:51 +01:00 committed by GitHub
parent 3632294706
commit fdc30c6cd5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 14 deletions

View File

@ -18,6 +18,9 @@
target = '$1' target = '$1'
[module] [module]
[[module.mounts]]
source = "assets"
target = "assets"
[[module.mounts]] [[module.mounts]]
source = "hugo_stats.json" source = "hugo_stats.json"
target = "assets/watching/hugo_stats.json" target = "assets/watching/hugo_stats.json"

View File

@ -12,8 +12,8 @@ linkTitle: Cards
{{< cards >}} {{< cards >}}
{{< card link="/" title="Image Card" image="https://source.unsplash.com/featured/800x600?landscape" subtitle="Unsplash Landscape" >}} {{< card link="/" title="Image Card" image="https://source.unsplash.com/featured/800x600?landscape" subtitle="Unsplash Landscape" >}}
{{< card link="/" title="Local Image" image="/images/space.jpg" subtitle="Image under assets directory, processed by Hugo." method="Resize" options="600x q80 webp" >}}
{{< card link="/" title="Local Image" image="/images/card-image-unprocessed.jpg" subtitle="Raw image under static directory." >}} {{< card link="/" title="Local Image" image="/images/card-image-unprocessed.jpg" subtitle="Raw image under static directory." >}}
{{< card link="/" title="Local Image" image="images/space.jpg" subtitle="Image under assets directory, processed by Hugo." method="Resize" options="600x q80 webp" >}}
{{< /cards >}} {{< /cards >}}
## Usage ## Usage
@ -28,8 +28,8 @@ linkTitle: Cards
``` ```
{{</* cards */>}} {{</* cards */>}}
{{</* card link="/" title="Image Card" image="https://source.unsplash.com/featured/800x600?landscape" subtitle="Unsplash Landscape" */>}} {{</* card link="/" title="Image Card" image="https://source.unsplash.com/featured/800x600?landscape" subtitle="Unsplash Landscape" */>}}
{{</* card link="/" title="Local Image" image="/images/space.jpg" subtitle="Image under assets directory, processed by Hugo." method="Resize" options="600x q80 webp" */>}}
{{</* card link="/" title="Local Image" image="/images/card-image-unprocessed.jpg" subtitle="Raw image under static directory." */>}} {{</* card link="/" title="Local Image" image="/images/card-image-unprocessed.jpg" subtitle="Raw image under static directory." */>}}
{{</* card link="/" title="Local Image" image="images/space.jpg" subtitle="Image under assets directory, processed by Hugo." method="Resize" options="600x q80 webp" */>}}
{{</* /cards */>}} {{</* /cards */>}}
``` ```

View File

@ -1,27 +1,37 @@
{{- $context := . -}}
{{- $link := .Get "link" -}} {{- $link := .Get "link" -}}
{{- $title := .Get "title" -}} {{- $title := .Get "title" -}}
{{- $icon := .Get "icon" -}} {{- $icon := .Get "icon" -}}
{{- $subtitle := .Get "subtitle" -}} {{- $subtitle := .Get "subtitle" -}}
{{- $image := .Get "image" -}} {{- $image := .Get "image" -}}
{{- $width := 0 -}}
{{- $height := 0 -}}
{{/* Image processing options */}}
{{- $method := .Get "method" | default "Resize" | humanize -}} {{- $method := .Get "method" | default "Resize" | humanize -}}
{{- $options := .Get "options" | default "800x webp q80" -}} {{- $options := .Get "options" | default "800x webp q80" -}}
{{- $context := . -}}
{{- if and $image (not (urls.Parse $image).Scheme) -}}
{{/*- Adding asset support for images here, so that Hugo can do its image processing magic. -*/}} {{/* Process images in assets */}}
{{/* Unfortunately we cannot pass .Resize/.Fit/.Fill as variables, so we're left with chaining IFs */}}
{{- if not (urls.Parse $image).Scheme -}}
{{- with resources.Get $image -}} {{- with resources.Get $image -}}
{{- $processed := "" -}}
{{- if eq $method "Resize" -}} {{- if eq $method "Resize" -}}
{{- $image = (.Resize $options).RelPermalink -}} {{- $processed = (.Resize $options) -}}
{{- else if eq $method "Fit" -}} {{- else if eq $method "Fit" -}}
{{- $image = (.Fit $options).RelPermalink -}} {{- $processed = (.Fit $options) -}}
{{- else if eq $method "Fill" -}} {{- else if eq $method "Fill" -}}
{{- $image = (.Fill $options).RelPermalink -}} {{- $processed = (.Fill $options) -}}
{{- else if eq $method "Crop" -}} {{- else if eq $method "Crop" -}}
{{- $image = (.Crop $options).RelPermalink -}} {{- $processed = (.Crop $options) -}}
{{- else -}}
{{- errorf "Invalid image processing command: Must be one of Crop, Fit, Fill or Resize." -}}
{{- end -}} {{- end -}}
{{- $width = $processed.Width -}}
{{- $height = $processed.Height -}}
{{- $image = $processed.RelPermalink -}}
{{- else -}}
{{/* Otherwise, use relative link of the image */}}
{{- $image = ($image | relURL) -}}
{{- end -}} {{- end -}}
{{- end -}} {{- end -}}
@ -33,6 +43,7 @@
{{- $external := strings.HasPrefix $link "http" -}} {{- $external := strings.HasPrefix $link "http" -}}
{{- $href := cond (strings.HasPrefix $link "/") ($link | relURL) $link -}} {{- $href := cond (strings.HasPrefix $link "/") ($link | relURL) $link -}}
<a <a
class="hextra-card group flex flex-col justify-start overflow-hidden rounded-lg border border-gray-200 text-current no-underline dark:shadow-none hover:shadow-gray-100 dark:hover:shadow-none shadow-gray-100 active:shadow-sm active:shadow-gray-200 transition-all duration-200 {{ $linkClass }}" class="hextra-card group flex flex-col justify-start overflow-hidden rounded-lg border border-gray-200 text-current no-underline dark:shadow-none hover:shadow-gray-100 dark:hover:shadow-none shadow-gray-100 active:shadow-sm active:shadow-gray-200 transition-all duration-200 {{ $linkClass }}"
href="{{ $href }}" href="{{ $href }}"
@ -41,7 +52,15 @@
{{- end -}} {{- end -}}
> >
{{- with $image -}} {{- with $image -}}
<img alt="{{ $title }}" loading="lazy" decoding="async" style="color: transparent;" src="{{ $image | safeURL }}" /> <img
alt="{{ $title }}"
loading="lazy"
decoding="async"
style="color: transparent;"
src="{{ $image | safeURL }}"
{{ with $width }}width="{{ . }}"{{ end }}
{{ with $height }}height="{{ . }}"{{ end }}
/>
{{- end -}} {{- end -}}
{{- $padding := "p-4" -}} {{- $padding := "p-4" -}}