Fix some related content issues with content adapters

Fixes #13443
This commit is contained in:
Bjørn Erik Pedersen
2025-02-25 11:28:59 +01:00
parent 227e429267
commit 381c0da85d
8 changed files with 151 additions and 16 deletions

View File

@@ -151,6 +151,20 @@ func (p *pageState) Key() string {
return "page-" + strconv.FormatUint(p.pid, 10)
}
// RelatedKeywords implements the related.Document interface needed for fast page searches.
func (p *pageState) RelatedKeywords(cfg related.IndexConfig) ([]related.Keyword, error) {
v, found, err := page.NamedPageMetaValue(p, cfg.Name)
if err != nil {
return nil, err
}
if !found {
return nil, nil
}
return cfg.ToKeywords(v)
}
func (p *pageState) resetBuildState() {
// Nothing to do for now.
}

View File

@@ -68,7 +68,6 @@ type pageCommon struct {
page.PageMetaInternalProvider
page.Positioner
page.RawContentProvider
page.RelatedKeywordsProvider
page.RefProvider
page.ShortcodeInfoProvider
page.SitesProvider

View File

@@ -27,8 +27,6 @@ import (
"github.com/gohugoio/hugo/markup/converter"
xmaps "golang.org/x/exp/maps"
"github.com/gohugoio/hugo/related"
"github.com/gohugoio/hugo/source"
"github.com/gohugoio/hugo/common/constants"
@@ -215,16 +213,6 @@ func (p *pageMeta) PathInfo() *paths.Path {
return p.pathInfo
}
// RelatedKeywords implements the related.Document interface needed for fast page searches.
func (p *pageMeta) RelatedKeywords(cfg related.IndexConfig) ([]related.Keyword, error) {
v, err := p.Param(cfg.Name)
if err != nil {
return nil, err
}
return cfg.ToKeywords(v)
}
func (p *pageMeta) IsSection() bool {
return p.Kind() == kinds.KindSection
}

View File

@@ -202,7 +202,6 @@ func (h *HugoSites) doNewPage(m *pageMeta) (*pageState, *paths.Path, error) {
ResourceParamsProvider: m,
PageMetaProvider: m,
PageMetaInternalProvider: m,
RelatedKeywordsProvider: m,
OutputFormatsProvider: page.NopPage,
ResourceTypeProvider: pageTypesProvider,
MediaTypeProvider: pageTypesProvider,

View File

@@ -1,4 +1,4 @@
// Copyright 2024 The Hugo Authors. All rights reserved.
// Copyright 2025 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@ import (
"github.com/gohugoio/hugo/markup/asciidocext"
"github.com/gohugoio/hugo/markup/pandoc"
"github.com/gohugoio/hugo/markup/rst"
"github.com/gohugoio/hugo/related"
)
const filesPagesFromDataTempleBasic = `
@@ -73,10 +74,11 @@ Pfile Content
{{ $title := printf "%s:%s" $pd $pp }}
{{ $date := "2023-03-01" | time.AsTime }}
{{ $dates := dict "date" $date }}
{{ $keywords := slice "foo" "Bar"}}
{{ $contentMarkdown := dict "value" "**Hello World**" "mediaType" "text/markdown" }}
{{ $contentMarkdownDefault := dict "value" "**Hello World Default**" }}
{{ $contentHTML := dict "value" "<b>Hello World!</b> No **markdown** here." "mediaType" "text/html" }}
{{ $.AddPage (dict "kind" "page" "path" "P1" "title" $title "dates" $dates "content" $contentMarkdown "params" (dict "param1" "param1v" ) ) }}
{{ $.AddPage (dict "kind" "page" "path" "P1" "title" $title "dates" $dates "keywords" $keywords "content" $contentMarkdown "params" (dict "param1" "param1v" ) ) }}
{{ $.AddPage (dict "kind" "page" "path" "p2" "title" "p2title" "dates" $dates "content" $contentHTML ) }}
{{ $.AddPage (dict "kind" "page" "path" "p3" "title" "p3title" "dates" $dates "content" $contentMarkdownDefault "draft" false ) }}
{{ $.AddPage (dict "kind" "page" "path" "p4" "title" "p4title" "dates" $dates "content" $contentMarkdownDefault "draft" $data.draft ) }}
@@ -329,6 +331,24 @@ func TestPagesFromGoTmplRemoveGoTmpl(t *testing.T) {
b.AssertFileContent("public/docs/index.html", "RegularPagesRecursive: pfile:/docs/pfile|$")
}
// Issue #13443.
func TestPagesFromGoRelatedKeywords(t *testing.T) {
t.Parallel()
b := hugolib.Test(t, filesPagesFromDataTempleBasic)
p1 := b.H.Sites[0].RegularPages()[0]
icfg := related.IndexConfig{
Name: "keywords",
}
k, err := p1.RelatedKeywords(icfg)
b.Assert(err, qt.IsNil)
b.Assert(k, qt.DeepEquals, icfg.StringsToKeywords("foo", "Bar"))
icfg.Name = "title"
k, err = p1.RelatedKeywords(icfg)
b.Assert(err, qt.IsNil)
b.Assert(k, qt.DeepEquals, icfg.StringsToKeywords("p1:p1"))
}
func TestPagesFromGoTmplLanguagePerFile(t *testing.T) {
filesTemplate := `
-- hugo.toml --