소스 검색

feat: add JSON-LD structured data for website, posts, and pubs

Forms part of the transition from Microdata to JSON-LD.

See #413
George Cushen 5 년 전
부모
커밋
2e01db268d
4개의 변경된 파일79개의 추가작업 그리고 0개의 파일을 삭제
  1. 49 0
      layouts/partials/jsonld/article.html
  2. 14 0
      layouts/partials/jsonld/main.html
  3. 14 0
      layouts/partials/jsonld/website.html
  4. 2 0
      layouts/partials/site_head.html

+ 49 - 0
layouts/partials/jsonld/article.html

@@ -0,0 +1,49 @@
+{{ $page := .page }}
+{{ $summary := .summary }}
+{{ $featured_image := ($page.Resources.ByType "image").GetMatch "*featured*" }}
+
+{{/* Get microdata type. */}}
+{{ $microdata_type := "Article" }}
+{{ if eq $page.Type "post" }}
+  {{ $microdata_type = "BlogPosting" }}
+{{ end }}
+
+{{/* Get author name. */}}
+{{ $author_username := "" }}
+{{ if and (not $page.Params.authors) ($page.Scratch.Get "superuser_username") }}
+  {{ $author_username = $page.Scratch.Get "superuser_username" }}
+{{ else if $page.Params.authors }}
+  {{ $author_username = index $page.Params.authors 0 }}
+{{ end }}
+{{ $author := "" }}
+{{ $taxonomy := "authors" }}
+{{ $profile_page := site.GetPage (printf "/%s/%s" $taxonomy $author_username) }}
+{{ with $profile_page }}
+  {{ $author = .Params.name | default .Title }}
+{{ end }}
+
+<script type="application/ld+json">
+  {
+    "@context": "https://schema.org",
+    "@type": "BlogPosting",
+    "mainEntityOfPage": {
+      "@type": "WebPage",
+      "@id": {{$page.Permalink}}
+    },
+    "headline": {{$page.Title}},
+    {{ if $featured_image }}
+    "image": [
+      {{$featured_image.Permalink}}
+    ],
+    {{end}}
+    "datePublished": {{ $page.PublishDate.Format "2006-01-02T15:04:05Z07:00" }},
+    "dateModified": {{ $page.Lastmod.Format "2006-01-02T15:04:05Z07:00" }},
+    {{with $author}}
+    "author": {
+      "@type": "Person",
+      "name": {{.}}
+    },
+    {{end}}
+    "description": {{$summary}}
+  }
+</script>

+ 14 - 0
layouts/partials/jsonld/main.html

@@ -0,0 +1,14 @@
+{{ $page := .page }}
+{{ $summary := .summary }}
+
+{{- if $page.IsHome -}}
+
+  {{ partial "jsonld/website.html" $page }}
+
+{{- else if $page.IsPage -}}
+
+  {{ if or (eq $page.Type "post") (eq $page.Type "publication") }}
+    {{ partial "jsonld/article.html" (dict "page" $page "summary" $summary) }}
+  {{ end }}
+
+{{- end }}

+ 14 - 0
layouts/partials/jsonld/website.html

@@ -0,0 +1,14 @@
+<script type="application/ld+json">
+{
+  "@context": "https://schema.org",
+  "@type": "WebSite",
+  {{- if site.Params.search.engine }}
+  "potentialAction": {
+    "@type": "SearchAction",
+    "target": {{ printf "%s?q={search_term_string}" site.BaseURL }},
+    "query-input": "required name=search_term_string"
+  },
+  {{end -}}
+  "url": {{site.BaseURL}}
+}
+</script>

+ 2 - 0
layouts/partials/site_head.html

@@ -189,6 +189,8 @@
     {{ if not .Date.IsZero }}<meta property="og:updated_time" content="{{ .Date.Format "2006-01-02T15:04:05-07:00" | safeHTML }}">{{ end }}
   {{ end }}
 
+  {{ partial "jsonld/main" (dict "page" . "summary" $desc) }}
+
   {{ partial "cookie_consent" . }}
 
   {{ partial "custom_head" . }}