Skip to content

Commit 516318a

Browse files
Gustedearl-warren
authored andcommitted
fix media description render for orgmode
- In org mode you can specify an description for media via the following syntax `[[description][media link]]`. The description is then used as title or alt. - This patch fixes the rendering of the description by seperating the description and non-description cases and using `org.String()`. - Added unit tests. - Inspired by https://github.com/niklasfasching/go-org/blob/6eb20dbda93cb88c3503f7508dc78cbbc639378f/org/html_writer.go#L406-L427 - Resolves https://codeberg.org/Codeberg/Community/issues/848 (cherry picked from commit 8b8aab83113b34bade61964e2097ed497abc39e9)
1 parent ebff051 commit 516318a

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

modules/markup/orgmode/orgmode.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,18 +153,31 @@ func (r *Writer) WriteRegularLink(l org.RegularLink) {
153153
link = []byte(util.URLJoin(r.URLPrefix, lnk))
154154
}
155155

156-
description := string(link)
157-
if l.Description != nil {
158-
description = r.WriteNodesAsString(l.Description...)
159-
}
156+
// Inspired by https://github.com/niklasfasching/go-org/blob/6eb20dbda93cb88c3503f7508dc78cbbc639378f/org/html_writer.go#L406-L427
160157
switch l.Kind() {
161158
case "image":
162-
imageSrc := getMediaURL(link)
163-
fmt.Fprintf(r, `<img src="%s" alt="%s" title="%s" />`, imageSrc, description, description)
159+
if l.Description == nil {
160+
imageSrc := getMediaURL(link)
161+
fmt.Fprintf(r, `<img src="%s" alt="%s" title="%s" />`, imageSrc, link, link)
162+
} else {
163+
description := strings.TrimPrefix(org.String(l.Description...), "file:")
164+
imageSrc := getMediaURL([]byte(description))
165+
fmt.Fprintf(r, `<a href="%s"><img src="%s" alt="%s" /></a>`, link, imageSrc, imageSrc)
166+
}
164167
case "video":
165-
videoSrc := getMediaURL(link)
166-
fmt.Fprintf(r, `<video src="%s" title="%s">%s</video>`, videoSrc, description, description)
168+
if l.Description == nil {
169+
imageSrc := getMediaURL(link)
170+
fmt.Fprintf(r, `<video src="%s" title="%s">%s</video>`, imageSrc, link, link)
171+
} else {
172+
description := strings.TrimPrefix(org.String(l.Description...), "file:")
173+
videoSrc := getMediaURL([]byte(description))
174+
fmt.Fprintf(r, `<a href="%s"><video src="%s" title="%s"></video></a>`, link, videoSrc, videoSrc)
175+
}
167176
default:
177+
description := string(link)
178+
if l.Description != nil {
179+
description = r.WriteNodesAsString(l.Description...)
180+
}
168181
fmt.Fprintf(r, `<a href="%s" title="%s">%s</a>`, link, description, description)
169182
}
170183
}

modules/markup/orgmode/orgmode_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func TestRender_StandardLinks(t *testing.T) {
4242
"<p><a href=\""+lnk+"\" title=\"WikiPage\">WikiPage</a></p>")
4343
}
4444

45-
func TestRender_Images(t *testing.T) {
45+
func TestRender_Media(t *testing.T) {
4646
setting.AppURL = AppURL
4747
setting.AppSubURL = AppSubURL
4848

@@ -60,6 +60,18 @@ func TestRender_Images(t *testing.T) {
6060

6161
test("[[file:"+url+"]]",
6262
"<p><img src=\""+result+"\" alt=\""+result+"\" title=\""+result+"\" /></p>")
63+
64+
// With description.
65+
test("[[https://example.com][https://example.com/example.svg]]",
66+
`<p><a href="https://example.com"><img src="https://example.com/example.svg" alt="https://example.com/example.svg" /></a></p>`)
67+
test("[[https://example.com][https://example.com/example.mp4]]",
68+
`<p><a href="https://example.com"><video src="https://example.com/example.mp4" title="https://example.com/example.mp4"></video></a></p>`)
69+
70+
// Without description.
71+
test("[[https://example.com/example.svg]]",
72+
`<p><img src="https://example.com/example.svg" alt="https://example.com/example.svg" title="https://example.com/example.svg" /></p>`)
73+
test("[[https://example.com/example.mp4]]",
74+
`<p><video src="https://example.com/example.mp4" title="https://example.com/example.mp4">https://example.com/example.mp4</video></p>`)
6375
}
6476

6577
func TestRender_Source(t *testing.T) {

0 commit comments

Comments
 (0)