Skip to content

Commit 7699336

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

File tree

4 files changed

+117
-36
lines changed

4 files changed

+117
-36
lines changed

config/allconfig/allconfig.go

+50-32
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,18 @@ 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+
} else {
313+
c.DefaultOutputFormat = defaultOutputFormat.Name
314+
}
315+
304316
disabledLangs := make(map[string]bool)
305317
for _, lang := range c.DisableLanguages {
306318
disabledLangs[lang] = true
@@ -391,22 +403,23 @@ func (c *Config) CompileConfig(logger loggers.Logger) error {
391403
}
392404

393405
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,
406+
Timeout: timeout,
407+
BaseURL: baseURL,
408+
BaseURLLiveReload: baseURL,
409+
DisabledKinds: disabledKinds,
410+
DisabledLanguages: disabledLangs,
411+
IgnoredLogs: ignoredLogIDs,
412+
KindOutputFormats: kindOutputFormats,
413+
DefaultOutputFormat: defaultOutputFormat,
414+
ContentTypes: media.DefaultContentTypes.FromTypes(c.MediaTypes.Config),
415+
CreateTitle: helpers.GetTitleFunc(c.TitleCaseStyle),
416+
IsUglyURLSection: isUglyURL,
417+
IgnoreFile: ignoreFile,
418+
SegmentFilter: c.Segments.Config.Get(func(s string) { logger.Warnf("Render segment %q not found in configuration", s) }, c.RootConfig.RenderSegments...),
419+
MainSections: c.MainSections,
420+
Clock: clock,
421+
HTTPCache: httpCache,
422+
transientErr: transientErr,
410423
}
411424

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

431444
// ConfigCompiled holds values and functions that are derived from the config.
432445
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
446+
Timeout time.Duration
447+
BaseURL urls.BaseURL
448+
BaseURLLiveReload urls.BaseURL
449+
ServerInterface string
450+
KindOutputFormats map[string]output.Formats
451+
DefaultOutputFormat output.Format
452+
ContentTypes media.ContentTypes
453+
DisabledKinds map[string]bool
454+
DisabledLanguages map[string]bool
455+
IgnoredLogs map[string]bool
456+
CreateTitle func(s string) string
457+
IsUglyURLSection func(section string) bool
458+
IgnoreFile func(filename string) bool
459+
SegmentFilter segments.SegmentFilter
460+
MainSections []string
461+
Clock time.Time
462+
HTTPCache httpcache.ConfigCompiled
449463

450464
// This is set to the last transient error found during config compilation.
451465
// With themes/modules we compute the configuration in multiple passes, and
@@ -505,6 +519,10 @@ type RootConfig struct {
505519
// Set this to true to put all languages below their language ID.
506520
DefaultContentLanguageInSubdir bool
507521

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

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

+9-3
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,20 @@ func (pco *pageContentOutput) initRenderHooks() error {
335337
}
336338

337339
templ, found1 := getHookTemplate(pco.po.f)
338-
339340
if !found1 || pco.po.p.reusePageOutputContent() {
341+
defaultOutputFormat := pco.po.p.s.conf.C.DefaultOutputFormat
342+
343+
candidates := pco.po.p.s.renderFormats
344+
340345
// Some hooks may only be available in HTML, and if
341346
// this site is configured to not have HTML output, we need to
342347
// make sure we have a fallback. This should be very rare.
343-
candidates := pco.po.p.s.renderFormats
344348
if pco.po.f.MediaType.FirstSuffix.Suffix != "html" {
345349
if _, found := candidates.GetBySuffix("html"); !found {
346350
candidates = append(candidates, output.HTMLFormat)
347351
}
348352
}
353+
349354
// Check if some of the other output formats would give a different template.
350355
for _, f := range candidates {
351356
if f.Name == pco.po.f.Name {
@@ -354,7 +359,7 @@ func (pco *pageContentOutput) initRenderHooks() error {
354359
templ2, found2 := getHookTemplate(f)
355360

356361
if found2 {
357-
if !found1 {
362+
if !found1 && f.Name == defaultOutputFormat.Name {
358363
templ = templ2
359364
found1 = true
360365
break
@@ -367,6 +372,7 @@ func (pco *pageContentOutput) initRenderHooks() error {
367372
}
368373
}
369374
}
375+
370376
if !found1 {
371377
if tp == hooks.CodeBlockRendererType {
372378
// 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)