Skip to content

Commit 363ab48

Browse files
committed
Fix handling of "outputs" from content adapter pages
Fixes #13689
1 parent 80f0595 commit 363ab48

File tree

6 files changed

+66
-27
lines changed

6 files changed

+66
-27
lines changed

hugolib/content_map.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ func (m *pageMap) addPagesFromGoTmplFi(fi hugofs.FileMetaInfo, buildConfig *Buil
356356
Watching: s.Conf.Watching(),
357357
HandlePage: func(pt *pagesfromdata.PagesFromTemplate, pc *pagemeta.PageConfig) error {
358358
s := pt.Site.(*Site)
359-
if err := pc.Compile(pt.GoTmplFi.Meta().PathInfo.Base(), true, "", s.Log, s.conf.MediaTypes.Config); err != nil {
359+
if err := pc.Compile(pt.GoTmplFi.Meta().PathInfo.Base(), true, "", s.Log, s.conf.OutputFormats.Config, s.conf.MediaTypes.Config); err != nil {
360360
return err
361361
}
362362

hugolib/page__meta.go

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ type pageMeta struct {
5454

5555
resource.Staler
5656
*pageMetaParams
57-
pageMetaFrontMatter
5857

5958
// Set for standalone pages, e.g. robotsTXT.
6059
standaloneOutputFormat output.Format
@@ -79,7 +78,6 @@ func (m *pageMeta) setMetaPostPrepareRebuild() {
7978
Path: m.pageConfig.Path,
8079
Params: params,
8180
}
82-
m.pageMetaFrontMatter = pageMetaFrontMatter{}
8381
}
8482

8583
type pageMetaParams struct {
@@ -94,11 +92,6 @@ type pageMetaParams struct {
9492
cascadeOriginal *maps.Ordered[page.PageMatcher, maps.Params] // contains the original cascade as defined in the front matter.
9593
}
9694

97-
// From page front matter.
98-
type pageMetaFrontMatter struct {
99-
configuredOutputFormats output.Formats // outputs defined in front matter.
100-
}
101-
10295
func (m *pageMetaParams) init(preserveOriginal bool) {
10396
if preserveOriginal {
10497
m.paramsOriginal = xmaps.Clone[maps.Params](m.pageConfig.Params)
@@ -531,16 +524,7 @@ params:
531524
for i, s := range o {
532525
o[i] = strings.ToLower(s)
533526
}
534-
if len(o) > 0 {
535-
// Output formats are explicitly set in front matter, use those.
536-
outFormats, err := p.s.conf.OutputFormats.Config.GetByNames(o...)
537-
if err != nil {
538-
p.s.Log.Errorf("Failed to resolve output formats: %s", err)
539-
} else {
540-
pm.configuredOutputFormats = outFormats
541-
params[loki] = outFormats
542-
}
543-
}
527+
pm.pageConfig.Outputs = o
544528
case "draft":
545529
draft = new(bool)
546530
*draft = cast.ToBool(v)
@@ -672,7 +656,7 @@ params:
672656
return err
673657
}
674658

675-
if err := pcfg.Compile("", false, ext, p.s.Log, p.s.conf.MediaTypes.Config); err != nil {
659+
if err := pcfg.Compile("", false, ext, p.s.Log, p.s.conf.OutputFormats.Config, p.s.conf.MediaTypes.Config); err != nil {
676660
return err
677661
}
678662

@@ -829,8 +813,8 @@ func (p *pageMeta) newContentConverter(ps *pageState, markup string) (converter.
829813

830814
// The output formats this page will be rendered to.
831815
func (m *pageMeta) outputFormats() output.Formats {
832-
if len(m.configuredOutputFormats) > 0 {
833-
return m.configuredOutputFormats
816+
if len(m.pageConfig.ConfiguredOutputFormats) > 0 {
817+
return m.pageConfig.ConfiguredOutputFormats
834818
}
835819
return m.s.conf.C.KindOutputFormats[m.Kind()]
836820
}

hugolib/pagesfromdata/pagesfromgotmpl_integration_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,3 +779,45 @@ Single.
779779

780780
b.AssertFileContent("public/tags/index.html", "Terms: mytag: 1|§s")
781781
}
782+
783+
func TestContentAdapterOutputsIssue13689(t *testing.T) {
784+
t.Parallel()
785+
786+
files := `
787+
-- hugo.toml --
788+
disableKinds = ['home','rss','section','sitemap','taxonomy','term']
789+
[outputs]
790+
page = ['html','json']
791+
-- layouts/page.html --
792+
html: {{ .Title }}
793+
-- layouts/page.json --
794+
json: {{ .Title }}
795+
-- content/p1.md --
796+
---
797+
title: p1
798+
---
799+
-- content/p2.md --
800+
---
801+
title: p2
802+
outputs:
803+
- html
804+
---
805+
-- content/_content.gotmpl --
806+
{{ $page := dict "path" "p3" "title" "p3" }}
807+
{{ $.AddPage $page }}
808+
809+
{{ $page := dict "path" "p4" "title" "p4" "outputs" (slice "html") }}
810+
{{ $.AddPage $page }}
811+
`
812+
813+
b := hugolib.Test(t, files)
814+
815+
b.AssertFileExists("public/p1/index.html", true)
816+
b.AssertFileExists("public/p1/index.json", true)
817+
b.AssertFileExists("public/p2/index.html", true)
818+
b.AssertFileExists("public/p2/index.json", false)
819+
b.AssertFileExists("public/p3/index.html", true)
820+
b.AssertFileExists("public/p3/index.json", true)
821+
b.AssertFileExists("public/p4/index.html", true)
822+
b.AssertFileExists("public/p4/index.json", false) // currently returns true
823+
}

hugolib/site.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ func (s *Site) initRenderFormats() {
804804
Tree: s.pageMap.treePages,
805805
Handle: func(key string, n contentNodeI, match doctree.DimensionFlag) (bool, error) {
806806
if p, ok := n.(*pageState); ok {
807-
for _, f := range p.m.configuredOutputFormats {
807+
for _, f := range p.m.pageConfig.ConfiguredOutputFormats {
808808
if !formatSet[f.Name] {
809809
formats = append(formats, f)
810810
formatSet[f.Name] = true

resources/page/pagemeta/page_frontmatter.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/gohugoio/hugo/hugofs/files"
3030
"github.com/gohugoio/hugo/markup"
3131
"github.com/gohugoio/hugo/media"
32+
"github.com/gohugoio/hugo/output"
3233
"github.com/gohugoio/hugo/resources/kinds"
3334
"github.com/gohugoio/hugo/resources/page"
3435
"github.com/gohugoio/hugo/resources/resource"
@@ -114,9 +115,10 @@ type PageConfig struct {
114115
Content Source
115116

116117
// Compiled values.
117-
CascadeCompiled *maps.Ordered[page.PageMatcher, maps.Params] `mapstructure:"-" json:"-"`
118-
ContentMediaType media.Type `mapstructure:"-" json:"-"`
119-
IsFromContentAdapter bool `mapstructure:"-" json:"-"`
118+
CascadeCompiled *maps.Ordered[page.PageMatcher, maps.Params] `mapstructure:"-" json:"-"`
119+
ContentMediaType media.Type `mapstructure:"-" json:"-"`
120+
ConfiguredOutputFormats output.Formats `mapstructure:"-" json:"-"`
121+
IsFromContentAdapter bool `mapstructure:"-" json:"-"`
120122
}
121123

122124
var DefaultPageConfig = PageConfig{
@@ -150,7 +152,7 @@ func (p *PageConfig) Validate(pagesFromData bool) error {
150152
}
151153

152154
// Compile sets up the page configuration after all fields have been set.
153-
func (p *PageConfig) Compile(basePath string, pagesFromData bool, ext string, logger loggers.Logger, mediaTypes media.Types) error {
155+
func (p *PageConfig) Compile(basePath string, pagesFromData bool, ext string, logger loggers.Logger, outputFormats output.Formats, mediaTypes media.Types) error {
154156
// In content adapters, we always get relative paths.
155157
if basePath != "" {
156158
p.Path = path.Join(basePath, p.Path)
@@ -195,6 +197,15 @@ func (p *PageConfig) Compile(basePath string, pagesFromData bool, ext string, lo
195197
p.Content.Markup = p.ContentMediaType.SubType
196198
}
197199

200+
if len(p.Outputs) > 0 {
201+
outFormats, err := outputFormats.GetByNames(p.Outputs...)
202+
if err != nil {
203+
return fmt.Errorf("failed to resolve output formats %v: %w", p.Outputs, err)
204+
} else {
205+
p.ConfiguredOutputFormats = outFormats
206+
}
207+
}
208+
198209
if pagesFromData {
199210
if p.Kind == "" {
200211
p.Kind = kinds.KindPage
@@ -205,6 +216,7 @@ func (p *PageConfig) Compile(basePath string, pagesFromData bool, ext string, lo
205216
// but also because people tend to use use the filename to name their resources (with spaces and all),
206217
// and this isn't relevant when creating resources from an API where it's easy to add textual meta data.
207218
p.Path = paths.NormalizePathStringBasic(p.Path)
219+
208220
}
209221

210222
if p.Cascade != nil {

resources/page/pagemeta/page_frontmatter_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/gohugoio/hugo/config"
2323
"github.com/gohugoio/hugo/config/testconfig"
2424
"github.com/gohugoio/hugo/media"
25+
"github.com/gohugoio/hugo/output"
2526

2627
"github.com/gohugoio/hugo/resources/page/pagemeta"
2728

@@ -175,7 +176,7 @@ func TestContentMediaTypeFromMarkup(t *testing.T) {
175176
} {
176177
var pc pagemeta.PageConfig
177178
pc.Content.Markup = test.in
178-
c.Assert(pc.Compile("", true, "", logger, media.DefaultTypes), qt.IsNil)
179+
c.Assert(pc.Compile("", true, "", logger, output.DefaultFormats, media.DefaultTypes), qt.IsNil)
179180
c.Assert(pc.ContentMediaType.Type, qt.Equals, test.expected)
180181
}
181182
}

0 commit comments

Comments
 (0)