Displaying terms from a specific taxonomy used in a section's pages

Let’s say your content looks like this:

content/
├── blog/
│   ├── blog-1.md
│   ├── blog-2.md
│   └── _index.md
├── books/
│   ├── book-1.md
│   ├── book-2.md
│   └── _index.md
└── _index.md

And your site configuration includes:

[taxonomies]
author = 'authors'

To display a sorted, unique list of authors used anywhere in the books section:

{{ $type := "books" }}
{{ $taxonomy := "authors" }}

{{ $termPages := slice }}
{{ range $term, $weightedPages := index site.Taxonomies $taxonomy }}
  {{ if where .Pages "Type" $type }}
    {{ $termPages = $termPages | append .Page }}
  {{ end }}
{{ end }}

{{ with $termPages }}
  <p>{{ strings.FirstUpper $taxonomy }} in the {{ $type }} section:
    <ul>
      {{ range . }}
        <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
      {{ end }}
    </ul>
  </p>
{{ end }}

Rendered to:

<p>Authors in the books section:
  <ul>
    <li><a href="/authors/enos-mills/">Enos Mills</a></li>
    <li><a href="/authors/john-muir/">John Muir</a></li>
  </ul>
</p>

Motivation for this topic:

2 Likes