From 18a9ca7d7a03f3162a1b950fa3ba2a5e578fccc7 Mon Sep 17 00:00:00 2001 From: Joe Mooring Date: Fri, 20 Jun 2025 10:42:56 -0700 Subject: [PATCH] tpl/tplimpl: Copy embedded HTML table render hook to each output format Closes #13351 --- tpl/tplimpl/templatestore.go | 19 +++++++-- tpl/tplimpl/templatestore_integration_test.go | 40 +++++++++++++++++++ 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/tpl/tplimpl/templatestore.go b/tpl/tplimpl/templatestore.go index bbb7f27cc..23c821cac 100644 --- a/tpl/tplimpl/templatestore.go +++ b/tpl/tplimpl/templatestore.go @@ -1018,7 +1018,7 @@ func (s *TemplateStore) allRawTemplates() iter.Seq[tpl.Template] { } func (s *TemplateStore) insertEmbedded() error { - return fs.WalkDir(embeddedTemplatesFs, ".", func(path string, d fs.DirEntry, err error) error { + return fs.WalkDir(embeddedTemplatesFs, ".", func(tpath string, d fs.DirEntry, err error) error { if err != nil { return err } @@ -1026,7 +1026,7 @@ func (s *TemplateStore) insertEmbedded() error { return nil } - templb, err := embeddedTemplatesFs.ReadFile(path) + templb, err := embeddedTemplatesFs.ReadFile(tpath) if err != nil { return err } @@ -1034,7 +1034,7 @@ func (s *TemplateStore) insertEmbedded() error { // Get the newlines on Windows in line with how we had it back when we used Go Generate // to write the templates to Go files. templ := string(bytes.ReplaceAll(templb, []byte("\r\n"), []byte("\n"))) - name := strings.TrimPrefix(filepath.ToSlash(path), "embedded/templates/") + name := strings.TrimPrefix(filepath.ToSlash(tpath), "embedded/templates/") insertOne := func(name, content string) error { pi := s.opts.PathParser.Parse(files.ComponentFolderLayouts, name) @@ -1064,6 +1064,19 @@ func (s *TemplateStore) insertEmbedded() error { return nil } + // Copy the embedded HTML table render hook to each output format. + // See https://github.com/gohugoio/hugo/issues/13351. + if name == path.Join(containerMarkup, "render-table.html") { + for _, of := range s.opts.OutputFormats { + path := paths.TrimExt(name) + "." + of.Name + of.MediaType.FirstSuffix.FullSuffix + if err := insertOne(path, templ); err != nil { + return err + } + } + + return nil + } + if err := insertOne(name, templ); err != nil { return err } diff --git a/tpl/tplimpl/templatestore_integration_test.go b/tpl/tplimpl/templatestore_integration_test.go index 0b3ce7a56..9da959350 100644 --- a/tpl/tplimpl/templatestore_integration_test.go +++ b/tpl/tplimpl/templatestore_integration_test.go @@ -1508,3 +1508,43 @@ mytexts|safeHTML: {{ partial "mytext.txt" . | safeHTML }} "mytexts|safeHTML:
mytext
", ) } + +func TestIssue13351(t *testing.T) { + t.Parallel() + + files := ` +-- hugo.toml -- +disableKinds = ['page','rss','section','sitemap','taxonomy','term'] +[outputs] +home = ['html','json'] +[outputFormats.html] +weight = 1 +[outputFormats.json] +weight = 2 +-- content/_index.md -- +--- +title: home +--- +a|b +:--|:-- +1|2 +-- layouts/index.html -- +{{ .Content }} +-- layouts/index.json -- +{{ .Content }} +` + + b := hugolib.Test(t, files) + b.AssertFileContent("public/index.html", "") + b.AssertFileContent("public/index.json", "
") + + f := strings.ReplaceAll(files, "weight = 1", "weight = 0") + b = hugolib.Test(t, f) + b.AssertFileContent("public/index.html", "
") + b.AssertFileContent("public/index.json", "
") + + f = strings.ReplaceAll(files, "weight = 1", "") + b = hugolib.Test(t, f) + b.AssertFileContent("public/index.html", "
") + b.AssertFileContent("public/index.json", "
") +}