mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-19 21:21:39 +02:00
configurable permalinks support
A sample config.yaml for a site might contain: ```yaml permalinks: post: /:year/:month/:title/ ``` Then, any article in the `post` section, will have the canonical URL formed via the permalink specification given. Signed-off-by: Noah Campbell <noahcampbell@gmail.com>
This commit is contained in:
committed by
Noah Campbell
parent
4f335f0c7f
commit
07978e4a49
@@ -251,23 +251,35 @@ func (p *Page) permalink() (*url.URL, error) {
|
||||
pSlug := strings.TrimSpace(p.Slug)
|
||||
pUrl := strings.TrimSpace(p.Url)
|
||||
var permalink string
|
||||
if len(pSlug) > 0 {
|
||||
if p.Site.Config != nil && p.Site.Config.UglyUrls {
|
||||
permalink = path.Join(dir, p.Slug, p.Extension)
|
||||
} else {
|
||||
permalink = dir + "/" + p.Slug + "/"
|
||||
var err error
|
||||
|
||||
if override, ok := p.Site.Permalinks[p.Section]; ok {
|
||||
permalink, err = override.Expand(p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else if len(pUrl) > 2 {
|
||||
permalink = pUrl
|
||||
//fmt.Printf("have an override for %q in section %s → %s\n", p.Title, p.Section, permalink)
|
||||
} else {
|
||||
_, t := path.Split(p.FileName)
|
||||
if p.Site.Config != nil && p.Site.Config.UglyUrls {
|
||||
x := replaceExtension(strings.TrimSpace(t), p.Extension)
|
||||
permalink = path.Join(dir, x)
|
||||
|
||||
if len(pSlug) > 0 {
|
||||
if p.Site.Config != nil && p.Site.Config.UglyUrls {
|
||||
permalink = path.Join(dir, p.Slug, p.Extension)
|
||||
} else {
|
||||
permalink = dir + "/" + p.Slug + "/"
|
||||
}
|
||||
} else if len(pUrl) > 2 {
|
||||
permalink = pUrl
|
||||
} else {
|
||||
file, _ := fileExt(strings.TrimSpace(t))
|
||||
permalink = path.Join(dir, file)
|
||||
_, t := path.Split(p.FileName)
|
||||
if p.Site.Config != nil && p.Site.Config.UglyUrls {
|
||||
x := replaceExtension(strings.TrimSpace(t), p.Extension)
|
||||
permalink = path.Join(dir, x)
|
||||
} else {
|
||||
file, _ := fileExt(strings.TrimSpace(t))
|
||||
permalink = path.Join(dir, file)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
base, err := url.Parse(baseUrl)
|
||||
@@ -555,6 +567,18 @@ func (p *Page) TargetPath() (outfile string) {
|
||||
return
|
||||
}
|
||||
|
||||
// If there's a Permalink specification, we use that
|
||||
if override, ok := p.Site.Permalinks[p.Section]; ok {
|
||||
var err error
|
||||
outfile, err = override.Expand(p)
|
||||
if err == nil {
|
||||
if strings.HasSuffix(outfile, "/") {
|
||||
outfile += "index.html"
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if len(strings.TrimSpace(p.Slug)) > 0 {
|
||||
outfile = strings.TrimSpace(p.Slug) + "." + p.Extension
|
||||
} else {
|
||||
|
Reference in New Issue
Block a user