| 1 |
// Copyright 2024 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package related_test |
| 15 |
|
| 16 |
import ( |
| 17 |
"fmt" |
| 18 |
"math/rand" |
| 19 |
"strings" |
| 20 |
"testing" |
| 21 |
|
| 22 |
"github.com/gohugoio/hugo/hugolib" |
| 23 |
) |
| 24 |
|
| 25 |
func TestRelatedFragments(t *testing.T) { |
| 26 |
t.Parallel() |
| 27 |
|
| 28 |
files := ` |
| 29 |
-- hugo.toml -- |
| 30 |
baseURL = "http://example.com/" |
| 31 |
disableKinds = ["taxonomy", "term", "RSS", "sitemap", "robotsTXT"] |
| 32 |
[related] |
| 33 |
includeNewer = false |
| 34 |
threshold = 80 |
| 35 |
toLower = false |
| 36 |
[[related.indices]] |
| 37 |
name = 'pagerefs' |
| 38 |
type = 'fragments' |
| 39 |
applyFilter = true |
| 40 |
weight = 90 |
| 41 |
[[related.indices]] |
| 42 |
name = 'keywords' |
| 43 |
weight = 80 |
| 44 |
-- content/p1.md -- |
| 45 |
--- |
| 46 |
title: p1 |
| 47 |
pagerefs: ['ref1'] |
| 48 |
--- |
| 49 |
{{< see-also >}} |
| 50 |
|
| 51 |
## P1 title |
| 52 |
|
| 53 |
-- content/p2.md -- |
| 54 |
--- |
| 55 |
title: p2 |
| 56 |
--- |
| 57 |
|
| 58 |
## P2 title 1 |
| 59 |
|
| 60 |
## P2 title 2 |
| 61 |
|
| 62 |
## First title {#ref1} |
| 63 |
{{< see-also "ref1" >}} |
| 64 |
-- content/p3.md -- |
| 65 |
--- |
| 66 |
title: p3 |
| 67 |
keywords: ['foo'] |
| 68 |
--- |
| 69 |
|
| 70 |
## P3 title 1 |
| 71 |
|
| 72 |
## P3 title 2 |
| 73 |
|
| 74 |
## Common p3, p4, p5 |
| 75 |
-- content/p4.md -- |
| 76 |
--- |
| 77 |
title: p4 |
| 78 |
--- |
| 79 |
|
| 80 |
## Common p3, p4, p5 |
| 81 |
|
| 82 |
## P4 title 1 |
| 83 |
|
| 84 |
-- content/p5.md -- |
| 85 |
--- |
| 86 |
title: p5 |
| 87 |
keywords: ['foo'] |
| 88 |
--- |
| 89 |
|
| 90 |
## P5 title 1 |
| 91 |
|
| 92 |
## Common p3, p4, p5 |
| 93 |
|
| 94 |
-- layouts/_shortcodes/see-also.html -- |
| 95 |
{{ $p1 := site.GetPage "p1" }} |
| 96 |
{{ $p2 := site.GetPage "p2" }} |
| 97 |
{{ $p3 := site.GetPage "p3" }} |
| 98 |
P1 Fragments: {{ $p1.Fragments.Identifiers }} |
| 99 |
P2 Fragments: {{ $p2.Fragments.Identifiers }} |
| 100 |
Contains ref1: {{ $p2.Fragments.Identifiers.Contains "ref1" }} |
| 101 |
Count ref1: {{ $p2.Fragments.Identifiers.Count "ref1" }} |
| 102 |
{{ $opts := dict "document" .Page "fragments" $.Params }} |
| 103 |
{{ $related1 := site.RegularPages.Related $opts }} |
| 104 |
{{ $related2 := site.RegularPages.Related $p3 }} |
| 105 |
Len Related 1: {{ len $related1 }} |
| 106 |
Len Related 2: {{ len $related2 }} |
| 107 |
Related 1: {{ template "list-related" $related1 }} |
| 108 |
Related 2: {{ template "list-related" $related2 }} |
| 109 |
|
| 110 |
{{ define "list-related" }}{{ range $i, $e := . }} {{ $i }}: {{ .Title }}: {{ with .HeadingsFiltered}}{{ range $i, $e := .}}h{{ $i }}: {{ .Title }}|{{ .ID }}|{{ end }}{{ end }}::END{{ end }}{{ end }} |
| 111 |
|
| 112 |
-- layouts/single.html -- |
| 113 |
Content: {{ .Content }} |
| 114 |
|
| 115 |
|
| 116 |
` |
| 117 |
|
| 118 |
b := hugolib.Test(t, files) |
| 119 |
|
| 120 |
expect := ` |
| 121 |
P1 Fragments: [p1-title] |
| 122 |
P2 Fragments: [p2-title-1 p2-title-2 ref1] |
| 123 |
Len Related 1: 1 |
| 124 |
Related 2: 2 |
| 125 |
` |
| 126 |
|
| 127 |
for _, p := range []string{"p1", "p2"} { |
| 128 |
b.AssertFileContent("public/"+p+"/index.html", expect) |
| 129 |
} |
| 130 |
|
| 131 |
b.AssertFileContent( |
| 132 |
"public/p1/index.html", |
| 133 |
"Related 1: 0: p2: h0: First title|ref1|::END", |
| 134 |
"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", |
| 135 |
) |
| 136 |
} |
| 137 |
|
| 138 |
// See issue 15199. |
| 139 |
func TestRelatedFragmentsTokenize(t *testing.T) { |
| 140 |
t.Parallel() |
| 141 |
|
| 142 |
files := ` |
| 143 |
-- hugo.toml -- |
| 144 |
disableKinds = ['home', 'section', 'taxonomy', 'term', 'rss', 'sitemap'] |
| 145 |
[related] |
| 146 |
includeNewer = true |
| 147 |
threshold = 80 |
| 148 |
toLower = true |
| 149 |
[[related.indices]] |
| 150 |
type = 'fragments' |
| 151 |
weight = 100 |
| 152 |
tokenize = true |
| 153 |
-- content/p1.md -- |
| 154 |
--- |
| 155 |
title: p1 |
| 156 |
--- |
| 157 |
## Hugo Templates Overview |
| 158 |
-- content/p2.md -- |
| 159 |
--- |
| 160 |
title: p2 |
| 161 |
--- |
| 162 |
## Templates in Hugo |
| 163 |
-- content/p3.md -- |
| 164 |
--- |
| 165 |
title: p3 |
| 166 |
--- |
| 167 |
## JavaScript and CSS |
| 168 |
-- layouts/page.html -- |
| 169 |
Related: {{ range site.RegularPages.Related . }}{{ .Title }} {{ end }} |
| 170 |
` |
| 171 |
b := hugolib.Test(t, files) |
| 172 |
|
| 173 |
b.AssertFileContent("public/p1/index.html", "Related: p2") |
| 174 |
b.AssertFileContent("public/p2/index.html", "Related: p1") |
| 175 |
b.AssertFileContent("public/p3/index.html", "Related: ") |
| 176 |
} |
| 177 |
|
| 178 |
// See issue 15199. |
| 179 |
func TestRelatedFragmentsTokenizeHTMLEntities(t *testing.T) { |
| 180 |
t.Parallel() |
| 181 |
|
| 182 |
files := ` |
| 183 |
-- hugo.toml -- |
| 184 |
baseURL = "http://example.com/" |
| 185 |
disableKinds = ["home", "section", "taxonomy", "term", "rss", "sitemap"] |
| 186 |
[related] |
| 187 |
includeNewer = true |
| 188 |
threshold = 80 |
| 189 |
toLower = true |
| 190 |
[[related.indices]] |
| 191 |
type = 'fragments' |
| 192 |
weight = 100 |
| 193 |
tokenize = true |
| 194 |
-- content/p1.md -- |
| 195 |
--- |
| 196 |
title: p1 |
| 197 |
--- |
| 198 |
## Types of apples |
| 199 |
-- content/p2.md -- |
| 200 |
--- |
| 201 |
title: p2 |
| 202 |
--- |
| 203 |
## Something about "apples" |
| 204 |
-- layouts/page.html -- |
| 205 |
Related: {{ range site.RegularPages.Related . }}{{ .Title }} {{ end }} |
| 206 |
` |
| 207 |
b := hugolib.Test(t, files) |
| 208 |
|
| 209 |
b.AssertFileContent("public/p1/index.html", "Related: p2") |
| 210 |
b.AssertFileContent("public/p2/index.html", "Related: p1") |
| 211 |
} |
| 212 |
|
| 213 |
// See issue 15199. |
| 214 |
func TestRelatedBasicTokenize(t *testing.T) { |
| 215 |
t.Parallel() |
| 216 |
|
| 217 |
files := ` |
| 218 |
-- hugo.toml -- |
| 219 |
disableKinds = ['home', 'section', 'taxonomy', 'term', 'rss', 'sitemap'] |
| 220 |
[related] |
| 221 |
includeNewer = true |
| 222 |
threshold = 80 |
| 223 |
toLower = true |
| 224 |
[[related.indices]] |
| 225 |
name = 'title' |
| 226 |
weight = 100 |
| 227 |
tokenize = true |
| 228 |
-- content/p1.md -- |
| 229 |
--- |
| 230 |
title: Hugo Templates Overview |
| 231 |
--- |
| 232 |
-- content/p2.md -- |
| 233 |
--- |
| 234 |
title: Templates in Hugo |
| 235 |
--- |
| 236 |
-- content/p3.md -- |
| 237 |
--- |
| 238 |
title: JavaScript and CSS |
| 239 |
--- |
| 240 |
-- layouts/page.html -- |
| 241 |
Related: {{ range site.RegularPages.Related . }}{{ .Title }} {{ end }} |
| 242 |
` |
| 243 |
b := hugolib.Test(t, files) |
| 244 |
|
| 245 |
b.AssertFileContent("public/p1/index.html", "Related: Templates in Hugo") |
| 246 |
b.AssertFileContent("public/p2/index.html", "Related: Hugo Templates Overview") |
| 247 |
b.AssertFileContent("public/p3/index.html", "Related: ") |
| 248 |
} |
| 249 |
|
| 250 |
// See issue 15199. |
| 251 |
func TestRelatedFragmentsTokenizeApplyFilter(t *testing.T) { |
| 252 |
t.Parallel() |
| 253 |
|
| 254 |
files := ` |
| 255 |
-- hugo.toml -- |
| 256 |
disableKinds = ['home', 'section', 'taxonomy', 'term', 'rss', 'sitemap'] |
| 257 |
[related] |
| 258 |
includeNewer = true |
| 259 |
threshold = 80 |
| 260 |
toLower = true |
| 261 |
[[related.indices]] |
| 262 |
type = 'fragments' |
| 263 |
weight = 100 |
| 264 |
applyFilter = true |
| 265 |
tokenize = true |
| 266 |
-- content/p1.md -- |
| 267 |
--- |
| 268 |
title: p1 |
| 269 |
--- |
| 270 |
## Hugo Templates Overview |
| 271 |
-- content/p2.md -- |
| 272 |
--- |
| 273 |
title: p2 |
| 274 |
--- |
| 275 |
## Templates in Hugo |
| 276 |
|
| 277 |
## Unrelated Topic |
| 278 |
-- layouts/page.html -- |
| 279 |
Related: {{ range site.RegularPages.Related . }}{{ .Title }}:{{ with .HeadingsFiltered }}{{ range . }}{{ .Title }}|{{ end }}{{ end }}::END {{ end }} |
| 280 |
` |
| 281 |
b := hugolib.Test(t, files) |
| 282 |
|
| 283 |
b.AssertFileContent("public/p1/index.html", "Related: p2:Templates in Hugo|::END") |
| 284 |
b.AssertFileContent("public/p2/index.html", "Related: p1:Hugo Templates Overview|::END") |
| 285 |
} |
| 286 |
|
| 287 |
// See issue 15199. |
| 288 |
func TestRelatedTokenizeCaseSensitive(t *testing.T) { |
| 289 |
t.Parallel() |
| 290 |
|
| 291 |
files := ` |
| 292 |
-- hugo.toml -- |
| 293 |
disableKinds = ['home', 'section', 'taxonomy', 'term', 'rss', 'sitemap'] |
| 294 |
[related] |
| 295 |
includeNewer = true |
| 296 |
threshold = 80 |
| 297 |
toLower = false |
| 298 |
[[related.indices]] |
| 299 |
type = 'fragments' |
| 300 |
weight = 100 |
| 301 |
tokenize = true |
| 302 |
-- content/p1.md -- |
| 303 |
--- |
| 304 |
title: p1 |
| 305 |
--- |
| 306 |
## Hugo Templates |
| 307 |
-- content/p2.md -- |
| 308 |
--- |
| 309 |
title: p2 |
| 310 |
--- |
| 311 |
## hugo templates |
| 312 |
-- layouts/page.html -- |
| 313 |
Related: {{ range site.RegularPages.Related . }}{{ .Title }} {{ end }} |
| 314 |
` |
| 315 |
b := hugolib.Test(t, files) |
| 316 |
|
| 317 |
// toLower=false: "Hugo" != "hugo", so no match. |
| 318 |
b.AssertFileContent("public/p1/index.html", "Related: ") |
| 319 |
b.AssertFileContent("public/p2/index.html", "Related: ") |
| 320 |
} |
| 321 |
|
| 322 |
// See issue 15238. |
| 323 |
func TestRelatedTokenizeMinTokenLength(t *testing.T) { |
| 324 |
t.Parallel() |
| 325 |
|
| 326 |
files := ` |
| 327 |
-- hugo.toml -- |
| 328 |
disableKinds = ['home', 'section', 'taxonomy', 'term', 'rss', 'sitemap'] |
| 329 |
[related] |
| 330 |
includeNewer = true |
| 331 |
threshold = 80 |
| 332 |
toLower = true |
| 333 |
[[related.indices]] |
| 334 |
name = 'title' |
| 335 |
weight = 100 |
| 336 |
tokenize = true |
| 337 |
minTokenLength = 4 |
| 338 |
-- content/p1.md -- |
| 339 |
--- |
| 340 |
title: The long journey home |
| 341 |
--- |
| 342 |
-- content/p2.md -- |
| 343 |
--- |
| 344 |
title: Planning your next big journey |
| 345 |
--- |
| 346 |
-- content/p3.md -- |
| 347 |
--- |
| 348 |
title: Go to the top |
| 349 |
--- |
| 350 |
-- layouts/page.html -- |
| 351 |
Related: {{ range site.RegularPages.Related . }}{{ .Title }} {{ end }} |
| 352 |
` |
| 353 |
b := hugolib.Test(t, files) |
| 354 |
|
| 355 |
// p1 and p2 share "journey" (>= minTokenLength=4); short words are filtered. |
| 356 |
b.AssertFileContent("public/p1/index.html", "Related: Planning your next big journey") |
| 357 |
b.AssertFileContent("public/p2/index.html", "Related: The long journey home") |
| 358 |
// All tokens in p3's title ("go", "to", "the", "top") are < minTokenLength=4. |
| 359 |
b.AssertFileContent("public/p3/index.html", "Related: ") |
| 360 |
} |
| 361 |
|
| 362 |
// Repetitive headings must not inflate a document's rank above a document |
| 363 |
// that matches on an additional index. |
| 364 |
func TestRelatedFragmentsTokenizeDeduplication(t *testing.T) { |
| 365 |
t.Parallel() |
| 366 |
|
| 367 |
files := ` |
| 368 |
-- hugo.toml -- |
| 369 |
disableKinds = ['home', 'section', 'taxonomy', 'term', 'rss', 'sitemap'] |
| 370 |
[related] |
| 371 |
includeNewer = true |
| 372 |
threshold = 80 |
| 373 |
toLower = true |
| 374 |
[[related.indices]] |
| 375 |
type = 'fragments' |
| 376 |
weight = 100 |
| 377 |
tokenize = true |
| 378 |
[[related.indices]] |
| 379 |
name = 'keywords' |
| 380 |
weight = 100 |
| 381 |
-- content/p1.md -- |
| 382 |
--- |
| 383 |
title: p1 |
| 384 |
keywords: ['fruit'] |
| 385 |
--- |
| 386 |
## I love apples, apples and more apples |
| 387 |
-- content/p2.md -- |
| 388 |
--- |
| 389 |
title: p2 |
| 390 |
keywords: ['fruit'] |
| 391 |
--- |
| 392 |
## I love apples, apples and more apples |
| 393 |
## I love apples, apples and more apples |
| 394 |
## I love apples, apples and more apples |
| 395 |
-- content/p3.md -- |
| 396 |
--- |
| 397 |
title: p3 |
| 398 |
--- |
| 399 |
## I love apples, apples and more apples |
| 400 |
## I love apples, apples and more apples |
| 401 |
## I love apples, apples and more apples |
| 402 |
-- layouts/page.html -- |
| 403 |
Related: {{ range site.RegularPages.Related . }}{{ .Title }} {{ end }} |
| 404 |
` |
| 405 |
b := hugolib.Test(t, files) |
| 406 |
|
| 407 |
// p1 and p3 share the same unique fragment tokens; p1 also matches on |
| 408 |
// keywords ("fruit"). Without deduplication, p3's repeated headings |
| 409 |
// inflate its weight above p1's combined fragment+keywords weight. |
| 410 |
b.AssertFileContent("public/p2/index.html", "Related: p1 p3") |
| 411 |
} |
| 412 |
|
| 413 |
func BenchmarkRelatedSite(b *testing.B) { |
| 414 |
var files strings.Builder |
| 415 |
files.WriteString(` |
| 416 |
-- hugo.toml -- |
| 417 |
baseURL = "http://example.com/" |
| 418 |
disableKinds = ["taxonomy", "term", "RSS", "sitemap", "robotsTXT"] |
| 419 |
[related] |
| 420 |
includeNewer = false |
| 421 |
threshold = 80 |
| 422 |
toLower = false |
| 423 |
[[related.indices]] |
| 424 |
name = 'keywords' |
| 425 |
weight = 70 |
| 426 |
[[related.indices]] |
| 427 |
name = 'pagerefs' |
| 428 |
type = 'fragments' |
| 429 |
weight = 30 |
| 430 |
-- layouts/single.html -- |
| 431 |
Len related: {{ site.RegularPages.Related . | len }} |
| 432 |
`) |
| 433 |
|
| 434 |
createContent := func(n int) string { |
| 435 |
var base strings.Builder |
| 436 |
base.WriteString(`--- |
| 437 |
title: "Page %d" |
| 438 |
keywords: ['k%d'] |
| 439 |
--- |
| 440 |
`) |
| 441 |
|
| 442 |
for range 32 { |
| 443 |
base.WriteString(fmt.Sprintf("\n## Title %d", rand.Intn(100))) |
| 444 |
} |
| 445 |
|
| 446 |
return fmt.Sprintf(base.String(), n, rand.Intn(32)) |
| 447 |
} |
| 448 |
|
| 449 |
for i := 1; i < 100; i++ { |
| 450 |
files.WriteString(fmt.Sprintf("\n-- content/posts/p%d.md --\n"+createContent(i+1), i+1)) |
| 451 |
} |
| 452 |
|
| 453 |
cfg := hugolib.IntegrationTestConfig{ |
| 454 |
T: b, |
| 455 |
TxtarString: files.String(), |
| 456 |
} |
| 457 |
|
| 458 |
for b.Loop() { |
| 459 |
b.StopTimer() |
| 460 |
bb := hugolib.NewIntegrationTestBuilder(cfg) |
| 461 |
b.StartTimer() |
| 462 |
bb.Build() |
| 463 |
} |
| 464 |
} |