hugolib: Fix bundle path when slug is set

Fixes #4870
This commit is contained in:
Bjørn Erik Pedersen 2019-05-31 09:40:58 +02:00
parent 8d898ad667
commit 3e6cb2cb77
4 changed files with 54 additions and 7 deletions

View file

@ -15,6 +15,9 @@ package hugolib
import ( import (
"net/url" "net/url"
"strings"
"github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/resources/page" "github.com/gohugoio/hugo/resources/page"
) )
@ -95,8 +98,9 @@ func (l pagePaths) OutputFormats() page.OutputFormats {
func createTargetPathDescriptor(s *Site, p page.Page, pm *pageMeta) (page.TargetPathDescriptor, error) { func createTargetPathDescriptor(s *Site, p page.Page, pm *pageMeta) (page.TargetPathDescriptor, error) {
var ( var (
dir string dir string
baseName string baseName string
contentBaseName string
) )
d := s.Deps d := s.Deps
@ -104,6 +108,14 @@ func createTargetPathDescriptor(s *Site, p page.Page, pm *pageMeta) (page.Target
if !p.File().IsZero() { if !p.File().IsZero() {
dir = p.File().Dir() dir = p.File().Dir()
baseName = p.File().TranslationBaseName() baseName = p.File().TranslationBaseName()
contentBaseName = p.File().ContentBaseName()
}
if baseName != contentBaseName {
// See https://github.com/gohugoio/hugo/issues/4870
// A leaf bundle
dir = strings.TrimSuffix(dir, contentBaseName+helpers.FilePathSeparator)
baseName = contentBaseName
} }
alwaysInSubDir := p.Kind() == kindSitemap alwaysInSubDir := p.Kind() == kindSitemap

View file

@ -460,7 +460,7 @@ HEADLESS {{< myShort >}}
assert.Equal(1, len(s.headlessPages)) assert.Equal(1, len(s.headlessPages))
regular := s.getPage(page.KindPage, "a/index") regular := s.getPage(page.KindPage, "a/index")
assert.Equal("/a/s1/", regular.RelPermalink()) assert.Equal("/s1/", regular.RelPermalink())
headless := s.getPage(page.KindPage, "b/index") headless := s.getPage(page.KindPage, "b/index")
assert.NotNil(headless) assert.NotNil(headless)
@ -481,12 +481,12 @@ HEADLESS {{< myShort >}}
th := testHelper{s.Cfg, s.Fs, t} th := testHelper{s.Cfg, s.Fs, t}
th.assertFileContent(filepath.FromSlash(workDir+"/public/a/s1/index.html"), "TheContent") th.assertFileContent(filepath.FromSlash(workDir+"/public/s1/index.html"), "TheContent")
th.assertFileContent(filepath.FromSlash(workDir+"/public/a/s1/l1.png"), "PNG") th.assertFileContent(filepath.FromSlash(workDir+"/public/s1/l1.png"), "PNG")
th.assertFileNotExist(workDir + "/public/b/s2/index.html") th.assertFileNotExist(workDir + "/public/s2/index.html")
// But the bundled resources needs to be published // But the bundled resources needs to be published
th.assertFileContent(filepath.FromSlash(workDir+"/public/b/s2/l1.png"), "PNG") th.assertFileContent(filepath.FromSlash(workDir+"/public/s2/l1.png"), "PNG")
} }
@ -942,3 +942,32 @@ date: 2017-01-15
b.AssertFileContent("public/mybundle/data.json", "My changed data") b.AssertFileContent("public/mybundle/data.json", "My changed data")
} }
// https://github.com/gohugoio/hugo/issues/4870
func TestBundleSlug(t *testing.T) {
t.Parallel()
assert := require.New(t)
const pageTemplate = `---
title: Title
slug: %s
---
`
b := newTestSitesBuilder(t)
b.WithTemplatesAdded("index.html", `{{ range .Site.RegularPages }}|{{ .RelPermalink }}{{ end }}|`)
b.WithSimpleConfigFile().
WithContent("about/services1/misc.md", fmt.Sprintf(pageTemplate, "this-is-the-slug")).
WithContent("about/services2/misc/index.md", fmt.Sprintf(pageTemplate, "this-is-another-slug"))
b.CreateSites().Build(BuildCfg{})
b.AssertHome(
"|/about/services1/this-is-the-slug/|/",
"|/about/services2/this-is-another-slug/|")
assert.True(b.CheckExists("public/about/services1/this-is-the-slug/index.html"))
assert.True(b.CheckExists("public/about/services2/this-is-another-slug/index.html"))
}

View file

@ -549,6 +549,10 @@ func (s *sitesBuilder) AssertFileContentFn(filename string, f func(s string) boo
} }
} }
func (s *sitesBuilder) AssertHome(matches ...string) {
s.AssertFileContent("public/index.html", matches...)
}
func (s *sitesBuilder) AssertFileContent(filename string, matches ...string) { func (s *sitesBuilder) AssertFileContent(filename string, matches ...string) {
content := s.FileContent(filename) content := s.FileContent(filename)
for _, match := range matches { for _, match := range matches {

View file

@ -221,6 +221,8 @@ func CreateTargetPaths(d TargetPathDescriptor) (tp TargetPaths) {
link = pagePath link = pagePath
// TODO(bep) this should not happen after the fix in https://github.com/gohugoio/hugo/issues/4870
// but we may need some more testing before we can remove it.
if baseNameSameAsType { if baseNameSameAsType {
link = strings.TrimSuffix(link, d.BaseName) link = strings.TrimSuffix(link, d.BaseName)
} }