Skip to content

Commit

Permalink
Include schema.org for pages
Browse files Browse the repository at this point in the history
  • Loading branch information
cmahnke committed Jan 2, 2025
1 parent 056f006 commit 63153b9
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 4 deletions.
4 changes: 4 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,7 @@ enableRobotsTXT = true
[segments.manifests]
[[segments.manifests.includes]]
output = 'iiif-manifest'

[security]
[security.http]
mediaTypes = ['^application/json$']
1 change: 1 addition & 0 deletions config/_default/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
description = "Persönliche Homepage von Christian Mahnke"
debug = true
pagetheme = "#701611"
keywordAsString = true

[params.author]
email = '[email protected]'
Expand Down
47 changes: 47 additions & 0 deletions layouts/partials/data/functions/wikidata-label.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{{- $entityBase := "http://www.wikidata.org/entity/" -}}
{{- $defaultLangs := slice "en" "de" -}}

{{- $debug := partialCached "partials/functions/debug/enabled.html" . -}}

{{- $url := .url -}}

{{- $uri := "" -}}
{{- $id := "" -}}
{{- if hasPrefix $url "http://www.wikidata.org/entity/" -}}
{{- $id = replaceRE `http://www.wikidata.org/entity/` "" $url -}}
{{- $uri = $url -}}
{{- else if hasPrefix "" "https" -}}
{{- $id = replaceRE `https://www.wikidata.org/wiki/` "" $url -}}
{{- $uri = printf "%s%s" $entityBase $id -}}
{{- else -}}
{{- $id = $url -}}
{{- $uri = printf "%s%s" $entityBase $id -}}
{{- end -}}

{{- $data := dict -}}
{{- $jsonURL := printf "%s%s" (replace $uri "http://" "https://") ".json" -}}

{{- if $debug -}}
{{- warnf "[data/functions/wikidata-label.html] Getting label for %s from %s" $id $jsonURL -}}
{{- end -}}

{{- with resources.GetRemote $jsonURL (dict "headers" (dict "Accept" "application/json")) -}}
{{- with .Err -}}
{{ errorf "%s" . -}}
{{- else -}}
{{ $data = .Content | transform.Unmarshal -}}
{{- end -}}
{{- else -}}
{{ errorf "Unable to get remote resource %q" $jsonURL }}
{{- end -}}

{{- $entity := $data.entities.Get $id -}}

{{- $label := "" -}}
{{- range $defaultLangs -}}
{{- if isset $entity.labels.Get . -}}
{{- $label = ($entity.labels.Get .).label -}}
{{- end -}}
{{ end }}

{{- return $label -}}
8 changes: 6 additions & 2 deletions layouts/partials/data/schema.org.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
{{- $debug := partialCached "partials/functions/debug/enabled.html" . -}}

{{- if .IsHome -}}
{{- partial "schema.org/home.html" . -}}
{{- else -}}
{{- $schemaTemplate := printf "schema.org/%s.html" .Kind -}}
{{- if templates.Exists $schemaTemplate -}}
{{ partial $schemaTemplate . }}
{{- if templates.Exists (printf "partials/%s" $schemaTemplate) -}}
{{- partial $schemaTemplate . -}}
{{- else if $debug -}}
{{- warnf "[data/schema.org.html] Can't find %s" $schemaTemplate -}}
{{- end -}}
{{- end -}}
7 changes: 5 additions & 2 deletions layouts/partials/data/wikidata.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{{- $defaultRelation := "related" -}}
{{- $entityBase := "http://www.wikidata.org/entity/" -}}

{{- with .Params.wikidata -}}
{{- $entries := dict -}}
{{- if not (reflect.IsMap .) -}}
Expand All @@ -18,9 +20,10 @@
<ul class="wikidata">
<div class="wikidata-title">{{ i18n "WikidataEntries" }}</div>
{{- range $url, $relation := $entries -}}
{{ $id := replaceRE `https://www.wikidata.org/wiki/` "" $url }}
{{- $id := replaceRE `https://www.wikidata.org/wiki/` "" $url -}}
{{- $uri := printf "%s%s" $entityBase $id -}}
<li class="wikidata-entry">
<a href="{{ $url }}" title="{{ $id}}" rel="{{ $relation }}">{{ i18n "WikidataEntry" }}</a>
<a href="{{ $uri }}" title="{{ $id }}" rel="{{ $relation }}">{{ i18n "WikidataEntry" }}</a>
</li>
{{- end -}}
</ul>
Expand Down
95 changes: 95 additions & 0 deletions layouts/partials/schema.org/page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
{{- $defaultRelation := "related" -}}
{{- $entityBase := "http://www.wikidata.org/entity/" -}}
{{- $authorName := "" }}
{{- with site.Params.author }}
{{- if reflect.IsMap . }}
{{- with .name }}
{{- $authorName = . }}
{{- end }}
{{- else }}
{{- $authorName = . }}
{{- end }}
{{- end }}
<script type="application/ld+json">
{
"@context" : "http://schema.org",
"@type": "BlogPosting",
"@id": {{ .Permalink }},
"url": {{ .Permalink }},
"name": {{ .Title }},
"datePublished": {{ .Date }}
{{- with $.Description -}}
,"description": .
{{- end -}}
{{- if ne $authorName "" -}}
,"author": {
"@type": "Person",
"@id": {{ printf "%s%s" .Site.BaseURL "#Person" }},
"name": {{ $authorName }},
"url": {{ .Site.BaseURL }}
}
{{- end -}}
,"isPartOf": {
"@type" : "Blog",
"@id": {{ printf "%s%s" .Site.BaseURL "/post" }},
"name": {{ printf "%s %s" .Site.Title "Blog" }}
}
{{- $keywords := slice -}}
{{- with .Keywords -}}
{{- if not site.Params.keywordAsString -}}
{{- if not (reflect.IsSlice .) -}}
{{- $keywords = split . "," -}}
{{- else -}}
{{- $keywords = . -}}
{{- end -}}
{{- else -}}
{{- $keywords = split (delimit . " ") ", " -}}
{{- end -}}
{{- end -}}
{{- with .Params.tags -}}
{{- range $tag := . -}}
{{- if hasPrefix . "hidden_" -}}
{{- continue -}}
{{- end -}}
{{- if ne . "" -}}
{{- $keywords = $keywords | append . -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- $about := $keywords -}}
{{- with .Params.wikidata -}}
{{- $entries := dict -}}
{{- if not (reflect.IsMap .) -}}
{{- $list := slice -}}
{{- if not (reflect.IsSlice .) -}}
{{- $list = slice . -}}
{{- else -}}
{{- $list = . -}}
{{- end -}}
{{- range $list -}}
{{- $entries = merge $entries (dict . $defaultRelation) -}}
{{- end -}}
{{- else -}}
{{- $entries = . -}}
{{- end -}}

{{- range $url, $relation := $entries -}}
{{- $id := replaceRE `https://www.wikidata.org/wiki/` "" $url -}}
{{- $uri := printf "%s%s" $entityBase $id -}}
{{- $name := $id -}}
{{/* Fails due to https://github.com/gohugoio/hugo/issues/11601
{{- $name := partial "data/functions/wikidata-label.html" (dict "url" $id) -}}
*/}}
{{- $thing := dict "@type" "Thing" "@id" $uri "name" $name -}}
{{- $about = $about | append $thing -}}
{{- end -}}

{{- end -}}
{{- with $about -}}
, "about": {{ jsonify $about | safeJS }}
{{- end -}}
{{- with $keywords -}}
, "keywords": {{ jsonify $keywords | safeJS }}
{{- end -}}
}
</script>

0 comments on commit 63153b9

Please sign in to comment.