Skip to content

Commit 612f6e3

Browse files
committed
hugolib: Fix ref/relref issue with duplicate base filenames
This commit also makes that function 80x faster. Fixes #2507
1 parent 50d1113 commit 612f6e3

File tree

4 files changed

+13
-17
lines changed

4 files changed

+13
-17
lines changed

‎hugolib/page_collections.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ func (c *PageCollections) refreshPageCaches() {
6161
// shortcodes. If the user says "sect/doc1.en.md", he/she knows
6262
// what he/she is looking for.
6363
for _, p := range c.AllRegularPages {
64-
// TODO(bep) section
6564
cache[filepath.ToSlash(p.Source.Path())] = p
65+
// Ref/Relref supports this potentially ambiguous lookup.
66+
cache[p.Source.LogicalName()] = p
6667
}
6768
default:
6869
for _, p := range c.indexPages {

‎hugolib/page_collections_test.go‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ func TestGetPage(t *testing.T) {
110110
}
111111
}
112112

113+
content := fmt.Sprintf(pageCollectionsPageTemplate, "UniqueBase")
114+
writeSource(t, fs, filepath.Join("content", "sect3", "unique.md"), content)
115+
113116
s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{SkipRender: true})
114117

115118
tests := []struct {
@@ -122,7 +125,8 @@ func TestGetPage(t *testing.T) {
122125
{KindPage, []string{"sect3", "page1.md"}, "Title3_1"},
123126
{KindPage, []string{"sect4/page2.md"}, "Title4_2"},
124127
{KindPage, []string{filepath.FromSlash("sect5/page3.md")}, "Title5_3"},
125-
// TODO(bep) section maybe support sect5/page2, aka relref.
128+
// Ref/Relref supports this potentially ambiguous lookup.
129+
{KindPage, []string{"unique.md"}, "UniqueBase"},
126130
}
127131

128132
for i, test := range tests {

‎hugolib/site.go‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -427,13 +427,7 @@ func (s *SiteInfo) refLink(ref string, page *Page, relative bool, outputFormat s
427427
var link string
428428

429429
if refURL.Path != "" {
430-
for _, page := range s.AllRegularPages {
431-
refPath := filepath.FromSlash(refURL.Path)
432-
if page.Source.Path() == refPath || page.Source.LogicalName() == refPath {
433-
target = page
434-
break
435-
}
436-
}
430+
target := s.getPage(KindPage, refURL.Path)
437431

438432
if target == nil {
439433
return "", fmt.Errorf("No page found with path or logical name \"%s\".\n", refURL.Path)

‎hugolib/site_test.go‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,7 @@ func findPage(site *Site, f string) *Page {
907907

908908
func setupLinkingMockSite(t *testing.T) *Site {
909909
sources := []source.ByteSource{
910+
{Name: filepath.FromSlash("level2/unique.md"), Content: []byte("")},
910911
{Name: filepath.FromSlash("index.md"), Content: []byte("")},
911912
{Name: filepath.FromSlash("rootfile.md"), Content: []byte("")},
912913
{Name: filepath.FromSlash("root-image.png"), Content: []byte("")},
@@ -957,15 +958,11 @@ func TestRefLinking(t *testing.T) {
957958
relative bool
958959
expected string
959960
}{
960-
// Note: There are no magic in the index.md name. This was fixed in Hugo 0.20.
961-
// Before that, index.md would wrongly resolve to "/".
962-
// See #3396 -- there is an ambiguity in the examples below, even if they do work.
963-
// TODO(bep) better test cases
964-
{"index.md", "", true, "/"},
965-
{"common.md", "", true, "/level2/common/"},
961+
{"unique.md", "", true, "/level2/unique/"},
962+
{"level2/common.md", "", true, "/level2/common/"},
966963
{"3-root.md", "", true, "/level2/level3/3-root/"},
967-
{"index.md", "amp", true, "/amp/"},
968-
{"index.md", "amp", false, "http://auth/amp/"},
964+
{"level2/level3/index.md", "amp", true, "/amp/level2/level3/"},
965+
{"level2/index.md", "amp", false, "http://auth/amp/level2/"},
969966
} {
970967
if out, err := site.Info.refLink(test.link, currentPage, test.relative, test.outputFormat); err != nil || out != test.expected {
971968
t.Errorf("[%d] Expected %s to resolve to (%s), got (%s) - error: %s", i, test.link, test.expected, out, err)

0 commit comments

Comments
 (0)