hugolib: Make sure everything ends up in its lang root in multihost mode

Fixes #4105
This commit is contained in:
Bjørn Erik Pedersen 2017-11-20 10:34:30 +01:00
parent 118b83d74b
commit 089fe49309
5 changed files with 181 additions and 144 deletions

View file

@ -254,7 +254,6 @@ func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) {
doc3 := enSite.RegularPages[2] doc3 := enSite.RegularPages[2]
permalink = doc3.Permalink() permalink = doc3.Permalink()
require.NoError(t, err, "permalink call failed")
// Note that /superbob is a custom URL set in frontmatter. // Note that /superbob is a custom URL set in frontmatter.
// We respect that URL literally (it can be /search.json) // We respect that URL literally (it can be /search.json)
// and do no not do any language code prefixing. // and do no not do any language code prefixing.
@ -1155,6 +1154,7 @@ NOTE: without slug, "doc2" should be used, without ".en" as URL
title: doc3 title: doc3
weight: 3 weight: 3
publishdate: "2000-01-03" publishdate: "2000-01-03"
aliases: [/en/al/alias1,/al/alias2/]
tags: tags:
- tag2 - tag2
- tag1 - tag1

View file

@ -68,6 +68,22 @@ languageName = "Nynorsk"
assert.Len(s1h.Translations(), 2) assert.Len(s1h.Translations(), 2)
assert.Equal("https://example.com/", s1h.Permalink()) assert.Equal("https://example.com/", s1h.Permalink())
// For “regular multilingual” we kept the aliases pages with url in front matter
// as a literal value that we use as is.
// There is an ambiguity in the guessing.
// For multihost, we never want any content in the root.
//
// check url in front matter:
pageWithURLInFrontMatter := s1.getPage(KindPage, "sect/doc3.en.md")
assert.NotNil(pageWithURLInFrontMatter)
assert.Equal("/superbob", pageWithURLInFrontMatter.URL())
assert.Equal("/superbob/", pageWithURLInFrontMatter.RelPermalink())
th.assertFileContent("public/en/superbob/index.html", "doc3|Hello|en")
// check alias:
th.assertFileContent("public/en/al/alias1/index.html", `content="0; url=https://example.com/superbob/"`)
th.assertFileContent("public/en/al/alias2/index.html", `content="0; url=https://example.com/superbob/"`)
s2 := sites.Sites[1] s2 := sites.Sites[1]
assert.Equal([]string{"s1", "s2", "frs1", "frs2"}, s2.StaticDirs()) assert.Equal([]string{"s1", "s2", "frs1", "frs2"}, s2.StaticDirs())

View file

@ -53,6 +53,9 @@ type targetPathDescriptor struct {
// language subdir. // language subdir.
LangPrefix string LangPrefix string
// Whether this is a multihost multilingual setup.
IsMultihost bool
// Page.URLPath.URL. Will override any Slug etc. for regular pages. // Page.URLPath.URL. Will override any Slug etc. for regular pages.
URL string URL string
@ -87,6 +90,7 @@ func (p *Page) initTargetPathDescriptor() error {
UglyURLs: p.s.Info.uglyURLs, UglyURLs: p.s.Info.uglyURLs,
Dir: filepath.ToSlash(p.Source.Dir()), Dir: filepath.ToSlash(p.Source.Dir()),
URL: p.URLPath.URL, URL: p.URLPath.URL,
IsMultihost: p.s.owner.IsMultihost(),
} }
if p.Slug != "" { if p.Slug != "" {
@ -177,7 +181,11 @@ func createTargetPath(d targetPathDescriptor) string {
if d.Kind == KindPage { if d.Kind == KindPage {
// Always use URL if it's specified // Always use URL if it's specified
if d.URL != "" { if d.URL != "" {
if d.IsMultihost && d.LangPrefix != "" && !strings.HasPrefix(d.URL, "/"+d.LangPrefix) {
pagePath = filepath.Join(d.LangPrefix, pagePath, d.URL)
} else {
pagePath = filepath.Join(pagePath, d.URL) pagePath = filepath.Join(pagePath, d.URL)
}
if strings.HasSuffix(d.URL, "/") || !strings.Contains(d.URL, ".") { if strings.HasSuffix(d.URL, "/") || !strings.Contains(d.URL, ".") {
pagePath = filepath.Join(pagePath, d.Type.BaseName+d.Type.MediaType.FullSuffix()) pagePath = filepath.Join(pagePath, d.Type.BaseName+d.Type.MediaType.FullSuffix())
} }

View file

@ -40,9 +40,10 @@ func TestPageTargetPath(t *testing.T) {
BaseName: "_redirects", BaseName: "_redirects",
} }
for _, multiHost := range []bool{false, true} {
for _, langPrefix := range []string{"", "no"} { for _, langPrefix := range []string{"", "no"} {
for _, uglyURLs := range []bool{false, true} { for _, uglyURLs := range []bool{false, true} {
t.Run(fmt.Sprintf("langPrefix=%q,uglyURLs=%t", langPrefix, uglyURLs), t.Run(fmt.Sprintf("multihost=%t,langPrefix=%q,uglyURLs=%t", multiHost, langPrefix, uglyURLs),
func(t *testing.T) { func(t *testing.T) {
tests := []struct { tests := []struct {
@ -157,6 +158,7 @@ func TestPageTargetPath(t *testing.T) {
test.d.PathSpec = pathSpec test.d.PathSpec = pathSpec
test.d.UglyURLs = uglyURLs test.d.UglyURLs = uglyURLs
test.d.LangPrefix = langPrefix test.d.LangPrefix = langPrefix
test.d.IsMultihost = multiHost
test.d.Dir = filepath.FromSlash(test.d.Dir) test.d.Dir = filepath.FromSlash(test.d.Dir)
isUgly := uglyURLs && !test.d.Type.NoUgly isUgly := uglyURLs && !test.d.Type.NoUgly
@ -174,6 +176,8 @@ func TestPageTargetPath(t *testing.T) {
if test.d.LangPrefix != "" && !(test.d.Kind == KindPage && test.d.URL != "") { if test.d.LangPrefix != "" && !(test.d.Kind == KindPage && test.d.URL != "") {
expected = "/" + test.d.LangPrefix + expected expected = "/" + test.d.LangPrefix + expected
} else if multiHost && test.d.LangPrefix != "" && test.d.URL != "" {
expected = "/" + test.d.LangPrefix + expected
} }
expected = filepath.FromSlash(expected) expected = filepath.FromSlash(expected)
@ -187,4 +191,5 @@ func TestPageTargetPath(t *testing.T) {
}) })
} }
} }
}
} }

View file

@ -16,6 +16,7 @@ package hugolib
import ( import (
"fmt" "fmt"
"path" "path"
"strings"
"sync" "sync"
"github.com/gohugoio/hugo/helpers" "github.com/gohugoio/hugo/helpers"
@ -380,6 +381,13 @@ func (s *Site) renderAliases() error {
a = path.Join(a, f.Path) a = path.Join(a, f.Path)
} }
lang := p.Lang()
if s.owner.multihost && !strings.HasPrefix(a, "/"+lang) {
// These need to be in its language root.
a = path.Join(lang, a)
}
if err := s.writeDestAlias(a, plink, p); err != nil { if err := s.writeDestAlias(a, plink, p); err != nil {
return err return err
} }