mirror of
https://github.com/CaiJimmy/hugo-theme-stack.git
synced 2024-11-27 04:03:48 +01:00
feat(helper/image): new way to set default image
This commit is contained in:
parent
cfd4cdb731
commit
a2662603df
9 changed files with 45 additions and 16 deletions
|
@ -37,9 +37,13 @@ DefaultContentLanguage = "en" # Theme i18n support
|
|||
[params.widgets.tagCloud]
|
||||
limit = 10
|
||||
[params.opengraph]
|
||||
defaultImage = ""
|
||||
[params.opengraph.twitter]
|
||||
site = ""
|
||||
[params.defaultImage]
|
||||
[params.defaultImage.opengraph]
|
||||
enabled = false
|
||||
local = false
|
||||
src = ""
|
||||
|
||||
[menu]
|
||||
[[menu.main]]
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
{{ end }}
|
||||
</div>
|
||||
|
||||
{{ $image := partial "helper/image" . }}
|
||||
{{ $image := partial "helper/image" (dict "Context" .) }}
|
||||
{{ if $image.exists }}
|
||||
<div class="taxonomy-image">
|
||||
{{ if $image.resource }}
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
</footer>
|
||||
</div>
|
||||
|
||||
{{ $image := partial "helper/image" . }}
|
||||
{{ $image := partial "helper/image" (dict "Context" .) }}
|
||||
|
||||
{{ if $image.exists }}
|
||||
<div class="article-image">
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
{{ $image := partial "helper/image" . }}
|
||||
{{ $image := partial "helper/image" (dict "Context" .) }}
|
||||
<article class="{{ if $image.exists }}has-image{{ end }}">
|
||||
{{ if $image.exists }}
|
||||
<div class="article-image">
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
{{ $image := partial "helper/image" .context }}
|
||||
{{ $image := partial "helper/image" (dict "Context" .context) }}
|
||||
<article class="{{ if $image.exists }}has-image{{ end }}">
|
||||
<a href="{{ .context.Permalink }}">
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
{{ $image := partial "helper/image" . }}
|
||||
{{ $image := partial "helper/image" (dict "Context" .) }}
|
||||
{{ $context := . }}
|
||||
<div class="article-details">
|
||||
{{ with $categories := .Params.categories }}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
<header class="article-header">
|
||||
{{ $image := partial "helper/image" . }}
|
||||
{{ $image := partial "helper/image" (dict "Context" .) }}
|
||||
|
||||
{{ if $image.exists }}
|
||||
<div class="article-image">
|
||||
|
|
|
@ -40,13 +40,9 @@
|
|||
{{- end -}}
|
||||
{{- end -}}
|
||||
|
||||
{{ $image := partial "helper/image" . }}
|
||||
{{ $image := partial "helper/image" (dict "Context" . "Type" "opengraph") }}
|
||||
{{- if $image.exists -}}
|
||||
<meta name="twitter:card" content="summary_large_image">
|
||||
<meta property='og:image' content='{{ $image.permalink }}' />
|
||||
<meta name="twitter:image" content='{{ $image.permalink }}' />
|
||||
{{- else if .Site.Params.opengraph.defaultImage -}}
|
||||
{{ $image := resources.Get .Site.Params.opengraph.defaultImage }}
|
||||
<meta property='og:image' content='{{ absURL $image.RelPermalink }}' />
|
||||
<meta name="twitter:image" content='{{ absURL $image.RelPermalink }}' />
|
||||
<meta property='og:image' content='{{ absURL $image.permalink }}' />
|
||||
<meta name="twitter:image" content='{{ absURL $image.permalink }}' />
|
||||
{{- end -}}
|
|
@ -1,5 +1,6 @@
|
|||
{{ $result := dict "exists" false "permalink" nil "resource" nil "isDefault" false }}
|
||||
{{ $imageField := .Params.image }}
|
||||
{{ $imageField := .Context.Params.image }}
|
||||
|
||||
{{ if $imageField }}
|
||||
<!-- If page has `image` field set -->
|
||||
{{ $result = merge $result (dict "exists" true) }}
|
||||
|
@ -9,7 +10,7 @@
|
|||
<!-- Is a external image -->
|
||||
{{ $result = merge $result (dict "permalink" $imageField) }}
|
||||
{{ else }}
|
||||
{{ $pageResourceImage := .Resources.GetMatch (printf "%s" ($imageField | safeURL)) }}
|
||||
{{ $pageResourceImage := .Context.Resources.GetMatch (printf "%s" ($imageField | safeURL)) }}
|
||||
{{ $siteResourceImage := resources.GetMatch (printf "%s" ($imageField | safeURL)) }}
|
||||
|
||||
{{ if $pageResourceImage }}
|
||||
|
@ -27,6 +28,34 @@
|
|||
{{ end }}
|
||||
{{ end }}
|
||||
|
||||
{{ else if and (ne .Type nil) (index .Context.Site.Params.defaultImage .Type) }}
|
||||
<!-- Type arg is set, check for defaultImage setting -->
|
||||
|
||||
{{ $defaultImageSetting := index .Context.Site.Params.defaultImage .Type }}
|
||||
|
||||
{{ if $defaultImageSetting.enabled }}
|
||||
{{ $result = merge $result (dict "isDefault" true) }}
|
||||
{{ $result = merge $result (dict "exists" true) }}
|
||||
|
||||
{{ if $defaultImageSetting.local }}
|
||||
{{ $siteResourceImage := resources.GetMatch (printf "%s" ($defaultImageSetting.src | safeURL)) }}
|
||||
|
||||
{{ if $siteResourceImage }}
|
||||
<!-- Try search image under site's assets folder -->
|
||||
{{ $result = merge $result (dict "permalink" $siteResourceImage.RelPermalink) }}
|
||||
{{ $result = merge $result (dict "resource" $siteResourceImage) }}
|
||||
{{ else }}
|
||||
<!-- Can not find the image -->
|
||||
{{ errorf "Failed loading image: %q" $defaultImageSetting.src }}
|
||||
{{ $result = merge $result (dict "exists" false) }}
|
||||
{{ end }}
|
||||
|
||||
{{ else }}
|
||||
|
||||
{{ $result = merge $result (dict "permalink" $defaultImageSetting.src) }}
|
||||
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
|
||||
{{ return $result }}
|
Loading…
Reference in a new issue