Skip to content

Commit 760c13a

Browse files
committed
Fix RSS with baseURL with sub dir when render hooks is enabled
Fixes #13332
1 parent 3bd73d2 commit 760c13a

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

hugolib/rss_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,51 @@ Figure:
9696

9797
b.AssertFileContent("public/index.xml", "img src="http://example.com/images/sunset.jpg")
9898
}
99+
100+
// Issue 13332.
101+
func TestRSSCanonifyURLsSubDir(t *testing.T) {
102+
t.Parallel()
103+
104+
files := `
105+
-- hugo.toml --
106+
baseURL = 'https://example.org/subdir'
107+
disableKinds = ['section','sitemap','taxonomy','term']
108+
[markup.goldmark.renderHooks.image]
109+
enableDefault = true
110+
[markup.goldmark.renderHooks.link]
111+
enableDefault = true
112+
-- layouts/_default/_markup/render-image.html --
113+
{{- $u := urls.Parse .Destination -}}
114+
{{- $src := $u.String | relURL -}}
115+
<img srcset="{{ $src }}" src="{{ $src }} 2x">
116+
<img src="{{ $src }}">
117+
{{- /**/ -}}
118+
-- layouts/_default/home.html --
119+
{{ .Content }}|
120+
-- layouts/_default/single.html --
121+
{{ .Content }}|
122+
-- layouts/_default/rss.xml --
123+
{{ with site.GetPage "/s1/p2" }}
124+
{{ .Content | transform.XMLEscape | safeHTML }}
125+
{{ end }}
126+
-- content/s1/p1.md --
127+
---
128+
title: p1
129+
---
130+
-- content/s1/p2/index.md --
131+
---
132+
title: p2
133+
---
134+
![alt](a.jpg)
135+
136+
[p1](/s1/p1)
137+
-- content/s1/p2/a.jpg --
138+
`
139+
140+
b := Test(t, files)
141+
142+
b.AssertFileContent("public/index.xml", "https://example.org/subdir/s1/p1/")
143+
b.AssertFileContent("public/index.xml",
144+
"img src=&#34;https://example.org/subdir/a.jpg",
145+
"img srcset=&#34;https://example.org/subdir/a.jpg&#34; src=&#34;https://example.org/subdir/a.jpg 2x")
146+
}

transform/urlreplacers/absurl.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
package urlreplacers
1515

16-
import "github.com/gohugoio/hugo/transform"
16+
import (
17+
"github.com/gohugoio/hugo/transform"
18+
)
1719

1820
var ar = newAbsURLReplacer()
1921

transform/urlreplacers/absurlreplacer.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ package urlreplacers
1616
import (
1717
"bytes"
1818
"io"
19+
"net/url"
1920
"unicode"
2021
"unicode/utf8"
2122

23+
"github.com/gohugoio/hugo/common/paths"
2224
"github.com/gohugoio/hugo/transform"
2325
)
2426

@@ -31,6 +33,9 @@ type absurllexer struct {
3133
// path may be set to a "." relative path
3234
path []byte
3335

36+
// The root path, without leading slash.
37+
root []byte
38+
3439
pos int // input position
3540
start int // item start position
3641

@@ -119,6 +124,9 @@ func checkCandidateBase(l *absurllexer) {
119124
}
120125
l.pos += relURLPrefixLen
121126
l.w.Write(l.path)
127+
if len(l.root) > 0 && bytes.HasPrefix(l.content[l.pos:], l.root) {
128+
l.pos += len(l.root)
129+
}
122130
l.start = l.pos
123131
}
124132

@@ -174,7 +182,11 @@ func checkCandidateSrcset(l *absurllexer) {
174182
for i, f := range fields {
175183
if f[0] == '/' {
176184
l.w.Write(l.path)
177-
l.w.Write(f[1:])
185+
n := 1
186+
if len(l.root) > 0 && bytes.HasPrefix(f[n:], l.root) {
187+
n += len(l.root)
188+
}
189+
l.w.Write(f[n:])
178190

179191
} else {
180192
l.w.Write(f)
@@ -229,10 +241,15 @@ func (l *absurllexer) replace() {
229241
}
230242

231243
func doReplace(path string, ct transform.FromTo, quotes [][]byte) {
244+
var root string
245+
if u, err := url.Parse(path); err == nil {
246+
root = paths.TrimLeading(u.Path)
247+
}
232248
lexer := &absurllexer{
233249
content: ct.From().Bytes(),
234250
w: ct.To(),
235251
path: []byte(path),
252+
root: []byte(root),
236253
quotes: quotes,
237254
}
238255

0 commit comments

Comments
 (0)