html: Support pretty relative links
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
This commit is contained in:
parent
84d56e9562
commit
5dadf8c4c2
2 changed files with 38 additions and 3 deletions
|
@ -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
|
||||
|
@ -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 != "" {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue