]> source.dussan.org Git - gitea.git/commitdiff
fix: rendering internal file links in org (#29669) (#29705)
authorGiteabot <teabot@gitea.io>
Sun, 10 Mar 2024 17:29:17 +0000 (01:29 +0800)
committerGitHub <noreply@github.com>
Sun, 10 Mar 2024 17:29:17 +0000 (18:29 +0100)
Backport #29669 by @ankitrgadiya

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

Co-authored-by: Ankit R Gadiya <git@argp.in>
modules/markup/orgmode/orgmode.go
modules/markup/orgmode/orgmode_test.go

index ac1cedff6d4c1c4023f732e018832d68bf9276c0..53bacb0a4ef038bd016e5a806a8d33741003f0b8 100644 (file)
@@ -143,10 +143,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
index b320cc7f5b07b5a7961c180daf10c31194fc2d2b..cf64093fe4195466f1d9b03ef1e682312c2f631f 100644 (file)
@@ -19,24 +19,50 @@ const AppURL = "http://localhost:3000/"
 func TestRender_StandardLinks(t *testing.T) {
        setting.AppURL = AppURL
 
-       test := 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: isWiki,
                }, input)
                assert.NoError(t, err)
                assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
        }
 
        test("[[https://google.com/]]",
-               `<p><a href="https://google.com/">https://google.com/</a></p>`)
+               `<p><a href="https://google.com/">https://google.com/</a></p>`, false)
        test("[[WikiPage][The WikiPage Desc]]",
-               `<p><a href="/relative-path/WikiPage">The WikiPage Desc</a></p>`)
+               `<p><a href="/relative-path/wiki/WikiPage">The WikiPage Desc</a></p>`, true)
        test("[[ImageLink.svg][The Image Desc]]",
-               `<p><a href="/relative-path/media/branch/main/ImageLink.svg">The Image Desc</a></p>`)
+               `<p><a href="/relative-path/media/branch/main/ImageLink.svg">The Image Desc</a></p>`, false)
+}
+
+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]]",
+               `<p><a href="/relative-path/src/branch/main/test.org">Test</a></p>`)
+       test("[[./test.org][Test]]",
+               `<p><a href="/relative-path/src/branch/main/test.org">Test</a></p>`)
+       test("[[test.org][Test]]",
+               `<p><a href="/relative-path/src/branch/main/test.org">Test</a></p>`)
+       test("[[path/to/test.org][Test]]",
+               `<p><a href="/relative-path/src/branch/main/path/to/test.org">Test</a></p>`)
 }
 
 func TestRender_Media(t *testing.T) {