For render hooks, only fallback to HTML (or the defaultOutputFormat) template

Closes #13242
This commit is contained in:
Bjørn Erik Pedersen 2025-01-15 15:45:13 +01:00
parent de7137cc35
commit 76993369a0
4 changed files with 117 additions and 36 deletions

View file

@ -301,6 +301,18 @@ func (c *Config) CompileConfig(logger loggers.Logger) error {
} }
} }
defaultOutputFormat := outputFormats[0]
c.DefaultOutputFormat = strings.ToLower(c.DefaultOutputFormat)
if c.DefaultOutputFormat != "" {
f, found := outputFormats.GetByName(c.DefaultOutputFormat)
if !found {
return fmt.Errorf("unknown default output format %q", c.DefaultOutputFormat)
}
defaultOutputFormat = f
} else {
c.DefaultOutputFormat = defaultOutputFormat.Name
}
disabledLangs := make(map[string]bool) disabledLangs := make(map[string]bool)
for _, lang := range c.DisableLanguages { for _, lang := range c.DisableLanguages {
disabledLangs[lang] = true disabledLangs[lang] = true
@ -398,6 +410,7 @@ func (c *Config) CompileConfig(logger loggers.Logger) error {
DisabledLanguages: disabledLangs, DisabledLanguages: disabledLangs,
IgnoredLogs: ignoredLogIDs, IgnoredLogs: ignoredLogIDs,
KindOutputFormats: kindOutputFormats, KindOutputFormats: kindOutputFormats,
DefaultOutputFormat: defaultOutputFormat,
ContentTypes: media.DefaultContentTypes.FromTypes(c.MediaTypes.Config), ContentTypes: media.DefaultContentTypes.FromTypes(c.MediaTypes.Config),
CreateTitle: helpers.GetTitleFunc(c.TitleCaseStyle), CreateTitle: helpers.GetTitleFunc(c.TitleCaseStyle),
IsUglyURLSection: isUglyURL, IsUglyURLSection: isUglyURL,
@ -435,6 +448,7 @@ type ConfigCompiled struct {
BaseURLLiveReload urls.BaseURL BaseURLLiveReload urls.BaseURL
ServerInterface string ServerInterface string
KindOutputFormats map[string]output.Formats KindOutputFormats map[string]output.Formats
DefaultOutputFormat output.Format
ContentTypes media.ContentTypes ContentTypes media.ContentTypes
DisabledKinds map[string]bool DisabledKinds map[string]bool
DisabledLanguages map[string]bool DisabledLanguages map[string]bool
@ -505,6 +519,10 @@ type RootConfig struct {
// Set this to true to put all languages below their language ID. // Set this to true to put all languages below their language ID.
DefaultContentLanguageInSubdir bool DefaultContentLanguageInSubdir bool
// The default output format to use for the site.
// If not set, we will use the first output format.
DefaultOutputFormat string
// Disable generation of redirect to the default language when DefaultContentLanguageInSubdir is enabled. // Disable generation of redirect to the default language when DefaultContentLanguageInSubdir is enabled.
DisableDefaultLanguageRedirect bool DisableDefaultLanguageRedirect bool

View file

@ -79,6 +79,63 @@ xml-heading: Heading in p2|
`) `)
} }
// Issue 13242.
func TestRenderHooksRSSOnly(t *testing.T) {
files := `
-- hugo.toml --
baseURL = "https://example.org"
disableKinds = ["taxonomy", "term"]
-- layouts/index.html --
{{ $p := site.GetPage "p1.md" }}
{{ $p2 := site.GetPage "p2.md" }}
P1: {{ $p.Content }}
P2: {{ $p2.Content }}
-- layouts/index.xml --
{{ $p2 := site.GetPage "p2.md" }}
{{ $p3 := site.GetPage "p3.md" }}
P2: {{ $p2.Content }}
P3: {{ $p3.Content }}
-- layouts/_default/_markup/render-link.rss.xml --
xml-link: {{ .Destination | safeURL }}|
-- layouts/_default/_markup/render-heading.rss.xml --
xml-heading: {{ .Text }}|
-- content/p1.md --
---
title: "p1"
---
P1. [I'm an inline-style link](https://www.gohugo.io)
# Heading in p1
-- content/p2.md --
---
title: "p2"
---
P2. [I'm an inline-style link](https://www.bep.is)
# Heading in p2
-- content/p3.md --
---
title: "p3"
outputs: ["rss"]
---
P3. [I'm an inline-style link](https://www.example.org)
`
b := Test(t, files)
b.AssertFileContent("public/index.html", `
P1: <p>P1. <a href="https://www.gohugo.io">I&rsquo;m an inline-style link</a></p>
<h1 id="heading-in-p1">Heading in p1</h1>
<h1 id="heading-in-p2">Heading in p2</h1>
`)
b.AssertFileContent("public/index.xml", `
P2: <p>P2. xml-link: https://www.bep.is|</p>
P3: <p>P3. xml-link: https://www.example.org|</p>
xml-heading: Heading in p2|
`)
}
// https://github.com/gohugoio/hugo/issues/6629 // https://github.com/gohugoio/hugo/issues/6629
func TestRenderLinkWithMarkupInText(t *testing.T) { func TestRenderLinkWithMarkupInText(t *testing.T) {
b := newTestSitesBuilder(t) b := newTestSitesBuilder(t)

View file

@ -318,7 +318,9 @@ func (pco *pageContentOutput) initRenderHooks() error {
} }
if found { if found {
if isitp, ok := templ.(tpl.IsInternalTemplateProvider); ok && isitp.IsInternalTemplate() { if isitp, ok := templ.(tpl.IsInternalTemplateProvider); ok && isitp.IsInternalTemplate() {
renderHookConfig := pco.po.p.s.conf.Markup.Goldmark.RenderHooks renderHookConfig := pco.po.p.s.conf.Markup.Goldmark.RenderHooks
switch templ.Name() { switch templ.Name() {
case "_default/_markup/render-link.html": case "_default/_markup/render-link.html":
if !renderHookConfig.Link.IsEnableDefault() { if !renderHookConfig.Link.IsEnableDefault() {
@ -335,17 +337,20 @@ func (pco *pageContentOutput) initRenderHooks() error {
} }
templ, found1 := getHookTemplate(pco.po.f) templ, found1 := getHookTemplate(pco.po.f)
if !found1 || pco.po.p.reusePageOutputContent() { if !found1 || pco.po.p.reusePageOutputContent() {
defaultOutputFormat := pco.po.p.s.conf.C.DefaultOutputFormat
candidates := pco.po.p.s.renderFormats
// Some hooks may only be available in HTML, and if // Some hooks may only be available in HTML, and if
// this site is configured to not have HTML output, we need to // this site is configured to not have HTML output, we need to
// make sure we have a fallback. This should be very rare. // make sure we have a fallback. This should be very rare.
candidates := pco.po.p.s.renderFormats
if pco.po.f.MediaType.FirstSuffix.Suffix != "html" { if pco.po.f.MediaType.FirstSuffix.Suffix != "html" {
if _, found := candidates.GetBySuffix("html"); !found { if _, found := candidates.GetBySuffix("html"); !found {
candidates = append(candidates, output.HTMLFormat) candidates = append(candidates, output.HTMLFormat)
} }
} }
// Check if some of the other output formats would give a different template. // Check if some of the other output formats would give a different template.
for _, f := range candidates { for _, f := range candidates {
if f.Name == pco.po.f.Name { if f.Name == pco.po.f.Name {
@ -354,7 +359,7 @@ func (pco *pageContentOutput) initRenderHooks() error {
templ2, found2 := getHookTemplate(f) templ2, found2 := getHookTemplate(f)
if found2 { if found2 {
if !found1 { if !found1 && f.Name == defaultOutputFormat.Name {
templ = templ2 templ = templ2
found1 = true found1 = true
break break
@ -367,6 +372,7 @@ func (pco *pageContentOutput) initRenderHooks() error {
} }
} }
} }
if !found1 { if !found1 {
if tp == hooks.CodeBlockRendererType { if tp == hooks.CodeBlockRendererType {
// No user provided template for code blocks, so we use the native Go version -- which is also faster. // No user provided template for code blocks, so we use the native Go version -- which is also faster.

View file

@ -208,7 +208,7 @@ func TestRebuildRenameTextFileInLeafBundle(t *testing.T) {
b.RenameFile("content/mysection/mysectionbundle/mysectionbundletext.txt", "content/mysection/mysectionbundle/mysectionbundletext2.txt").Build() b.RenameFile("content/mysection/mysectionbundle/mysectionbundletext.txt", "content/mysection/mysectionbundle/mysectionbundletext2.txt").Build()
b.AssertFileContent("public/mysection/mysectionbundle/index.html", "mysectionbundletext2", "My Section Bundle Text 2 Content.", "Len Resources: 2|") b.AssertFileContent("public/mysection/mysectionbundle/index.html", "mysectionbundletext2", "My Section Bundle Text 2 Content.", "Len Resources: 2|")
b.AssertRenderCountPage(8) b.AssertRenderCountPage(8)
b.AssertRenderCountContent(8) b.AssertRenderCountContent(9)
}) })
} }