Add Page.GetTerms

Fixes #6905
This commit is contained in:
Bjørn Erik Pedersen 2020-02-19 09:16:27 +01:00
parent 82029c1ec9
commit fa520a2d98
8 changed files with 82 additions and 15 deletions

View file

@ -216,6 +216,18 @@ Because we are leveraging the front matter system to define taxonomies for conte
### Example: List Tags in a Single Page Template ### Example: List Tags in a Single Page Template
{{< new-in "0.65.0" >}}
```go-html-template
<ul>
{{ range (.GetTerms "tags") }}
<li><a href="{{ .Permalink }}">{{ .LinkTitle }}</a></li>
{{ end }}
</ul>
```
Before Hugo 0.65.0 you needed to do something like this:
```go-html-template ```go-html-template
{{ $taxo := "tags" }} <!-- Use the plural form here --> {{ $taxo := "tags" }} <!-- Use the plural form here -->
<ul id="{{ $taxo }}"> <ul id="{{ $taxo }}">

View file

@ -599,7 +599,7 @@ func (m *pageMap) attachPageToViews(s string, b *contentNode) {
if s == "/" { if s == "/" {
// To avoid getting an empty key. // To avoid getting an empty key.
s = "home" s = page.KindHome
} }
key := cleanTreeKey(path.Join(viewName.plural, termKey, s)) key := cleanTreeKey(path.Join(viewName.plural, termKey, s))
m.taxonomyEntries.Insert(key, bv) m.taxonomyEntries.Insert(key, bv)

View file

@ -131,6 +131,34 @@ func (p *pageState) GitInfo() *gitmap.GitInfo {
return p.gitInfo return p.gitInfo
} }
// GetTerms gets the terms defined on this page in the given taxonomy.
func (p *pageState) GetTerms(taxonomy string) page.Pages {
taxonomy = strings.ToLower(taxonomy)
m := p.s.pageMap
prefix := cleanTreeKey(taxonomy)
var self string
if p.IsHome() {
// TODO(bep) make this less magical, see taxonomyEntries.Insert.
self = "/" + page.KindHome
} else {
self = p.treeRef.key
}
var pas page.Pages
m.taxonomies.WalkPrefixListable(prefix, func(s string, n *contentNode) bool {
if _, found := m.taxonomyEntries.Get(s + self); found {
pas = append(pas, n.p)
}
return false
})
page.SortByDefault(pas)
return pas
}
func (p *pageState) MarshalJSON() ([]byte, error) { func (p *pageState) MarshalJSON() ([]byte, error) {
return page.MarshalPageToJSON(p) return page.MarshalPageToJSON(p)
} }

View file

@ -378,14 +378,10 @@ contentDir="content/sv"
{"List terms", func(b testing.TB) *sitesBuilder { {"List terms", func(b testing.TB) *sitesBuilder {
pageTemplateTemplate := ` pageTemplateTemplate := `
{{ $taxo := "categories" }}
<ul> <ul>
{{ range .Param $taxo }} {{ range (.GetTerms "categories") }}
{{ $name := . }} <li><a href="{{ .Permalink }}">{{ .LinkTitle }}</a></li>
{{ with $.Site.GetPage (printf "/%s/%s" $taxo ($name | urlize)) }} {{ end }}
<li><a href="{{ .Permalink }}">{{ $name }}</a></li>
{{ end }}
{{ end }}
</ul> </ul>
` `

View file

@ -542,25 +542,41 @@ func TestTaxonomiesPageCollections(t *testing.T) {
t.Parallel() t.Parallel()
b := newTestSitesBuilder(t) b := newTestSitesBuilder(t)
b.WithContent("p1.md", `--- b.WithContent(
"_index.md", `---
title: "Home Sweet Home"
categories: [ "dogs", "gorillas"]
---
`,
"section/_index.md", `---
title: "Section"
categories: [ "cats", "dogs", "birds"]
---
`,
"section/p1.md", `---
title: "Page1" title: "Page1"
categories: ["funny", "cats"] categories: ["funny", "cats"]
--- ---
`, "p2.md", `--- `, "section/p2.md", `---
title: "Page2" title: "Page2"
categories: ["funny"] categories: ["funny"]
--- ---
`) `)
b.WithTemplatesAdded("index.html", ` b.WithTemplatesAdded("index.html", `
{{ $home := site.Home }}
{{ $section := site.GetPage "section" }}
{{ $categories := site.GetPage "categories" }} {{ $categories := site.GetPage "categories" }}
{{ $funny := site.GetPage "categories/funny" }} {{ $funny := site.GetPage "categories/funny" }}
{{ $cats := site.GetPage "categories/cats" }} {{ $cats := site.GetPage "categories/cats" }}
{{ $p1 := site.GetPage "section/p1" }}
Categories Pages: {{ range $categories.Pages}}{{.RelPermalink }}|{{ end }}:END Categories Pages: {{ range $categories.Pages}}{{.RelPermalink }}|{{ end }}:END
Funny Pages: {{ range $funny.Pages}}{{.RelPermalink }}|{{ end }}:END Funny Pages: {{ range $funny.Pages}}{{.RelPermalink }}|{{ end }}:END
Cats Pages: {{ range $cats.Pages}}{{.RelPermalink }}|{{ end }}:END Cats Pages: {{ range $cats.Pages}}{{.RelPermalink }}|{{ end }}:END
P1 Terms: {{ range $p1.GetTerms "categories" }}{{.RelPermalink }}|{{ end }}:END
Section Terms: {{ range $section.GetTerms "categories" }}{{.RelPermalink }}|{{ end }}:END
Home Terms: {{ range $home.GetTerms "categories" }}{{.RelPermalink }}|{{ end }}:END
`) `)
b.Build(BuildCfg{}) b.Build(BuildCfg{})
@ -575,12 +591,15 @@ Cats Pages: {{ range $cats.Pages}}{{.RelPermalink }}|{{ end }}:END
b.Assert(funny.Parent(), qt.Equals, cat) b.Assert(funny.Parent(), qt.Equals, cat)
b.AssertFileContent("public/index.html", ` b.AssertFileContent("public/index.html", `
Categories Pages: /categories/cats/|/categories/funny/|:END Categories Pages: /categories/birds/|/categories/cats/|/categories/dogs/|/categories/funny/|/categories/gorillas/|:END
Funny Pages: /p1/|/p2/|:END Funny Pages: /section/p1/|/section/p2/|:END
Cats Pages: /p1/|:END Cats Pages: /section/p1/|/section/|:END
P1 Terms: /categories/cats/|/categories/funny/|:END
Section Terms: /categories/birds/|/categories/cats/|/categories/dogs/|:END
Home Terms: /categories/dogs/|/categories/gorillas/|:END
`) `)
b.AssertFileContent("public/categories/funny/index.xml", `<link>http://example.com/p1/</link>`) b.AssertFileContent("public/categories/funny/index.xml", `<link>http://example.com/section/p1/</link>`)
b.AssertFileContent("public/categories/index.xml", `<link>http://example.com/categories/funny/</link>`) b.AssertFileContent("public/categories/index.xml", `<link>http://example.com/categories/funny/</link>`)
} }

View file

@ -252,6 +252,10 @@ type PageWithoutContent interface {
maps.Scratcher maps.Scratcher
RelatedKeywordsProvider RelatedKeywordsProvider
// GetTerms gets the terms of a given taxonomy,
// e.g. GetTerms("categories")
GetTerms(taxonomy string) Pages
DeprecatedWarningPageMethods DeprecatedWarningPageMethods
} }

View file

@ -174,6 +174,10 @@ func (p *nopPage) GetParam(key string) interface{} {
return nil return nil
} }
func (p *nopPage) GetTerms(taxonomy string) Pages {
return nil
}
func (p *nopPage) GitInfo() *gitmap.GitInfo { func (p *nopPage) GitInfo() *gitmap.GitInfo {
return nil return nil
} }

View file

@ -222,6 +222,10 @@ func (p *testPage) GetParam(key string) interface{} {
panic("not implemented") panic("not implemented")
} }
func (p *testPage) GetTerms(taxonomy string) Pages {
panic("not implemented")
}
func (p *testPage) GetRelatedDocsHandler() *RelatedDocsHandler { func (p *testPage) GetRelatedDocsHandler() *RelatedDocsHandler {
return relatedDocsHandler return relatedDocsHandler
} }