hugolib: Normalize permalink path segments

When constructing permalinks, ensure that most inputs used as path
segments are normalized with PathSpec.MakeSegment instead of
PathSpec.URLize.

The primary exception to that rule is with taxonomy titles in
pageToPermalinkTitle(). The approach taken here is to use URLize for
taxonomy pages. Everything else will use MakeSegment. The reason for
this exception is that people use taxonomies such as "s1/p1" to generate
URLs precisely they way they wish (see #5223). Tests have been added to
check for this case.

Fixes #4926
This commit is contained in:
Cameron Moore
2018-09-21 14:03:17 -05:00
committed by Bjørn Erik Pedersen
parent 06d28a464d
commit fae48d7457
3 changed files with 51 additions and 36 deletions

View File

@@ -79,9 +79,11 @@ category = "categories"
other = "others"
empty = "empties"
permalinked = "permalinkeds"
subcats = "subcats"
[permalinks]
permalinkeds = "/perma/:slug/"
subcats = "/subcats/:slug/"
`
pageTemplate := `---
@@ -94,6 +96,8 @@ others:
%s
permalinkeds:
%s
subcats:
%s
---
# Doc
`
@@ -105,10 +109,11 @@ permalinkeds:
fs := th.Fs
writeSource(t, fs, "content/p1.md", fmt.Sprintf(pageTemplate, "t1/c1", "- Tag1", "- cat1\n- \"cAt/dOg\"", "- o1", "- pl1"))
writeSource(t, fs, "content/p2.md", fmt.Sprintf(pageTemplate, "t2/c1", "- tag2", "- cat1", "- o1", "- pl1"))
writeSource(t, fs, "content/p3.md", fmt.Sprintf(pageTemplate, "t2/c12", "- tag2", "- cat2", "- o1", "- pl1"))
writeSource(t, fs, "content/p4.md", fmt.Sprintf(pageTemplate, "Hello World", "", "", "- \"Hello Hugo world\"", "- pl1"))
writeSource(t, fs, "content/p1.md", fmt.Sprintf(pageTemplate, "t1/c1", "- Tag1", "- cat1\n- \"cAt/dOg\"", "- o1", "- pl1", ""))
writeSource(t, fs, "content/p2.md", fmt.Sprintf(pageTemplate, "t2/c1", "- tag2", "- cat1", "- o1", "- pl1", ""))
writeSource(t, fs, "content/p3.md", fmt.Sprintf(pageTemplate, "t2/c12", "- tag2", "- cat2", "- o1", "- pl1", ""))
writeSource(t, fs, "content/p4.md", fmt.Sprintf(pageTemplate, "Hello World", "", "", "- \"Hello Hugo world\"", "- pl1", ""))
writeSource(t, fs, "content/p5.md", fmt.Sprintf(pageTemplate, "Sub/categories", "", "", "", "", "- \"sc0/sp1\""))
writeNewContentFile(t, fs.Source, "Category Terms", "2017-01-01", "content/categories/_index.md", 10)
writeNewContentFile(t, fs.Source, "Tag1 List", "2017-01-01", "content/tags/Tag1/_index.md", 10)
@@ -175,6 +180,7 @@ permalinkeds:
"others": 2,
"empties": 0,
"permalinkeds": 1,
"subcats": 1,
}
for taxonomy, count := range taxonomyTermPageCounts {
@@ -217,6 +223,11 @@ permalinkeds:
require.NotNil(t, permalinkeds)
require.Equal(t, fixURL("/blog/permalinkeds/"), permalinkeds.RelPermalink())
// Issue #5223
sp1 := s.getPage(KindTaxonomy, "subcats", "sc0/sp1")
require.NotNil(t, sp1)
require.Equal(t, fixURL("/blog/subcats/sc0/sp1/"), sp1.RelPermalink())
// Issue #3070 preserveTaxonomyNames
if preserveTaxonomyNames {
helloWorld := s.getPage(KindTaxonomy, "others", "Hello Hugo world")