Skip to content

Commit 3dcfc07

Browse files
committed
Fix relative links in postprocessed images
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 <[email protected]>
1 parent f166f9b commit 3dcfc07

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

modules/markup/html.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ func visitNode(ctx *RenderContext, procs []processor, node *html.Node, visitText
364364
}
365365
case html.ElementNode:
366366
if node.Data == "img" {
367-
for _, attr := range node.Attr {
367+
for i, attr := range node.Attr {
368368
if attr.Key != "src" {
369369
continue
370370
}
@@ -377,6 +377,7 @@ func visitNode(ctx *RenderContext, procs []processor, node *html.Node, visitText
377377

378378
attr.Val = util.URLJoin(prefix, attr.Val)
379379
}
380+
node.Attr[i] = attr
380381
}
381382
} else if node.Data == "a" {
382383
visitText = false

modules/markup/html_test.go

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

428+
func TestRender_RelativeImages(t *testing.T) {
429+
setting.AppURL = AppURL
430+
setting.AppSubURL = AppSubURL
431+
tree := util.URLJoin(AppSubURL, "src", "master")
432+
433+
test := func(input, expected, expectedUncyclo string) {
434+
buffer, err := markdown.RenderString(&RenderContext{
435+
URLPrefix: tree,
436+
}, input)
437+
assert.NoError(t, err)
438+
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
439+
buffer, err = markdown.RenderString(&RenderContext{
440+
URLPrefix: setting.AppSubURL,
441+
Metas: localMetas,
442+
IsUncyclo: true,
443+
}, input)
444+
assert.NoError(t, err)
445+
assert.Equal(t, strings.TrimSpace(expectedUncyclo), strings.TrimSpace(buffer))
446+
}
447+
448+
mediatree := util.URLJoin(AppSubURL, "media", "master")
449+
450+
url := util.URLJoin(mediatree, "Link")
451+
urlUncyclo := util.URLJoin(AppSubURL, "wiki", "raw", "Link")
452+
453+
test(
454+
`<img src="Link">`,
455+
`<img src="`+url+`"/>`,
456+
`<img src="`+urlUncyclo+`"/>`)
457+
458+
}
459+
428460
func Test_ParseClusterFuzz(t *testing.T) {
429461
setting.AppURL = AppURL
430462
setting.AppSubURL = AppSubURL

0 commit comments

Comments
 (0)