Better social card logic

This commit is contained in:
C. Lente 2023-03-04 16:36:06 -03:00
parent 8b4ac7a925
commit 488da01892
No known key found for this signature in database
GPG Key ID: B0B503C26E05781B
8 changed files with 22 additions and 25 deletions

View File

@ -123,13 +123,17 @@ of the [demo](https://clente.github.io/hugo-bearcub/) for more information.
# Basic config
baseURL = "https://example.com"
theme = "hugo-bearcub"
author = "John Doe"
copyright = "John Doe (CC BY 4.0)"
defaultContentLanguage = "en"
# Generate a nice robots.txt for SEO
enableRobotsTXT = true
# Your name. For more information on why this must be a list, see
# https://discourse.gohugo.io/t/site-author-usage/31459/8
[author]
name = "John Doe"
# Setup syntax highlighting without inline styles. For more information about
# why you'd want to avoid inline styles, see
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/style-src#unsafe_inline_styles
@ -189,9 +193,9 @@ enableRobotsTXT = true
hideUntranslated = false
# (EXPERIMENTAL) This theme is capable of dynamically generating social cards
# for posts that don't have `images` defined in their front matter. By setting
# for posts that don't have `images` defined in their front matter; By setting
# `generateSocialCard` to false, you can prevent this behavior. For more
# information about images, see https://gohugo.io/functions/images/
# information see layouts/partials/seo_tags.html
generateSocialCard = true
# Social media. Delete any item you aren't using to make sure it won't show up

View File

@ -1,13 +1,17 @@
# Basic config
baseURL = "https://example.com"
theme = "hugo-bearcub"
author = "John Doe"
copyright = "John Doe (CC BY 4.0)"
defaultContentLanguage = "en"
# Generate a nice robots.txt for SEO
enableRobotsTXT = true
# Your name. For more information on why this must be a list, see
# https://discourse.gohugo.io/t/site-author-usage/31459/8
[author]
name = "John Doe"
# Setup syntax highlighting without inline styles. For more information about
# why you'd want to avoid inline styles, see
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/style-src#unsafe_inline_styles
@ -67,9 +71,9 @@ enableRobotsTXT = true
hideUntranslated = false
# (EXPERIMENTAL) This theme is capable of dynamically generating social cards
# for posts that don't have `images` defined in their front matter. By setting
# for posts that don't have `images` defined in their front matter; By setting
# `generateSocialCard` to false, you can prevent this behavior. For more
# information about images, see https://gohugo.io/functions/images/
# information see layouts/partials/seo_tags.html
generateSocialCard = true
# Social media. Delete any item you aren't using to make sure it won't show up

View File

@ -1,7 +1,6 @@
---
title: "Início"
menu: "main"
images: ["/hugo-bearcub/images/share.webp"]
weight: 1
---

View File

@ -1,6 +1,5 @@
---
title: "Blog"
menu: "main"
images: ["images/share.webp"]
weight: 2
---

View File

@ -1,7 +1,6 @@
---
title: "Home"
menu: "main"
images: ["/hugo-bearcub/images/share.webp"]
weight: 1
---

View File

@ -1,6 +1,5 @@
---
title: "Blog"
menu: "main"
images: ["/hugo-bearcub/images/share.webp"]
weight: 2
---

View File

@ -3,7 +3,11 @@
<meta name="description" content="{{ with .Description }}{{ . }}{{ else }}{{if .IsPage}}{{ .Summary }}{{ else }}{{ with .Site.Params.description }}{{ . }}{{ end }}{{ end }}{{ end }}" />
<meta name="keywords" content="{{ if .IsPage}}{{ range $index, $tag := .Params.tags }}{{ $tag }},{{ end }}{{ else }}{{ range $plural, $terms := .Site.Taxonomies }}{{ range $term, $val := $terms }}{{ printf "%s," $term }}{{ end }}{{ end }}{{ end }}" />
{{ if (default false .Site.Params.generateSocialCard) }}
<!-- Only generate social card if: -->
<!-- - generateSocialCard = true -->
<!-- - there aren't images set in frontmatter -->
<!-- - page is of .Kind "page" -->
{{ if and (and (default false .Site.Params.generateSocialCard) (not (isset .Params "images"))) (eq .Kind "page") }}
{{ partial "social_card.html" . }}
{{ else }}
<!-- Open Graph / Facebook -->

View File

@ -13,7 +13,8 @@
)) }}
{{ $date := .Date.Format (default "2006-01-02" .Site.Params.dateFormat) }}
{{ $byline := (printf "%s | %s" ($.Param "author") $date) }}
{{ $author := (default $.Site.Author.name ($.Param "author") ) }}
{{ $byline := (printf "%s | %s" $author $date) }}
{{ $fg = $fg.Filter (images.Text $byline (dict
"font" $font
@ -34,11 +35,7 @@
<meta property="og:type" content="{{ if .IsPage }}article{{ else }}website{{ end }}" />
<meta property="og:url" content="{{ .Permalink }}" />
{{- with .Params.images -}}
{{- range first 6 . }}<meta property="og:image" content="{{ . | absURL }}" />{{ end -}}
{{- else -}}
<meta property="og:image" content="{{ $card.Permalink | absURL }}"/>
{{- end -}}
{{- if .IsPage }}
{{- $iso8601 := "2006-01-02T15:04:05-07:00" -}}
@ -71,13 +68,9 @@
<!-- Twitter -->
<!-- Source: https://github.com/gohugoio/hugo/blob/master/tpl/tplimpl/embedded/templates/twitter_cards.html -->
{{- with .Params.images -}}
<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="{{ index . 0 | absURL }}"/>
{{ else -}}
<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="{{ $card.Permalink | absURL }}"/>
{{- end }}
<meta name="twitter:title" content="{{ .Title }}"/>
<meta name="twitter:description" content="{{ with .Description }}{{ . }}{{ else }}{{if .IsPage}}{{ .Summary }}{{ else }}{{ with .Site.Params.description }}{{ . }}{{ end }}{{ end }}{{ end -}}"/>
{{ with .Site.Social.twitter -}}
@ -95,11 +88,7 @@
{{ with .Lastmod }}<meta itemprop="dateModified" {{ .Format $iso8601 | printf "content=%q" | safeHTMLAttr }} />{{ end}}
<meta itemprop="wordCount" content="{{ .WordCount }}">
{{- with .Params.images -}}
{{- range first 6 . -}}<meta itemprop="image" content="{{ . | absURL }}">{{ end -}}
{{- else -}}
<meta itemprop="image" content="{{ $card.Permalink | absURL }}"/>
{{- end -}}
<!-- Output all taxonomies as schema.org keywords -->
<meta itemprop="keywords" content="{{ if .IsPage}}{{ range $index, $tag := .Params.tags }}{{ $tag }},{{ end }}{{ else }}{{ range $plural, $terms := .Site.Taxonomies }}{{ range $term, $val := $terms }}{{ printf "%s," $term }}{{ end }}{{ end }}{{ end }}" />