Skip to content

Commit

Permalink
Fix RSS with baseURL with subrid when render hooks is enabled
Browse files Browse the repository at this point in the history
Fixes #13332
  • Loading branch information
bep committed Feb 2, 2025
1 parent 835579b commit ca4dcda
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
40 changes: 40 additions & 0 deletions hugolib/rss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,43 @@ Figure:

b.AssertFileContent("public/index.xml", "img src="http://example.com/images/sunset.jpg")
}

// Issue 13332.
func TestRSSCanonifyURLsSubDir(t *testing.T) {
t.Parallel()

files := `
-- hugo.toml --
baseURL = 'https://example.org/subdir'
disableKinds = ['section','sitemap','taxonomy','term']
[markup.goldmark.renderHooks.image]
enableDefault = true
[markup.goldmark.renderHooks.link]
enableDefault = true
-- layouts/_default/home.html --
{{ .Content }}|
-- layouts/_default/single.html --
{{ .Content }}|
-- layouts/_default/rss.xml --
{{ with site.GetPage "/s1/p2" }}
{{ .Summary | transform.XMLEscape | safeHTML }}
{{ end }}
-- content/s1/p1.md --
---
title: p1
---
-- content/s1/p2/index.md --
---
title: p2
---
![alt](a.jpg)
[p1](/s1/p1)
-- content/s1/p2/a.jpg --
`

b := Test(t, files)

b.AssertFileContent("public/index.xml", "https://example.org/subdir/s1/p1/")
b.AssertFileContent("public/index.xml", "https://example.org/subdir/s1/p2/a.jpg")
}
4 changes: 3 additions & 1 deletion transform/urlreplacers/absurl.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

package urlreplacers

import "github.com/gohugoio/hugo/transform"
import (
"github.com/gohugoio/hugo/transform"
)

var ar = newAbsURLReplacer()

Expand Down
13 changes: 13 additions & 0 deletions transform/urlreplacers/absurlreplacer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ package urlreplacers
import (
"bytes"
"io"
"net/url"
"unicode"
"unicode/utf8"

"github.com/gohugoio/hugo/common/paths"
"github.com/gohugoio/hugo/transform"
)

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

// The root path, without leading slash.
root []byte

pos int // input position
start int // item start position

Expand Down Expand Up @@ -119,6 +124,9 @@ func checkCandidateBase(l *absurllexer) {
}
l.pos += relURLPrefixLen
l.w.Write(l.path)
if len(l.root) > 0 && bytes.HasPrefix(l.content[l.pos:], l.root) {
l.pos += len(l.root)
}
l.start = l.pos
}

Expand Down Expand Up @@ -229,10 +237,15 @@ func (l *absurllexer) replace() {
}

func doReplace(path string, ct transform.FromTo, quotes [][]byte) {
var root string
if u, err := url.Parse(path); err == nil {
root = paths.TrimLeading(u.Path)
}
lexer := &absurllexer{
content: ct.From().Bytes(),
w: ct.To(),
path: []byte(path),
root: []byte(root),
quotes: quotes,
}

Expand Down

0 comments on commit ca4dcda

Please sign in to comment.