~erock/pico

b2c19b10d54976871a2a1a11f36df923fdb8848a — Eric Bower a month ago 0515a61
feat: parse nav links to convert relative urls to absolute

This allows users to include posts -- from their prose blog -- as links
in the nav without needing to specify an absolute URL.
2 files changed, 21 insertions(+), 2 deletions(-)

M prose/api.go
M shared/config.go
M prose/api.go => prose/api.go +19 -1
@@ 204,7 204,25 @@ func blogHandler(w http.ResponseWriter, r *http.Request) {
			if parsedText.Title != "" {
				headerTxt.Title = parsedText.Title
			}
			headerTxt.Nav = parsedText.Nav

			headerTxt.Nav = []Link{}
			for _, nav := range parsedText.Nav {
				u, _ := url.Parse(nav.URL)
				finURL := nav.URL
				if !u.IsAbs() {
					finURL = cfg.FullPostURL(
						post.Username,
						nav.URL,
						onSubdomain,
						withUserName,
					)
				}
				headerTxt.Nav = append(headerTxt.Nav, Link{
					URL:  finURL,
					Text: nav.Text,
				})
			}

			readmeTxt.Contents = template.HTML(parsedText.Html)
			if len(readmeTxt.Contents) > 0 {
				readmeTxt.HasText = true

M shared/config.go => shared/config.go +2 -1
@@ 6,6 6,7 @@ import (
	"log"
	"net/url"
	"path"
	"strings"

	"git.sr.ht/~erock/pico/wish/cms/config"
	"go.uber.org/zap"


@@ 67,7 68,7 @@ func (c *ConfigSite) PostURL(username, slug string) string {
}

func (c *ConfigSite) FullPostURL(username, slug string, onSubdomain bool, withUserName bool) string {
	fname := url.PathEscape(slug)
	fname := url.PathEscape(strings.TrimLeft(slug, "/"))
	if c.IsSubdomains() && onSubdomain {
		return fmt.Sprintf("%s://%s.%s/%s", c.Protocol, username, c.Domain, fname)
	}