[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 385fc6ee6b
)
This commit is contained in:
parent
f6cf951b5d
commit
781d2a68cc
2 changed files with 14 additions and 3 deletions
|
@ -135,7 +135,12 @@ type Writer struct {
|
||||||
|
|
||||||
const mailto = "mailto:"
|
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)
|
link := html.EscapeString(l.URL)
|
||||||
if l.Protocol == "file" {
|
if l.Protocol == "file" {
|
||||||
link = link[len("file:"):]
|
link = link[len("file:"):]
|
||||||
|
@ -162,14 +167,14 @@ func (r *Writer) WriteRegularLink(l org.RegularLink) {
|
||||||
if l.Description == nil {
|
if l.Description == nil {
|
||||||
fmt.Fprintf(r, `<img src="%s" alt="%s" />`, link, link)
|
fmt.Fprintf(r, `<img src="%s" alt="%s" />`, link, link)
|
||||||
} else {
|
} 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)
|
fmt.Fprintf(r, `<a href="%s"><img src="%s" alt="%s" /></a>`, link, imageSrc, imageSrc)
|
||||||
}
|
}
|
||||||
case "video":
|
case "video":
|
||||||
if l.Description == nil {
|
if l.Description == nil {
|
||||||
fmt.Fprintf(r, `<video src="%s">%s</video>`, link, link)
|
fmt.Fprintf(r, `<video src="%s">%s</video>`, link, link)
|
||||||
} else {
|
} 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)
|
fmt.Fprintf(r, `<a href="%s"><video src="%s">%s</video></a>`, link, videoSrc, videoSrc)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -76,6 +76,12 @@ func TestRender_Media(t *testing.T) {
|
||||||
`<p><img src="https://example.com/example.svg" alt="https://example.com/example.svg" /></p>`)
|
`<p><img src="https://example.com/example.svg" alt="https://example.com/example.svg" /></p>`)
|
||||||
test("[[https://example.com/example.mp4]]",
|
test("[[https://example.com/example.mp4]]",
|
||||||
`<p><video src="https://example.com/example.mp4">https://example.com/example.mp4</video></p>`)
|
`<p><video src="https://example.com/example.mp4">https://example.com/example.mp4</video></p>`)
|
||||||
|
|
||||||
|
// Text description.
|
||||||
|
test("[[file:./lem-post.png][file:./lem-post.png]]",
|
||||||
|
`<p><a href="http://localhost:3000/gogits/gogs/lem-post.png"><img src="http://localhost:3000/gogits/gogs/lem-post.png" alt="http://localhost:3000/gogits/gogs/lem-post.png" /></a></p>`)
|
||||||
|
test("[[file:./lem-post.mp4][file:./lem-post.mp4]]",
|
||||||
|
`<p><a href="http://localhost:3000/gogits/gogs/lem-post.mp4"><video src="http://localhost:3000/gogits/gogs/lem-post.mp4">http://localhost:3000/gogits/gogs/lem-post.mp4</video></a></p>`)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRender_Source(t *testing.T) {
|
func TestRender_Source(t *testing.T) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue