Commit Diff


commit - 4465ac43a29e475856275fd88b3b7d72f5574a00
commit + d71a84298a824064e37aa790ae24e3661952461f
blob - a3b026f5f7fa41ba7aeaf90a0a06e4958c193530
blob + f5480cd9368a68799d58cf807370418a0f8f85f6
--- README.md
+++ README.md
@@ -81,14 +81,22 @@ external_rel
 ### schema
 Подключает JSON-LD схемы для связанных объектов.
 
+Принимает в контексте либо страницу, либо dict с ключами page и schema.
+Схема - это строка или массив строк.
+
 Подключение в head:
 ```go-html-template
-{{- .Store.Set "schema" "Person" }}
 {{- partial "schema/json-ld.html" . | safeHTML }}
 ```
+или
+```go-html-template
+{{- partial "schema/json-ld.html" (dict "page" . "schema" "BreadcrumbList") | safeHTML }}
+```
 
-Если в Page Bundle есть файл <тип>.jsonld (например, Person.jsonld), он будет использован при генерации схемы.
+Если в Page Bundle есть файл <тип>.jsonld (например, Person.jsonld), он будет добавлен в список schema.
 
+Если по итогу для страницы список schema будет пустым, то будет отрендерен [встроенный](https://gohugo.io/templates/embedded/#schema) шаблон.
+
 #### Статья
 Для указания publisher в статье укажите publisher в Params страницы, либо в настройках сайта.
 
blob - a5491c970b9c47fc418d5f1e8c0d76bee4cbda60
blob + 13c25db16ba08213295d4ec8ffb21f6b9bd482cc
--- layouts/_partials/schema/Article.html
+++ layouts/_partials/schema/Article.html
@@ -1,19 +1,21 @@
-{{ $thing := partial "schema/Thing" . }}
-{{ $result := $thing }}
+{{ $iso := "2006-01-02T15:04:05Z0700" }}
 
-{{ $result = merge $result (dict
-  "mainEntityOfPage" (dict "@type" "WebPage" "@id" (printf "%s#WebPage" .Permalink))
-  "headline" .LinkTitle
-  "inLanguage" (or .Language.LanguageCode .Language.Lang)
-  "dateCreated" (.Date.Format "2006-01-02T15:04:05Z0700")
-  "dateModified" (.Lastmod.Format "2006-01-02T15:04:05Z0700")
-  "datePublished" (.PublishDate.Format "2006-01-02T15:04:05Z0700")
-  "copyrightYear" .PublishDate.Year
-  "wordCount" .WordCount
-) }}
+{{ $result := merge
+  (partial "schema/Thing" .)
+  (dict
+    "mainEntityOfPage" (dict "@type" "WebPage" "@id" (printf "%s#WebPage" .Permalink))
+    "headline" .LinkTitle
+    "inLanguage" (or .Language.LanguageCode .Language.Lang)
+    "dateCreated" (.Date.Format $iso)
+    "dateModified" (.Lastmod.Format $iso)
+    "datePublished" (.PublishDate.Format $iso)
+    "copyrightYear" .PublishDate.Year
+    "wordCount" .WordCount
+  )
+}}
 
 {{ with .ExpiryDate }}
-  {{ $result = merge $result (dict "expires" (.Format "2006-01-02T15:04:05Z0700")) }}
+  {{ $result = merge $result (dict "expires" (.Format $iso)) }}
 {{ end }}
 
 {{ with .Keywords }}
@@ -44,8 +46,7 @@
   {{ with .GetTerms "authors" }}
     {{ $authors := slice }}
     {{ range . }}
-      {{ .Store.Set "schema" "Person" }}
-      {{ $authors = $authors | append (partial "schema/load" .) }}
+      {{ $authors = $authors | append (partial "schema/load" (dict "page" . "schema" "Person")) }}
     {{ end }}
     {{ if eq (len $authors) 1 }}
       {{ $authors = index $authors 0 }}
@@ -55,8 +56,8 @@
 {{ end }}
 
 {{ if not (index $result "publisher") }}
-  {{ with (or .Params.publisher (index site.Params "radium" "publisher")) }}
-    {{ $result = merge $result (dict "publisher" (partial "schema/load" .)) }}
+  {{ with (or .Params.publisher site.Params.publisher) }}
+    {{ $result = merge $result (dict "publisher" (partial "schema/load" . )) }}
   {{ end }}
 {{ end }}
 
blob - eb5f3bafd60ec9152ba190b211bba6780ab57f74
blob + 616c87b50b2017a0f6386cd541a5858d398aa6f6
--- layouts/_partials/schema/BreadcrumbList.html
+++ layouts/_partials/schema/BreadcrumbList.html
@@ -1,4 +1,3 @@
-{{ $result := partial "schema/Thing" . }}
 {{ $breadcrumbs := slice }}
 {{ $position := 1 }}
 
@@ -8,7 +7,8 @@
     "position" $position
     "name" .Title
     "item" .Permalink
-  ) }}
+    )
+  }}
   {{ $position = add $position 1 }}
 {{ end }}
 
@@ -17,7 +17,10 @@
   "position" $position
   "name" .Title
   "item" .Permalink
-) }}
-{{ $result = merge $result (dict "itemListElement" $breadcrumbs) }}
+  )
+}}
 
-{{ return $result }}
+{{ return merge
+    (partial "schema/Thing" .)
+    (dict "itemListElement" $breadcrumbs)
+}}
blob - c802472c3e1842b880b5ab4bba11591ef81394a3
blob + fe526747efd14aa260f8ed6380f5d99a4f4fc21f
--- layouts/_partials/schema/Thing.html
+++ layouts/_partials/schema/Thing.html
@@ -1,33 +1,14 @@
-{{ $type := .Store.Get "schema" | default "Thing" }}
-{{ $processedParams := dict }}
-{{ with .Resources.Get (printf "%s.jsonld" $type) }}
-  {{ with . | transform.Unmarshal (dict "format" "json") }}
-    {{ range $key, $value := . }}
-      {{ if and
-        (reflect.IsMap $value)
-        (index $value "@id")
-        (eq (len $value) 1)
-        (findRE `^/.+#.+$` (index $value "@id"))
-      }}
-        {{ $processedParams = merge $processedParams (dict $key (partial "schema/load" (index $value "@id"))) }}
-      {{ else }}
-        {{ $processedParams = merge $processedParams (dict $key $value) }}
-      {{ end }}
-    {{ end }}
-  {{ end }}
-{{ end }}
-
-{{ $out := dict
-  "@type" $type
-  "@id" (printf "%s#%s" .Permalink $type)
+{{ $result := dict
+  "@type" "Thing"
+  "@id" (printf "%s#Thing" .Permalink)
   "url" .Permalink
-  "name" (or $processedParams.name .Title)
-  "description" (or $processedParams.description .Description .Summary | plainify)
+  "name" .Title
+  "description" (or .Description .Summary | plainify)
 }}
 
-{{ $images := or $processedParams.image (apply (partial "_funcs/get-page-images" . | first 6) "index" "." "Permalink") }}
+{{ $images := (apply (partial "_funcs/get-page-images" . | first 6) "index" "." "Permalink") }}
 {{ if gt (len $images) 0 }}
-  {{ $out = merge $out (dict "image" $images) }}
+  {{ $result = merge $result (dict "image" $images) }}
 {{ end }}
 
-{{ return merge $out $processedParams }}
+{{ return $result }}
blob - 517b5a05613e990ce80b9993b8bca9d414cc99d6
blob + 43a4a1c69c9620c9da3a02014274569d4fc5fd65
--- layouts/_partials/schema/json-ld.html
+++ layouts/_partials/schema/json-ld.html
@@ -1,9 +1,29 @@
-{{- if not (.Store.Get "schema") }}{{ .Store.Set "schema" "Thing" }}{{ end -}}
-{{- with (partial "schema/load" .) -}}
-{{- $.Store.Set ($.Store.Get "schema") . }}
+{{- $page := . }}
+{{- $schema := "" }}
+{{- if reflect.IsMap . }}
+  {{- $page = .page }}
+  {{- $schema = .schema }}
+{{- end }}
+{{- $schemas := slice }}
+
+{{- with $schema }}
+  {{- $schemas = cond (reflect.IsSlice .) . (slice .) }}
+{{- end }}
+
+{{- range $page.Resources.Match "*.jsonld" }}
+  {{- $schemas = $schemas | append (strings.TrimSuffix ".jsonld" .Name) }}
+{{- end }}
+
+{{- $results := slice }}
+{{- range uniq $schemas }}
+  {{- with (partial "schema/load" (dict "page" $page "schema" .)) }}
+    {{- $results = $results | append (merge (dict "@context" "http://schema.org") .) }}
+  {{- end }}
+{{- end }}
+{{- with $results }}
 <script type="application/ld+json">
-{{ (merge (dict "@context" "http://schema.org") . ) | jsonify (dict "indent" "  ") | safeJS }}
+{{ if eq (len .) 1 }}{{ index . 0 | jsonify (dict "indent" "  ") | safeJS }}{{ else }}{{ . | jsonify (dict "indent" "  ") | safeJS }}{{ end }}
 </script>
 {{- else }}
-{{- template "_internal/schema.html" . }}
+  {{ template "_internal/schema.html" $page }}
 {{- end }}
blob - 9924965a2cbf08e6bb6a436716d80f5434a1d1cc
blob + 92b34e5e6ea880248fa6441775ec09e20415cd9b
--- layouts/_partials/schema/load.html
+++ layouts/_partials/schema/load.html
@@ -1,17 +1,41 @@
-{{ $result := dict }}
 {{ $page := . }}
-{{ if eq (printf "%T" .) "string" }}
-    {{ $parts := split . "#" }}
-    {{ $page = site.GetPage (index $parts 0) }}
-    {{ if not $page }}
-        {{ errorf "No schema %s for page %s" . page.RelPermalink }}
-    {{ else }}
-        {{ $page.Store.Set "schema" (index $parts 1) }}
-    {{ end }}
+{{ $schema := "" }}
+{{ if reflect.IsMap . }}
+  {{ $page = .page }}
+  {{ $schema = .schema }}
+{{ else }}
+  {{ $parts := split $page "#" }}
+  {{ $page = site.GetPage (index $parts 0) }}
+  {{ if not $page }}
+    {{ errorf "No schema %s for page %s" (index $parts 1) (index $parts 0) }}
+  {{ else }}
+    {{ $schema = (index $parts 1) }}
+  {{ end }}
 {{ end }}
 
+{{ $result := dict }}
 {{ with $page }}
-    {{ $schema := .Store.Get "schema" }}
-    {{ $result = (partialCached (printf "schema/%s" $schema) . . $schema) }}
+  {{ $processedParams := dict }}
+  {{ with .Resources.Get (printf "%s.jsonld" $schema) }}
+    {{ with . | transform.Unmarshal (dict "format" "json") }}
+      {{ range $key, $value := . }}
+        {{ if and
+          (reflect.IsMap $value)
+          (index $value "@id")
+          (eq (len $value) 1)
+          (findRE `^/.+#.+$` (index $value "@id"))
+        }}
+          {{ $processedParams = merge $processedParams (dict $key (partial "schema/load" (index $value "@id"))) }}
+        {{ else }}
+          {{ $processedParams = merge $processedParams (dict $key $value) }}
+        {{ end }}
+      {{ end }}
+    {{ end }}
+  {{ end }}
+  {{ $result = merge
+    (partialCached (printf "schema/%s" $schema) . . $schema)
+    (dict "@type" $schema "@id" (printf "%s#%s" .Permalink $schema))
+    $processedParams
+  }}
 {{ end }}
 {{ return $result }}