diff --git a/common/paths/pathparser.go b/common/paths/pathparser.go index 898eee341..20f5ee30d 100644 --- a/common/paths/pathparser.go +++ b/common/paths/pathparser.go @@ -54,15 +54,27 @@ func NormalizePathStringBasic(s string) string { // ParseIdentity parses component c with path s into a StringIdentity. func (pp *PathParser) ParseIdentity(c, s string) identity.StringIdentity { + p := pp.parsePooled(c, s) + defer putPath(p) + return identity.StringIdentity(p.IdentifierBase()) +} + +// ParseBaseAndBaseNameNoIdentifier parses component c with path s into a base and a base name without any identifier. +func (pp *PathParser) ParseBaseAndBaseNameNoIdentifier(c, s string) (string, string) { + p := pp.parsePooled(c, s) + defer putPath(p) + return p.Base(), p.BaseNameNoIdentifier() +} + +func (pp *PathParser) parsePooled(c, s string) *Path { s = NormalizePathStringBasic(s) p := getPath() p.component = c - defer putPath(p) p, err := pp.doParse(c, s, p) if err != nil { panic(err) } - return identity.StringIdentity(p.IdentifierBase()) + return p } // Parse parses component c with path s into Path using Hugo's content path rules. @@ -259,7 +271,9 @@ type Path struct { var pathPool = &sync.Pool{ New: func() any { - return &Path{} + p := &Path{} + p.reset() + return p }, } @@ -268,6 +282,11 @@ func getPath() *Path { } func putPath(p *Path) { + p.reset() + pathPool.Put(p) +} + +func (p *Path) reset() { p.s = "" p.posContainerLow = -1 p.posContainerHigh = -1 @@ -279,7 +298,6 @@ func putPath(p *Path) { p.disabled = false p.trimLeadingSlash = false p.unnormalized = nil - pathPool.Put(p) } // TrimLeadingSlash returns a copy of the Path with the leading slash removed. diff --git a/hugolib/pagecollections.go b/hugolib/pagecollections.go index fe7c2f1e7..5ed2a28f0 100644 --- a/hugolib/pagecollections.go +++ b/hugolib/pagecollections.go @@ -131,22 +131,21 @@ func (c *pageFinder) getContentNode(context page.Page, isReflink bool, ref strin return c.getContentNodeForRef(context, isReflink, true, inRef, ref) } - var refs []string - // We are always looking for a content file and having an extension greatly simplifies the code that follows, // even in the case where the extension does not match this one. if ref == "/" { - refs = append(refs, "/_index"+defaultContentExt) + if n, err := c.getContentNodeForRef(context, isReflink, false, inRef, "/_index"+defaultContentExt); n != nil || err != nil { + return n, err + } } else if strings.HasSuffix(ref, "/index") { - refs = append(refs, ref+"/index"+defaultContentExt) - refs = append(refs, ref+defaultContentExt) + if n, err := c.getContentNodeForRef(context, isReflink, false, inRef, ref+"/index"+defaultContentExt); n != nil || err != nil { + return n, err + } + if n, err := c.getContentNodeForRef(context, isReflink, false, inRef, ref+defaultContentExt); n != nil || err != nil { + return n, err + } } else { - refs = append(refs, ref+defaultContentExt) - } - - for _, ref := range refs { - n, err := c.getContentNodeForRef(context, isReflink, false, inRef, ref) - if n != nil || err != nil { + if n, err := c.getContentNodeForRef(context, isReflink, false, inRef, ref+defaultContentExt); n != nil || err != nil { return n, err } } @@ -175,7 +174,7 @@ func (c *pageFinder) getContentNodeForRef(context page.Page, isReflink, hadExten rel := path.Join(baseDir, ref) - relPath := contentPathParser.Parse(files.ComponentFolderContent, rel) + relPath, _ := contentPathParser.ParseBaseAndBaseNameNoIdentifier(files.ComponentFolderContent, rel) n, err := c.getContentNodeFromPath(relPath, ref) if n != nil || err != nil { @@ -195,9 +194,9 @@ func (c *pageFinder) getContentNodeForRef(context page.Page, isReflink, hadExten return nil, nil } - refPath := contentPathParser.Parse(files.ComponentFolderContent, ref) + relPath, nameNoIdentifier := contentPathParser.ParseBaseAndBaseNameNoIdentifier(files.ComponentFolderContent, ref) - n, err := c.getContentNodeFromPath(refPath, ref) + n, err := c.getContentNodeFromPath(relPath, ref) if n != nil || err != nil { return n, err @@ -221,7 +220,7 @@ func (c *pageFinder) getContentNodeForRef(context page.Page, isReflink, hadExten return nil, nil } - n = c.pageMap.pageReverseIndex.Get(refPath.BaseNameNoIdentifier()) + n = c.pageMap.pageReverseIndex.Get(nameNoIdentifier) if n == ambiguousContentNode { return nil, fmt.Errorf("page reference %q is ambiguous", inRef) } @@ -254,9 +253,7 @@ func (c *pageFinder) getContentNodeFromRefReverseLookup(ref string, fi hugofs.Fi return nil, nil } -func (c *pageFinder) getContentNodeFromPath(refPath *paths.Path, ref string) (contentNodeI, error) { - s := refPath.Base() - +func (c *pageFinder) getContentNodeFromPath(s string, ref string) (contentNodeI, error) { n := c.pageMap.treePages.Get(s) if n != nil { return n, nil