From 5665a0212bf391a2b61add34a4633b5d7495cf34 Mon Sep 17 00:00:00 2001 From: Ankit R Gadiya Date: Sun, 10 Mar 2024 22:00:14 +0530 Subject: [PATCH] fix: rendering internal file links in org (#29669) The internal links to other files in the repository were not rendering with the Src Prefix (/src/branch-name/file-path). This commit fixes that by using the `SrcLink` as base if available. Resolves #29668 --- modules/markup/orgmode/orgmode.go | 8 ++++++ modules/markup/orgmode/orgmode_test.go | 38 ++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/modules/markup/orgmode/orgmode.go b/modules/markup/orgmode/orgmode.go index 7f253ae5f1..25f8d15ef4 100644 --- a/modules/markup/orgmode/orgmode.go +++ b/modules/markup/orgmode/orgmode.go @@ -142,10 +142,18 @@ func (r *Writer) resolveLink(kind, link string) string { // so we need to try to guess the link kind again here kind = org.RegularLink{URL: link}.Kind() } + base := r.Ctx.Links.Base + if r.Ctx.IsWiki { + base = r.Ctx.Links.WikiLink() + } else if r.Ctx.Links.HasBranchInfo() { + base = r.Ctx.Links.SrcLink() + } + if kind == "image" || kind == "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 95f53c9cc9..75b60ed81f 100644 --- a/modules/markup/orgmode/orgmode_test.go +++ b/modules/markup/orgmode/orgmode_test.go @@ -19,6 +19,30 @@ const AppURL = "http://localhost:3000/" func TestRender_StandardLinks(t *testing.T) { setting.AppURL = AppURL + test := func(input, expected string, isWiki bool) { + buffer, err := RenderString(&markup.RenderContext{ + Ctx: git.DefaultContext, + Links: markup.Links{ + Base: "/relative-path", + BranchPath: "branch/main", + }, + IsWiki: isWiki, + }, input) + assert.NoError(t, err) + assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer)) + } + + test("[[https://google.com/]]", + `

https://google.com/

`, false) + test("[[WikiPage][The WikiPage Desc]]", + `

The WikiPage Desc

`, true) + test("[[ImageLink.svg][The Image Desc]]", + `

The Image Desc

`, false) +} + +func TestRender_InternalLinks(t *testing.T) { + setting.AppURL = AppURL + test := func(input, expected string) { buffer, err := RenderString(&markup.RenderContext{ Ctx: git.DefaultContext, @@ -31,12 +55,14 @@ func TestRender_StandardLinks(t *testing.T) { assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer)) } - test("[[https://google.com/]]", - `

https://google.com/

`) - test("[[WikiPage][The WikiPage Desc]]", - `

The WikiPage Desc

`) - test("[[ImageLink.svg][The Image Desc]]", - `

The Image Desc

`) + test("[[file:test.org][Test]]", + `

Test

`) + test("[[./test.org][Test]]", + `

Test

`) + test("[[test.org][Test]]", + `

Test

`) + test("[[path/to/test.org][Test]]", + `

Test

`) } func TestRender_Media(t *testing.T) {