mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
Revert " Fix handling of taxonomy terms containing slashes"
See #4090
See #5571
This reverts commit fff132537b
.
This commit is contained in:
parent
0584432b07
commit
4bae8b04aa
8 changed files with 50 additions and 94 deletions
|
@ -77,17 +77,6 @@ func (filepathBridge) Separator() string {
|
||||||
|
|
||||||
var fpb filepathBridge
|
var fpb filepathBridge
|
||||||
|
|
||||||
// segmentReplacer replaces some URI-reserved characters in a path segments.
|
|
||||||
var segmentReplacer = strings.NewReplacer("/", "-", "#", "-")
|
|
||||||
|
|
||||||
// MakeSegment returns a copy of string s that is appropriate for a path
|
|
||||||
// segment. MakeSegment is similar to MakePath but disallows the '/' and
|
|
||||||
// '#' characters because of their reserved meaning in URIs.
|
|
||||||
func (p *PathSpec) MakeSegment(s string) string {
|
|
||||||
return p.MakePathSanitized(segmentReplacer.Replace(s))
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// MakePath takes a string with any characters and replace it
|
// MakePath takes a string with any characters and replace it
|
||||||
// so the string could be used in a path.
|
// so the string could be used in a path.
|
||||||
// It does so by creating a Unicode-sanitized string, with the spaces replaced,
|
// It does so by creating a Unicode-sanitized string, with the spaces replaced,
|
||||||
|
|
|
@ -36,39 +36,6 @@ import (
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMakeSegment(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
input string
|
|
||||||
expected string
|
|
||||||
}{
|
|
||||||
{" FOO bar ", "foo-bar"},
|
|
||||||
{"Foo.Bar/fOO_bAr-Foo", "foo.bar-foo_bar-foo"},
|
|
||||||
{"FOO,bar:FooBar", "foobarfoobar"},
|
|
||||||
{"foo/BAR.HTML", "foo-bar.html"},
|
|
||||||
{"трям/трям", "трям-трям"},
|
|
||||||
{"은행", "은행"},
|
|
||||||
{"Say What??", "say-what"},
|
|
||||||
{"Your #1 Fan", "your-1-fan"},
|
|
||||||
{"Red & Blue", "red-blue"},
|
|
||||||
{"double//slash", "double-slash"},
|
|
||||||
{"triple///slash", "triple-slash"},
|
|
||||||
{"-my/way-", "my-way"},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
v := newTestCfg()
|
|
||||||
|
|
||||||
l := langs.NewDefaultLanguage(v)
|
|
||||||
p, err := NewPathSpec(hugofs.NewMem(v), l)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
output := p.MakeSegment(test.input)
|
|
||||||
if output != test.expected {
|
|
||||||
t.Errorf("Expected %#v, got %#v\n", test.expected, output)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMakePath(t *testing.T) {
|
func TestMakePath(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
input string
|
input string
|
||||||
|
|
|
@ -567,7 +567,7 @@ func (h *HugoSites) createMissingPages() error {
|
||||||
origKey := key
|
origKey := key
|
||||||
|
|
||||||
if s.Info.preserveTaxonomyNames {
|
if s.Info.preserveTaxonomyNames {
|
||||||
key = s.PathSpec.MakeSegment(key)
|
key = s.PathSpec.MakePathSanitized(key)
|
||||||
}
|
}
|
||||||
for _, p := range taxonomyPages {
|
for _, p := range taxonomyPages {
|
||||||
// Some people may have /authors/MaxMustermann etc. as paths.
|
// Some people may have /authors/MaxMustermann etc. as paths.
|
||||||
|
|
|
@ -197,13 +197,7 @@ func createTargetPath(d targetPathDescriptor) string {
|
||||||
if d.ExpandedPermalink != "" {
|
if d.ExpandedPermalink != "" {
|
||||||
pagePath = filepath.Join(pagePath, d.ExpandedPermalink)
|
pagePath = filepath.Join(pagePath, d.ExpandedPermalink)
|
||||||
} else {
|
} else {
|
||||||
pagePath = ""
|
pagePath = filepath.Join(d.Sections...)
|
||||||
for i, section := range d.Sections {
|
|
||||||
if i > 0 {
|
|
||||||
pagePath += helpers.FilePathSeparator
|
|
||||||
}
|
|
||||||
pagePath += d.PathSpec.MakeSegment(section)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
needsBase = false
|
needsBase = false
|
||||||
}
|
}
|
||||||
|
|
|
@ -151,13 +151,6 @@ func TestPageTargetPath(t *testing.T) {
|
||||||
BaseName: "mypage",
|
BaseName: "mypage",
|
||||||
Addends: "c/d/e",
|
Addends: "c/d/e",
|
||||||
Type: output.HTMLFormat}, "/a/b/mypage/c/d/e/index.html"},
|
Type: output.HTMLFormat}, "/a/b/mypage/c/d/e/index.html"},
|
||||||
{
|
|
||||||
"Unclean Taxonomy Term", targetPathDescriptor{
|
|
||||||
Kind: KindTaxonomy,
|
|
||||||
BaseName: "_index",
|
|
||||||
Sections: []string{"tags", "x/y"},
|
|
||||||
Type: output.HTMLFormat,
|
|
||||||
Addends: "page/3"}, "/tags/x-y/page/3/index.html"},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, test := range tests {
|
for i, test := range tests {
|
||||||
|
|
|
@ -20,7 +20,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var pageYamlWithTaxonomiesA = `---
|
var pageYamlWithTaxonomiesA = `---
|
||||||
tags: ['a', 'B', 'c', 'x/y']
|
tags: ['a', 'B', 'c']
|
||||||
categories: 'd'
|
categories: 'd'
|
||||||
---
|
---
|
||||||
YAML frontmatter with tags and categories taxonomy.`
|
YAML frontmatter with tags and categories taxonomy.`
|
||||||
|
@ -30,7 +30,6 @@ tags:
|
||||||
- "a"
|
- "a"
|
||||||
- "B"
|
- "B"
|
||||||
- "c"
|
- "c"
|
||||||
- "x/y"
|
|
||||||
categories: 'd'
|
categories: 'd'
|
||||||
---
|
---
|
||||||
YAML frontmatter with tags and categories taxonomy.`
|
YAML frontmatter with tags and categories taxonomy.`
|
||||||
|
@ -46,14 +45,13 @@ var pageJSONWithTaxonomies = `{
|
||||||
"tags": [
|
"tags": [
|
||||||
"a",
|
"a",
|
||||||
"b",
|
"b",
|
||||||
"c",
|
"c"
|
||||||
"x/y"
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
JSON Front Matter with tags and categories`
|
JSON Front Matter with tags and categories`
|
||||||
|
|
||||||
var pageTomlWithTaxonomies = `+++
|
var pageTomlWithTaxonomies = `+++
|
||||||
tags = [ "a", "B", "c", "x/y" ]
|
tags = [ "a", "B", "c" ]
|
||||||
categories = "d"
|
categories = "d"
|
||||||
+++
|
+++
|
||||||
TOML Front Matter with tags and categories`
|
TOML Front Matter with tags and categories`
|
||||||
|
@ -77,7 +75,7 @@ func TestParseTaxonomies(t *testing.T) {
|
||||||
param := p.getParamToLower("tags")
|
param := p.getParamToLower("tags")
|
||||||
|
|
||||||
if params, ok := param.([]string); ok {
|
if params, ok := param.([]string); ok {
|
||||||
expected := []string{"a", "b", "c", "x/y"}
|
expected := []string{"a", "b", "c"}
|
||||||
if !reflect.DeepEqual(params, expected) {
|
if !reflect.DeepEqual(params, expected) {
|
||||||
t.Errorf("Expected %s: got: %s", expected, params)
|
t.Errorf("Expected %s: got: %s", expected, params)
|
||||||
}
|
}
|
||||||
|
|
|
@ -152,13 +152,9 @@ func pageToPermalinkDate(p *Page, dateField string) (string, error) {
|
||||||
|
|
||||||
// pageToPermalinkTitle returns the URL-safe form of the title
|
// pageToPermalinkTitle returns the URL-safe form of the title
|
||||||
func pageToPermalinkTitle(p *Page, _ string) (string, error) {
|
func pageToPermalinkTitle(p *Page, _ string) (string, error) {
|
||||||
if p.Kind == KindTaxonomy {
|
// Page contains Node which has Title
|
||||||
// Taxonomies are allowed to have '/' characters, so don't normalize
|
// (also contains URLPath which has Slug, sometimes)
|
||||||
// them with MakeSegment.
|
return p.s.PathSpec.URLize(p.title), nil
|
||||||
return p.s.PathSpec.MakePathSanitized(p.title), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return p.s.PathSpec.MakeSegment(p.title), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// pageToPermalinkFilename returns the URL-safe form of the filename
|
// pageToPermalinkFilename returns the URL-safe form of the filename
|
||||||
|
@ -170,7 +166,7 @@ func pageToPermalinkFilename(p *Page, _ string) (string, error) {
|
||||||
_, name = filepath.Split(dir)
|
_, name = filepath.Split(dir)
|
||||||
}
|
}
|
||||||
|
|
||||||
return p.s.PathSpec.MakeSegment(name), nil
|
return p.s.PathSpec.URLize(name), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// if the page has a slug, return the slug, else return the title
|
// if the page has a slug, return the slug, else return the title
|
||||||
|
@ -185,30 +181,18 @@ func pageToPermalinkSlugElseTitle(p *Page, a string) (string, error) {
|
||||||
if strings.HasSuffix(p.Slug, "-") {
|
if strings.HasSuffix(p.Slug, "-") {
|
||||||
p.Slug = p.Slug[0 : len(p.Slug)-1]
|
p.Slug = p.Slug[0 : len(p.Slug)-1]
|
||||||
}
|
}
|
||||||
return p.s.PathSpec.MakeSegment(p.Slug), nil
|
return p.s.PathSpec.URLize(p.Slug), nil
|
||||||
}
|
}
|
||||||
return pageToPermalinkTitle(p, a)
|
return pageToPermalinkTitle(p, a)
|
||||||
}
|
}
|
||||||
|
|
||||||
func pageToPermalinkSection(p *Page, _ string) (string, error) {
|
func pageToPermalinkSection(p *Page, _ string) (string, error) {
|
||||||
// Page contains Node contains URLPath which has Section
|
// Page contains Node contains URLPath which has Section
|
||||||
return p.s.PathSpec.MakeSegment(p.Section()), nil
|
return p.s.PathSpec.URLize(p.Section()), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func pageToPermalinkSections(p *Page, _ string) (string, error) {
|
func pageToPermalinkSections(p *Page, _ string) (string, error) {
|
||||||
// TODO(bep) we have some superflous URLize in this file, but let's
|
return path.Join(p.CurrentSection().sections...), nil
|
||||||
// deal with that later.
|
|
||||||
|
|
||||||
cs := p.CurrentSection()
|
|
||||||
if cs == nil {
|
|
||||||
return "", errors.New("\":sections\" attribute requires parent page but is nil")
|
|
||||||
}
|
|
||||||
|
|
||||||
sections := make([]string, len(cs.sections))
|
|
||||||
for i := range cs.sections {
|
|
||||||
sections[i] = p.s.PathSpec.MakeSegment(cs.sections[i])
|
|
||||||
}
|
|
||||||
return path.Join(sections...), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
|
@ -45,9 +45,8 @@ func TestByCountOrderOfTaxonomies(t *testing.T) {
|
||||||
st = append(st, t.Name)
|
st = append(st, t.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
expect := []string{"a", "b", "c", "x/y"}
|
if !reflect.DeepEqual(st, []string{"a", "b", "c"}) {
|
||||||
if !reflect.DeepEqual(st, expect) {
|
t.Fatalf("ordered taxonomies do not match [a, b, c]. Got: %s", st)
|
||||||
t.Fatalf("ordered taxonomies do not match %v. Got: %s", expect, st)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,10 +68,8 @@ func doTestTaxonomiesWithAndWithoutContentFile(t *testing.T, preserveTaxonomyNam
|
||||||
baseURL = "http://example.com/blog"
|
baseURL = "http://example.com/blog"
|
||||||
preserveTaxonomyNames = %t
|
preserveTaxonomyNames = %t
|
||||||
uglyURLs = %t
|
uglyURLs = %t
|
||||||
|
|
||||||
paginate = 1
|
paginate = 1
|
||||||
defaultContentLanguage = "en"
|
defaultContentLanguage = "en"
|
||||||
|
|
||||||
[Taxonomies]
|
[Taxonomies]
|
||||||
tag = "tags"
|
tag = "tags"
|
||||||
category = "categories"
|
category = "categories"
|
||||||
|
@ -80,7 +77,6 @@ other = "others"
|
||||||
empty = "empties"
|
empty = "empties"
|
||||||
permalinked = "permalinkeds"
|
permalinked = "permalinkeds"
|
||||||
subcats = "subcats"
|
subcats = "subcats"
|
||||||
|
|
||||||
[permalinks]
|
[permalinks]
|
||||||
permalinkeds = "/perma/:slug/"
|
permalinkeds = "/perma/:slug/"
|
||||||
subcats = "/subcats/:slug/"
|
subcats = "/subcats/:slug/"
|
||||||
|
@ -243,3 +239,38 @@ subcats:
|
||||||
th.assertFileContent(pathFunc("public/empties/index.html"), "Terms List", "Empties")
|
th.assertFileContent(pathFunc("public/empties/index.html"), "Terms List", "Empties")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://github.com/gohugoio/hugo/issues/5513
|
||||||
|
func TestTaxonomyPathSeparation(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
config := `
|
||||||
|
baseURL = "https://example.com"
|
||||||
|
[taxonomies]
|
||||||
|
"news/tag" = "news/tags"
|
||||||
|
"news/category" = "news/categories"
|
||||||
|
`
|
||||||
|
|
||||||
|
pageContent := `
|
||||||
|
+++
|
||||||
|
title = "foo"
|
||||||
|
"news/categories" = ["a", "b", "c"]
|
||||||
|
+++
|
||||||
|
Content.
|
||||||
|
`
|
||||||
|
|
||||||
|
b := newTestSitesBuilder(t)
|
||||||
|
b.WithConfigFile("toml", config)
|
||||||
|
b.WithContent("page.md", pageContent)
|
||||||
|
b.WithContent("news/categories/b/_index.md", `
|
||||||
|
---
|
||||||
|
title: "This is B"
|
||||||
|
---
|
||||||
|
`)
|
||||||
|
|
||||||
|
b.CreateSites().Build(BuildCfg{})
|
||||||
|
|
||||||
|
b.AssertFileContent("public/news/categories/index.html", "Taxonomy Term Page 1|News/Categories|Hello|https://example.com/news/categories/|")
|
||||||
|
b.AssertFileContent("public/news/categories/a/index.html", "Taxonomy List Page 1|A|Hello|https://example.com/news/categories/a/|")
|
||||||
|
b.AssertFileContent("public/news/categories/b/index.html", "Taxonomy List Page 1|This is B|Hello|https://example.com/news/categories/b/|")
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue