Skip to content

Instantly share code, notes, and snippets.

@zinefer
Last active April 21, 2019 04:32
Show Gist options
  • Save zinefer/f319ee126f0ed53eb0775f7eba63c5e9 to your computer and use it in GitHub Desktop.
Save zinefer/f319ee126f0ed53eb0775f7eba63c5e9 to your computer and use it in GitHub Desktop.
SEO partial for HUGO
{{- $title := .Site.Title }}
{{- if not .IsHome }}
{{- $title = .Title }}
{{- end }}
{{- $author := dict "@type" "Person" "name" .Site.Params.Author }}
{{- $logo := dict "@type" "ImageObject" "url" .Site.Params.PublisherLogo "width" 60 "height" 60 }}
{{- $publisher := dict "@type" "Organization" "name" .Site.Params.Publisher "logo" $logo }}
{{- with or (index (.Resources.Match "thumb.*") 0) (index (.Resources.ByType "image") 0) }}
{{- $image := .Permalink }}
{{- end }}
{{- $keywords := slice "Blog" }}
{{- if isset .Params "tags" }}
{{- $keywords = union .Params.tags $keywords }}
{{- end }}
{{- $scratch := newScratch }}
{{- $scratch.SetInMap "seo" "@context" "http://schema.org" }}
{{- $scratch.SetInMap "seo" "@type" "BlogPosting" }}
{{- $scratch.SetInMap "seo" "mainEntityOfPage" (dict "@type" "WebPage" "@id" .Site.BaseURL) }}
{{- $scratch.SetInMap "seo" "articleSection" .Section }}
{{- $scratch.SetInMap "seo" "name" $title }}
{{- $scratch.SetInMap "seo" "headline" $title }}
{{- $scratch.SetInMap "seo" "description" (.Description | default .Site.Params.Description) }}
{{- $scratch.SetInMap "seo" "inLanguage" .Site.LanguageCode }}
{{- $scratch.SetInMap "seo" "author" $author }}
{{- $scratch.SetInMap "seo" "creator" $author }}
{{- $scratch.SetInMap "seo" "accountablePerson" $author }}
{{- $scratch.SetInMap "seo" "copyrightHolder" $author }}
{{- $scratch.SetInMap "seo" "datePublished" .Date }}
{{- $scratch.SetInMap "seo" "dateModified" .Date }}
{{- $scratch.SetInMap "seo" "copyrightYear" (.Date.Format "2006") }}
{{- $scratch.SetInMap "seo" "publisher" $publisher }}
{{- with or (index (.Resources.Match "thumb.*") 0) (index (.Resources.ByType "image") 0) }}
{{- $scratch.SetInMap "seo" "image" .Permalink }}
{{- end }}
{{- $scratch.SetInMap "seo" "url" .Permalink }}
{{- $scratch.SetInMap "seo" "wordCount" .WordCount }}
{{- $scratch.SetInMap "seo" "keywords" $keywords }}
<script type="application/ld+json">{{ $scratch.Get "seo" | jsonify }}</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment