Repository: gohugoio/hugo (language: go)

Issue:
Add Page method/field to taxonomy object
We can get a reference to a _term_ page with:

```text
{{ $termPage := site.Taxonomies.tags.foo.Page }}
```

It would be convenient if we could get a reference to a _taxonomy_ page with:

```text
{{ $taxonomyPage := site.Taxonomies.tags.Page }}
```

That may not be possible, despite the fact we lowercase terms internally (e.g., there shouldn't be a collision between `tags.page` (a term) and `tags.Page` (a method/field).

The alternative has always seemed less elegant compared to how you can get a reference to a term page:

```text
{{ site.GetPage "/tags" }}
```


Discussion / hints:
This is what ` site.Taxonomies.tags` gets you:

```go
type Taxonomy map[string]WeightedPages
```

* We cannot change the type (or: not until we get some better "struct iterator" support in Go templates)
* We cannot add state other than `WeightedPages` (e.g. `tags`)

What we in theory could do, but that would not work for empty taxonomies:

```go
func (t Taxonomy) Page() page.Page {
   var first WeightedPages
   range _, v := t {
        first = v
        break
    }
    if first == nil {
         return nil
     }
     return first.Page().Parent()
}
```

I tested with this:

```go
// Page returns the taxonomy page or nil if the taxonomy has no terms.
func (i Taxonomy) Page() Page {
	var first WeightedPages
	for _, v := range i {
		first = v
		break
	}
	if first == nil {
		return nil
	}
	return first.Page().Parent()
}
```

and this:

```text
{{ range $taxonomy, $terms := site.Taxonomies }}
  {{ with $terms }}
    <h2><a href="{{ .Page.RelPermalink }}">{{ .Page.LinkTitle }}</a></h2>
    {{ range $term, $weightedPages := . }}
      <h3><a href="{{ .Page.RelPermalink }}">{{ .Page.LinkTitle }}</a></h3>
      {{ range $weightedPages }}
        <a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
      {{ end }}
    {{ end }}
  {{ end }}
{{ end }}
```

Works great.

I think this is OK as long as we document the "returns nil if no term pages" part.

And it's kind of sneaky, so I like it.

Fix patch:
diff --git a/resources/page/taxonomy.go b/resources/page/taxonomy.go
index 7258ec197c0..2732c80402f 100644
--- a/resources/page/taxonomy.go
+++ b/resources/page/taxonomy.go
@@ -112,6 +112,14 @@ func (i Taxonomy) ByCount() OrderedTaxonomy {
 	return ia
 }
 
+// Page returns the taxonomy page or nil if the taxonomy has no terms.
+func (i Taxonomy) Page() Page {
+	for _, v := range i {
+		return v.Page().Parent()
+	}
+	return nil
+}
+
 // Pages returns the Pages for this taxonomy.
 func (ie OrderedTaxonomyEntry) Pages() Pages {
 	return ie.WeightedPages.Pages()


Test patch:
diff --git a/resources/page/taxonomy_integration_test.go b/resources/page/taxonomy_integration_test.go
index a028857cb7f..70e204dd19a 100644
--- a/resources/page/taxonomy_integration_test.go
+++ b/resources/page/taxonomy_integration_test.go
@@ -53,3 +53,29 @@ authors: [John Smith]
 		"Robert Jones count: 1",
 	)
 }
+
+func TestTaxonomiesPage(t *testing.T) {
+	t.Parallel()
+
+	files := `
+-- hugo.toml --
+disableKinds = ['rss','section','sitemap']
+[taxonomies]
+tag = 'tags'
+category = 'categories'
+-- content/p1.md --
+---
+title: p1
+tags: [tag-a]
+---
+-- layouts/_default/list.html --
+{{- with site.Taxonomies.tags.Page }}{{ .RelPermalink }}{{ end }}|
+{{- with site.Taxonomies.categories.Page }}{{ .RelPermalink }}{{ end }}|
+-- layouts/_default/single.html --
+{{ .Title }}
+`
+
+	b := hugolib.Test(t, files)
+
+	b.AssertFileContent("public/index.html", "/tags/||")
+}