Skip to content

Commit

Permalink
feat: add the json-ld module
Browse files Browse the repository at this point in the history
  • Loading branch information
razonyang committed Mar 5, 2024
1 parent 023d91d commit 31e3553
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 0 deletions.
3 changes: 3 additions & 0 deletions modules/json-ld/go.mod
@@ -0,0 +1,3 @@
module github.com/hugomods/seo/modules/json-ld

go 1.18
3 changes: 3 additions & 0 deletions modules/json-ld/hugo.toml
@@ -0,0 +1,3 @@
[params.hugopress.modules.seo-json-ld.hooks.head-begin]

[params.hugopress.modules.seo-json-ld.hooks.body-end]
@@ -0,0 +1 @@
{{- partial "seo/modules/json-ld/script" .Page -}}
@@ -0,0 +1 @@
{{- partial "seo/modules/json-ld/index" .Page -}}
@@ -0,0 +1,3 @@
{{- $page := .page }}
{{- $structure := partial (printf "seo/modules/json-ld/structures/%s" .structure) $page }}
{{- $page.Store.SetInMap "seo-json-ld" (index $structure "@type") $structure }}
10 changes: 10 additions & 0 deletions modules/json-ld/layouts/partials/seo/modules/json-ld/index.html
@@ -0,0 +1,10 @@
{{- if .IsHome }}
{{- partial "seo/modules/json-ld/append" (dict "page" . "structure" "website") }}
{{- end }}
{{- $params := default dict .Params.json_ld }}
{{- $structures := slice "news-article" }}
{{- range $structures }}
{{- if isset $params . }}
{{- partial "seo/modules/json-ld/append" (dict "page" . "structure" .) }}
{{- end }}
{{- end }}
@@ -0,0 +1,9 @@
{{- with .Store.Get "seo-json-ld" }}
{{- $json := . }}
{{- if eq (len .) 1 }}
{{- $json = jsonify (index (sort .) 0) }}
{{- else }}
{{- $json = jsonify . }}
{{- end }}
{{- $json | printf `<script type="application/ld+json">%s</script>` | safeHTML }}
{{- end -}}
@@ -0,0 +1,13 @@
{{- $s := newScratch }}
{{- $s.Set "@context" "https://schema.org" }}
{{- $s.Set "@type" "NewsArticle" }}
{{- $s.Set "headline" .Title }}
{{- $s.Set "datePublished" .Date }}
{{- with .Lastmod }}
{{- $s.Set "dateModified" . }}
{{- end }}
{{- range partial "_funcs/get-page-images" . }}
{{- $s.Add "image" (slice .Permalink) }}
{{- end }}
{{/* TODO: author */}}
{{- return $s.Values }}
@@ -0,0 +1,6 @@
{{- return dict
"@context" "https://schema.org"
"@type" "WebSite"
"name" (default site.Title site.Params.seo.json_ld.website.name)
"url" site.Home.Permalink
}}

0 comments on commit 31e3553

Please sign in to comment.