mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-18 21:11:19 +02:00
Add page fragments support to Related
The main topic of this commit is that you can now index fragments (content heading identifiers) when calling `.Related`. You can do this by: * Configure one or more indices with type `fragments` * The name of those index configurations maps to an (optional) front matter slice with fragment references. This allows you to link page<->fragment and page<->page. * This also will index all the fragments (heading identifiers) of the pages. It's also possible to use type `fragments` indices in shortcode, e.g.: ``` {{ $related := site.RegularPages.Related .Page }} ``` But, and this is important, you need to include the shortcode using the `{{<` delimiter. Not doing so will create infinite loops and timeouts. This commit also: * Adds two new methods to Page: Fragments (can also be used to build ToC) and HeadingsFiltered (this is only used in Related Content with index type `fragments` and `enableFilter` set to true. * Consolidates all `.Related*` methods into one, which takes either a `Page` or an options map as its only argument. * Add `context.Context` to all of the content related Page API. Turns out it wasn't strictly needed for this particular feature, but it will soon become usefil, e.g. in #9339. Closes #10711 Updates #9339 Updates #10725
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
package page
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"testing"
|
||||
@@ -43,7 +44,7 @@ func TestSplitPageGroups(t *testing.T) {
|
||||
t.Parallel()
|
||||
c := qt.New(t)
|
||||
pages := createTestPages(21)
|
||||
groups, _ := pages.GroupBy("Weight", "desc")
|
||||
groups, _ := pages.GroupBy(context.Background(), "Weight", "desc")
|
||||
chunks := splitPageGroups(groups, 5)
|
||||
c.Assert(len(chunks), qt.Equals, 5)
|
||||
|
||||
@@ -56,7 +57,7 @@ func TestSplitPageGroups(t *testing.T) {
|
||||
// first group 10 in weight
|
||||
c.Assert(pg.Key, qt.Equals, 10)
|
||||
for _, p := range pg.Pages {
|
||||
c.Assert(p.FuzzyWordCount()%2 == 0, qt.Equals, true) // magic test
|
||||
c.Assert(p.FuzzyWordCount(context.Background())%2 == 0, qt.Equals, true) // magic test
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -71,7 +72,7 @@ func TestSplitPageGroups(t *testing.T) {
|
||||
// last should have 5 in weight
|
||||
c.Assert(pg.Key, qt.Equals, 5)
|
||||
for _, p := range pg.Pages {
|
||||
c.Assert(p.FuzzyWordCount()%2 != 0, qt.Equals, true) // magic test
|
||||
c.Assert(p.FuzzyWordCount(context.Background())%2 != 0, qt.Equals, true) // magic test
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -83,7 +84,7 @@ func TestPager(t *testing.T) {
|
||||
t.Parallel()
|
||||
c := qt.New(t)
|
||||
pages := createTestPages(21)
|
||||
groups, _ := pages.GroupBy("Weight", "desc")
|
||||
groups, _ := pages.GroupBy(context.Background(), "Weight", "desc")
|
||||
|
||||
urlFactory := func(page int) string {
|
||||
return fmt.Sprintf("page/%d/", page)
|
||||
@@ -149,7 +150,7 @@ func TestPagerNoPages(t *testing.T) {
|
||||
t.Parallel()
|
||||
c := qt.New(t)
|
||||
pages := createTestPages(0)
|
||||
groups, _ := pages.GroupBy("Weight", "desc")
|
||||
groups, _ := pages.GroupBy(context.Background(), "Weight", "desc")
|
||||
|
||||
urlFactory := func(page int) string {
|
||||
return fmt.Sprintf("page/%d/", page)
|
||||
@@ -249,9 +250,9 @@ func TestProbablyEqualPageLists(t *testing.T) {
|
||||
t.Parallel()
|
||||
fivePages := createTestPages(5)
|
||||
zeroPages := createTestPages(0)
|
||||
zeroPagesByWeight, _ := createTestPages(0).GroupBy("Weight", "asc")
|
||||
fivePagesByWeight, _ := createTestPages(5).GroupBy("Weight", "asc")
|
||||
ninePagesByWeight, _ := createTestPages(9).GroupBy("Weight", "asc")
|
||||
zeroPagesByWeight, _ := createTestPages(0).GroupBy(context.Background(), "Weight", "asc")
|
||||
fivePagesByWeight, _ := createTestPages(5).GroupBy(context.Background(), "Weight", "asc")
|
||||
ninePagesByWeight, _ := createTestPages(9).GroupBy(context.Background(), "Weight", "asc")
|
||||
|
||||
for i, this := range []struct {
|
||||
v1 any
|
||||
@@ -287,7 +288,7 @@ func TestPaginationPage(t *testing.T) {
|
||||
}
|
||||
|
||||
fivePages := createTestPages(7)
|
||||
fivePagesFuzzyWordCount, _ := createTestPages(7).GroupBy("FuzzyWordCount", "asc")
|
||||
fivePagesFuzzyWordCount, _ := createTestPages(7).GroupBy(context.Background(), "FuzzyWordCount", "asc")
|
||||
|
||||
p1, _ := newPaginatorFromPages(fivePages, 2, urlFactory)
|
||||
p2, _ := newPaginatorFromPageGroups(fivePagesFuzzyWordCount, 2, urlFactory)
|
||||
@@ -301,10 +302,10 @@ func TestPaginationPage(t *testing.T) {
|
||||
page21, _ := f2.page(1)
|
||||
page2Nil, _ := f2.page(3)
|
||||
|
||||
c.Assert(page11.FuzzyWordCount(), qt.Equals, 3)
|
||||
c.Assert(page11.FuzzyWordCount(context.Background()), qt.Equals, 3)
|
||||
c.Assert(page1Nil, qt.IsNil)
|
||||
|
||||
c.Assert(page21, qt.Not(qt.IsNil))
|
||||
c.Assert(page21.FuzzyWordCount(), qt.Equals, 3)
|
||||
c.Assert(page21.FuzzyWordCount(context.Background()), qt.Equals, 3)
|
||||
c.Assert(page2Nil, qt.IsNil)
|
||||
}
|
||||
|
Reference in New Issue
Block a user