From 781d2a68ccb276bf13caf0b378b74d9efeab3d39 Mon Sep 17 00:00:00 2001 From: Gusted Date: Thu, 1 Feb 2024 17:08:39 +0100 Subject: [PATCH] [GITEA] Fix orgmode link resolver for text descriptions - Backport of #2276 - It's possible that the description of an `Regularlink` is `Text` and not another `Regularlink`. Therefor if it's `Text`, convert it to an `Regularlink` trough the 'old' behavior (pass it trough `org.String` and trim `file:` prefix). - Adds unit tests. - Resolves https://codeberg.org/Codeberg/Community/issues/1430 (cherry picked from commit 385fc6ee6be25859066a716aa15be09991e2d33c) --- modules/markup/orgmode/orgmode.go | 11 ++++++++--- modules/markup/orgmode/orgmode_test.go | 6 ++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/modules/markup/orgmode/orgmode.go b/modules/markup/orgmode/orgmode.go index f7a051476b..a9e0f4aea4 100644 --- a/modules/markup/orgmode/orgmode.go +++ b/modules/markup/orgmode/orgmode.go @@ -135,7 +135,12 @@ type Writer struct { const mailto = "mailto:" -func (r *Writer) resolveLink(l org.RegularLink) string { +func (r *Writer) resolveLink(node org.Node) string { + l, ok := node.(org.RegularLink) + if !ok { + l = org.RegularLink{URL: strings.TrimPrefix(org.String(node), "file:")} + } + link := html.EscapeString(l.URL) if l.Protocol == "file" { link = link[len("file:"):] @@ -162,14 +167,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(l.Description[0]) 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(l.Description[0]) fmt.Fprintf(r, ``, link, videoSrc, videoSrc) } default: diff --git a/modules/markup/orgmode/orgmode_test.go b/modules/markup/orgmode/orgmode_test.go index cb9b24f6d0..1f9ec0bac5 100644 --- a/modules/markup/orgmode/orgmode_test.go +++ b/modules/markup/orgmode/orgmode_test.go @@ -76,6 +76,12 @@ func TestRender_Media(t *testing.T) { `

https://example.com/example.svg

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

`) + + // Text description. + test("[[file:./lem-post.png][file:./lem-post.png]]", + `

http://localhost:3000/gogits/gogs/lem-post.png

`) + test("[[file:./lem-post.mp4][file:./lem-post.mp4]]", + `

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