Skip to content

Commit

Permalink
feat(json-ld): add support for FAQs structure (#27)
Browse files Browse the repository at this point in the history
fix(json-ld): correct page context and transform structure template name
  • Loading branch information
razonyang committed Mar 6, 2024
1 parent d13472d commit 9091f8c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
{{- partial "seo/modules/json-ld/append" (dict "page" . "structure" "website") }}
{{- end }}
{{- $params := default dict .Params.json_ld }}
{{- $structures := slice "news-article" }}
{{- $structures := slice "news_article" "faq_page" }}
{{- $page := . }}
{{- range $structures }}
{{- if isset $params . }}
{{- partial "seo/modules/json-ld/append" (dict "page" . "structure" .) }}
{{- partial "seo/modules/json-ld/append" (dict "page" $page "structure" (replace . "_" "-")) }}
{{- end }}
{{- end }}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{{- with .Store.Get "seo-json-ld" }}
{{- $json := . }}
{{- $structures := sort . }}
{{- $json := "" }}
{{- if eq (len .) 1 }}
{{- $json = jsonify (index (sort .) 0) }}
{{- $json = jsonify (index $structures 0) }}
{{- else }}
{{- $json = jsonify . }}
{{- $json = jsonify $structures }}
{{- end }}
{{- $json | printf `<script type="application/ld+json">%s</script>` | safeHTML }}
{{- end -}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{{- $s := newScratch }}
{{- $s.Set "@context" "https://schema.org" }}
{{- $s.Set "@type" "FAQPage" }}
{{- $params := default dict .Params.json_ld.faq_page }}
{{- $data := default "" $params.data }}
{{- $questionField := default "question" $params.question_field }}
{{- $answerField := default "answer" $params.answer_field }}
{{- $questions := slice }}
{{- with index site.Data $data }}
{{- range . }}
{{- $question := "" }}
{{- $answer := "" }}
{{- with index . $questionField }}
{{- $question = . }}
{{- else }}
{{- warnf "[seo|json-ld/faq] no such question field in %q data: %s" $data $questionField }}
{{- continue }}
{{- end }}
{{- with index . $answerField }}
{{- $answer = . | $.RenderString }}
{{- else }}
{{- warnf "[seo/json-ld/faq] no such answer field in %q data: %s" $data $questionField }}
{{- continue }}
{{- end }}
{{- $s.Add "mainEntity" (slice (dict
"@type" "Question"
"name" $question
"acceptedAnswer" (dict "@type" "Answer" "text" $answer)))
}}
{{- end }}
{{- else }}
{{- warnf "[seo/json-ld/faq] no such FAQs data: %q" $data }}
{{- end }}
{{- return $s.Values }}

0 comments on commit 9091f8c

Please sign in to comment.