mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-27 22:21:07 +03: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:
parent
0afec0a9f4
commit
90da7664bf
66 changed files with 1363 additions and 829 deletions
|
@ -14,6 +14,7 @@
|
|||
package hugolib
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
|
@ -247,7 +248,7 @@ CSV: {{< myShort >}}
|
|||
func BenchmarkReplaceShortcodeTokens(b *testing.B) {
|
||||
type input struct {
|
||||
in []byte
|
||||
replacements map[string]string
|
||||
tokenHandler func(ctx context.Context, token string) ([]byte, error)
|
||||
expect []byte
|
||||
}
|
||||
|
||||
|
@ -263,22 +264,30 @@ func BenchmarkReplaceShortcodeTokens(b *testing.B) {
|
|||
{strings.Repeat("A ", 3000) + " HAHAHUGOSHORTCODE-1HBHB." + strings.Repeat("BC ", 1000) + " HAHAHUGOSHORTCODE-1HBHB.", map[string]string{"HAHAHUGOSHORTCODE-1HBHB": "Hello World"}, []byte(strings.Repeat("A ", 3000) + " Hello World." + strings.Repeat("BC ", 1000) + " Hello World.")},
|
||||
}
|
||||
|
||||
in := make([]input, b.N*len(data))
|
||||
cnt := 0
|
||||
in := make([]input, b.N*len(data))
|
||||
for i := 0; i < b.N; i++ {
|
||||
for _, this := range data {
|
||||
in[cnt] = input{[]byte(this.input), this.replacements, this.expect}
|
||||
replacements := make(map[string]shortcodeRenderer)
|
||||
for k, v := range this.replacements {
|
||||
replacements[k] = prerenderedShortcode{s: v}
|
||||
}
|
||||
tokenHandler := func(ctx context.Context, token string) ([]byte, error) {
|
||||
return []byte(this.replacements[token]), nil
|
||||
}
|
||||
in[cnt] = input{[]byte(this.input), tokenHandler, this.expect}
|
||||
cnt++
|
||||
}
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
cnt = 0
|
||||
ctx := context.Background()
|
||||
for i := 0; i < b.N; i++ {
|
||||
for j := range data {
|
||||
currIn := in[cnt]
|
||||
cnt++
|
||||
results, err := replaceShortcodeTokens(currIn.in, currIn.replacements)
|
||||
results, err := expandShortcodeTokens(ctx, currIn.in, currIn.tokenHandler)
|
||||
if err != nil {
|
||||
b.Fatalf("[%d] failed: %s", i, err)
|
||||
continue
|
||||
|
@ -383,7 +392,16 @@ func TestReplaceShortcodeTokens(t *testing.T) {
|
|||
},
|
||||
} {
|
||||
|
||||
results, err := replaceShortcodeTokens([]byte(this.input), this.replacements)
|
||||
replacements := make(map[string]shortcodeRenderer)
|
||||
for k, v := range this.replacements {
|
||||
replacements[k] = prerenderedShortcode{s: v}
|
||||
}
|
||||
tokenHandler := func(ctx context.Context, token string) ([]byte, error) {
|
||||
return []byte(this.replacements[token]), nil
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
results, err := expandShortcodeTokens(ctx, []byte(this.input), tokenHandler)
|
||||
|
||||
if b, ok := this.expect.(bool); ok && !b {
|
||||
if err == nil {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue