support for images in page bundles as relative paths

This commit is contained in:
Gabriele Musco 2022-10-24 08:02:44 +02:00
parent bedcf7a212
commit e4a10bab22
No known key found for this signature in database
GPG Key ID: 1068D795C80E51DE
7 changed files with 55 additions and 11 deletions

View File

@ -4,6 +4,14 @@
{{ $mscratch.Add "articles" slice }}
{{ $pages := where .Site.RegularPages "Params.nosearch" "!=" "true" }}
{{ range $pages }}
{{ $image := "" }}
{{ if .Params.Image }}
{{ if or (hasPrefix .Params.Image "/") (hasPrefix .Params.Image "http://") (hasPrefix .Params.Image "https://") }}
{{ $image = .Params.Image }}
{{ else }}
{{ $image = path.Join "/" (path.Join .File.Dir .Params.Image) }}
{{ end }}
{{ end }}
{{ $mscratch.Add "articles" (dict
"title" .Title
"date" (.Date.Format "2006-01-02")
@ -11,7 +19,7 @@
"summary" (or .Params.description .Summary)
"text" (lower .Plain)
"link" .Permalink
"image" (or .Params.Image "")) }}
"image" $image) }}
{{ end }}
"pages": {{ $mscratch.Get "articles" | jsonify }}
}

View File

@ -11,13 +11,21 @@
{{ $mscratch := newScratch }}
{{ $mscratch.Add "articles" slice }}
{{ range (.Paginate $postsList).Pages }}
{{ $image := "" }}
{{ if .Params.Image }}
{{ if or (hasPrefix .Params.Image "/") (hasPrefix .Params.Image "http://") (hasPrefix .Params.Image "https://") }}
{{ $image = .Params.Image }}
{{ else }}
{{ $image = path.Join (path.Join .File.Dir .Params.Image) }}
{{ end }}
{{ end }}
{{ $mscratch.Add "articles" (dict
"title" .Title
"date" (.Date.Format "2006-01-02")
"tags" (sort (or .Params.tags slice))
"summary" (or .Params.description .Summary)
"link" .Permalink
"image" (or .Params.Image "")) }}
"image" $image) }}
{{ end }}
"articles": {{ $mscratch.Get "articles" | jsonify }},
"test": "{{range $postsList}}{{.Title}} {{end}}",

View File

@ -17,7 +17,11 @@
{{ if .Site.Params.imageInArticlePreview }}
{{ if .Params.Image }}
<a class="unstyledLink" href="{{ .Permalink }}">
<img src="{{ .Params.Image }}" />
{{ if or (hasPrefix .Params.Image "/") (hasPrefix .Params.Image "http://") (hasPrefix .Params.Image "https://") }}
<img src="{{ .Params.Image }}" />
{{ else }}
<img src="/{{ path.Join .File.Dir .Params.Image }}" />
{{ end }}
</a>
{{ end }}
{{end}}

View File

@ -1,5 +1,13 @@
<div class="featured postlist gridView">
{{ range . }}
{{ $image := "" }}
{{ if .Params.Image }}
{{ if or (hasPrefix .Params.Image "/") (hasPrefix .Params.Image "http://") (hasPrefix .Params.Image "https://") }}
{{ $image = .Params.Image }}
{{ else }}
{{ $image = path.Join "/" (path.Join .File.Dir .Params.Image) }}
{{ end }}
{{ end }}
<a class="featuredCardLink" href="{{ .Permalink }}">
<article class="featuredCard"
style="
@ -7,7 +15,7 @@
rgba(0, 0, 0, 0) 0%,
rgba(0, 0, 0, 0.1) 45%,
rgba(0, 0, 0, 0.7) 100%
), url('{{ .Params.Image }}');
), url('{{ $image }}');
">
<div class="contentArea">
<h2>{{ .Title }}</h2>

View File

@ -15,10 +15,16 @@
{{ end }}
{{ end }}
<meta property="og:url" content="{{ .Permalink }}" />
{{ $image := .Site.Params.icon512 | default "/img/icon.svg" }}
{{ if .Params.Image }}
{{ if or (hasPrefix .Params.Image "/") (hasPrefix .Params.Image "http://") (hasPrefix .Params.Image "https://") }}
{{ $image = .Params.Image }}
{{ else }}
{{ $image = path.Join (path.Join .File.Dir .Params.Image) }}
{{ end }}
{{ end }}
<meta property="og:image"
content='{{ if .Params.Image }}{{ .Params.Image | absURL }}
{{ else }}{{ .Site.Params.icon512 | default "/img/icon.svg" | absURL }}
{{ end }}' />
content="{{ $image | absURL }}" />
{{ if .Params.description }}
<meta property="og:description" content="{{ .Params.description }}" />
{{ else }}

View File

@ -7,10 +7,16 @@
{{ if .IsPage }}
<meta name="twitter:title" content="{{ .Title }}" />
{{ $image := .Site.Params.icon512 | default "/img/icon.svg" }}
{{ if .Params.Image }}
{{ if or (hasPrefix .Params.Image "/") (hasPrefix .Params.Image "http://") (hasPrefix .Params.Image "https://") }}
{{ $image = .Params.Image }}
{{ else }}
{{ $image = path.Join (path.Join .File.Dir .Params.Image) }}
{{ end }}
{{ end }}
<meta name="twitter:image"
content='{{ if .Params.Image }}{{ .Params.Image | absURL }}
{{ else }}{{ .Site.Params.icon512 | default "/img/icon.svg" | absURL }}
{{ end }}' />
content="{{ $image | absURL }}" />
{{ if .Params.description }}
<meta name="twitter:description" content="{{ .Params.description }}" />
{{ else }}

View File

@ -14,7 +14,11 @@
{{ end }}
{{ if .Params.Image }}
<figure style="margin: 0">
<img src="{{ .Params.Image }}" alt="" />
{{ if or (hasPrefix .Params.Image "/") (hasPrefix .Params.Image "http://") (hasPrefix .Params.Image "https://") }}
<img src="{{ .Params.Image }}" />
{{ else }}
<img src="/{{ path.Join .File.Dir .Params.Image }}" />
{{ end }}
{{ if .Params.ImageCaption }}
<figcaption>
<p>{{ .Params.ImageCaption | markdownify }}</p>