}
}
+
+// https://github.com/gohugoio/hugo/issues/4895
+func TestTemplateBOM(t *testing.T) {
+
+ b := newTestSitesBuilder(t).WithSimpleConfigFile()
+ bom := "\ufeff"
+
+ b.WithTemplatesAdded(
+ "_default/baseof.html", bom+`
+ Base: {{ block "main" . }}base main{{ end }}`,
+ "_default/single.html", bom+`{{ define "main" }}Hi!?{{ end }}`)
+
+ b.WithContent("page.md", `---
+title: "Page"
+---
+
+Page Content
+`)
+
+ b.CreateSites().Build(BuildCfg{})
+
+ b.AssertFileContent("public/page/index.html", "Base: Hi!?")
+
+}
}
+func removeLeadingBOM(s string) string {
+ const bom = '\ufeff'
+
+ for i, r := range s {
+ if i == 0 && r != bom {
+ return s
+ }
+ if i > 0 {
+ return s[i:]
+ }
+ }
+
+ return s
+
+}
+
func (t *templateHandler) addTemplateFile(name, baseTemplatePath, path string) error {
t.checkState()
if err != nil {
return templateInfo{filename: filename, fs: fs}, err
}
- s := string(b)
+
+ s := removeLeadingBOM(string(b))
realFilename := filename
if fi, err := fs.Stat(filename); err == nil {