[GITEA] Fix orgmode link resolver for text descriptions

- 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 385fc6ee6b)
This commit is contained in:
Gusted 2024-02-01 17:08:39 +01:00 committed by Earl Warren
parent fde08f91fd
commit 2ed825c5be
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
2 changed files with 14 additions and 3 deletions

View file

@ -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, `<img src="%s" alt="%s" />`, link, link)
} else {
imageSrc := r.resolveLink(l.Description[0].(org.RegularLink))
imageSrc := r.resolveLink(l.Description[0])
fmt.Fprintf(r, `<a href="%s"><img src="%s" alt="%s" /></a>`, link, imageSrc, imageSrc)
}
case "video":
if l.Description == nil {
fmt.Fprintf(r, `<video src="%s">%s</video>`, link, link)
} else {
videoSrc := r.resolveLink(l.Description[0].(org.RegularLink))
videoSrc := r.resolveLink(l.Description[0])
fmt.Fprintf(r, `<a href="%s"><video src="%s">%s</video></a>`, link, videoSrc, videoSrc)
}
default: