Skip to content

Commit 6e2f2dd

Browse files
committed
hugolib: Fix output formats override when no outputs definition given
A common use case for this is to redefine the built-in output format `RSS` to give it a different URL. Before this commit, that was not possible without also providing an `outputs` definition. Fixes #3447
1 parent 94b5be6 commit 6e2f2dd

File tree

2 files changed

+64
-8
lines changed

2 files changed

+64
-8
lines changed

‎hugolib/site_output.go‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626

2727
func createSiteOutputFormats(allFormats output.Formats, cfg config.Provider) (map[string]output.Formats, error) {
2828
if !cfg.IsSet("outputs") {
29-
return createDefaultOutputFormats(cfg)
29+
return createDefaultOutputFormats(allFormats, cfg)
3030
}
3131

3232
outFormats := make(map[string]output.Formats)
@@ -64,29 +64,31 @@ func createSiteOutputFormats(allFormats output.Formats, cfg config.Provider) (ma
6464

6565
}
6666

67-
func createDefaultOutputFormats(cfg config.Provider) (map[string]output.Formats, error) {
67+
func createDefaultOutputFormats(allFormats output.Formats, cfg config.Provider) (map[string]output.Formats, error) {
6868
outFormats := make(map[string]output.Formats)
69+
rssOut, _ := allFormats.GetByName(output.RSSFormat.Name)
70+
htmlOut, _ := allFormats.GetByName(output.HTMLFormat.Name)
71+
6972
for _, kind := range allKinds {
7073
var formats output.Formats
7174
// All have HTML
72-
formats = append(formats, output.HTMLFormat)
75+
formats = append(formats, htmlOut)
7376

7477
// All but page have RSS
7578
if kind != KindPage {
76-
rssType := output.RSSFormat
7779

7880
rssBase := cfg.GetString("rssURI")
7981
if rssBase == "" || rssBase == "index.xml" {
80-
rssBase = rssType.BaseName
82+
rssBase = rssOut.BaseName
8183
} else {
8284
// Remove in Hugo 0.22.
8385
helpers.Deprecated("Site config", "rssURI", "Set baseName in outputFormats.RSS", false)
8486
// RSS has now a well defined media type, so strip any suffix provided
8587
rssBase = strings.TrimSuffix(rssBase, path.Ext(rssBase))
8688
}
8789

88-
rssType.BaseName = rssBase
89-
formats = append(formats, rssType)
90+
rssOut.BaseName = rssBase
91+
formats = append(formats, rssOut)
9092

9193
}
9294

‎hugolib/site_output_test.go‎

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131

3232
func TestDefaultOutputFormats(t *testing.T) {
3333
t.Parallel()
34-
defs, err := createDefaultOutputFormats(viper.New())
34+
defs, err := createDefaultOutputFormats(output.DefaultFormats, viper.New())
3535

3636
require.NoError(t, err)
3737

@@ -53,6 +53,30 @@ func TestDefaultOutputFormats(t *testing.T) {
5353
}
5454
}
5555

56+
func TestDefaultOutputFormatsWithOverrides(t *testing.T) {
57+
t.Parallel()
58+
59+
htmlOut := output.HTMLFormat
60+
htmlOut.BaseName = "htmlindex"
61+
rssOut := output.RSSFormat
62+
rssOut.BaseName = "feed"
63+
64+
defs, err := createDefaultOutputFormats(output.Formats{htmlOut, rssOut}, viper.New())
65+
66+
homeDefs := defs[KindHome]
67+
68+
rss, found := homeDefs.GetByName("RSS")
69+
require.True(t, found)
70+
require.Equal(t, rss.BaseName, "feed")
71+
72+
html, found := homeDefs.GetByName("HTML")
73+
require.True(t, found)
74+
require.Equal(t, html.BaseName, "htmlindex")
75+
76+
require.NoError(t, err)
77+
78+
}
79+
5680
func TestSiteWithPageOutputs(t *testing.T) {
5781
for _, outputs := range [][]string{{"html", "json", "calendar"}, {"json"}} {
5882
t.Run(fmt.Sprintf("%v", outputs), func(t *testing.T) {
@@ -231,3 +255,33 @@ Content: {{ .Content }}
231255
}
232256

233257
}
258+
259+
// Issue #3447
260+
func TestRedefineRSSOutputFormat(t *testing.T) {
261+
siteConfig := `
262+
baseURL = "http://example.com/blog"
263+
264+
paginate = 1
265+
defaultContentLanguage = "en"
266+
267+
disableKinds = ["page", "section", "taxonomy", "taxonomyTerm", "sitemap", "robotsTXT", "404"]
268+
269+
[outputFormats]
270+
[outputFormats.RSS]
271+
mediatype = "application/rss"
272+
baseName = "feed"
273+
274+
`
275+
276+
mf := afero.NewMemMapFs()
277+
writeToFs(t, mf, "content/foo.html", `foo`)
278+
279+
th, h := newTestSitesFromConfig(t, mf, siteConfig)
280+
281+
err := h.Build(BuildCfg{})
282+
283+
require.NoError(t, err)
284+
285+
th.assertFileContent("public/feed.xml", "Recent content on")
286+
287+
}

0 commit comments

Comments
 (0)