Переглянути джерело

feat: automatically detect logo image

Simplify the process of adding a logo to a site.

BREAKING CHANGE:
Removes param for logo from params.toml. Instead, move logo image to `assets/images/logo.png`, creating the folders if necessary.

See #1439
George Cushen 5 роки тому
батько
коміт
0cc80f90ee

+ 0 - 4
exampleSite/config/_default/params.toml

@@ -46,10 +46,6 @@ org_name = ""
 # Description for social sharing and search engines. If undefined, superuser role is used in place.
 description = ""
 
-# Display a logo in navigation bar rather than title (optional).
-#   To enable, place an image in `static/img/` and reference its filename below. To disable, set the value to "".
-logo = ""
-
 ############################
 ## Site Features
 ############################

+ 16 - 0
layouts/partials/functions/get_logo.html

@@ -0,0 +1,16 @@
+{{/* Function to get logo image. */}}
+{{/* Inputs: constraint: max_height/fit (str); size (int) */}}
+{{/* Output: resource (obj) */}}
+
+{{ $logo := resources.Get "images/logo.png" }}
+{{ $logo_proc := $logo }}
+
+{{ if eq .constraint "max_height" }}
+  {{/* Resize logo to fit specified max height. */}}
+  {{ $logo_proc = ($logo.Resize (printf "x%s" (string .size))) }}
+{{ else }}
+  {{/* Constrain logo to fit within specified dimensions. */}}
+  {{ $logo_proc = ($logo.Fit (printf "%sx%s" (string .size) (string .size))) }}
+{{ end }}
+
+{{ return $logo_proc }}

+ 4 - 2
layouts/partials/navbar.html

@@ -4,10 +4,12 @@
   <div class="container">
 
     {{if $show_logo}}
+    {{ $has_logo := fileExists "assets/images/logo.png" }}
     <div class="d-none d-lg-inline-flex">
       <a class="navbar-brand" href="{{ "/" | relLangURL }}">
-        {{- if site.Params.logo -}}
-          <img src="{{ printf "/img/%s" site.Params.logo | relURL }}" alt="{{ site.Title }}">
+        {{- if $has_logo -}}
+          {{ $logo := (partial "functions/get_logo" (dict "constraint" "max_height" "size" 70)) }}
+          <img src="{{ $logo.RelPermalink }}" alt="{{ site.Title }}">
         {{- else -}}
           {{- site.Title -}}
         {{- end -}}

+ 3 - 2
layouts/partials/site_head.html

@@ -148,6 +148,7 @@
   <link rel="canonical" href="{{ .Permalink }}">
 
   {{ $featured_image := (.Resources.ByType "image").GetMatch "*featured*" }}
+  {{ $has_logo := fileExists "assets/images/logo.png" }}
   {{ $og_image := "" }}
   {{ $twitter_card := "summary_large_image" }}
   {{ if $featured_image }}
@@ -156,8 +157,8 @@
     {{ $og_image = printf "img/%s" .Params.header.image | absURL }}
   {{ else if site.Params.sharing_image }}
     {{ $og_image = printf "img/%s" site.Params.sharing_image | absURL }}
-  {{ else if site.Params.logo }}
-    {{ $og_image = (printf "img/%s" site.Params.logo) | absURL }}
+  {{ else if $has_logo }}
+    {{ $og_image = (partial "functions/get_logo" (dict "constraint" "fit" "size" 300)).Permalink }}
     {{ $twitter_card = "summary" }}
   {{ else if site.Params.avatar }}
     {{ $og_image = (printf "img/%s" site.Params.avatar) | absURL }}