Skip to content

Commit f188763

Browse files
committed
For render hooks, only fallback to HTML (or the defaultOutputFormat) template
Closes gohugoio#13242
1 parent de7137c commit f188763

File tree

4 files changed

+115
-43
lines changed

4 files changed

+115
-43
lines changed

config/allconfig/allconfig.go

+51-32
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,19 @@ func (c *Config) CompileConfig(logger loggers.Logger) error {
301301
}
302302
}
303303

304+
defaultOutputFormat := outputFormats[0]
305+
c.DefaultOutputFormat = strings.ToLower(c.DefaultOutputFormat)
306+
if c.DefaultOutputFormat != "" {
307+
f, found := outputFormats.GetByName(c.DefaultOutputFormat)
308+
if !found {
309+
return fmt.Errorf("unknown default output format %q", c.DefaultOutputFormat)
310+
}
311+
defaultOutputFormat = f
312+
313+
} else {
314+
c.DefaultOutputFormat = defaultOutputFormat.Name
315+
}
316+
304317
disabledLangs := make(map[string]bool)
305318
for _, lang := range c.DisableLanguages {
306319
disabledLangs[lang] = true
@@ -391,22 +404,23 @@ func (c *Config) CompileConfig(logger loggers.Logger) error {
391404
}
392405

393406
c.C = &ConfigCompiled{
394-
Timeout: timeout,
395-
BaseURL: baseURL,
396-
BaseURLLiveReload: baseURL,
397-
DisabledKinds: disabledKinds,
398-
DisabledLanguages: disabledLangs,
399-
IgnoredLogs: ignoredLogIDs,
400-
KindOutputFormats: kindOutputFormats,
401-
ContentTypes: media.DefaultContentTypes.FromTypes(c.MediaTypes.Config),
402-
CreateTitle: helpers.GetTitleFunc(c.TitleCaseStyle),
403-
IsUglyURLSection: isUglyURL,
404-
IgnoreFile: ignoreFile,
405-
SegmentFilter: c.Segments.Config.Get(func(s string) { logger.Warnf("Render segment %q not found in configuration", s) }, c.RootConfig.RenderSegments...),
406-
MainSections: c.MainSections,
407-
Clock: clock,
408-
HTTPCache: httpCache,
409-
transientErr: transientErr,
407+
Timeout: timeout,
408+
BaseURL: baseURL,
409+
BaseURLLiveReload: baseURL,
410+
DisabledKinds: disabledKinds,
411+
DisabledLanguages: disabledLangs,
412+
IgnoredLogs: ignoredLogIDs,
413+
KindOutputFormats: kindOutputFormats,
414+
DefaultOutputFormat: defaultOutputFormat,
415+
ContentTypes: media.DefaultContentTypes.FromTypes(c.MediaTypes.Config),
416+
CreateTitle: helpers.GetTitleFunc(c.TitleCaseStyle),
417+
IsUglyURLSection: isUglyURL,
418+
IgnoreFile: ignoreFile,
419+
SegmentFilter: c.Segments.Config.Get(func(s string) { logger.Warnf("Render segment %q not found in configuration", s) }, c.RootConfig.RenderSegments...),
420+
MainSections: c.MainSections,
421+
Clock: clock,
422+
HTTPCache: httpCache,
423+
transientErr: transientErr,
410424
}
411425

412426
for _, s := range allDecoderSetups {
@@ -430,22 +444,23 @@ func (c *Config) IsLangDisabled(lang string) bool {
430444

431445
// ConfigCompiled holds values and functions that are derived from the config.
432446
type ConfigCompiled struct {
433-
Timeout time.Duration
434-
BaseURL urls.BaseURL
435-
BaseURLLiveReload urls.BaseURL
436-
ServerInterface string
437-
KindOutputFormats map[string]output.Formats
438-
ContentTypes media.ContentTypes
439-
DisabledKinds map[string]bool
440-
DisabledLanguages map[string]bool
441-
IgnoredLogs map[string]bool
442-
CreateTitle func(s string) string
443-
IsUglyURLSection func(section string) bool
444-
IgnoreFile func(filename string) bool
445-
SegmentFilter segments.SegmentFilter
446-
MainSections []string
447-
Clock time.Time
448-
HTTPCache httpcache.ConfigCompiled
447+
Timeout time.Duration
448+
BaseURL urls.BaseURL
449+
BaseURLLiveReload urls.BaseURL
450+
ServerInterface string
451+
KindOutputFormats map[string]output.Formats
452+
DefaultOutputFormat output.Format
453+
ContentTypes media.ContentTypes
454+
DisabledKinds map[string]bool
455+
DisabledLanguages map[string]bool
456+
IgnoredLogs map[string]bool
457+
CreateTitle func(s string) string
458+
IsUglyURLSection func(section string) bool
459+
IgnoreFile func(filename string) bool
460+
SegmentFilter segments.SegmentFilter
461+
MainSections []string
462+
Clock time.Time
463+
HTTPCache httpcache.ConfigCompiled
449464

450465
// This is set to the last transient error found during config compilation.
451466
// With themes/modules we compute the configuration in multiple passes, and
@@ -505,6 +520,10 @@ type RootConfig struct {
505520
// Set this to true to put all languages below their language ID.
506521
DefaultContentLanguageInSubdir bool
507522

523+
// The default output format to use for the site.
524+
// If not set, we will use the first output format.
525+
DefaultOutputFormat string
526+
508527
// Disable generation of redirect to the default language when DefaultContentLanguageInSubdir is enabled.
509528
DisableDefaultLanguageRedirect bool
510529

hugolib/content_render_hooks_test.go

+57
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,63 @@ xml-heading: Heading in p2|
7979
`)
8080
}
8181

82+
// Issue 13242.
83+
func TestRenderHooksRSSOnly(t *testing.T) {
84+
files := `
85+
-- hugo.toml --
86+
baseURL = "https://example.org"
87+
disableKinds = ["taxonomy", "term"]
88+
-- layouts/index.html --
89+
{{ $p := site.GetPage "p1.md" }}
90+
{{ $p2 := site.GetPage "p2.md" }}
91+
P1: {{ $p.Content }}
92+
P2: {{ $p2.Content }}
93+
-- layouts/index.xml --
94+
{{ $p2 := site.GetPage "p2.md" }}
95+
{{ $p3 := site.GetPage "p3.md" }}
96+
P2: {{ $p2.Content }}
97+
P3: {{ $p3.Content }}
98+
-- layouts/_default/_markup/render-link.rss.xml --
99+
xml-link: {{ .Destination | safeURL }}|
100+
-- layouts/_default/_markup/render-heading.rss.xml --
101+
xml-heading: {{ .Text }}|
102+
-- content/p1.md --
103+
---
104+
title: "p1"
105+
---
106+
P1. [I'm an inline-style link](https://www.gohugo.io)
107+
108+
# Heading in p1
109+
110+
-- content/p2.md --
111+
---
112+
title: "p2"
113+
---
114+
P2. [I'm an inline-style link](https://www.bep.is)
115+
116+
# Heading in p2
117+
118+
-- content/p3.md --
119+
---
120+
title: "p3"
121+
outputs: ["rss"]
122+
---
123+
P3. [I'm an inline-style link](https://www.example.org)
124+
`
125+
b := Test(t, files)
126+
127+
b.AssertFileContent("public/index.html", `
128+
P1: <p>P1. <a href="https://www.gohugo.io">I&rsquo;m an inline-style link</a></p>
129+
<h1 id="heading-in-p1">Heading in p1</h1>
130+
<h1 id="heading-in-p2">Heading in p2</h1>
131+
`)
132+
b.AssertFileContent("public/index.xml", `
133+
P2: <p>P2. xml-link: https://www.bep.is|</p>
134+
P3: <p>P3. xml-link: https://www.example.org|</p>
135+
xml-heading: Heading in p2|
136+
`)
137+
}
138+
82139
// https://github.com/gohugoio/hugo/issues/6629
83140
func TestRenderLinkWithMarkupInText(t *testing.T) {
84141
b := newTestSitesBuilder(t)

hugolib/page__per_output.go

+6-10
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,9 @@ func (pco *pageContentOutput) initRenderHooks() error {
318318
}
319319
if found {
320320
if isitp, ok := templ.(tpl.IsInternalTemplateProvider); ok && isitp.IsInternalTemplate() {
321+
321322
renderHookConfig := pco.po.p.s.conf.Markup.Goldmark.RenderHooks
323+
322324
switch templ.Name() {
323325
case "_default/_markup/render-link.html":
324326
if !renderHookConfig.Link.IsEnableDefault() {
@@ -335,17 +337,10 @@ func (pco *pageContentOutput) initRenderHooks() error {
335337
}
336338

337339
templ, found1 := getHookTemplate(pco.po.f)
338-
339340
if !found1 || pco.po.p.reusePageOutputContent() {
340-
// Some hooks may only be available in HTML, and if
341-
// this site is configured to not have HTML output, we need to
342-
// make sure we have a fallback. This should be very rare.
343341
candidates := pco.po.p.s.renderFormats
344-
if pco.po.f.MediaType.FirstSuffix.Suffix != "html" {
345-
if _, found := candidates.GetBySuffix("html"); !found {
346-
candidates = append(candidates, output.HTMLFormat)
347-
}
348-
}
342+
defaultOutputFormat := pco.po.p.s.conf.C.DefaultOutputFormat
343+
349344
// Check if some of the other output formats would give a different template.
350345
for _, f := range candidates {
351346
if f.Name == pco.po.f.Name {
@@ -354,7 +349,7 @@ func (pco *pageContentOutput) initRenderHooks() error {
354349
templ2, found2 := getHookTemplate(f)
355350

356351
if found2 {
357-
if !found1 {
352+
if !found1 && f.Name == defaultOutputFormat.Name {
358353
templ = templ2
359354
found1 = true
360355
break
@@ -367,6 +362,7 @@ func (pco *pageContentOutput) initRenderHooks() error {
367362
}
368363
}
369364
}
365+
370366
if !found1 {
371367
if tp == hooks.CodeBlockRendererType {
372368
// No user provided template for code blocks, so we use the native Go version -- which is also faster.

hugolib/rebuild_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ func TestRebuildRenameTextFileInLeafBundle(t *testing.T) {
208208
b.RenameFile("content/mysection/mysectionbundle/mysectionbundletext.txt", "content/mysection/mysectionbundle/mysectionbundletext2.txt").Build()
209209
b.AssertFileContent("public/mysection/mysectionbundle/index.html", "mysectionbundletext2", "My Section Bundle Text 2 Content.", "Len Resources: 2|")
210210
b.AssertRenderCountPage(8)
211-
b.AssertRenderCountContent(8)
211+
b.AssertRenderCountContent(9)
212212
})
213213
}
214214

0 commit comments

Comments
 (0)