mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-30 05:40:21 -05:00
Fix crossrefs on Windows
Have to convert path slashes to file path slashes before the URL path is compared to a file path. Fixes #957
This commit is contained in:
parent
a044734541
commit
e1340c060b
2 changed files with 79 additions and 1 deletions
|
@ -172,7 +172,8 @@ func (s *SiteInfo) refLink(ref string, page *Page, relative bool) (string, error
|
||||||
|
|
||||||
if refURL.Path != "" {
|
if refURL.Path != "" {
|
||||||
for _, page := range []*Page(*s.Pages) {
|
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
|
target = page
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
|
@ -307,6 +307,83 @@ func TestDraftAndFutureRender(t *testing.T) {
|
||||||
viper.Set("BuildFuture", false)
|
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("<p>Ref 2: %s/sect/doc2%s</p>\n", expectedBase, expectedUrlSuffix)},
|
||||||
|
{filepath.FromSlash(fmt.Sprintf("sect/doc2%s", expectedPathSuffix)), fmt.Sprintf("<p>Ref 1: %s/sect/doc1%s</p>\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
|
// Issue #939
|
||||||
func Test404ShouldAlwaysHaveUglyUrls(t *testing.T) {
|
func Test404ShouldAlwaysHaveUglyUrls(t *testing.T) {
|
||||||
for _, uglyURLs := range []bool{true, false} {
|
for _, uglyURLs := range []bool{true, false} {
|
||||||
|
|
Loading…
Reference in a new issue