Skip to content

Commit

Permalink
hugolib: Allow relative URLs in front matter
Browse files Browse the repository at this point in the history
Before this commit you would have to do this in multilingual setups:

```
---
title: "Custom!"
url: "/jp/custom/foo"
---
```

This commit allows for relative URLs, e.g:

```
---
title: "Custom!"
url: "custom/foo"
---
```

Which is obviously easier and more portable.

The meaning of relative may change to include more in the future (e.g. role based access).

Fixes gohugoio#5704
  • Loading branch information
bep committed Mar 31, 2019
1 parent f9d6fec commit 0f7ac3c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
41 changes: 41 additions & 0 deletions hugolib/page_permalink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,44 @@ Content
}

}

func TestRelativeURLInFrontMatter(t *testing.T) {
//assert := require.New(t)

config := `
defaultContentLanguage = "en"
defaultContentLanguageInSubdir = false
[Languages]
[Languages.en]
weight = 10
contentDir = "content/en"
[Languages.nn]
weight = 20
contentDir = "content/nn"
`

pageTempl := `---
title: "A page"
url: %q
---
Some content.
`

b := newTestSitesBuilder(t).WithConfigFile("toml", config)
b.WithContent("content/en/blog/page1.md", fmt.Sprintf(pageTempl, "myblog/p1/"))
b.WithContent("content/en/blog/_index.md", fmt.Sprintf(pageTempl, "this-is-my-english-blog"))
b.WithContent("content/nn/blog/page1.md", fmt.Sprintf(pageTempl, "myblog/p1/"))
b.WithContent("content/nn/blog/_index.md", fmt.Sprintf(pageTempl, "this-is-my-blog"))

b.Build(BuildCfg{})

b.AssertFileContent("public/nn/myblog/p1/index.html", "Single: A page|Hello|nn|RelPermalink: /nn/myblog/p1/|")
b.AssertFileContent("public/nn/this-is-my-blog/index.html", "List Page 1|A page|Hello|/nn/this-is-my-blog/|")
b.AssertFileContent("public/this-is-my-english-blog/index.html", "List Page 1|A page|Hello|/this-is-my-english-blog/|")
b.AssertFileContent("public/myblog/p1/index.html", "Single: A page|Hello|en|RelPermalink: /myblog/p1/|Permalink: /myblog/p1/|")

}
4 changes: 2 additions & 2 deletions hugolib/site_render.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func (s *Site) render404() error {
s: s,
kind: kind404,
urlPaths: pagemeta.URLPath{
URL: path.Join(s.GetURLLanguageBasePath(), "404.html"),
URL: "404.html",
},
},
output.HTMLFormat,
Expand Down Expand Up @@ -271,7 +271,7 @@ func (s *Site) renderRobotsTXT() error {
s: s,
kind: kindRobotsTXT,
urlPaths: pagemeta.URLPath{
URL: path.Join(s.GetURLLanguageBasePath(), "robots.txt"),
URL: "robots.txt",
},
},
output.RobotsTxtFormat)
Expand Down
5 changes: 5 additions & 0 deletions resources/page/page_paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ func CreateTargetPaths(d TargetPathDescriptor) (tp TargetPaths) {

}

if d.URL != "" && !strings.HasPrefix(d.URL, "/") {
// Treat this as a context relative URL
d.ForcePrefix = true
}

pagePath := slash

var (
Expand Down
1 change: 1 addition & 0 deletions resources/page/page_paths_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func TestPageTargetPath(t *testing.T) {
BaseName: "mypage",
URL: "/some/other/path",
Type: output.HTMLFormat}, TargetPaths{TargetFilename: "/some/other/path/index.html", SubResourceBaseTarget: "/some/other/path", Link: "/some/other/path/"}},

{
"HTML page with expanded permalink", TargetPathDescriptor{
Kind: KindPage,
Expand Down

0 comments on commit 0f7ac3c

Please sign in to comment.