mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-23 21:53:09 +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:
@@ -21,6 +21,123 @@ import (
|
||||
"github.com/gohugoio/hugo/hugolib"
|
||||
)
|
||||
|
||||
func TestRelatedFragments(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
files := `
|
||||
-- hugo.toml --
|
||||
baseURL = "http://example.com/"
|
||||
disableKinds = ["taxonomy", "term", "RSS", "sitemap", "robotsTXT"]
|
||||
[related]
|
||||
includeNewer = false
|
||||
threshold = 80
|
||||
toLower = false
|
||||
[[related.indices]]
|
||||
name = 'pagerefs'
|
||||
type = 'fragments'
|
||||
applyFilter = true
|
||||
weight = 90
|
||||
[[related.indices]]
|
||||
name = 'keywords'
|
||||
weight = 80
|
||||
-- content/p1.md --
|
||||
---
|
||||
title: p1
|
||||
pagerefs: ['ref1']
|
||||
---
|
||||
{{< see-also >}}
|
||||
|
||||
## P1 title
|
||||
|
||||
-- content/p2.md --
|
||||
---
|
||||
title: p2
|
||||
---
|
||||
|
||||
## P2 title 1
|
||||
|
||||
## P2 title 2
|
||||
|
||||
## First title {#ref1}
|
||||
{{< see-also "ref1" >}}
|
||||
-- content/p3.md --
|
||||
---
|
||||
title: p3
|
||||
keywords: ['foo']
|
||||
---
|
||||
|
||||
## P3 title 1
|
||||
|
||||
## P3 title 2
|
||||
|
||||
## Common p3, p4, p5
|
||||
-- content/p4.md --
|
||||
---
|
||||
title: p4
|
||||
---
|
||||
|
||||
## Common p3, p4, p5
|
||||
|
||||
## P4 title 1
|
||||
|
||||
-- content/p5.md --
|
||||
---
|
||||
title: p5
|
||||
keywords: ['foo']
|
||||
---
|
||||
|
||||
## P5 title 1
|
||||
|
||||
## Common p3, p4, p5
|
||||
|
||||
-- layouts/shortcodes/see-also.html --
|
||||
{{ $p1 := site.GetPage "p1" }}
|
||||
{{ $p2 := site.GetPage "p2" }}
|
||||
{{ $p3 := site.GetPage "p3" }}
|
||||
P1 Fragments: {{ $p1.Fragments.Identifiers }}
|
||||
P2 Fragments: {{ $p2.Fragments.Identifiers }}
|
||||
Contains ref1: {{ $p2.Fragments.Identifiers.Contains "ref1" }}
|
||||
Count ref1: {{ $p2.Fragments.Identifiers.Count "ref1" }}
|
||||
{{ $opts := dict "document" .Page "fragments" $.Params }}
|
||||
{{ $related1 := site.RegularPages.Related $opts }}
|
||||
{{ $related2 := site.RegularPages.Related $p3 }}
|
||||
Len Related 1: {{ len $related1 }}
|
||||
Len Related 2: {{ len $related2 }}
|
||||
Related 1: {{ template "list-related" $related1 }}
|
||||
Related 2: {{ template "list-related" $related2 }}
|
||||
|
||||
{{ define "list-related" }}{{ range $i, $e := . }} {{ $i }}: {{ .Title }}: {{ with .HeadingsFiltered}}{{ range $i, $e := .}}h{{ $i }}: {{ .Title }}|{{ .ID }}|{{ end }}{{ end }}::END{{ end }}{{ end }}
|
||||
|
||||
-- layouts/_default/single.html --
|
||||
Content: {{ .Content }}
|
||||
|
||||
|
||||
`
|
||||
|
||||
b := hugolib.NewIntegrationTestBuilder(
|
||||
hugolib.IntegrationTestConfig{
|
||||
T: t,
|
||||
TxtarString: files,
|
||||
}).Build()
|
||||
|
||||
expect := `
|
||||
P1 Fragments: [p1-title]
|
||||
P2 Fragments: [p2-title-1 p2-title-2 ref1]
|
||||
Len Related 1: 1
|
||||
Related 2: 2
|
||||
`
|
||||
|
||||
for _, p := range []string{"p1", "p2"} {
|
||||
b.AssertFileContent("public/"+p+"/index.html", expect)
|
||||
}
|
||||
|
||||
b.AssertFileContent("public/p1/index.html",
|
||||
"Related 1: 0: p2: h0: First title|ref1|::END",
|
||||
"Related 2: 0: p5: h0: Common p3, p4, p5|common-p3-p4-p5|::END 1: p4: h0: Common p3, p4, p5|common-p3-p4-p5|::END",
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
func BenchmarkRelatedSite(b *testing.B) {
|
||||
files := `
|
||||
-- config.toml --
|
||||
@@ -33,6 +150,10 @@ disableKinds = ["taxonomy", "term", "RSS", "sitemap", "robotsTXT"]
|
||||
[[related.indices]]
|
||||
name = 'keywords'
|
||||
weight = 70
|
||||
[[related.indices]]
|
||||
name = 'pagerefs'
|
||||
type = 'fragments'
|
||||
weight = 30
|
||||
-- layouts/_default/single.html --
|
||||
{{ range site.RegularPages }}
|
||||
{{ $tmp := .WordCount }}
|
||||
|
Reference in New Issue
Block a user