diff --git a/modules/markup/orgmode/orgmode.go b/modules/markup/orgmode/orgmode.go index e0c9b13a9e..8efe4e395d 100644 --- a/modules/markup/orgmode/orgmode.go +++ b/modules/markup/orgmode/orgmode.go @@ -147,11 +147,21 @@ func (r *Writer) resolveLink(node org.Node) string { } if len(link) > 0 && !markup.IsLinkStr(link) && 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() { case "image", "video": base = r.Ctx.Links.ResolveMediaLink(r.Ctx.IsWiki) } + link = util.URLJoin(base, link) } return link diff --git a/modules/markup/orgmode/orgmode_test.go b/modules/markup/orgmode/orgmode_test.go index e963718883..5ced819984 100644 --- a/modules/markup/orgmode/orgmode_test.go +++ b/modules/markup/orgmode/orgmode_test.go @@ -36,6 +36,10 @@ func TestRender_StandardLinks(t *testing.T) { assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer)) } + // No BranchPath or TreePath set. + test("[[file:comfy][comfy]]", + `

comfy

`) + test("[[https://google.com/]]", `

https://google.com/

`) @@ -44,6 +48,46 @@ func TestRender_StandardLinks(t *testing.T) { `

WikiPage

`) } +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]]", + `

comfy

`) + testBranchTree("[[file:comfy][comfy]]", + `

comfy

`) + + testBranch("[[file:./src][./src/]]", + `

./src/

`) + testBranchTree("[[file:./src][./src/]]", + `

./src/

`) +} + func TestRender_Media(t *testing.T) { setting.AppURL = AppURL setting.AppSubURL = AppSubURL