Merge pull request '[BUG] Fix relative links on orgmode' (#2391) from gusted/forgejo-bp-2385 into v1.21/forgejo

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/2391
Reviewed-by: Otto <otto@codeberg.org>
This commit is contained in:
Gusted 2024-02-19 12:29:21 +00:00
commit 84ef9bba44
2 changed files with 57 additions and 3 deletions

View file

@ -147,11 +147,21 @@ func (r *Writer) resolveLink(node org.Node) string {
} }
if len(link) > 0 && !markup.IsLinkStr(link) && if len(link) > 0 && !markup.IsLinkStr(link) &&
link[0] != '#' && !strings.HasPrefix(link, mailto) { link[0] != '#' && !strings.HasPrefix(link, mailto) {
base := r.Ctx.Links.Base
var base string
if r.Ctx.IsWiki {
base = r.Ctx.Links.WikiLink()
} else if r.Ctx.Links.HasBranchInfo() {
base = r.Ctx.Links.SrcLink()
} else {
base = r.Ctx.Links.Base
}
switch l.Kind() { switch l.Kind() {
case "image", "video": case "image", "video":
base = r.Ctx.Links.ResolveMediaLink(r.Ctx.IsWiki) base = r.Ctx.Links.ResolveMediaLink(r.Ctx.IsWiki)
} }
link = util.URLJoin(base, link) link = util.URLJoin(base, link)
} }
return link return link

View file

@ -36,14 +36,58 @@ func TestRender_StandardLinks(t *testing.T) {
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer)) assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
} }
googleRendered := "<p><a href=\"https://google.com/\" title=\"https://google.com/\">https://google.com/</a></p>" // No BranchPath or TreePath set.
test("[[https://google.com/]]", googleRendered) test("[[file:comfy][comfy]]",
`<p><a href="http://localhost:3000/gogits/gogs/comfy" title="comfy">comfy</a></p>`)
test("[[https://google.com/]]",
`<p><a href="https://google.com/" title="https://google.com/">https://google.com/</a></p>`)
lnk := util.URLJoin(AppSubURL, "WikiPage") lnk := util.URLJoin(AppSubURL, "WikiPage")
test("[[WikiPage][WikiPage]]", test("[[WikiPage][WikiPage]]",
"<p><a href=\""+lnk+"\" title=\"WikiPage\">WikiPage</a></p>") "<p><a href=\""+lnk+"\" title=\"WikiPage\">WikiPage</a></p>")
} }
func TestRender_BaseLinks(t *testing.T) {
setting.AppURL = AppURL
setting.AppSubURL = AppSubURL
testBranch := func(input, expected string) {
buffer, err := RenderString(&markup.RenderContext{
Ctx: git.DefaultContext,
Links: markup.Links{
Base: setting.AppSubURL,
BranchPath: "branch/main",
},
}, input)
assert.NoError(t, err)
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
}
testBranchTree := func(input, expected string) {
buffer, err := RenderString(&markup.RenderContext{
Ctx: git.DefaultContext,
Links: markup.Links{
Base: setting.AppSubURL,
BranchPath: "branch/main",
TreePath: "deep/nested/folder",
},
}, input)
assert.NoError(t, err)
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
}
testBranch("[[file:comfy][comfy]]",
`<p><a href="http://localhost:3000/gogits/gogs/src/branch/main/comfy" title="comfy">comfy</a></p>`)
testBranchTree("[[file:comfy][comfy]]",
`<p><a href="http://localhost:3000/gogits/gogs/src/branch/main/deep/nested/folder/comfy" title="comfy">comfy</a></p>`)
testBranch("[[file:./src][./src/]]",
`<p><a href="http://localhost:3000/gogits/gogs/src/branch/main/src" title="./src/">./src/</a></p>`)
testBranchTree("[[file:./src][./src/]]",
`<p><a href="http://localhost:3000/gogits/gogs/src/branch/main/deep/nested/folder/src" title="./src/">./src/</a></p>`)
}
func TestRender_Media(t *testing.T) { func TestRender_Media(t *testing.T) {
setting.AppURL = AppURL setting.AppURL = AppURL
setting.AppSubURL = AppSubURL setting.AppSubURL = AppSubURL