Skip to content

Commit

Permalink
fix orgmode media link resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Feb 3, 2024
1 parent 28aa745 commit 6cd906b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
36 changes: 22 additions & 14 deletions modules/markup/orgmode/orgmode.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,42 +133,50 @@ type Writer struct {
Ctx *markup.RenderContext
}

const mailto = "mailto:"

func (r *Writer) resolveLink(link string) string {
func (r *Writer) resolveLink(kind string, link string) string {
link = strings.TrimPrefix(link, "file:")
if len(link) > 0 && !markup.IsLinkStr(link) &&
link[0] != '#' && !strings.HasPrefix(link, mailto) {
link = util.URLJoin(r.Ctx.Links.ResolveMediaLink(r.Ctx.IsWiki), link)
if !strings.HasPrefix(link, "#") && // not a URL fragment
!markup.IsLinkStr(link) && // not an absolute URL
!strings.HasPrefix(link, "mailto:") {
if kind == "regular" {
// orgmode reports the link kind as "regular" for "[[ImageLink.svg][The Image Desc]]"
// so we need to try to guess the link kind again here
kind = org.RegularLink{URL: link}.Kind()
}
base := r.Ctx.Links.Base
if kind == "image" || kind == "video" {
base = r.Ctx.Links.ResolveMediaLink(r.Ctx.IsWiki)
}
link = util.URLJoin(base, link)
}
return link
}

// WriteRegularLink renders images, links or videos
func (r *Writer) WriteRegularLink(l org.RegularLink) {
link := r.resolveLink(l.URL)
link := r.resolveLink(l.Kind(), l.URL)

// Inspired by https://github.com/niklasfasching/go-org/blob/6eb20dbda93cb88c3503f7508dc78cbbc639378f/org/html_writer.go#L406-L427
switch l.Kind() {
case "image":
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 {
imageSrc := r.resolveLink(org.String(l.Description...))
fmt.Fprintf(r, `<a href="%s"><img src="%s" alt="%s" /></a>`, link, imageSrc, imageSrc)
imageSrc := r.resolveLink(l.Kind(), org.String(l.Description...))
_, _ = 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)
_, _ = fmt.Fprintf(r, `<video src="%s">%s</video>`, link, link)
} else {
videoSrc := r.resolveLink(org.String(l.Description...))
fmt.Fprintf(r, `<a href="%s"><video src="%s">%s</video></a>`, link, videoSrc, videoSrc)
videoSrc := r.resolveLink(l.Kind(), org.String(l.Description...))
_, _ = fmt.Fprintf(r, `<a href="%s"><video src="%s">%s</video></a>`, link, videoSrc, videoSrc)
}
default:
description := link
if l.Description != nil {
description = r.WriteNodesAsString(l.Description...)
}
fmt.Fprintf(r, `<a href="%s">%s</a>`, link, description)
_, _ = fmt.Fprintf(r, `<a href="%s">%s</a>`, link, description)
}
}
10 changes: 6 additions & 4 deletions modules/markup/orgmode/orgmode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func TestRender_StandardLinks(t *testing.T) {
buffer, err := RenderString(&markup.RenderContext{
Ctx: git.DefaultContext,
Links: markup.Links{
Base: "/relative-path",
Base: "/relative-path",
BranchPath: "branch/main",
},
}, input)
assert.NoError(t, err)
Expand All @@ -32,9 +33,10 @@ func TestRender_StandardLinks(t *testing.T) {

test("[[https://google.com/]]",
`<p><a href="https://google.com/">https://google.com/</a></p>`)

test("[[WikiPage][The WikiPage Name]]",
`<p><a href="/relative-path/WikiPage">The WikiPage Name</a></p>`)
test("[[WikiPage][The WikiPage Desc]]",
`<p><a href="/relative-path/WikiPage">The WikiPage Desc</a></p>`)
test("[[ImageLink.svg][The Image Desc]]",
`<p><a href="/relative-path/media/branch/main/ImageLink.svg">The Image Desc</a></p>`)
}

func TestRender_Media(t *testing.T) {
Expand Down

0 comments on commit 6cd906b

Please sign in to comment.