This is a very simple image render hook that ignores a lot of things like remote URLs, images in the assets
directory, title attribute, etc. It just demonstrates the resources.Copy
approach, nothing else.
markdown
![kitten](kitten.jpg)
layouts/_default/_markup/render-image.html
{{ with .Page.Resources.Get .Destination }}
{{ $targetDir := path.Dir .Key }}
{{ $targetBaseName := path.BaseName .Key }}
{{ $targetExt := path.Ext .Key }}
{{/* Do whatever image processing you need here. For example, resizing. */}}
{{ with $r := .Resize "300x" }}
{{ $targetPath := printf "%s/%s-%d%s" $targetDir $targetBaseName (.Key | crypto.FNV32a) $targetExt }}
{{ with resources.Copy $targetPath . }}
<img src="{{ .RelPermalink }}" width="{{ $r.Width }}" height="{{ $r.Height }}" alt="{{ $.PlainText }}">
{{ end }}
{{ end }}
{{ end }}
rendered HTML
<img src="/posts/post-1/kitten-302475903.jpg" width="300" height="200" alt="kitten">
published files
public/
├── posts/
│ ├── post-1/
│ │ ├── index.html
│ │ ├── kitten-302475903.jpg
│ │ └── kitten.jpg
│ └── index.html
├── favicon.ico
└── index.html