markup/goldmark: Change link and image render hook enablement to enums

Closes #13535
This commit is contained in:
Joe Mooring
2025-06-21 06:41:08 -07:00
committed by Bjørn Erik Pedersen
parent b8ba33ca95
commit 84b31721bf
9 changed files with 293 additions and 35 deletions

View File

@@ -192,3 +192,115 @@ iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAA
`<img src="/dir/p1/pixel.png" alt="alt4" id="&#34;&gt;&lt;script&gt;alert()&lt;/script&gt;">`,
)
}
// Issue 13535
func TestEmbeddedLinkAndImageRenderHookConfig(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
disableKinds = ['home','rss','section','sitemap','taxonomy','term']
[markup.goldmark]
duplicateResourceFiles = false
[markup.goldmark.renderHooks.image]
#KEY_VALUE
[markup.goldmark.renderHooks.link]
#KEY_VALUE
#LANGUAGES
-- content/s1/p1/index.md --
---
title: p1
---
[p2](p2)
[a](a.txt)
![b](b.jpg)
-- content/s1/p1/a.txt --
-- content/s1/p1/b.jpg --
-- content/s1/p2.md --
---
title: p2
---
-- layouts/all.html --
{{ .Content }}
`
const customHooks string = `
-- layouts/s1/_markup/render-link.html --
custom link render hook: {{ .Text }}|{{ .Destination }}
-- layouts/s1/_markup/render-image.html --
custom image render hook: {{ .Text }}|{{ .Destination }}
`
const languages string = `
[languages.en]
[languages.fr]
`
const (
fileToCheck = "public/s1/p1/index.html"
wantCustom string = "<p>custom link render hook: p2|p2</p>\n<p>custom link render hook: a|a.txt</p>\n<p>custom image render hook: b|b.jpg</p>"
wantEmbedded string = "<p><a href=\"/s1/p2/\">p2</a></p>\n<p><a href=\"/s1/p1/a.txt\">a</a></p>\n<p><img src=\"/s1/p1/b.jpg\" alt=\"b\"></p>"
wantGoldmark string = "<p><a href=\"p2\">p2</a></p>\n<p><a href=\"a.txt\">a</a></p>\n<p><img src=\"b.jpg\" alt=\"b\"></p>"
)
tests := []struct {
id string // the test id
isMultilingual bool // whether the site is multilingual single-host
hasCustomHooks bool // whether the site has custom link and image render hooks
keyValuePair string // the enableDefault (deprecated in v0.148.0) or useEmbedded key-value pair
want string // the expected content of public/s1/p1/index.html
}{
{"01", false, false, "", wantGoldmark}, // monolingual
{"02", false, false, "enableDefault = false", wantGoldmark}, // monolingual, enableDefault = false
{"03", false, false, "enableDefault = true", wantEmbedded}, // monolingual, enableDefault = true
{"04", false, false, "useEmbedded = 'always'", wantEmbedded}, // monolingual, useEmbedded = 'always'
{"05", false, false, "useEmbedded = 'auto'", wantGoldmark}, // monolingual, useEmbedded = 'auto'
{"06", false, false, "useEmbedded = 'fallback'", wantEmbedded}, // monolingual, useEmbedded = 'fallback'
{"07", false, false, "useEmbedded = 'never'", wantGoldmark}, // monolingual, useEmbedded = 'never'
{"08", false, true, "", wantCustom}, // monolingual, with custom hooks
{"09", false, true, "enableDefault = false", wantCustom}, // monolingual, with custom hooks, enableDefault = false
{"10", false, true, "enableDefault = true", wantCustom}, // monolingual, with custom hooks, enableDefault = true
{"11", false, true, "useEmbedded = 'always'", wantEmbedded}, // monolingual, with custom hooks, useEmbedded = 'always'
{"12", false, true, "useEmbedded = 'auto'", wantCustom}, // monolingual, with custom hooks, useEmbedded = 'auto'
{"13", false, true, "useEmbedded = 'fallback'", wantCustom}, // monolingual, with custom hooks, useEmbedded = 'fallback'
{"14", false, true, "useEmbedded = 'never'", wantCustom}, // monolingual, with custom hooks, useEmbedded = 'never'
{"15", true, false, "", wantEmbedded}, // multilingual
{"16", true, false, "enableDefault = false", wantGoldmark}, // multilingual, enableDefault = false
{"17", true, false, "enableDefault = true", wantEmbedded}, // multilingual, enableDefault = true
{"18", true, false, "useEmbedded = 'always'", wantEmbedded}, // multilingual, useEmbedded = 'always'
{"19", true, false, "useEmbedded = 'auto'", wantEmbedded}, // multilingual, useEmbedded = 'auto'
{"20", true, false, "useEmbedded = 'fallback'", wantEmbedded}, // multilingual, useEmbedded = 'fallback'
{"21", true, false, "useEmbedded = 'never'", wantGoldmark}, // multilingual, useEmbedded = 'never'
{"22", true, true, "", wantCustom}, // multilingual, with custom hooks
{"23", true, true, "enableDefault = false", wantCustom}, // multilingual, with custom hooks, enableDefault = false
{"24", true, true, "enableDefault = true", wantCustom}, // multilingual, with custom hooks, enableDefault = true
{"25", true, true, "useEmbedded = 'always'", wantEmbedded}, // multilingual, with custom hooks, useEmbedded = 'always'
{"26", true, true, "useEmbedded = 'auto'", wantCustom}, // multilingual, with custom hooks, useEmbedded = 'auto'
{"27", true, true, "useEmbedded = 'fallback'", wantCustom}, // multilingual, with custom hooks, useEmbedded = 'fallback'
{"28", true, true, "useEmbedded = 'never'", wantCustom}, // multilingual, with custom hooks, useEmbedded = 'never'
}
for _, tt := range tests {
t.Run(tt.id, func(t *testing.T) {
t.Parallel()
f := files
if tt.isMultilingual {
f = strings.ReplaceAll(f, "#LANGUAGES", languages)
}
if tt.hasCustomHooks {
f = f + customHooks
}
f = strings.ReplaceAll(f, "#KEY_VALUE", tt.keyValuePair)
b := hugolib.Test(t, f)
b.AssertFileContent(fileToCheck, tt.want)
})
}
}

View File

@@ -44,6 +44,7 @@ import (
"github.com/gohugoio/hugo/hugofs/files"
"github.com/gohugoio/hugo/hugolib/doctree"
"github.com/gohugoio/hugo/identity"
gc "github.com/gohugoio/hugo/markup/goldmark/goldmark_config"
"github.com/gohugoio/hugo/media"
"github.com/gohugoio/hugo/metrics"
"github.com/gohugoio/hugo/output"
@@ -202,6 +203,9 @@ type StoreOptions struct {
// Whether we are in watch or server mode.
Watching bool
// The render hook configuration.
RenderHooks gc.RenderHooks
// compiled.
legacyMappingTaxonomy map[string]legacyOrdinalMapping
legacyMappingTerm map[string]legacyOrdinalMapping
@@ -1422,20 +1426,36 @@ func (s *TemplateStore) insertTemplates(include func(fi hugofs.FileMetaInfo) boo
var ti *TemplInfo
var err error
if pi.Type() == paths.TypeShortcode {
ti, err = s.insertShortcode(pi, fi, partialRebuild, s.treeShortcodes)
if err != nil || ti == nil {
return err
var insertFunc func() (*TemplInfo, error)
insertFunc = func() (*TemplInfo, error) {
return s.insertTemplate(pi, fi, SubCategoryMain, partialRebuild, s.treeMain)
}
switch pi.Type() {
case paths.TypeShortcode:
insertFunc = func() (*TemplInfo, error) {
return s.insertShortcode(pi, fi, partialRebuild, s.treeShortcodes)
}
} else {
ti, err = s.insertTemplate(pi, fi, SubCategoryMain, partialRebuild, s.treeMain)
case paths.TypeMarkup:
skipImageRenderHook := pi.Name() == "render-image.html" && s.opts.RenderHooks.Image.UseEmbedded == "always"
skipLinkRenderHook := pi.Name() == "render-link.html" && s.opts.RenderHooks.Link.UseEmbedded == "always"
if skipImageRenderHook || skipLinkRenderHook {
insertFunc = nil
}
}
if insertFunc != nil {
ti, err = insertFunc()
if err != nil || ti == nil {
return err
}
}
if err := s.tns.readTemplateInto(ti); err != nil {
return err
if ti != nil {
if err := s.tns.readTemplateInto(ti); err != nil {
return err
}
}
return nil