Skip to content

Commit 18a9ca7

Browse files
jmooringbep
authored andcommitted
tpl/tplimpl: Copy embedded HTML table render hook to each output format
Closes #13351
1 parent b6c8dfa commit 18a9ca7

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

tpl/tplimpl/templatestore.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,23 +1018,23 @@ func (s *TemplateStore) allRawTemplates() iter.Seq[tpl.Template] {
10181018
}
10191019

10201020
func (s *TemplateStore) insertEmbedded() error {
1021-
return fs.WalkDir(embeddedTemplatesFs, ".", func(path string, d fs.DirEntry, err error) error {
1021+
return fs.WalkDir(embeddedTemplatesFs, ".", func(tpath string, d fs.DirEntry, err error) error {
10221022
if err != nil {
10231023
return err
10241024
}
10251025
if d == nil || d.IsDir() || strings.HasPrefix(d.Name(), ".") {
10261026
return nil
10271027
}
10281028

1029-
templb, err := embeddedTemplatesFs.ReadFile(path)
1029+
templb, err := embeddedTemplatesFs.ReadFile(tpath)
10301030
if err != nil {
10311031
return err
10321032
}
10331033

10341034
// Get the newlines on Windows in line with how we had it back when we used Go Generate
10351035
// to write the templates to Go files.
10361036
templ := string(bytes.ReplaceAll(templb, []byte("\r\n"), []byte("\n")))
1037-
name := strings.TrimPrefix(filepath.ToSlash(path), "embedded/templates/")
1037+
name := strings.TrimPrefix(filepath.ToSlash(tpath), "embedded/templates/")
10381038

10391039
insertOne := func(name, content string) error {
10401040
pi := s.opts.PathParser.Parse(files.ComponentFolderLayouts, name)
@@ -1064,6 +1064,19 @@ func (s *TemplateStore) insertEmbedded() error {
10641064
return nil
10651065
}
10661066

1067+
// Copy the embedded HTML table render hook to each output format.
1068+
// See https://github.com/gohugoio/hugo/issues/13351.
1069+
if name == path.Join(containerMarkup, "render-table.html") {
1070+
for _, of := range s.opts.OutputFormats {
1071+
path := paths.TrimExt(name) + "." + of.Name + of.MediaType.FirstSuffix.FullSuffix
1072+
if err := insertOne(path, templ); err != nil {
1073+
return err
1074+
}
1075+
}
1076+
1077+
return nil
1078+
}
1079+
10671080
if err := insertOne(name, templ); err != nil {
10681081
return err
10691082
}

tpl/tplimpl/templatestore_integration_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,3 +1508,43 @@ mytexts|safeHTML: {{ partial "mytext.txt" . | safeHTML }}
15081508
"mytexts|safeHTML: <div>mytext</div>",
15091509
)
15101510
}
1511+
1512+
func TestIssue13351(t *testing.T) {
1513+
t.Parallel()
1514+
1515+
files := `
1516+
-- hugo.toml --
1517+
disableKinds = ['page','rss','section','sitemap','taxonomy','term']
1518+
[outputs]
1519+
home = ['html','json']
1520+
[outputFormats.html]
1521+
weight = 1
1522+
[outputFormats.json]
1523+
weight = 2
1524+
-- content/_index.md --
1525+
---
1526+
title: home
1527+
---
1528+
a|b
1529+
:--|:--
1530+
1|2
1531+
-- layouts/index.html --
1532+
{{ .Content }}
1533+
-- layouts/index.json --
1534+
{{ .Content }}
1535+
`
1536+
1537+
b := hugolib.Test(t, files)
1538+
b.AssertFileContent("public/index.html", "<table>")
1539+
b.AssertFileContent("public/index.json", "<table>")
1540+
1541+
f := strings.ReplaceAll(files, "weight = 1", "weight = 0")
1542+
b = hugolib.Test(t, f)
1543+
b.AssertFileContent("public/index.html", "<table>")
1544+
b.AssertFileContent("public/index.json", "<table>")
1545+
1546+
f = strings.ReplaceAll(files, "weight = 1", "")
1547+
b = hugolib.Test(t, f)
1548+
b.AssertFileContent("public/index.html", "<table>")
1549+
b.AssertFileContent("public/index.json", "<table>")
1550+
}

0 commit comments

Comments
 (0)