use first image in content as featured image using regex, hugo forum topic 52527

This commit is contained in:
rra
2024-11-25 17:04:52 +01:00
parent 5fe7cacea3
commit 86c9df0c66
12 changed files with 49 additions and 2 deletions

View File

@ -8,7 +8,7 @@
<h2 class="p-name"><a href="{{ .Permalink }}" class="u-url">{{ .Title }}</a></h2>
</header>
{{ with (index (.Resources.ByType "image") 0) }}
{{ $height := add .Height 0.0}}
{{ $ratio := div $height .Width}}
@ -24,7 +24,17 @@
{{ .Summary }}
</div>
</div>
<small>this should show the first image in the content</small>
{{$first_image := ""}}
{{$matches := findRESubmatch `<img\s.*?src=(?:'|")([^'">]+)(?:'|")` .Content 1 }}
{{range $matches}}{{$first_image = index . 1}}{{end}}
{{with .Resources.Get $first_image}}
{{.ResourceType}}
{{.MediaType}}
<img src="{{ .Permalink }}" alt="">
{{/* uncomment below to crash the build*/}}
{{/* .Width */}}
{{end}}
<footer class='post-footer'>
<a href="{{ .RelPermalink }}">
<svg width="11" height="11" viewBox="0 0 11 11" xmlns="http://www.w3.org/2000/svg">