resource: Make resource counters for name and title independent

This is the most flexible with the current syntax, and probably what most people would expcect.

Updates #4335
This commit is contained in:
Bjørn Erik Pedersen 2018-01-29 10:44:09 +01:00
parent 863a812e07
commit df20b05463
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
2 changed files with 23 additions and 23 deletions

View file

@ -464,10 +464,10 @@ func AssignMetadata(metadata []map[string]interface{}, resources ...Resource) er
}
var (
nameSet, titleSet bool
currentCounter = 0
counterFound bool
resourceSrcKey = strings.ToLower(r.Name())
nameSet, titleSet bool
nameCounter, titleCounter = 0, 0
nameCounterFound, titleCounterFound bool
resourceSrcKey = strings.ToLower(r.Name())
)
ma := r.(metaAssigner)
@ -491,15 +491,16 @@ func AssignMetadata(metadata []map[string]interface{}, resources ...Resource) er
name, found := meta["name"]
if found {
name := cast.ToString(name)
if !counterFound {
counterFound = strings.Contains(name, counterPlaceHolder)
if !nameCounterFound {
nameCounterFound = strings.Contains(name, counterPlaceHolder)
}
if counterFound && currentCounter == 0 {
currentCounter = counters[srcKey] + 1
counters[srcKey] = currentCounter
if nameCounterFound && nameCounter == 0 {
counterKey := "name_" + srcKey
nameCounter = counters[counterKey] + 1
counters[counterKey] = nameCounter
}
ma.setName(replaceResourcePlaceholders(name, currentCounter))
ma.setName(replaceResourcePlaceholders(name, nameCounter))
nameSet = true
}
}
@ -508,14 +509,15 @@ func AssignMetadata(metadata []map[string]interface{}, resources ...Resource) er
title, found := meta["title"]
if found {
title := cast.ToString(title)
if !counterFound {
counterFound = strings.Contains(title, counterPlaceHolder)
if !titleCounterFound {
titleCounterFound = strings.Contains(title, counterPlaceHolder)
}
if counterFound && currentCounter == 0 {
currentCounter = counters[srcKey] + 1
counters[srcKey] = currentCounter
if titleCounterFound && titleCounter == 0 {
counterKey := "title_" + srcKey
titleCounter = counters[counterKey] + 1
counters[counterKey] = titleCounter
}
ma.setTitle((replaceResourcePlaceholders(title, currentCounter)))
ma.setTitle((replaceResourcePlaceholders(title, titleCounter)))
titleSet = true
}
}

View file

@ -280,14 +280,14 @@ func TestAssignMetadata(t *testing.T) {
},
}, func(err error) {
assert.NoError(err)
assert.Equal("Resource #1", logo2.Title())
assert.Equal("Resource #2", logo2.Title())
assert.Equal("Logo Name #1", logo2.Name())
assert.Equal("Resource #2", logo1.Title())
assert.Equal("Resource #4", logo1.Title())
assert.Equal("Logo Name #2", logo1.Name())
assert.Equal("Resource #1", foo2.Title())
assert.Equal("Resource #2", foo1.Title())
assert.Equal("Resource #3", foo1.Title())
assert.Equal("Name #2", foo1.Name())
assert.Equal("Resource #3", foo3.Title())
assert.Equal("Resource #5", foo3.Title())
assert.Equal(logo2, resources.GetByPrefix("logo name #1"))
@ -305,15 +305,13 @@ func TestAssignMetadata(t *testing.T) {
}, func(err error) {
assert.NoError(err)
assert.Equal("Third Logo #1", logo3.Title())
assert.Equal("Name #1", logo3.Name())
assert.Equal("Name #3", logo3.Name())
assert.Equal("Other Logo #1", logo2.Title())
assert.Equal("Name #1", logo2.Name())
assert.Equal("Other Logo #2", logo1.Title())
assert.Equal("Name #2", logo1.Name())
}},
// Start counting first time :counter is used
// See https://github.com/gohugoio/hugo/issues/4335
{[]map[string]interface{}{
map[string]interface{}{
"title": "Third Logo",