mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-17 21:01:26 +02:00
hugolib: Refactor/-work the permalink/target path logic
This is a pretty fundamental change in Hugo, but absolutely needed if we should have any hope of getting "multiple outputs" done. This commit's goal is to say: * Every file target path is created by `createTargetPath`, i.e. one function for all. * That function takes every page and site parameter into account, to avoid fragile string parsing to uglify etc. later on. * The path creation logic has full test coverage. * All permalinks, paginator URLs etc. are then built on top of that same logic. Fixes #1252 Fixes #2110 Closes #2374 Fixes #1885 Fixes #3102 Fixes #3179 Fixes #1641 Fixes #1989
This commit is contained in:
@@ -1162,20 +1162,6 @@ func TestPagePaths(t *testing.T) {
|
||||
s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{SkipRender: true})
|
||||
require.Len(t, s.RegularPages, 1)
|
||||
|
||||
// TODO(bep) output
|
||||
/* p := s.RegularPages[0]
|
||||
|
||||
expectedTargetPath := filepath.FromSlash(test.expected)
|
||||
expectedFullFilePath := filepath.FromSlash(test.path)
|
||||
|
||||
|
||||
if p.TargetPath() != expectedTargetPath {
|
||||
t.Fatalf("[%d] %s => TargetPath expected: '%s', got: '%s'", i, test.content, expectedTargetPath, p.TargetPath())
|
||||
}
|
||||
|
||||
if p.FullFilePath() != expectedFullFilePath {
|
||||
t.Fatalf("[%d] %s => FullFilePath expected: '%s', got: '%s'", i, test.content, expectedFullFilePath, p.FullFilePath())
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1488,6 +1474,73 @@ func TestShouldBuild(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Issue #1885 and #2110
|
||||
func TestDotInPath(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for _, uglyURLs := range []bool{false, true} {
|
||||
t.Run(fmt.Sprintf("uglyURLs=%t", uglyURLs), func(t *testing.T) {
|
||||
|
||||
cfg, fs := newTestCfg()
|
||||
th := testHelper{cfg, fs, t}
|
||||
|
||||
cfg.Set("permalinks", map[string]string{
|
||||
"post": ":section/:title",
|
||||
})
|
||||
|
||||
cfg.Set("uglyURLs", uglyURLs)
|
||||
cfg.Set("paginate", 1)
|
||||
|
||||
writeSource(t, fs, filepath.Join("layouts", "_default", "single.html"), "<html><body>{{.Content}}</body></html>")
|
||||
writeSource(t, fs, filepath.Join("layouts", "_default", "list.html"),
|
||||
"<html><body>P{{.Paginator.PageNumber}}|URL: {{.Paginator.URL}}|{{ if .Paginator.HasNext }}Next: {{.Paginator.Next.URL }}{{ end }}</body></html>")
|
||||
|
||||
for i := 0; i < 3; i++ {
|
||||
writeSource(t, fs, filepath.Join("content", "post", fmt.Sprintf("doc%d.md", i)),
|
||||
fmt.Sprintf(`---
|
||||
title: "test%d.dot"
|
||||
tags:
|
||||
- ".net"
|
||||
---
|
||||
# doc1
|
||||
*some content*`, i))
|
||||
}
|
||||
|
||||
s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
|
||||
require.Len(t, s.RegularPages, 3)
|
||||
|
||||
pathFunc := func(s string) string {
|
||||
if uglyURLs {
|
||||
return strings.Replace(s, "/index.html", ".html", 1)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
th.assertFileContent(pathFunc("public/post/test0.dot/index.html"), "some content")
|
||||
|
||||
if uglyURLs {
|
||||
th.assertFileContent("public/post/page/1.html", `canonical" href="/post.html"/`)
|
||||
th.assertFileContent("public/post.html", `<body>P1|URL: /post.html|Next: /post/page/2.html</body>`)
|
||||
th.assertFileContent("public/post/page/2.html", `<body>P2|URL: /post/page/2.html|Next: /post/page/3.html</body>`)
|
||||
} else {
|
||||
th.assertFileContent("public/post/page/1/index.html", `canonical" href="/post/"/`)
|
||||
th.assertFileContent("public/post/index.html", `<body>P1|URL: /post/|Next: /post/page/2/</body>`)
|
||||
th.assertFileContent("public/post/page/2/index.html", `<body>P2|URL: /post/page/2/|Next: /post/page/3/</body>`)
|
||||
th.assertFileContent("public/tags/.net/index.html", `<body>P1|URL: /tags/.net/|Next: /tags/.net/page/2/</body>`)
|
||||
|
||||
}
|
||||
|
||||
p := s.RegularPages[0]
|
||||
if uglyURLs {
|
||||
require.Equal(t, "/post/test0.dot.html", p.RelPermalink())
|
||||
} else {
|
||||
require.Equal(t, "/post/test0.dot/", p.RelPermalink())
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkParsePage(b *testing.B) {
|
||||
s := newTestSite(b)
|
||||
f, _ := os.Open("testdata/redis.cn.md")
|
||||
|
Reference in New Issue
Block a user