From c5b26892a9c6edd861c86993fce1c4cc234b9408 Mon Sep 17 00:00:00 2001 From: George Cushen Date: Sun, 9 Feb 2020 00:53:02 +0000 Subject: [PATCH] feat: add support for SVG logo format + fix logo URL in JSONLD Can now upload an SVG logo to `assets/images/logo.svg`. If your site is for an organization, Google currently requires that the logo is PNG rather than SVG otherwise the site may not receive rich search results based on the JSONLD (see https://developers.google.com/search/docs/data-types/logo). Also, this commit fixes the logo URL in the site's JSONLD metadata - irrespective of the above feature, the logo URL metadata broke when adding the new automated logo detection system in #1439. Close #1512 Fix #1516 Fix #1439 --- layouts/partials/functions/get_logo.html | 19 ++++++++++++------- layouts/partials/functions/get_logo_url.html | 12 ++++++++++++ layouts/partials/jsonld/article.html | 4 ++-- layouts/partials/jsonld/business.html | 2 +- layouts/partials/navbar.html | 2 +- layouts/partials/site_head.html | 2 +- 6 files changed, 29 insertions(+), 12 deletions(-) create mode 100644 layouts/partials/functions/get_logo_url.html diff --git a/layouts/partials/functions/get_logo.html b/layouts/partials/functions/get_logo.html index 4bf6b8e6..b4e11c1d 100644 --- a/layouts/partials/functions/get_logo.html +++ b/layouts/partials/functions/get_logo.html @@ -2,15 +2,20 @@ {{/* Inputs: constraint: max_height/fit (str); size (int) */}} {{/* Output: resource (obj) */}} -{{ $logo := resources.Get "images/logo.png" }} +{{/* Workaround fact Hugo does not support GetMatch in assets dir */}} +{{/* Hugo doesn't support image ops on SVG: https://discourse.gohugo.io/t/ho-do-i-convert-a-generic-resource-to-image-resource/22570/4 */}} +{{ $logo := resources.Get "images/logo.png" | default (resources.Get "images/logo.svg") }} {{ $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))) }} +{{/* If the type of image can be resized by Hugo, resize it given a `.size` argument to the function. */}} +{{ if resources.Get "images/logo.png" }} + {{ 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 }} {{ end }} {{ return $logo_proc }} diff --git a/layouts/partials/functions/get_logo_url.html b/layouts/partials/functions/get_logo_url.html new file mode 100644 index 00000000..0f2c23d5 --- /dev/null +++ b/layouts/partials/functions/get_logo_url.html @@ -0,0 +1,12 @@ +{{/* Function to get logo URL for JSONLD. */}} +{{/* Inputs: page context */}} +{{/* Output: logo URL (URL) */}} + +{{ $logo_url := "" }} +{{ if resources.Get "images/logo.png" | or (resources.Get "images/logo.svg") }} + {{ $logo_url = (partial "functions/get_logo" (dict "constraint" "fit" "size" 192)).Permalink }} +{{ else }} + {{ $logo_url = (partial "functions/get_icon" 192).Permalink }} +{{ end }} + +{{ return $logo_url }} diff --git a/layouts/partials/jsonld/article.html b/layouts/partials/jsonld/article.html index b8e30710..33550bf0 100644 --- a/layouts/partials/jsonld/article.html +++ b/layouts/partials/jsonld/article.html @@ -1,4 +1,4 @@ -{{ $page := .page }} +{{- $page := .page }} {{ $summary := .summary }} {{ $featured_image := ($page.Resources.ByType "image").GetMatch "*featured*" }} @@ -10,7 +10,7 @@ {{ $author := partial "functions/get_author_name" $page }} {{ $publisher := site.Params.org_name | default site.Title }} -{{ $logo_url := (printf "img/%s" (site.Params.logo | absURL)) | default (partial "functions/get_icon" 512).Permalink }} +{{ $logo_url := partial "functions/get_logo_url" $page -}}