Skip to content

Commit ec97808

Browse files
authored
feat(opengraph): update the partial (#819)
* chore: update opengraph partial * docs: improve Open Grapth section
1 parent 334158a commit ec97808

File tree

2 files changed

+100
-23
lines changed

2 files changed

+100
-23
lines changed

exampleSite/content/docs/guide/configuration.md

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -456,19 +456,41 @@ The llms.txt file is automatically generated from your content structure and mak
456456

457457
### Open Graph
458458

459-
To add [Open Graph](https://ogp.me/) metadata to a page, add values in the frontmatter params.
459+
To add [Open Graph](https://ogp.me/) metadata, you can:
460+
- add values in the front-matter params of a page
461+
- or add values in the Hugo configuration file
460462

461463
As a page can have multiple `image` and `video` tags, place their values in an array.
462464
Other Open Graph properties can have only one value.
463-
For example, this page has an `og:image` tag (which configures an image to preview on social shares) and an `og:audio` tag.
464465

465-
```yaml {filename="content/docs/guide/configuration.md"}
466-
title: "Configuration"
466+
{{< tabs items="Page Level, Global Level" >}}
467+
{{< tab >}}
468+
469+
```md {filename="mypage.md"}
470+
---
471+
title: "My Page"
472+
params:
473+
images:
474+
- "/images/image01.jpg"
475+
audio: "podcast02.mp3"
476+
videos:
477+
- "video01.mp4"
478+
---
479+
480+
Page content.
481+
```
482+
{{< /tab >}}
483+
{{< tab >}}
484+
```yaml {filename="hugo.yaml"}
467485
params:
468486
images:
469-
- "/img/config-image.jpg"
470-
audio: "config-talk.mp3"
487+
- "/images/image01.jpg"
488+
audio: "podcast02.mp3"
489+
videos:
490+
- "video01.mp4"
471491
```
492+
{{< /tab >}}
493+
{{< /tabs >}}
472494

473495
### Banner
474496

layouts/_partials/opengraph.html

Lines changed: 72 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,86 @@
1-
{{/* From https://github.com/gohugoio/gohugoioTheme/blob/master/layouts/partials/opengraph/opengraph.html */}}
1+
{{/* Adapted from https://github.com/gohugoio/hugo/blob/v0.149.0/docs/layouts/_partials/opengraph/opengraph.html */}}
22

3-
<meta property="og:title" content="{{ .Title }}" />
4-
<meta property="og:description" content="{{ with .Description }}{{ . }}{{ else }}{{if .IsPage}}{{ .Summary }}{{ else }}{{ with .Site.Params.description }}{{ . }}{{ end }}{{ end }}{{ end }}" />
5-
<meta property="og:type" content="{{ if .IsPage }}article{{ else }}website{{ end }}" />
6-
<meta property="og:url" content="{{ .Permalink }}" />
3+
<meta property="og:title" content="{{ .Title }}">
4+
<meta
5+
property="og:description"
6+
content="{{ with .Description }}
7+
{{ . }}
8+
{{ else }}
9+
{{ if .IsPage }}
10+
{{ .Summary }}
11+
{{ else }}
12+
{{ with .Site.Params.description }}{{ . }}{{ end }}
13+
{{ end }}
14+
{{ end }}">
15+
<meta
16+
property="og:type"
17+
content="{{ if .IsPage }}
18+
article
19+
{{ else }}
20+
website
21+
{{ end }}">
22+
<meta property="og:url" content="{{ .Permalink }}">
723

824
{{- with $.Params.images -}}
9-
{{- range first 6 . }}<meta property="og:image" content="{{ . | absURL }}" />{{ end -}}
25+
{{- range first 6 . }}
26+
<meta property="og:image" content="{{ . | absURL }}">
27+
{{ end -}}
1028
{{- else -}}
1129
{{- $featured := "" }}
1230
{{- with $featured -}}
13-
<meta property="og:image" content="{{ $featured.Permalink }}"/>
31+
<meta property="og:image" content="{{ $featured.Permalink }}">
1432
{{- else -}}
15-
{{- with $.Site.Params.images }}<meta property="og:image" content="{{ index . 0 | absURL }}"/>{{ end -}}
33+
{{- with $.Site.Params.images }}
34+
<meta property="og:image" content="{{ index . 0 | absURL }}">
35+
{{ end -}}
1636
{{- end -}}
1737
{{- end -}}
1838

1939
{{- if .IsPage }}
2040
{{- $iso8601 := "2006-01-02T15:04:05-07:00" -}}
21-
<meta property="article:section" content="{{ .Section }}" />
22-
{{ with .PublishDate }}<meta property="article:published_time" {{ .Format $iso8601 | printf "content=%q" | safeHTMLAttr }} />{{ end }}
23-
{{ with .Lastmod }}<meta property="article:modified_time" {{ .Format $iso8601 | printf "content=%q" | safeHTMLAttr }} />{{ end }}
41+
<meta property="article:section" content="{{ .Section }}">
42+
{{ with .PublishDate }}
43+
<meta
44+
property="article:published_time"
45+
{{ .Format $iso8601 | printf "content=%q" | safeHTMLAttr }}>
46+
{{ end }}
47+
{{ with .Lastmod }}
48+
<meta
49+
property="article:modified_time"
50+
{{ .Format $iso8601 | printf "content=%q" | safeHTMLAttr }}>
51+
{{ end }}
2452
{{- end -}}
2553

26-
{{- with .Params.audio }}<meta property="og:audio" content="{{ . }}" />{{ end }}
27-
{{- with .Params.locale }}<meta property="og:locale" content="{{ . }}" />{{ end }}
28-
{{- with .Site.Params.title }}<meta property="og:site_name" content="{{ . }}" />{{ end }}
29-
{{- with .Params.videos }}{{- range . }}
30-
<meta property="og:video" content="{{ . | absURL }}" />
31-
{{ end }}{{ end }}
54+
{{- with .Params.audio }}<meta property="og:audio" content="{{ . }}">{{ end }}
55+
{{- with .Params.locale }}
56+
<meta property="og:locale" content="{{ . }}">
57+
{{ end }}
58+
{{- with .Site.Params.title }}
59+
<meta property="og:site_name" content="{{ . }}">
60+
{{ end }}
61+
{{- with .Params.videos }}
62+
{{- range . }}
63+
<meta property="og:video" content="{{ . | absURL }}">
64+
{{ end }}
65+
66+
{{ end }}
67+
68+
{{- /* If it is part of a series, link to related articles */}}
69+
{{- $permalink := .Permalink }}
70+
{{- $siteSeries := .Site.Taxonomies.series }}
71+
{{ with .Params.series }}
72+
{{- range $name := . }}
73+
{{- $series := index $siteSeries ($name | urlize) }}
74+
{{- range $page := first 6 $series.Pages }}
75+
{{- if ne $page.Permalink $permalink }}
76+
<meta property="og:see_also" content="{{ $page.Permalink }}">
77+
{{ end }}
78+
{{- end }}
79+
{{ end }}
80+
81+
{{ end }}
82+
83+
{{- /* Facebook Page Admin ID for Domain Insights */}}
84+
{{- with site.Params.social.facebook_admin }}
85+
<meta property="fb:admins" content="{{ . }}">
86+
{{ end }}

0 commit comments

Comments
 (0)