use first image in content as featured image using regex, hugo forum topic 52527
This commit is contained in:
@ -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">
|
||||
|
Reference in New Issue
Block a user