hugolib: Fix preserveTaxonomyNames regression

Fixes #3070
This commit is contained in:
Bjørn Erik Pedersen 2017-02-23 10:03:48 +01:00
parent 831bfd36aa
commit c1425a166d
3 changed files with 31 additions and 7 deletions

View file

@ -326,6 +326,8 @@ func (h *HugoSites) createMissingPages() error {
foundTaxonomyTermsPage := false foundTaxonomyTermsPage := false
for key := range tax { for key := range tax {
foundTaxonomyPage := false foundTaxonomyPage := false
origKey := key
if s.Info.preserveTaxonomyNames { if s.Info.preserveTaxonomyNames {
key = s.PathSpec.MakePathSanitized(key) key = s.PathSpec.MakePathSanitized(key)
} }
@ -344,7 +346,7 @@ func (h *HugoSites) createMissingPages() error {
if s.isEnabled(KindTaxonomy) { if s.isEnabled(KindTaxonomy) {
if !foundTaxonomyPage { if !foundTaxonomyPage {
n := s.newTaxonomyPage(plural, key) n := s.newTaxonomyPage(plural, origKey)
s.Pages = append(s.Pages, n) s.Pages = append(s.Pages, n)
newPages = append(newPages, n) newPages = append(newPages, n)
} }

View file

@ -2097,12 +2097,11 @@ func (s *Site) newTaxonomyPage(plural, key string) *Page {
p.sections = []string{plural, key} p.sections = []string{plural, key}
if s.Info.preserveTaxonomyNames { if s.Info.preserveTaxonomyNames {
// Keep (mostly) as is in the title
// We make the first character upper case, mostly because
// it is easier to reason about in the tests.
p.Title = helpers.FirstUpper(key)
key = s.PathSpec.MakePathSanitized(key) key = s.PathSpec.MakePathSanitized(key)
}
if s.Info.preserveTaxonomyNames {
// keep as is in the title
p.Title = key
} else { } else {
p.Title = strings.Replace(strings.Title(key), "-", " ", -1) p.Title = strings.Replace(strings.Title(key), "-", " ", -1)
} }

View file

@ -49,12 +49,21 @@ func TestByCountOrderOfTaxonomies(t *testing.T) {
} }
} }
// Issue #2992
func TestTaxonomiesWithAndWithoutContentFile(t *testing.T) { func TestTaxonomiesWithAndWithoutContentFile(t *testing.T) {
for _, preserveTaxonomyNames := range []bool{false, true} {
t.Run(fmt.Sprintf("preserveTaxonomyNames %t", preserveTaxonomyNames), func(t *testing.T) {
doTestTaxonomiesWithAndWithoutContentFile(t, preserveTaxonomyNames)
})
}
}
func doTestTaxonomiesWithAndWithoutContentFile(t *testing.T, preserveTaxonomyNames bool) {
t.Parallel() t.Parallel()
siteConfig := ` siteConfig := `
baseURL = "http://example.com/blog" baseURL = "http://example.com/blog"
preserveTaxonomyNames = %t
paginate = 1 paginate = 1
defaultContentLanguage = "en" defaultContentLanguage = "en"
@ -77,6 +86,8 @@ others:
# Doc # Doc
` `
siteConfig = fmt.Sprintf(siteConfig, preserveTaxonomyNames)
th, h := newTestSitesFromConfigWithDefaultTemplates(t, siteConfig) th, h := newTestSitesFromConfigWithDefaultTemplates(t, siteConfig)
require.Len(t, h.Sites, 1) require.Len(t, h.Sites, 1)
@ -85,6 +96,7 @@ others:
writeSource(t, fs, "content/p1.md", fmt.Sprintf(pageTemplate, "t1/c1", "- tag1", "- cat1", "- o1")) writeSource(t, fs, "content/p1.md", fmt.Sprintf(pageTemplate, "t1/c1", "- tag1", "- cat1", "- o1"))
writeSource(t, fs, "content/p2.md", fmt.Sprintf(pageTemplate, "t2/c1", "- tag2", "- cat1", "- o1")) writeSource(t, fs, "content/p2.md", fmt.Sprintf(pageTemplate, "t2/c1", "- tag2", "- cat1", "- o1"))
writeSource(t, fs, "content/p3.md", fmt.Sprintf(pageTemplate, "t2/c12", "- tag2", "- cat2", "- o1")) writeSource(t, fs, "content/p3.md", fmt.Sprintf(pageTemplate, "t2/c12", "- tag2", "- cat2", "- o1"))
writeSource(t, fs, "content/p4.md", fmt.Sprintf(pageTemplate, "Hello World", "", "", "- \"Hello Hugo world\""))
writeNewContentFile(t, fs, "Category Terms", "2017-01-01", "content/categories/_index.md", 10) writeNewContentFile(t, fs, "Category Terms", "2017-01-01", "content/categories/_index.md", 10)
writeNewContentFile(t, fs, "Tag1 List", "2017-01-01", "content/tags/tag1/_index.md", 10) writeNewContentFile(t, fs, "Tag1 List", "2017-01-01", "content/tags/tag1/_index.md", 10)
@ -122,4 +134,15 @@ others:
require.Len(t, cat.Data["Pages"], 3) require.Len(t, cat.Data["Pages"], 3)
require.Equal(t, "t1/c1", cat.Pages[0].Title) require.Equal(t, "t1/c1", cat.Pages[0].Title)
// Issue #3070 preserveTaxonomyNames
if preserveTaxonomyNames {
helloWorld := s.getPage(KindTaxonomy, "others", "Hello Hugo world")
require.NotNil(t, helloWorld)
require.Equal(t, "Hello Hugo world", helloWorld.Title)
} else {
helloWorld := s.getPage(KindTaxonomy, "others", "hello-hugo-world")
require.NotNil(t, helloWorld)
require.Equal(t, "Hello Hugo World", helloWorld.Title)
}
} }