From d868b924442d939f5e4e6e28de72bb4af96186c9 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Fri, 2 Feb 2024 10:45:01 +0800 Subject: [PATCH 1/5] fix --- modules/markup/orgmode/orgmode.go | 20 ++++++-------------- modules/markup/orgmode/orgmode_test.go | 8 ++++++++ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/modules/markup/orgmode/orgmode.go b/modules/markup/orgmode/orgmode.go index abc641fbe2dfc..1d1a85c8e9f73 100644 --- a/modules/markup/orgmode/orgmode.go +++ b/modules/markup/orgmode/orgmode.go @@ -135,26 +135,18 @@ type Writer struct { const mailto = "mailto:" -func (r *Writer) resolveLink(l org.RegularLink) string { - link := html.EscapeString(l.URL) - if l.Protocol == "file" { - link = link[len("file:"):] - } +func (r *Writer) resolveLink(link string) string { + link = strings.TrimPrefix(link, "file:") if len(link) > 0 && !markup.IsLinkStr(link) && link[0] != '#' && !strings.HasPrefix(link, mailto) { - base := r.Ctx.Links.Base - switch l.Kind() { - case "image", "video": - base = r.Ctx.Links.ResolveMediaLink(r.Ctx.IsWiki) - } - link = util.URLJoin(base, link) + link = util.URLJoin(r.Ctx.Links.ResolveMediaLink(r.Ctx.IsWiki), link) } return link } // WriteRegularLink renders images, links or videos func (r *Writer) WriteRegularLink(l org.RegularLink) { - link := r.resolveLink(l) + link := r.resolveLink(l.URL) // Inspired by https://github.com/niklasfasching/go-org/blob/6eb20dbda93cb88c3503f7508dc78cbbc639378f/org/html_writer.go#L406-L427 switch l.Kind() { @@ -162,14 +154,14 @@ func (r *Writer) WriteRegularLink(l org.RegularLink) { if l.Description == nil { fmt.Fprintf(r, `%s`, link, link) } else { - imageSrc := r.resolveLink(l.Description[0].(org.RegularLink)) + imageSrc := r.resolveLink(org.String(l.Description...)) fmt.Fprintf(r, `%s`, link, imageSrc, imageSrc) } case "video": if l.Description == nil { fmt.Fprintf(r, ``, link, link) } else { - videoSrc := r.resolveLink(l.Description[0].(org.RegularLink)) + videoSrc := r.resolveLink(org.String(l.Description...)) fmt.Fprintf(r, ``, link, videoSrc, videoSrc) } default: diff --git a/modules/markup/orgmode/orgmode_test.go b/modules/markup/orgmode/orgmode_test.go index abf5ca8fcf385..0834089726e95 100644 --- a/modules/markup/orgmode/orgmode_test.go +++ b/modules/markup/orgmode/orgmode_test.go @@ -80,6 +80,14 @@ func TestRender_Media(t *testing.T) { `

https://example.com/example.svg

`) test("[[https://example.com/example.mp4]]", `

`) + + // test [[LINK][DESCRIPTION]] syntax with "file:" prefix + test(`[[file:https://example.com/foo%20bar.svg][Goto Image]]`, + `

Goto Image

`) + test(`[[file:https://example.com/link][https://example.com/image.jpg]]`, + `

https://example.com/image.jpg

`) + test(`[[file:https://example.com/link][file:https://example.com/image.jpg]]`, + `

https://example.com/image.jpg

`) } func TestRender_Source(t *testing.T) { From dc92cb17fc0db1288da4c59c2fa869f30547235b Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Fri, 2 Feb 2024 11:47:06 +0800 Subject: [PATCH 2/5] add a redundant case for "file:" prefix --- modules/markup/orgmode/orgmode_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/markup/orgmode/orgmode_test.go b/modules/markup/orgmode/orgmode_test.go index 0834089726e95..0d70e3f1fe0af 100644 --- a/modules/markup/orgmode/orgmode_test.go +++ b/modules/markup/orgmode/orgmode_test.go @@ -82,6 +82,8 @@ func TestRender_Media(t *testing.T) { `

`) // test [[LINK][DESCRIPTION]] syntax with "file:" prefix + test(`[[https://example.com/][file:https://example.com/foo%20bar.svg]]`, + `

https://example.com/foo%20bar.svg

`) test(`[[file:https://example.com/foo%20bar.svg][Goto Image]]`, `

Goto Image

`) test(`[[file:https://example.com/link][https://example.com/image.jpg]]`, From 28aa745107aa96dfe19a2b040160e5ec1e7a5ec7 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Fri, 2 Feb 2024 12:05:42 +0800 Subject: [PATCH 3/5] clarify relative path behavior --- modules/markup/orgmode/orgmode_test.go | 28 +++++++++----------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/modules/markup/orgmode/orgmode_test.go b/modules/markup/orgmode/orgmode_test.go index 0d70e3f1fe0af..da8d800a00625 100644 --- a/modules/markup/orgmode/orgmode_test.go +++ b/modules/markup/orgmode/orgmode_test.go @@ -10,26 +10,20 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/setting" - "code.gitea.io/gitea/modules/util" "github.com/stretchr/testify/assert" ) -const ( - AppURL = "http://localhost:3000/" - Repo = "gogits/gogs" - AppSubURL = AppURL + Repo + "/" -) +const AppURL = "http://localhost:3000/" func TestRender_StandardLinks(t *testing.T) { setting.AppURL = AppURL - setting.AppSubURL = AppSubURL test := func(input, expected string) { buffer, err := RenderString(&markup.RenderContext{ Ctx: git.DefaultContext, Links: markup.Links{ - Base: setting.AppSubURL, + Base: "/relative-path", }, }, input) assert.NoError(t, err) @@ -39,31 +33,28 @@ func TestRender_StandardLinks(t *testing.T) { test("[[https://google.com/]]", `

https://google.com/

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

WikiPage

`) + test("[[WikiPage][The WikiPage Name]]", + `

The WikiPage Name

`) } func TestRender_Media(t *testing.T) { setting.AppURL = AppURL - setting.AppSubURL = AppSubURL test := func(input, expected string) { buffer, err := RenderString(&markup.RenderContext{ Ctx: git.DefaultContext, Links: markup.Links{ - Base: setting.AppSubURL, + Base: "./relative-path", }, }, input) assert.NoError(t, err) assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer)) } - url := "../../.images/src/02/train.jpg" - result := util.URLJoin(AppSubURL, url) - - test("[[file:"+url+"]]", - `

`+result+`

`) + test("[[file:../../.images/src/02/train.jpg]]", + `

.images/src/02/train.jpg

`) + test("[[file:train.jpg]]", + `

relative-path/train.jpg

`) // With description. test("[[https://example.com][https://example.com/example.svg]]", @@ -94,7 +85,6 @@ func TestRender_Media(t *testing.T) { func TestRender_Source(t *testing.T) { setting.AppURL = AppURL - setting.AppSubURL = AppSubURL test := func(input, expected string) { buffer, err := RenderString(&markup.RenderContext{ From 6cd906bdf0ad83ada245c21dbd3447f663bf3541 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 3 Feb 2024 10:11:03 +0800 Subject: [PATCH 4/5] fix orgmode media link resolving --- modules/markup/orgmode/orgmode.go | 36 ++++++++++++++++---------- modules/markup/orgmode/orgmode_test.go | 10 ++++--- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/modules/markup/orgmode/orgmode.go b/modules/markup/orgmode/orgmode.go index 1d1a85c8e9f73..2bf6a88e28329 100644 --- a/modules/markup/orgmode/orgmode.go +++ b/modules/markup/orgmode/orgmode.go @@ -133,42 +133,50 @@ type Writer struct { Ctx *markup.RenderContext } -const mailto = "mailto:" - -func (r *Writer) resolveLink(link string) string { +func (r *Writer) resolveLink(kind string, link string) string { link = strings.TrimPrefix(link, "file:") - if len(link) > 0 && !markup.IsLinkStr(link) && - link[0] != '#' && !strings.HasPrefix(link, mailto) { - link = util.URLJoin(r.Ctx.Links.ResolveMediaLink(r.Ctx.IsWiki), link) + if !strings.HasPrefix(link, "#") && // not a URL fragment + !markup.IsLinkStr(link) && // not an absolute URL + !strings.HasPrefix(link, "mailto:") { + if kind == "regular" { + // orgmode reports the link kind as "regular" for "[[ImageLink.svg][The Image Desc]]" + // so we need to try to guess the link kind again here + kind = org.RegularLink{URL: link}.Kind() + } + base := r.Ctx.Links.Base + if kind == "image" || kind == "video" { + base = r.Ctx.Links.ResolveMediaLink(r.Ctx.IsWiki) + } + link = util.URLJoin(base, link) } return link } // WriteRegularLink renders images, links or videos func (r *Writer) WriteRegularLink(l org.RegularLink) { - link := r.resolveLink(l.URL) + link := r.resolveLink(l.Kind(), l.URL) // Inspired by https://github.com/niklasfasching/go-org/blob/6eb20dbda93cb88c3503f7508dc78cbbc639378f/org/html_writer.go#L406-L427 switch l.Kind() { case "image": if l.Description == nil { - fmt.Fprintf(r, `%s`, link, link) + _, _ = fmt.Fprintf(r, `%s`, link, link) } else { - imageSrc := r.resolveLink(org.String(l.Description...)) - fmt.Fprintf(r, `%s`, link, imageSrc, imageSrc) + imageSrc := r.resolveLink(l.Kind(), org.String(l.Description...)) + _, _ = fmt.Fprintf(r, `%s`, link, imageSrc, imageSrc) } case "video": if l.Description == nil { - fmt.Fprintf(r, ``, link, link) + _, _ = fmt.Fprintf(r, ``, link, link) } else { - videoSrc := r.resolveLink(org.String(l.Description...)) - fmt.Fprintf(r, ``, link, videoSrc, videoSrc) + videoSrc := r.resolveLink(l.Kind(), org.String(l.Description...)) + _, _ = fmt.Fprintf(r, ``, link, videoSrc, videoSrc) } default: description := link if l.Description != nil { description = r.WriteNodesAsString(l.Description...) } - fmt.Fprintf(r, `%s`, link, description) + _, _ = fmt.Fprintf(r, `%s`, link, description) } } diff --git a/modules/markup/orgmode/orgmode_test.go b/modules/markup/orgmode/orgmode_test.go index da8d800a00625..95f53c9cc9ff5 100644 --- a/modules/markup/orgmode/orgmode_test.go +++ b/modules/markup/orgmode/orgmode_test.go @@ -23,7 +23,8 @@ func TestRender_StandardLinks(t *testing.T) { buffer, err := RenderString(&markup.RenderContext{ Ctx: git.DefaultContext, Links: markup.Links{ - Base: "/relative-path", + Base: "/relative-path", + BranchPath: "branch/main", }, }, input) assert.NoError(t, err) @@ -32,9 +33,10 @@ func TestRender_StandardLinks(t *testing.T) { test("[[https://google.com/]]", `

https://google.com/

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

The WikiPage Name

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

The WikiPage Desc

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

The Image Desc

`) } func TestRender_Media(t *testing.T) { From 8b753654949cb6ec10ae8194b981cbb29650d455 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 3 Feb 2024 10:15:53 +0800 Subject: [PATCH 5/5] fix lint --- modules/markup/orgmode/orgmode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/markup/orgmode/orgmode.go b/modules/markup/orgmode/orgmode.go index 2bf6a88e28329..ac1cedff6d4c1 100644 --- a/modules/markup/orgmode/orgmode.go +++ b/modules/markup/orgmode/orgmode.go @@ -133,7 +133,7 @@ type Writer struct { Ctx *markup.RenderContext } -func (r *Writer) resolveLink(kind string, link string) string { +func (r *Writer) resolveLink(kind, link string) string { link = strings.TrimPrefix(link, "file:") if !strings.HasPrefix(link, "#") && // not a URL fragment !markup.IsLinkStr(link) && // not an absolute URL