]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
tpl/tplimpl: Copy embedded HTML table render hook to each output format
authorJoe Mooring <joe.mooring@veriphor.com>
Fri, 20 Jun 2025 17:42:56 +0000 (10:42 -0700)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sat, 21 Jun 2025 12:37:47 +0000 (14:37 +0200)
Closes #13351

tpl/tplimpl/templatestore.go
tpl/tplimpl/templatestore_integration_test.go

index bbb7f27cc57a63f18056794070a5b3d4d6d60f46..23c821cac3ef1041f7faf8fdc25a3fbc1a4bad6d 100644 (file)
@@ -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
                }
index 0b3ce7a563a2d035273754c641d629191d9c63a2..9da95935013a528311b8c9a961a87ef9e42defc3 100644 (file)
@@ -1508,3 +1508,43 @@ mytexts|safeHTML: {{ partial "mytext.txt" . | safeHTML }}
                "mytexts|safeHTML: <div>mytext</div>",
        )
 }
+
+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", "<table>")
+       b.AssertFileContent("public/index.json", "<table>")
+
+       f := strings.ReplaceAll(files, "weight = 1", "weight = 0")
+       b = hugolib.Test(t, f)
+       b.AssertFileContent("public/index.html", "<table>")
+       b.AssertFileContent("public/index.json", "<table>")
+
+       f = strings.ReplaceAll(files, "weight = 1", "")
+       b = hugolib.Test(t, f)
+       b.AssertFileContent("public/index.html", "<table>")
+       b.AssertFileContent("public/index.json", "<table>")
+}