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:
Bjørn Erik Pedersen 2023-02-11 16:20:24 +01:00
parent 0afec0a9f4
commit 90da7664bf
66 changed files with 1363 additions and 829 deletions

View file

@ -48,16 +48,16 @@ func TestInit(t *testing.T) {
var result string
f1 := func(name string) func() (any, error) {
return func() (any, error) {
f1 := func(name string) func(context.Context) (any, error) {
return func(context.Context) (any, error) {
result += name + "|"
doWork()
return name, nil
}
}
f2 := func() func() (any, error) {
return func() (any, error) {
f2 := func() func(context.Context) (any, error) {
return func(context.Context) (any, error) {
doWork()
return nil, nil
}
@ -75,6 +75,8 @@ func TestInit(t *testing.T) {
var wg sync.WaitGroup
ctx := context.Background()
// Add some concurrency and randomness to verify thread safety and
// init order.
for i := 0; i < 100; i++ {
@ -83,20 +85,20 @@ func TestInit(t *testing.T) {
defer wg.Done()
var err error
if rnd.Intn(10) < 5 {
_, err = root.Do()
_, err = root.Do(ctx)
c.Assert(err, qt.IsNil)
}
// Add a new branch on the fly.
if rnd.Intn(10) > 5 {
branch := branch1_2.Branch(f2())
_, err = branch.Do()
_, err = branch.Do(ctx)
c.Assert(err, qt.IsNil)
} else {
_, err = branch1_2_1.Do()
_, err = branch1_2_1.Do(ctx)
c.Assert(err, qt.IsNil)
}
_, err = branch1_2.Do()
_, err = branch1_2.Do(ctx)
c.Assert(err, qt.IsNil)
}(i)
@ -114,7 +116,7 @@ func TestInitAddWithTimeout(t *testing.T) {
return nil, nil
})
_, err := init.Do()
_, err := init.Do(context.Background())
c.Assert(err, qt.IsNil)
}
@ -133,7 +135,7 @@ func TestInitAddWithTimeoutTimeout(t *testing.T) {
return nil, nil
})
_, err := init.Do()
_, err := init.Do(context.Background())
c.Assert(err, qt.Not(qt.IsNil))
@ -149,7 +151,7 @@ func TestInitAddWithTimeoutError(t *testing.T) {
return nil, errors.New("failed")
})
_, err := init.Do()
_, err := init.Do(context.Background())
c.Assert(err, qt.Not(qt.IsNil))
}
@ -178,8 +180,8 @@ func TestInitBranchOrder(t *testing.T) {
base := New()
work := func(size int, f func()) func() (any, error) {
return func() (any, error) {
work := func(size int, f func()) func(context.Context) (any, error) {
return func(context.Context) (any, error) {
doWorkOfSize(size)
if f != nil {
f()
@ -205,13 +207,14 @@ func TestInitBranchOrder(t *testing.T) {
}
var wg sync.WaitGroup
ctx := context.Background()
for _, v := range inits {
v := v
wg.Add(1)
go func() {
defer wg.Done()
_, err := v.Do()
_, err := v.Do(ctx)
c.Assert(err, qt.IsNil)
}()
}
@ -225,17 +228,17 @@ func TestInitBranchOrder(t *testing.T) {
func TestResetError(t *testing.T) {
c := qt.New(t)
r := false
i := New().Add(func() (any, error) {
i := New().Add(func(context.Context) (any, error) {
if r {
return nil, nil
}
return nil, errors.New("r is false")
})
_, err := i.Do()
_, err := i.Do(context.Background())
c.Assert(err, qt.IsNotNil)
i.Reset()
r = true
_, err = i.Do()
_, err = i.Do(context.Background())
c.Assert(err, qt.IsNil)
}