diff --git a/hugolib/site.go b/hugolib/site.go index 6ff64b48e..172ac8d30 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -1195,7 +1195,17 @@ func (s *Site) layoutExists(layouts ...string) bool { func (s *Site) renderXML(name string, d interface{}, layouts ...string) (io.Reader, error) { renderBuffer := s.NewXMLBuffer() err := s.render(name, d, renderBuffer, layouts...) - return renderBuffer, err + + var outBuffer = new(bytes.Buffer) + + absURLInXML, err := transform.AbsURLInXML(viper.GetString("BaseUrl")) + if err != nil { + return nil, err + } + + transformer := transform.NewChain(absURLInXML...) + transformer.Apply(outBuffer, renderBuffer) + return outBuffer, err } func (s *Site) renderPage(name string, d interface{}, layouts ...string) (io.Reader, error) { diff --git a/transform/absurl.go b/transform/absurl.go index 6fb1cb4a1..0a0cd7239 100644 --- a/transform/absurl.go +++ b/transform/absurl.go @@ -31,6 +31,31 @@ func AbsURL(absURL string) (trs []link, err error) { return } +func AbsURLInXML(absURL string) (trs []link, err error) { + var baseURL *url.URL + + if baseURL, err = url.Parse(absURL); err != nil { + return + } + + base := strings.TrimRight(baseURL.String(), "/") + + var ( + srcedq = []byte(" src="" + base + "/") + hrefedq = []byte(" href="" + base + "/") + srcesq = []byte(" src='" + base + "/") + hrefesq = []byte(" href='" + base + "/") + ) + trs = append(trs, func(content []byte) []byte { + content = guardReplace(content, []byte(" src="//"), []byte(" src="/"), srcedq) + content = guardReplace(content, []byte(" src='//"), []byte(" src='/"), srcesq) + content = guardReplace(content, []byte(" href="//"), []byte(" href="/"), hrefedq) + content = guardReplace(content, []byte(" href='//"), []byte(" href='/"), hrefesq) + return content + }) + return +} + func guardReplace(content, guard, match, replace []byte) []byte { if !bytes.Contains(content, guard) { content = bytes.Replace(content, match, replace, -1) diff --git a/transform/chain_test.go b/transform/chain_test.go index 94135f6d7..936a6a1ad 100644 --- a/transform/chain_test.go +++ b/transform/chain_test.go @@ -9,10 +9,18 @@ const H5_JS_CONTENT_ABS_URL_WITH_NAV = "