mirror of
https://github.com/gohugoio/hugo.git
synced 2025-05-06 18:53:34 +00:00
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:
parent
863a812e07
commit
df20b05463
2 changed files with 23 additions and 23 deletions
|
@ -464,10 +464,10 @@ func AssignMetadata(metadata []map[string]interface{}, resources ...Resource) er
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
nameSet, titleSet bool
|
nameSet, titleSet bool
|
||||||
currentCounter = 0
|
nameCounter, titleCounter = 0, 0
|
||||||
counterFound bool
|
nameCounterFound, titleCounterFound bool
|
||||||
resourceSrcKey = strings.ToLower(r.Name())
|
resourceSrcKey = strings.ToLower(r.Name())
|
||||||
)
|
)
|
||||||
|
|
||||||
ma := r.(metaAssigner)
|
ma := r.(metaAssigner)
|
||||||
|
@ -491,15 +491,16 @@ func AssignMetadata(metadata []map[string]interface{}, resources ...Resource) er
|
||||||
name, found := meta["name"]
|
name, found := meta["name"]
|
||||||
if found {
|
if found {
|
||||||
name := cast.ToString(name)
|
name := cast.ToString(name)
|
||||||
if !counterFound {
|
if !nameCounterFound {
|
||||||
counterFound = strings.Contains(name, counterPlaceHolder)
|
nameCounterFound = strings.Contains(name, counterPlaceHolder)
|
||||||
}
|
}
|
||||||
if counterFound && currentCounter == 0 {
|
if nameCounterFound && nameCounter == 0 {
|
||||||
currentCounter = counters[srcKey] + 1
|
counterKey := "name_" + srcKey
|
||||||
counters[srcKey] = currentCounter
|
nameCounter = counters[counterKey] + 1
|
||||||
|
counters[counterKey] = nameCounter
|
||||||
}
|
}
|
||||||
|
|
||||||
ma.setName(replaceResourcePlaceholders(name, currentCounter))
|
ma.setName(replaceResourcePlaceholders(name, nameCounter))
|
||||||
nameSet = true
|
nameSet = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -508,14 +509,15 @@ func AssignMetadata(metadata []map[string]interface{}, resources ...Resource) er
|
||||||
title, found := meta["title"]
|
title, found := meta["title"]
|
||||||
if found {
|
if found {
|
||||||
title := cast.ToString(title)
|
title := cast.ToString(title)
|
||||||
if !counterFound {
|
if !titleCounterFound {
|
||||||
counterFound = strings.Contains(title, counterPlaceHolder)
|
titleCounterFound = strings.Contains(title, counterPlaceHolder)
|
||||||
}
|
}
|
||||||
if counterFound && currentCounter == 0 {
|
if titleCounterFound && titleCounter == 0 {
|
||||||
currentCounter = counters[srcKey] + 1
|
counterKey := "title_" + srcKey
|
||||||
counters[srcKey] = currentCounter
|
titleCounter = counters[counterKey] + 1
|
||||||
|
counters[counterKey] = titleCounter
|
||||||
}
|
}
|
||||||
ma.setTitle((replaceResourcePlaceholders(title, currentCounter)))
|
ma.setTitle((replaceResourcePlaceholders(title, titleCounter)))
|
||||||
titleSet = true
|
titleSet = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -280,14 +280,14 @@ func TestAssignMetadata(t *testing.T) {
|
||||||
},
|
},
|
||||||
}, func(err error) {
|
}, func(err error) {
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
assert.Equal("Resource #1", logo2.Title())
|
assert.Equal("Resource #2", logo2.Title())
|
||||||
assert.Equal("Logo Name #1", logo2.Name())
|
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("Logo Name #2", logo1.Name())
|
||||||
assert.Equal("Resource #1", foo2.Title())
|
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("Name #2", foo1.Name())
|
||||||
assert.Equal("Resource #3", foo3.Title())
|
assert.Equal("Resource #5", foo3.Title())
|
||||||
|
|
||||||
assert.Equal(logo2, resources.GetByPrefix("logo name #1"))
|
assert.Equal(logo2, resources.GetByPrefix("logo name #1"))
|
||||||
|
|
||||||
|
@ -305,15 +305,13 @@ func TestAssignMetadata(t *testing.T) {
|
||||||
}, func(err error) {
|
}, func(err error) {
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
assert.Equal("Third Logo #1", logo3.Title())
|
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("Other Logo #1", logo2.Title())
|
||||||
assert.Equal("Name #1", logo2.Name())
|
assert.Equal("Name #1", logo2.Name())
|
||||||
assert.Equal("Other Logo #2", logo1.Title())
|
assert.Equal("Other Logo #2", logo1.Title())
|
||||||
assert.Equal("Name #2", logo1.Name())
|
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{}{
|
||||||
map[string]interface{}{
|
map[string]interface{}{
|
||||||
"title": "Third Logo",
|
"title": "Third Logo",
|
||||||
|
|
Loading…
Add table
Reference in a new issue