Skip to content

Commit c65e49d

Browse files
zeripath6543
andauthored
Fix relative links in postprocessed images (#16334) (#16340)
* Fix relative links in postprocessed images (#16334) If a pre-post-processed file contains relative img tags these need to be updated and joined correctly with the prefix. Finally, the node attributes need to be updated. Fix #16308 Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: 6543 <6543@obermui.de>
1 parent 50084da commit c65e49d

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

modules/markup/html.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ func (ctx *postProcessCtx) visitNode(node *html.Node, visitText bool) {
401401
}
402402
case html.ElementNode:
403403
if node.Data == "img" {
404-
for _, attr := range node.Attr {
404+
for i, attr := range node.Attr {
405405
if attr.Key != "src" {
406406
continue
407407
}
@@ -414,6 +414,7 @@ func (ctx *postProcessCtx) visitNode(node *html.Node, visitText bool) {
414414

415415
attr.Val = util.URLJoin(prefix, attr.Val)
416416
}
417+
node.Attr[i] = attr
417418
}
418419
} else if node.Data == "a" {
419420
visitText = false

modules/markup/html_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,32 @@ func TestRender_ShortLinks(t *testing.T) {
384384
`<p><a href="https://example.org" rel="nofollow">[[foobar]]</a></p>`)
385385
}
386386

387+
func TestRender_RelativeImages(t *testing.T) {
388+
setting.AppURL = AppURL
389+
setting.AppSubURL = AppSubURL
390+
tree := util.URLJoin(AppSubURL, "src", "master")
391+
392+
test := func(input, expected, expectedWiki string) {
393+
buffer := markdown.RenderString(input, tree, localMetas)
394+
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
395+
buffer = markdown.RenderWiki([]byte(input), setting.AppSubURL, localMetas)
396+
assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(buffer))
397+
}
398+
399+
rawwiki := util.URLJoin(AppSubURL, "wiki", "raw")
400+
mediatree := util.URLJoin(AppSubURL, "media", "master")
401+
402+
test(
403+
`<img src="Link">`,
404+
`<img src="`+util.URLJoin(mediatree, "Link")+`"/>`,
405+
`<img src="`+util.URLJoin(rawwiki, "Link")+`"/>`)
406+
407+
test(
408+
`<img src="./icon.png">`,
409+
`<img src="`+util.URLJoin(mediatree, "icon.png")+`"/>`,
410+
`<img src="`+util.URLJoin(rawwiki, "icon.png")+`"/>`)
411+
}
412+
387413
func Test_ParseClusterFuzz(t *testing.T) {
388414
setting.AppURL = AppURL
389415
setting.AppSubURL = AppSubURL

0 commit comments

Comments
 (0)