mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
Fix aliases with relativeURLs
This commit is contained in:
parent
01e249e97c
commit
145b3fcce3
7 changed files with 54 additions and 43 deletions
|
@ -99,8 +99,11 @@ func (s *Site) publishDestAlias(allowRoot bool, path, permalink string, outputFo
|
||||||
OutputFormat: outputFormat,
|
OutputFormat: outputFormat,
|
||||||
}
|
}
|
||||||
|
|
||||||
return s.publisher.Publish(pd)
|
if s.Info.relativeURLs || s.Info.canonifyURLs {
|
||||||
|
pd.AbsURLPath = s.absURLPath(targetPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.publisher.Publish(pd)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a aliasHandler) targetPathAlias(src string) (string, error) {
|
func (a aliasHandler) targetPathAlias(src string) (string, error) {
|
||||||
|
|
|
@ -45,8 +45,18 @@ func TestAlias(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
c := qt.New(t)
|
c := qt.New(t)
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
urlPrefix string
|
||||||
|
settings map[string]interface{}
|
||||||
|
}{
|
||||||
|
{"http://example.com", map[string]interface{}{"baseURL": "http://example.com"}},
|
||||||
|
{"http://example.com", map[string]interface{}{"baseURL": "http://example.com", "canonifyURLs": true}},
|
||||||
|
{"../..", map[string]interface{}{"relativeURLs": true}},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
b := newTestSitesBuilder(t)
|
b := newTestSitesBuilder(t)
|
||||||
b.WithSimpleConfigFile().WithContent("blog/page.md", pageWithAlias)
|
b.WithSimpleConfigFileAndSettings(test.settings).WithContent("blog/page.md", pageWithAlias)
|
||||||
b.CreateSites().Build(BuildCfg{})
|
b.CreateSites().Build(BuildCfg{})
|
||||||
|
|
||||||
c.Assert(len(b.H.Sites), qt.Equals, 1)
|
c.Assert(len(b.H.Sites), qt.Equals, 1)
|
||||||
|
@ -55,8 +65,9 @@ func TestAlias(t *testing.T) {
|
||||||
// the real page
|
// the real page
|
||||||
b.AssertFileContent("public/blog/page/index.html", "For some moments the old man")
|
b.AssertFileContent("public/blog/page/index.html", "For some moments the old man")
|
||||||
// the alias redirectors
|
// the alias redirectors
|
||||||
b.AssertFileContent("public/foo/bar/index.html", "<meta http-equiv=\"refresh\" content=\"0; ")
|
b.AssertFileContent("public/foo/bar/index.html", "<meta http-equiv=\"refresh\" content=\"0; url="+test.urlPrefix+"/blog/page/\" />")
|
||||||
b.AssertFileContent("public/blog/rel/index.html", "<meta http-equiv=\"refresh\" content=\"0; ")
|
b.AssertFileContent("public/blog/rel/index.html", "<meta http-equiv=\"refresh\" content=\"0; url="+test.urlPrefix+"/blog/page/\" />")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAliasMultipleOutputFormats(t *testing.T) {
|
func TestAliasMultipleOutputFormats(t *testing.T) {
|
||||||
|
|
|
@ -1540,7 +1540,21 @@ func (s *SiteInfo) GetPage(ref ...string) (page.Page, error) {
|
||||||
|
|
||||||
func (s *Site) permalink(link string) string {
|
func (s *Site) permalink(link string) string {
|
||||||
return s.PathSpec.PermalinkForBaseURL(link, s.PathSpec.BaseURL.String())
|
return s.PathSpec.PermalinkForBaseURL(link, s.PathSpec.BaseURL.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Site) absURLPath(targetPath string) string {
|
||||||
|
var path string
|
||||||
|
if s.Info.relativeURLs {
|
||||||
|
path = helpers.GetDottedRelativePath(targetPath)
|
||||||
|
} else {
|
||||||
|
url := s.PathSpec.BaseURL.String()
|
||||||
|
if !strings.HasSuffix(url, "/") {
|
||||||
|
url += "/"
|
||||||
|
}
|
||||||
|
path = url
|
||||||
|
}
|
||||||
|
|
||||||
|
return path
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Site) lookupLayouts(layouts ...string) tpl.Template {
|
func (s *Site) lookupLayouts(layouts ...string) tpl.Template {
|
||||||
|
@ -1562,17 +1576,6 @@ func (s *Site) renderAndWriteXML(statCounter *uint64, name string, targetPath st
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var path string
|
|
||||||
if s.Info.relativeURLs {
|
|
||||||
path = helpers.GetDottedRelativePath(targetPath)
|
|
||||||
} else {
|
|
||||||
s := s.PathSpec.BaseURL.String()
|
|
||||||
if !strings.HasSuffix(s, "/") {
|
|
||||||
s += "/"
|
|
||||||
}
|
|
||||||
path = s
|
|
||||||
}
|
|
||||||
|
|
||||||
pd := publisher.Descriptor{
|
pd := publisher.Descriptor{
|
||||||
Src: renderBuffer,
|
Src: renderBuffer,
|
||||||
TargetPath: targetPath,
|
TargetPath: targetPath,
|
||||||
|
@ -1580,14 +1583,14 @@ func (s *Site) renderAndWriteXML(statCounter *uint64, name string, targetPath st
|
||||||
// For the minification part of XML,
|
// For the minification part of XML,
|
||||||
// we currently only use the MIME type.
|
// we currently only use the MIME type.
|
||||||
OutputFormat: output.RSSFormat,
|
OutputFormat: output.RSSFormat,
|
||||||
AbsURLPath: path,
|
AbsURLPath: s.absURLPath(targetPath),
|
||||||
}
|
}
|
||||||
|
|
||||||
return s.publisher.Publish(pd)
|
return s.publisher.Publish(pd)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Site) renderAndWritePage(statCounter *uint64, name string, targetPath string, p *pageState, templ tpl.Template) error {
|
func (s *Site) renderAndWritePage(statCounter *uint64, name string, targetPath string, p *pageState, templ tpl.Template) error {
|
||||||
|
s.Log.DEBUG.Printf("Render %s to %q", name, targetPath)
|
||||||
renderBuffer := bp.GetBuffer()
|
renderBuffer := bp.GetBuffer()
|
||||||
defer bp.PutBuffer(renderBuffer)
|
defer bp.PutBuffer(renderBuffer)
|
||||||
|
|
||||||
|
@ -1604,18 +1607,6 @@ func (s *Site) renderAndWritePage(statCounter *uint64, name string, targetPath s
|
||||||
isHTML := of.IsHTML
|
isHTML := of.IsHTML
|
||||||
isRSS := of.Name == "RSS"
|
isRSS := of.Name == "RSS"
|
||||||
|
|
||||||
var path string
|
|
||||||
|
|
||||||
if s.Info.relativeURLs {
|
|
||||||
path = helpers.GetDottedRelativePath(targetPath)
|
|
||||||
} else if isRSS || s.Info.canonifyURLs {
|
|
||||||
url := s.PathSpec.BaseURL.String()
|
|
||||||
if !strings.HasSuffix(url, "/") {
|
|
||||||
url += "/"
|
|
||||||
}
|
|
||||||
path = url
|
|
||||||
}
|
|
||||||
|
|
||||||
pd := publisher.Descriptor{
|
pd := publisher.Descriptor{
|
||||||
Src: renderBuffer,
|
Src: renderBuffer,
|
||||||
TargetPath: targetPath,
|
TargetPath: targetPath,
|
||||||
|
@ -1625,10 +1616,10 @@ func (s *Site) renderAndWritePage(statCounter *uint64, name string, targetPath s
|
||||||
|
|
||||||
if isRSS {
|
if isRSS {
|
||||||
// Always canonify URLs in RSS
|
// Always canonify URLs in RSS
|
||||||
pd.AbsURLPath = path
|
pd.AbsURLPath = s.absURLPath(targetPath)
|
||||||
} else if isHTML {
|
} else if isHTML {
|
||||||
if s.Info.relativeURLs || s.Info.canonifyURLs {
|
if s.Info.relativeURLs || s.Info.canonifyURLs {
|
||||||
pd.AbsURLPath = path
|
pd.AbsURLPath = s.absURLPath(targetPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.running() && s.Cfg.GetBool("watch") && !s.Cfg.GetBool("disableLiveReload") {
|
if s.running() && s.Cfg.GetBool("watch") && !s.Cfg.GetBool("disableLiveReload") {
|
||||||
|
|
|
@ -383,7 +383,7 @@ func (s *Site) renderMainLanguageRedirect() error {
|
||||||
if found {
|
if found {
|
||||||
mainLang := s.h.multilingual.DefaultLang
|
mainLang := s.h.multilingual.DefaultLang
|
||||||
if s.Info.defaultContentLanguageInSubdir {
|
if s.Info.defaultContentLanguageInSubdir {
|
||||||
mainLangURL := s.PathSpec.AbsURL(mainLang.Lang, false)
|
mainLangURL := s.PathSpec.AbsURL(mainLang.Lang+"/", false)
|
||||||
s.Log.DEBUG.Printf("Write redirect to main language %s: %s", mainLang, mainLangURL)
|
s.Log.DEBUG.Printf("Write redirect to main language %s: %s", mainLang, mainLangURL)
|
||||||
if err := s.publishDestAlias(true, "/", mainLangURL, html, nil); err != nil {
|
if err := s.publishDestAlias(true, "/", mainLangURL, html, nil); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -277,9 +277,14 @@ func (s *sitesBuilder) WithSimpleConfigFile() *sitesBuilder {
|
||||||
|
|
||||||
func (s *sitesBuilder) WithSimpleConfigFileAndBaseURL(baseURL string) *sitesBuilder {
|
func (s *sitesBuilder) WithSimpleConfigFileAndBaseURL(baseURL string) *sitesBuilder {
|
||||||
s.T.Helper()
|
s.T.Helper()
|
||||||
config := fmt.Sprintf("baseURL = %q", baseURL)
|
return s.WithSimpleConfigFileAndSettings(map[string]interface{}{"baseURL": baseURL})
|
||||||
|
}
|
||||||
|
|
||||||
config = config + commonConfigSections
|
func (s *sitesBuilder) WithSimpleConfigFileAndSettings(settings interface{}) *sitesBuilder {
|
||||||
|
s.T.Helper()
|
||||||
|
var buf bytes.Buffer
|
||||||
|
parser.InterfaceToConfig(settings, metadecoders.TOML, &buf)
|
||||||
|
config := buf.String() + commonConfigSections
|
||||||
return s.WithConfigFile("toml", config)
|
return s.WithConfigFile("toml", config)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -69,6 +69,7 @@ func newPrefixState() []*prefix {
|
||||||
return []*prefix{
|
return []*prefix{
|
||||||
{b: []byte("src="), f: checkCandidateBase},
|
{b: []byte("src="), f: checkCandidateBase},
|
||||||
{b: []byte("href="), f: checkCandidateBase},
|
{b: []byte("href="), f: checkCandidateBase},
|
||||||
|
{b: []byte("url="), f: checkCandidateBase},
|
||||||
{b: []byte("action="), f: checkCandidateBase},
|
{b: []byte("action="), f: checkCandidateBase},
|
||||||
{b: []byte("srcset="), f: checkCandidateSrcset},
|
{b: []byte("srcset="), f: checkCandidateSrcset},
|
||||||
}
|
}
|
||||||
|
|
|
@ -88,8 +88,8 @@ schemaless: <img srcset='//img.jpg' src='//basic.jpg'>
|
||||||
schemaless2: <img srcset="//img.jpg" src="//basic.jpg2> POST
|
schemaless2: <img srcset="//img.jpg" src="//basic.jpg2> POST
|
||||||
`
|
`
|
||||||
|
|
||||||
relPathVariations = `PRE. a href="/img/small.jpg" input action="/foo.html" POST.`
|
relPathVariations = `PRE. a href="/img/small.jpg" input action="/foo.html" meta url=/redirect/to/page/ POST.`
|
||||||
relPathVariationsCorrect = `PRE. a href="../../img/small.jpg" input action="../../foo.html" POST.`
|
relPathVariationsCorrect = `PRE. a href="../../img/small.jpg" input action="../../foo.html" meta url=../../redirect/to/page/ POST.`
|
||||||
|
|
||||||
testBaseURL = "http://base/"
|
testBaseURL = "http://base/"
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue