From eb27a1e023d9473e73a2ca552ec9ae777b697d39 Mon Sep 17 00:00:00 2001 From: "Daniel F. Dickinson" Date: Sun, 23 Apr 2023 14:17:52 -0400 Subject: [PATCH] Add schema.org BreadcrumbList to JSON+LD (#87) * Add schema.org BreadcrumbList to JSON+LD This implementation depends on .Page.Ancestors, which requires Hugo version v0.109.0. Signed-off-by: Daniel F. Dickinson * Fix extraneous comma in schema For blog posts we ended up with an extraneous comma due to it being outside the condition for the breadcrumb JSON+LD. Signed-off-by: Daniel F. Dickinson * Pretty print breadcrumb JSON+LD for schema Signed-off-by: Daniel F. Dickinson * Make breadcrumbs opt-in on non-blogSections Sections except blogSections (which is a configurable set which is defaulted to sections 'blog' and 'post'), may be part of `breadcrumbSections`. For the breadcrumb Sections, the section and any pages under it will have the `breadcrumb` attribute (of type `BreadcrumbList`) added to the JSON+LD for their schema.org structured data. Update documentation of `params` for example `config.yaml` in README. Signed-off-by: Daniel F. Dickinson --------- Signed-off-by: Daniel F. Dickinson --- README.md | 7 +++++ layouts/partials/meta_breadcrumb_json_ld.html | 29 +++++++++++++++++++ layouts/partials/meta_json_ld.html | 14 ++++++++- 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 layouts/partials/meta_breadcrumb_json_ld.html diff --git a/README.md b/README.md index 8cd211a..d650306 100644 --- a/README.md +++ b/README.md @@ -239,6 +239,13 @@ languageCode: "en-GB" # Set your language code (only needed for none multi params: alpine: # Add AlpineJS, default false. + blogSections: # Sections whose `JSON+LD` in the page `` + - blog # will be `@type: BlogPosting`. + - post # Defaults to a list including only 'post' and 'blog'. + breadcrumbSections: # Sections in which pages will have a `BreadcrumbListing` + - section2 # in the `JSON+LD` in the page's ``. + - section3 # This theme requires Hugo `v0.109.0` or higher to + # generate the `BreadcrumbListing`. cookieConsent: true # Show cookie consent form, default false. contact: "info@example.org" copyright: "This site is licensed under a diff --git a/layouts/partials/meta_breadcrumb_json_ld.html b/layouts/partials/meta_breadcrumb_json_ld.html new file mode 100644 index 0000000..eee976f --- /dev/null +++ b/layouts/partials/meta_breadcrumb_json_ld.html @@ -0,0 +1,29 @@ +{{- $breadcrumbScratch := newScratch -}} +{{- $crumbScratch := newScratch -}} +{{- $crumbScratch.Set "itemListElement" (slice) -}} +{{- $breadPos := 0 -}} +{{- range .Ancestors.Reverse -}} + {{- $breadPos = (add $breadPos 1) -}} + {{- $crumbScratch.Add "itemListElement" (dict + "@type" "ListItem" + "position" $breadPos + "item" (dict + "@id" .Permalink + "name" .LinkTitle + ) + ) -}} +{{- end -}} +{{- $breadPos = (add $breadPos 1) -}} +{{- $crumbScratch.Add "itemListElement" (dict + "@type" "ListItem" + "position" $breadPos + "item" (dict + "@id" .Permalink + "name" .LinkTitle + ) +) -}} +{{- $breadcrumbScratch.SetInMap "breadcrumb" "@context" "https://schema.org" -}} +{{- $breadcrumbScratch.SetInMap "breadcrumb" "@type" "BreadcrumbList" -}} +{{- $breadcrumbScratch.SetInMap "breadcrumb" "itemListElement" ($crumbScratch.Get "itemListElement") -}} +{{- $jsonifiedBreadcrumb := $breadcrumbScratch.Get "breadcrumb" | jsonify (dict "indent" " " "prefix" " ") -}} +{{- return $jsonifiedBreadcrumb -}} diff --git a/layouts/partials/meta_json_ld.html b/layouts/partials/meta_json_ld.html index 6fdafd7..2e3d361 100644 --- a/layouts/partials/meta_json_ld.html +++ b/layouts/partials/meta_json_ld.html @@ -1,8 +1,17 @@