diff --git a/dev.toml b/dev.toml index 59abc42..df91d84 100644 --- a/dev.toml +++ b/dev.toml @@ -18,6 +18,9 @@ target = '$1' [module] + [[module.mounts]] + source = "assets" + target = "assets" [[module.mounts]] source = "hugo_stats.json" target = "assets/watching/hugo_stats.json" diff --git a/exampleSite/content/docs/guide/shortcodes/cards.md b/exampleSite/content/docs/guide/shortcodes/cards.md index f62ece9..9833ebf 100644 --- a/exampleSite/content/docs/guide/shortcodes/cards.md +++ b/exampleSite/content/docs/guide/shortcodes/cards.md @@ -12,8 +12,8 @@ linkTitle: Cards {{< cards >}} {{< 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/space.jpg" subtitle="Image under assets directory, processed by Hugo." method="Resize" options="600x q80 webp" >}} {{< /cards >}} ## Usage @@ -28,8 +28,8 @@ linkTitle: Cards ``` {{}} {{}} - {{}} {{}} + {{}} {{}} ``` @@ -52,7 +52,7 @@ Additionally, the card supports adding image and processing through these parame | `method` | Sets Hugo's image processing method. | | `options` | Configures Hugo's image processing options. | -Card supports three kinds of images: +Card supports three kinds of images: 1. Remote image: the full URL in the `image` parameter. 2. Static image: use the relative path in Hugo's `static/` directory. diff --git a/layouts/shortcodes/card.html b/layouts/shortcodes/card.html index 8d30b3d..562ec3f 100644 --- a/layouts/shortcodes/card.html +++ b/layouts/shortcodes/card.html @@ -1,27 +1,37 @@ +{{- $context := . -}} {{- $link := .Get "link" -}} {{- $title := .Get "title" -}} {{- $icon := .Get "icon" -}} {{- $subtitle := .Get "subtitle" -}} {{- $image := .Get "image" -}} +{{- $width := 0 -}} +{{- $height := 0 -}} + +{{/* Image processing options */}} {{- $method := .Get "method" | default "Resize" | humanize -}} {{- $options := .Get "options" | default "800x webp q80" -}} -{{- $context := . -}} - -{{/*- Adding asset support for images here, so that Hugo can do its image processing magic. -*/}} -{{/* Unfortunately we cannot pass .Resize/.Fit/.Fill as variables, so we're left with chaining IFs */}} - -{{- if not (urls.Parse $image).Scheme -}} +{{- if and $image (not (urls.Parse $image).Scheme) -}} + {{/* Process images in assets */}} {{- with resources.Get $image -}} + {{- $processed := "" -}} {{- if eq $method "Resize" -}} - {{- $image = (.Resize $options).RelPermalink -}} + {{- $processed = (.Resize $options) -}} {{- else if eq $method "Fit" -}} - {{- $image = (.Fit $options).RelPermalink -}} + {{- $processed = (.Fit $options) -}} {{- else if eq $method "Fill" -}} - {{- $image = (.Fill $options).RelPermalink -}} + {{- $processed = (.Fill $options) -}} {{- 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 -}} + {{- $width = $processed.Width -}} + {{- $height = $processed.Height -}} + {{- $image = $processed.RelPermalink -}} + {{- else -}} + {{/* Otherwise, use relative link of the image */}} + {{- $image = ($image | relURL) -}} {{- end -}} {{- end -}} @@ -33,6 +43,7 @@ {{- $external := strings.HasPrefix $link "http" -}} {{- $href := cond (strings.HasPrefix $link "/") ($link | relURL) $link -}} + {{- with $image -}} - {{ $title }} + {{ $title }} {{- end -}} {{- $padding := "p-4" -}}