mirror of
https://github.com/gcushen/hugo-academic.git
synced 2025-07-26 19:15:16 +02:00
feat: add JSON-LD structured data for website, posts, and pubs
Forms part of the transition from Microdata to JSON-LD. See #413
This commit is contained in:
parent
bc85b6800a
commit
2e01db268d
4 changed files with 79 additions and 0 deletions
49
layouts/partials/jsonld/article.html
Normal file
49
layouts/partials/jsonld/article.html
Normal file
|
@ -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
layouts/partials/jsonld/main.html
Normal file
14
layouts/partials/jsonld/main.html
Normal file
|
@ -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
layouts/partials/jsonld/website.html
Normal file
14
layouts/partials/jsonld/website.html
Normal file
|
@ -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>
|
|
@ -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" . }}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue