Make HTML behave exactly like other content formats (note)

Fixes #11999
This commit is contained in:
Bjørn Erik Pedersen
2024-02-07 10:30:32 +01:00
parent 068ccde4c7
commit 676e6875da
9 changed files with 107 additions and 175 deletions

View File

@@ -10,6 +10,7 @@ import (
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"sync"
"testing"
@@ -273,6 +274,32 @@ func (s *IntegrationTestBuilder) AssertFileContentExact(filename string, matches
}
}
func (s *IntegrationTestBuilder) AssertPublishDir(matches ...string) {
s.Helper()
var buff bytes.Buffer
helpers.PrintFs(s.H.Fs.PublishDir, "", &buff)
printFsLines := strings.Split(buff.String(), "\n")
sort.Strings(printFsLines)
content := strings.TrimSpace((strings.Join(printFsLines, "\n")))
for _, m := range matches {
cm := qt.Commentf("Match: %q\nIn:\n%s", m, content)
lines := strings.Split(m, "\n")
for _, match := range lines {
match = strings.TrimSpace(match)
var negate bool
if strings.HasPrefix(match, "! ") {
negate = true
match = strings.TrimPrefix(match, "! ")
}
if negate {
s.Assert(content, qt.Not(qt.Contains), match, cm)
continue
}
s.Assert(content, qt.Contains, match, cm)
}
}
}
func (s *IntegrationTestBuilder) AssertFileExists(filename string, b bool) {
checker := qt.IsNil
if !b {

View File

@@ -1540,32 +1540,6 @@ CONTENT:{{ .Content }}
)
}
// https://github.com/gohugoio/hugo/issues/5478
func TestPageWithCommentedOutFrontMatter(t *testing.T) {
b := newTestSitesBuilder(t)
b.WithSimpleConfigFile()
b.WithContent("page.md", `<!--
+++
title = "hello"
+++
-->
This is the content.
`)
b.WithTemplatesAdded("layouts/_default/single.html", `
Title: {{ .Title }}
Content:{{ .Content }}
`)
b.CreateSites().Build(BuildCfg{})
b.AssertFileContent("public/page/index.html",
"Title: hello",
"Content:<p>This is the content.</p>",
)
}
func TestHomePageWithNoTitle(t *testing.T) {
b := newTestSitesBuilder(t).WithConfigFile("toml", `
title = "Site Title"

View File

@@ -757,3 +757,81 @@ func TestPageBundlerHome(t *testing.T) {
Title: Home|First Resource: data.json|Content: <p>Hook Len Page Resources 1</p>
`)
}
func TestHTMLFilesIsue11999(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
disableKinds = ["taxonomy", "term", "rss", "sitemap", "robotsTXT", "404"]
[permalinks]
posts = "/myposts/:slugorfilename"
-- content/posts/markdown-without-frontmatter.md --
-- content/posts/html-without-frontmatter.html --
<html>hello</html>
-- content/posts/html-with-frontmatter.html --
---
title: "HTML with frontmatter"
---
<html>hello</html>
-- content/posts/html-with-commented-out-frontmatter.html --
<!--
---
title: "HTML with commented out frontmatter"
---
-->
<html>hello</html>
-- content/posts/markdown-with-frontmatter.md --
---
title: "Markdown"
---
-- content/posts/mybundle/index.md --
---
title: My Bundle
---
-- content/posts/mybundle/data.txt --
Data.txt
-- content/posts/mybundle/html-in-bundle-without-frontmatter.html --
<html>hell</html>
-- content/posts/mybundle/html-in-bundle-with-frontmatter.html --
---
title: Hello
---
<html>hello</html>
-- content/posts/mybundle/html-in-bundle-with-commented-out-frontmatter.html --
<!--
---
title: "HTML with commented out frontmatter"
---
-->
<html>hello</html>
-- layouts/index.html --
{{ range site.RegularPages }}{{ .RelPermalink }}|{{ end }}$
-- layouts/_default/single.html --
{{ .Title }}|{{ .RelPermalink }}Resources: {{ range .Resources }}{{ .Name }}|{{ end }}$
`
b := Test(t, files)
b.AssertFileContent("public/index.html", "/myposts/html-with-commented-out-frontmatter/|/myposts/html-without-frontmatter/|/myposts/markdown-without-frontmatter/|/myposts/html-with-frontmatter/|/myposts/markdown-with-frontmatter/|/myposts/mybundle/|$")
b.AssertFileContent("public/myposts/mybundle/index.html",
"My Bundle|/myposts/mybundle/Resources: html-in-bundle-with-commented-out-frontmatter.html|html-in-bundle-without-frontmatter.html|html-in-bundle-with-frontmatter.html|data.txt|$")
b.AssertPublishDir(`
index.html
myposts/html-with-commented-out-frontmatter
myposts/html-with-commented-out-frontmatter/index.html
myposts/html-with-frontmatter
myposts/html-with-frontmatter/index.html
myposts/html-without-frontmatter
myposts/html-without-frontmatter/index.html
myposts/markdown-with-frontmatter
myposts/markdown-with-frontmatter/index.html
myposts/markdown-without-frontmatter
myposts/markdown-without-frontmatter/index.html
myposts/mybundle/data.txt
myposts/mybundle/index.html
! myposts/mybundle/html-in-bundle-with-frontmatter.html
`)
}

View File

@@ -15,7 +15,6 @@ package hugolib
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
@@ -27,8 +26,6 @@ import (
"github.com/bep/logg"
"github.com/gohugoio/hugo/common/paths"
"github.com/gohugoio/hugo/common/rungroup"
"github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/parser/pageparser"
"github.com/spf13/afero"
"github.com/gohugoio/hugo/source"
@@ -77,26 +74,6 @@ type pagesCollector struct {
g rungroup.Group[hugofs.FileMetaInfo]
}
func (c *pagesCollector) copyFile(fim hugofs.FileMetaInfo) error {
meta := fim.Meta()
f, err := meta.Open()
if err != nil {
return fmt.Errorf("copyFile: failed to open: %w", err)
}
s := c.m.s
target := filepath.Join(s.PathSpec.GetTargetLanguageBasePath(), meta.PathInfo.Path())
defer f.Close()
fs := s.PublishFsStatic
s.PathSpec.ProcessingStats.Incr(&s.PathSpec.ProcessingStats.Files)
return helpers.WriteToDisk(filepath.Clean(target), f, fs)
}
// Collect collects content by walking the file system and storing
// it in the content tree.
// It may be restricted by filenames set on the collector (partial build).
@@ -136,14 +113,7 @@ func (c *pagesCollector) Collect() (collectErr error) {
NumWorkers: numWorkers,
Handle: func(ctx context.Context, fi hugofs.FileMetaInfo) error {
if err := c.m.AddFi(fi); err != nil {
if errors.Is(err, pageparser.ErrPlainHTMLDocumentsNotSupported) {
// Reclassify this as a static file.
if err := c.copyFile(fi); err != nil {
return err
}
} else {
return hugofs.AddFileInfoToError(err, fi, c.fs)
}
return hugofs.AddFileInfoToError(err, fi, c.fs)
}
numFilesProcessedTotal.Add(1)
if numFilesProcessedTotal.Load()%1000 == 0 {

View File

@@ -32,11 +32,6 @@ import (
"github.com/gohugoio/hugo/resources/page"
)
const (
templateMissingFunc = "{{ .Title | funcdoesnotexists }}"
templateWithURLAbs = "<a href=\"/foobar.jpg\">Going</a>"
)
func TestDraftAndFutureRender(t *testing.T) {
t.Parallel()
c := qt.New(t)
@@ -577,60 +572,6 @@ func doTestSectionNaming(t *testing.T, canonify, uglify, pluralize bool) {
}
}
func TestAbsURLify(t *testing.T) {
t.Parallel()
c := qt.New(t)
sources := [][2]string{
{filepath.FromSlash("sect/doc1.html"), "<!doctype html><html><head></head><body><a href=\"#frag1\">link</a></body></html>"},
{filepath.FromSlash("blue/doc2.html"), "---\nf: t\n---\n<!doctype html><html><body>more content</body></html>"},
}
for _, baseURL := range []string{"http://auth/bub", "http://base", "//base"} {
for _, canonify := range []bool{true, false} {
cfg, fs := newTestCfg()
cfg.Set("uglyURLs", true)
cfg.Set("canonifyURLs", canonify)
cfg.Set("baseURL", baseURL)
configs, err := loadTestConfigFromProvider(cfg)
c.Assert(err, qt.IsNil)
for _, src := range sources {
writeSource(t, fs, filepath.Join("content", src[0]), src[1])
}
writeSource(t, fs, filepath.Join("layouts", "blue/single.html"), templateWithURLAbs)
s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Configs: configs}, BuildCfg{})
th := newTestHelper(s.conf, s.Fs, t)
tests := []struct {
file, expected string
}{
{"public/blue/doc2.html", "<a href=\"%s/foobar.jpg\">Going</a>"},
{"public/sect/doc1.html", "<!doctype html><html><head></head><body><a href=\"#frag1\">link</a></body></html>"},
}
for _, test := range tests {
expected := test.expected
if strings.Contains(expected, "%s") {
expected = fmt.Sprintf(expected, baseURL)
}
if !canonify {
expected = strings.Replace(expected, baseURL, "", -1)
}
th.assertFileContent(test.file, expected)
}
}
}
}
var weightedPage1 = `+++
weight = "2"
title = "One"