From 0c9e0a42f741fbdf5d5ff0e7268318340e6394b8 Mon Sep 17 00:00:00 2001 From: Ankit R Gadiya Date: Fri, 8 Mar 2024 19:11:04 +0530 Subject: [PATCH 1/3] fix: rendering internal file links in org 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 | 25 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/modules/markup/orgmode/orgmode.go b/modules/markup/orgmode/orgmode.go index 7f253ae5f12c1..25f8d15ef4739 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 95f53c9cc9ff5..612071ec1ecb8 100644 --- a/modules/markup/orgmode/orgmode_test.go +++ b/modules/markup/orgmode/orgmode_test.go @@ -39,6 +39,31 @@ func TestRender_StandardLinks(t *testing.T) { `

The Image Desc

`) } +func TestRender_InternalLinks(t *testing.T) { + setting.AppURL = AppURL + + test := func(input, expected string) { + buffer, err := RenderString(&markup.RenderContext{ + Ctx: git.DefaultContext, + Links: markup.Links{ + Base: "/relative-path", + BranchPath: "branch/main", + }, + }, input) + assert.NoError(t, err) + assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer)) + } + + 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) { setting.AppURL = AppURL From d40ae93f92891dca0f05801522e7a9c041e01320 Mon Sep 17 00:00:00 2001 From: Ankit R Gadiya Date: Fri, 8 Mar 2024 19:53:17 +0530 Subject: [PATCH 2/3] fix: unit tests for wiki links in orgmode --- modules/markup/orgmode/orgmode_test.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/modules/markup/orgmode/orgmode_test.go b/modules/markup/orgmode/orgmode_test.go index 612071ec1ecb8..0e828ae9819f2 100644 --- a/modules/markup/orgmode/orgmode_test.go +++ b/modules/markup/orgmode/orgmode_test.go @@ -31,10 +31,23 @@ func TestRender_StandardLinks(t *testing.T) { assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer)) } + testWiki := func(input, expected string) { + buffer, err := RenderString(&markup.RenderContext{ + Ctx: git.DefaultContext, + Links: markup.Links{ + Base: "/relative-path", + BranchPath: "branch/main", + }, + IsWiki: true, + }, input) + assert.NoError(t, err) + assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer)) + } + test("[[https://google.com/]]", `

https://google.com/

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

The WikiPage Desc

`) + testWiki("[[WikiPage][The WikiPage Desc]]", + `

The WikiPage Desc

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

The Image Desc

`) } From ba43dbea57ad1fbb834fda3f676aed0ee37150a9 Mon Sep 17 00:00:00 2001 From: Ankit R Gadiya Date: Sat, 9 Mar 2024 04:14:05 +0530 Subject: [PATCH 3/3] fix: merged testWiki and test functions --- modules/markup/orgmode/orgmode_test.go | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/modules/markup/orgmode/orgmode_test.go b/modules/markup/orgmode/orgmode_test.go index 0e828ae9819f2..75b60ed81f004 100644 --- a/modules/markup/orgmode/orgmode_test.go +++ b/modules/markup/orgmode/orgmode_test.go @@ -19,37 +19,25 @@ const AppURL = "http://localhost:3000/" func TestRender_StandardLinks(t *testing.T) { setting.AppURL = AppURL - test := func(input, expected string) { - buffer, err := RenderString(&markup.RenderContext{ - Ctx: git.DefaultContext, - Links: markup.Links{ - Base: "/relative-path", - BranchPath: "branch/main", - }, - }, input) - assert.NoError(t, err) - assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer)) - } - - testWiki := func(input, expected string) { + 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: true, + IsWiki: isWiki, }, input) assert.NoError(t, err) assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer)) } test("[[https://google.com/]]", - `

https://google.com/

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

The WikiPage Desc

`) + `

https://google.com/

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

The WikiPage Desc

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

The Image Desc

`) + `

The Image Desc

`, false) } func TestRender_InternalLinks(t *testing.T) {