Allow url construction to work if blog is under a sub-path

This commit is contained in:
Brian Picciano 2023-01-22 12:09:50 +01:00
parent f536b16e17
commit 1cfdac5e8c
3 changed files with 11 additions and 4 deletions

View File

@ -37,6 +37,7 @@ type rendererGetPostSeriesNextPreviousRes struct {
type renderer struct { type renderer struct {
url *url.URL url *url.URL
postStore post.Store postStore post.Store
gmiPublicURL *url.URL
httpPublicURL *url.URL httpPublicURL *url.URL
} }
@ -92,7 +93,7 @@ func (r renderer) PostBody(p post.StoredPost) (string, error) {
preprocessFuncs := post.PreprocessFunctions{ preprocessFuncs := post.PreprocessFunctions{
BlogURL: func(path string) string { BlogURL: func(path string) string {
return filepath.Join("/", path) return filepath.Join("/", r.gmiPublicURL.Path, path)
}, },
AssetURL: func(id string) string { AssetURL: func(id string) string {
return filepath.Join("/assets", id) return filepath.Join("/assets", id)
@ -220,6 +221,7 @@ func (a *api) tplHandler() (gemini.Handler, error) {
err := tpl.Execute(buf, renderer{ err := tpl.Execute(buf, renderer{
url: r.URL, url: r.URL,
postStore: a.params.PostStore, postStore: a.params.PostStore,
gmiPublicURL: a.params.PublicURL,
httpPublicURL: a.params.HTTPPublicURL, httpPublicURL: a.params.HTTPPublicURL,
}) })

View File

@ -31,14 +31,16 @@ func (a *api) blogURL(path string, abs bool) string {
// filepath.Join strips trailing slash, but we want to keep it // filepath.Join strips trailing slash, but we want to keep it
trailingSlash := strings.HasSuffix(path, "/") trailingSlash := strings.HasSuffix(path, "/")
res := filepath.Join("/", path) res := filepath.Join("/", a.params.PublicURL.Path, path)
if trailingSlash && res != "/" { if trailingSlash && res != "/" {
res += "/" res += "/"
} }
if abs { if abs {
res = a.params.PublicURL.String() + res u := *a.params.PublicURL
u.Path = res
res = u.String()
} }
return res return res
@ -88,6 +90,9 @@ func (a *api) tplFuncs() template.FuncMap {
"BlogURL": func(path string) string { "BlogURL": func(path string) string {
return a.blogURL(path, false) return a.blogURL(path, false)
}, },
"BlogURLAbs": func(path string) string {
return a.blogURL(path, true)
},
"StaticURL": func(path string) string { "StaticURL": func(path string) string {
path = filepath.Join("static", path) path = filepath.Join("static", path)
return a.blogURL(path, false) return a.blogURL(path, false)

View File

@ -114,7 +114,7 @@ emailSubscribe.onclick = async () => {
</p> </p>
<p> <p>
<a href="{{ BlogURL "feed.xml" }}">{{ BlogURL "feed.xml" }}</a> <a href="{{ BlogURLAbs "feed.xml" }}">{{ BlogURLAbs "feed.xml" }}</a>
</p> </p>
<p> <p>