diff --git a/hugolib/site.go b/hugolib/site.go index c9914b971..39fe888b5 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -172,7 +172,8 @@ func (s *SiteInfo) refLink(ref string, page *Page, relative bool) (string, error if refURL.Path != "" { for _, page := range []*Page(*s.Pages) { - if page.Source.Path() == refURL.Path || page.Source.LogicalName() == refURL.Path { + refPath := filepath.FromSlash(refURL.Path) + if page.Source.Path() == refPath || page.Source.LogicalName() == refPath { target = page break } diff --git a/hugolib/site_test.go b/hugolib/site_test.go index 4eb90ae1a..79921e110 100644 --- a/hugolib/site_test.go +++ b/hugolib/site_test.go @@ -307,6 +307,83 @@ func TestDraftAndFutureRender(t *testing.T) { viper.Set("BuildFuture", false) } +// Issue #957 +func TestCrossrefs(t *testing.T) { + for _, uglyUrls := range []bool{true, false} { + for _, relative := range []bool{true, false} { + doTestCrossrefs(t, relative, uglyUrls) + } + } +} + +func doTestCrossrefs(t *testing.T, relative, uglyUrls bool) { + baseUrl := "http://foo/bar" + viper.Set("baseurl", baseUrl) + viper.Set("UglyURLs", uglyUrls) + viper.Set("verbose", true) + + var refShortcode string + var expectedBase string + var expectedUrlSuffix string + var expectedPathSuffix string + + if relative { + refShortcode = "relref" + expectedBase = "/bar" + } else { + refShortcode = "ref" + expectedBase = baseUrl + } + + if uglyUrls { + expectedUrlSuffix = ".html" + expectedPathSuffix = ".html" + } else { + expectedUrlSuffix = "/" + expectedPathSuffix = "/index.html" + } + + sources := []source.ByteSource{ + {filepath.FromSlash("sect/doc1.md"), + []byte(fmt.Sprintf(`Ref 2: {{< %s "sect/doc2.md" >}}`, refShortcode))}, + {filepath.FromSlash("sect/doc2.md"), + []byte(fmt.Sprintf(`Ref 1: {{< %s "sect/doc1.md" >}}`, refShortcode))}, + } + + s := &Site{ + Source: &source.InMemorySource{ByteSource: sources}, + Targets: targetList{Page: &target.PagePub{UglyURLs: uglyUrls}}, + } + + s.initializeSiteInfo() + templatePrep(s) + + must(s.addTemplate("_default/single.html", "{{.Content}}")) + + createAndRenderPages(t, s) + + tests := []struct { + doc string + expected string + }{ + {filepath.FromSlash(fmt.Sprintf("sect/doc1%s", expectedPathSuffix)), fmt.Sprintf("

Ref 2: %s/sect/doc2%s

\n", expectedBase, expectedUrlSuffix)}, + {filepath.FromSlash(fmt.Sprintf("sect/doc2%s", expectedPathSuffix)), fmt.Sprintf("

Ref 1: %s/sect/doc1%s

\n", expectedBase, expectedUrlSuffix)}, + } + + for _, test := range tests { + file, err := hugofs.DestinationFS.Open(test.doc) + if err != nil { + t.Fatalf("Did not find %s in target: %s", test.doc, err) + } + content := helpers.ReaderToBytes(file) + + if !bytes.Equal(content, []byte(test.expected)) { + t.Errorf("%s content expected:\n%q\ngot:\n%q", test.doc, test.expected, string(content)) + } + } + +} + // Issue #939 func Test404ShouldAlwaysHaveUglyUrls(t *testing.T) { for _, uglyURLs := range []bool{true, false} {