From fa700333ba2649d14f1670dd2745957704a33b40 Mon Sep 17 00:00:00 2001 From: Gusted Date: Sun, 18 Feb 2024 21:03:34 +0100 Subject: [PATCH] [BUG] Fix relative links on orgmode - Backport of #2385 - For regular non-image nonvideo links, they should be made relative, this was done against `r.Ctx.Links.Base`, but since 637451a45e, that should instead be done by `SrcLink()` if there's branch information set in the context, because branch and treepath information are no longer set in `r.Ctx.Links.Base`. - This is consistent with how #2166 _fixed_ relative links. - Media is not affected, `TestRender_Media` test doesn't fail. - Adds unit tests. - Ref https://codeberg.org/Codeberg/Community/issues/1485 (cherry picked from commit a2442793d2303e73638c699000c829f29d68efe1) --- modules/markup/orgmode/orgmode.go | 12 ++++++- modules/markup/orgmode/orgmode_test.go | 48 ++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/modules/markup/orgmode/orgmode.go b/modules/markup/orgmode/orgmode.go index a9e0f4aea4..867b10dd19 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 1f9ec0bac5..3e1b8bb0fd 100644 --- a/modules/markup/orgmode/orgmode_test.go +++ b/modules/markup/orgmode/orgmode_test.go @@ -36,14 +36,58 @@ func TestRender_StandardLinks(t *testing.T) { assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer)) } - googleRendered := "

https://google.com/

" - test("[[https://google.com/]]", googleRendered) + // No BranchPath or TreePath set. + test("[[file:comfy][comfy]]", + `

comfy

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

https://google.com/

`) lnk := util.URLJoin(AppSubURL, "WikiPage") test("[[WikiPage][WikiPage]]", "

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