Add opengraph meta-data to doc site pages

master
Kaushal Modi 8 years ago
parent c9da3f45e0
commit e17a095ce2
  1. 7
      doc/ox-hugo-manual.org
  2. 4
      doc/themes/hugo-alabaster-theme/layouts/partials/head.html
  3. 92
      doc/themes/hugo-alabaster-theme/layouts/partials/opengraph.html
  4. 20
      doc/themes/hugo-alabaster-theme/layouts/partials/summary_minus_toc.html
  5. 40
      doc/themes/hugo-alabaster-theme/layouts/partials/twitter_cards.html

@ -1811,9 +1811,10 @@ for writing your posts in Org, there are examples for both in the
:PROPERTIES:
:EXPORT_FILE_NAME: replace-front-matter-keys
:END:
This is a =ox-hugo=-unique feature that allows you to
replace/substitute any front-matter key name to accommodate for your
favorite Hugo theme.
This is a =ox-hugo=-unique feature that allows you to replace any
front-matter key name to accommodate for your favorite Hugo theme.
#+hugo: more
The syntax is:

@ -38,4 +38,8 @@
{{ with .Params.mathjax }}
{{ partial "mathjax.html" }}
{{ end }}
<!-- Opengraph and Twitter -->
{{ partial "opengraph.html" . }}
{{ partial "twitter_cards.html" . }}
</head>

@ -0,0 +1,92 @@
{{ $permalink := .Permalink }}
<meta property="og:title" content="{{ .Title }}" />
<meta property="og:description"
content="{{ with .Description }}{{ . | plainify }}{{ else }}{{ if .IsPage }}{{ partial "summary_minus_toc.html" . | plainify }}{{ else }}{{ with .Site.Params.description }}{{ . | plainify }}{{ end }}{{ end }}{{ end }}" />
<meta property="og:type" content="{{ if .IsPage }}article{{ else }}website{{ end }}" />
<meta property="og:url" content="{{ .Permalink }}" />
{{ with .Params.images }}
{{ range first 6 . }}
{{ $image := . }}
{{ $image_link_absolute := (findRE "^/" $image) }}
{{ if $image_link_absolute }}
<meta name="og:image" content="{{ $image | absURL }}"/>
{{ else }}
<meta name="og:image" content="{{ (printf "%s%s" $permalink $image) }}"/>
{{ end }}
{{ end }}
{{ end }}
{{ if .IsPage }}
{{ if not .PublishDate.IsZero }}
<meta property="article:published_time" content="{{ .PublishDate.Format "2006-01-02T15:04:05-07:00" | safeHTML }}"/>
{{ else if not .Date.IsZero }}
<meta property="article:published_time" content="{{ .Date.Format "2006-01-02T15:04:05-07:00" | safeHTML }}"/>
{{ end }}
{{ if not .Lastmod.IsZero }}
<meta property="article:modified_time" content="{{ .Lastmod.Format "2006-01-02T15:04:05-07:00" | safeHTML }}"/>
{{ end }}
{{ else }}
{{ if not .Date.IsZero }}
<meta property="og:updated_time" content="{{ .Date.Format "2006-01-02T15:04:05-07:00" | safeHTML }}"/>
{{ end }}
{{ end }}
{{ with .Params.audio }}
{{ $audio := . }}
{{ $audio_link_absolute := (findRE "^/" $audio) }}
{{ if $audio_link_absolute }}
<meta name="og:audio" content="{{ $audio | absURL }}"/>
{{ else }}
<meta name="og:audio" content="{{ (printf "%s%s" $permalink $audio) }}"/>
{{ end }}
{{ end }}
{{ with .Params.locale }}
<meta property="og:locale" content="{{ . }}" />
{{ end }}
{{ with .Site.Params.title }}
<meta property="og:site_name" content="{{ . }}" />
{{ end }}
{{ with .Params.videos }}
{{ range . }}
{{ $video := . }}
{{ $video_link_absolute := (findRE "^/" $video) }}
{{ if $video_link_absolute }}
<meta name="og:video" content="{{ $video | absURL }}"/>
{{ else }}
<meta name="og:video" content="{{ (printf "%s%s" $permalink $video) }}"/>
{{ end }}
{{ end }}
{{ end }}
<!-- If it is part of a series, link to related articles -->
{{ $siteSeries := .Site.Taxonomies.series }}
{{ if $siteSeries }}
{{ with .Params.series }}
{{ range $name := . }}
{{ $series := index $siteSeries $name }}
{{ range $page := first 6 $series.Pages }}
{{ if ne $page.Permalink $permalink }}
<meta property="og:see_also" content="{{ $page.Permalink }}" />
{{ end }}
{{ end }}
{{ end }}
{{ end }}
{{ end }}
{{ if .IsPage }}
{{ range .Site.Authors }}
{{ with .Social.facebook }}
<meta property="article:author" content="https://www.facebook.com/{{ . }}" />
{{ end }}
{{ with .Site.Social.facebook }}
<meta property="article:publisher" content="https://www.facebook.com/{{ . }}" />
{{ end }}
<meta property="article:section" content="{{ .Section }}" />
{{ with .Params.tags }}
{{ range first 6 . }}
<meta property="article:tag" content="{{ . }}" />
{{ end }}
{{ end }}
{{ end }}
{{ end }}
<!-- Facebook Page Admin ID for Domain Insights -->
{{ with .Site.Social.facebook_admin }}
<meta property="fb:admins" content="{{ . }}" />
{{ end }}

@ -0,0 +1,20 @@
{{- $summary_has_org_toc := substr .Content 0 30 | findRE "[\".]ox-hugo-toc" -}}
{{- if $summary_has_org_toc -}}
{{- $content_splits := split .RawContent "<!--endtoc-->" -}} <!-- Need to use .RawContent as we will be parsing for 'more' comment later. -->
<!-- If Org TOC is present, the special comment endtoc would also be present.
In that case, output only the part *after* that comment as Summary. -->
{{- $summary_raw := index $content_splits 1 -}}
{{- $summary_splits := split $summary_raw "<!--more-->" -}}
{{- if eq (len $summary_splits) 2 -}}
{{- index $summary_splits 0 | markdownify -}}
{{- else -}}
{{- $summary_raw | markdownify | truncate 300 -}}
{{- end -}}
<!-- Print the whole Description/Summary if endtoc special comment is not found. -->
{{- else -}}
{{- with .Description -}}
{{- . | markdownify | printf "<p>%s</p>" | safeHTML -}}
{{- else -}}
{{- .Summary | printf "<p>%s</p>" | safeHTML -}}
{{- end -}}
{{- end -}}

@ -0,0 +1,40 @@
{{ $permalink := .Permalink }}
{{- with $.Params.images -}}
<meta name="twitter:card" content="summary_large_image"/>
<!-- As per https://developer.twitter.com/en/docs/tweets/optimize-with-cards/overview/summary-card-with-large-image,
the required card property is "twitter:image", not "twitter:image:src". -->
{{- $image := (index . 0) -}}
{{- $image_link_absolute := (findRE "^/" $image) -}}
{{- if $image_link_absolute -}}
<meta name="twitter:image" content="{{- $image | absURL -}}"/>
{{- else -}}
<meta name="twitter:image" content="{{- (printf "%s%s" $permalink $image) -}}"/>
{{- end -}}
{{ else -}}
{{- $images := $.Resources.ByType "image" -}}
{{- $featured := $images.GetMatch "*feature*" -}}
{{- $featured := cond (ne $featured nil) $featured ($images.GetMatch "{*cover*,*thumbnail*}") -}}
{{- with $featured -}}
<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="{{ $featured.Permalink }}"/>
{{- else -}}
{{- with $.Site.Params.images -}}
<meta name="twitter:card" content="summary_large_image"/>
{{- $image := (index . 0) -}}
{{- $image_link_absolute := (findRE "^/" $image) -}}
{{- if $image_link_absolute -}}
<meta name="twitter:image" content="{{- $image | absURL -}}"/>
{{- else -}}
<meta name="twitter:image" content="{{- (printf "%s%s" $permalink $image) -}}"/>
{{- end -}}
{{ else -}}
<meta name="twitter:card" content="summary"/>
{{- end -}}
{{- end -}}
{{- end }}
<meta name="twitter:title" content="{{ .Title }}"/>
<meta name="twitter:description" content="{{ with .Description }}{{ . | plainify }}{{ else }}{{ if .IsPage }}{{ partial "summary_minus_toc.html" . | plainify }}{{ else }}{{ with .Site.Params.description }}{{ . | plainify }}{{ end }}{{ end }}{{ end -}}"/>
{{ with .Site.Social.twitter -}}
<meta name="twitter:site" content="@{{ . }}"/>
<meta name="twitter:creator" content="@{{ . }}"/>
{{ end -}}
Loading…
Cancel
Save