Skip to content

Commit

Permalink
html: Support pretty relative links
Browse files Browse the repository at this point in the history
Hugo defaults to serving files with pretty urls [1] - this means
`/posts/foo.org` is served at `/posts/foo/`. This works because servers
default to serving index.html when a directory is specified and hugo renders
the post to `/posts/foo/index.html` instead of `/posts/foo.html`. To make
relative links work we need to (1) remove the fake `foo/` subdirectory from
unrooted links and (2) replace any `.org` suffix with `/`.

[1] https://gohugo.io/content-management/urls/#pretty-urls
  • Loading branch information
niklasfasching committed Jan 2, 2021
1 parent 84d56e9 commit 5dadf8c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
14 changes: 11 additions & 3 deletions org/html_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import (

// HTMLWriter exports an org document into a html document.
type HTMLWriter struct {
ExtendingWriter Writer
HighlightCodeBlock func(source, lang string, inline bool) string
ExtendingWriter Writer
HighlightCodeBlock func(source, lang string, inline bool) string
PrettyRelativeLinks bool

strings.Builder
document *Document
Expand Down Expand Up @@ -342,7 +343,14 @@ func (w *HTMLWriter) WriteRegularLink(l RegularLink) {
if l.Protocol == "file" {
url = url[len("file:"):]
}
if (l.Protocol == "file" || l.Protocol == "") && strings.HasSuffix(url, ".org") {
if isRelative := l.Protocol == "file" || l.Protocol == ""; isRelative && w.PrettyRelativeLinks {
if !strings.HasPrefix(url, "/") {
url = "../" + url
}
if strings.HasSuffix(url, ".org") {
url = strings.TrimSuffix(url, ".org") + "/"
}
} else if isRelative && strings.HasSuffix(url, ".org") {
url = strings.TrimSuffix(url, ".org") + ".html"
}
if prefix := w.document.Links[l.Protocol]; prefix != "" {
Expand Down
27 changes: 27 additions & 0 deletions org/html_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,30 @@ func TestExtendedHTMLWriter(t *testing.T) {
t.Errorf("WriteText method of extending writer was not called: CallCount %d", extendedWriter.callCount)
}
}

var prettyRelativeLinkTests = map[string]string{
"[[/hello.org][hello]]": `<p><a href="/hello/">hello</a></p>`,
"[[hello.org][hello]]": `<p><a href="../hello/">hello</a></p>`,
"[[file:/hello.org]]": `<p><a href="/hello/">/hello/</a></p>`,
"[[file:hello.org]]": `<p><a href="../hello/">../hello/</a></p>`,
"[[http://hello.org]]": `<p><a href="http://hello.org">http://hello.org</a></p>`,
"[[/foo.png]]": `<p><img src="/foo.png" alt="/foo.png" title="/foo.png" /></p>`,
"[[foo.png]]": `<p><img src="../foo.png" alt="../foo.png" title="../foo.png" /></p>`,
"[[/foo.png][foo]]": `<p><a href="/foo.png">foo</a></p>`,
"[[foo.png][foo]]": `<p><a href="../foo.png">foo</a></p>`,
}

func TestPrettyRelativeLinks(t *testing.T) {
for org, expected := range prettyRelativeLinkTests {
t.Run(org, func(t *testing.T) {
writer := NewHTMLWriter()
writer.PrettyRelativeLinks = true
actual, err := New().Silent().Parse(strings.NewReader(org), "./prettyRelativeLinkTests.org").Write(writer)
if err != nil {
t.Errorf("%s\n got error: %s", org, err)
} else if actual := strings.TrimSpace(actual); actual != expected {
t.Errorf("%s:\n%s'", org, diff(actual, expected))
}
})
}
}

2 comments on commit 5dadf8c

@schoenw
Copy link

@schoenw schoenw commented on 5dadf8c Apr 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this patch has broken hugo for me. I have files like

.../content/page/foo/index.org
.../content/page/foo/bar.pdf

and index.org refers to [[bar.pdf]]. Since hugo 0.81 (which I think includes this code) the link in

.../content/page/foo/index.html

resolves to .../page/foo/../bar.pdf (which is .../page/bar.pdf) while the file is actually in .../page/foo/bar.pdf.
I have reverted to hugo 0.80 for now but it would be nice to get this issue resolved in a future version of hugo.

@niklasfasching
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this patch has broken hugo for me. I have files like

.../content/page/foo/index.org
.../content/page/foo/bar.pdf

and index.org refers to [[bar.pdf]]. Since hugo 0.81 (which I think includes this code) the link in

.../content/page/foo/index.html

resolves to .../page/foo/../bar.pdf (which is .../page/bar.pdf) while the file is actually in .../page/foo/bar.pdf.
I have reverted to hugo 0.80 for now but it would be nice to get this issue resolved in a future version of hugo.

See #53. Will revert

Please sign in to comment.