Skip to content

Commit 6cd906b

Browse files
committed
fix orgmode media link resolving
1 parent 28aa745 commit 6cd906b

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

modules/markup/orgmode/orgmode.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -133,42 +133,50 @@ type Writer struct {
133133
Ctx *markup.RenderContext
134134
}
135135

136-
const mailto = "mailto:"
137-
138-
func (r *Writer) resolveLink(link string) string {
136+
func (r *Writer) resolveLink(kind string, link string) string {
139137
link = strings.TrimPrefix(link, "file:")
140-
if len(link) > 0 && !markup.IsLinkStr(link) &&
141-
link[0] != '#' && !strings.HasPrefix(link, mailto) {
142-
link = util.URLJoin(r.Ctx.Links.ResolveMediaLink(r.Ctx.IsWiki), link)
138+
if !strings.HasPrefix(link, "#") && // not a URL fragment
139+
!markup.IsLinkStr(link) && // not an absolute URL
140+
!strings.HasPrefix(link, "mailto:") {
141+
if kind == "regular" {
142+
// orgmode reports the link kind as "regular" for "[[ImageLink.svg][The Image Desc]]"
143+
// so we need to try to guess the link kind again here
144+
kind = org.RegularLink{URL: link}.Kind()
145+
}
146+
base := r.Ctx.Links.Base
147+
if kind == "image" || kind == "video" {
148+
base = r.Ctx.Links.ResolveMediaLink(r.Ctx.IsWiki)
149+
}
150+
link = util.URLJoin(base, link)
143151
}
144152
return link
145153
}
146154

147155
// WriteRegularLink renders images, links or videos
148156
func (r *Writer) WriteRegularLink(l org.RegularLink) {
149-
link := r.resolveLink(l.URL)
157+
link := r.resolveLink(l.Kind(), l.URL)
150158

151159
// Inspired by https://github.com/niklasfasching/go-org/blob/6eb20dbda93cb88c3503f7508dc78cbbc639378f/org/html_writer.go#L406-L427
152160
switch l.Kind() {
153161
case "image":
154162
if l.Description == nil {
155-
fmt.Fprintf(r, `<img src="%s" alt="%s" />`, link, link)
163+
_, _ = fmt.Fprintf(r, `<img src="%s" alt="%s" />`, link, link)
156164
} else {
157-
imageSrc := r.resolveLink(org.String(l.Description...))
158-
fmt.Fprintf(r, `<a href="%s"><img src="%s" alt="%s" /></a>`, link, imageSrc, imageSrc)
165+
imageSrc := r.resolveLink(l.Kind(), org.String(l.Description...))
166+
_, _ = fmt.Fprintf(r, `<a href="%s"><img src="%s" alt="%s" /></a>`, link, imageSrc, imageSrc)
159167
}
160168
case "video":
161169
if l.Description == nil {
162-
fmt.Fprintf(r, `<video src="%s">%s</video>`, link, link)
170+
_, _ = fmt.Fprintf(r, `<video src="%s">%s</video>`, link, link)
163171
} else {
164-
videoSrc := r.resolveLink(org.String(l.Description...))
165-
fmt.Fprintf(r, `<a href="%s"><video src="%s">%s</video></a>`, link, videoSrc, videoSrc)
172+
videoSrc := r.resolveLink(l.Kind(), org.String(l.Description...))
173+
_, _ = fmt.Fprintf(r, `<a href="%s"><video src="%s">%s</video></a>`, link, videoSrc, videoSrc)
166174
}
167175
default:
168176
description := link
169177
if l.Description != nil {
170178
description = r.WriteNodesAsString(l.Description...)
171179
}
172-
fmt.Fprintf(r, `<a href="%s">%s</a>`, link, description)
180+
_, _ = fmt.Fprintf(r, `<a href="%s">%s</a>`, link, description)
173181
}
174182
}

modules/markup/orgmode/orgmode_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ func TestRender_StandardLinks(t *testing.T) {
2323
buffer, err := RenderString(&markup.RenderContext{
2424
Ctx: git.DefaultContext,
2525
Links: markup.Links{
26-
Base: "/relative-path",
26+
Base: "/relative-path",
27+
BranchPath: "branch/main",
2728
},
2829
}, input)
2930
assert.NoError(t, err)
@@ -32,9 +33,10 @@ func TestRender_StandardLinks(t *testing.T) {
3233

3334
test("[[https://google.com/]]",
3435
`<p><a href="https://google.com/">https://google.com/</a></p>`)
35-
36-
test("[[WikiPage][The WikiPage Name]]",
37-
`<p><a href="/relative-path/WikiPage">The WikiPage Name</a></p>`)
36+
test("[[WikiPage][The WikiPage Desc]]",
37+
`<p><a href="/relative-path/WikiPage">The WikiPage Desc</a></p>`)
38+
test("[[ImageLink.svg][The Image Desc]]",
39+
`<p><a href="/relative-path/media/branch/main/ImageLink.svg">The Image Desc</a></p>`)
3840
}
3941

4042
func TestRender_Media(t *testing.T) {

0 commit comments

Comments
 (0)