repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

commit
b2c19b1
parent
0515a61
author
Eric Bower
date
2022-08-10 15:14:36 +0000 UTC
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, -2
M prose/api.go
+19, -1
 1@@ -204,7 +204,25 @@ func blogHandler(w http.ResponseWriter, r *http.Request) {
 2 			if parsedText.Title != "" {
 3 				headerTxt.Title = parsedText.Title
 4 			}
 5-			headerTxt.Nav = parsedText.Nav
 6+
 7+			headerTxt.Nav = []Link{}
 8+			for _, nav := range parsedText.Nav {
 9+				u, _ := url.Parse(nav.URL)
10+				finURL := nav.URL
11+				if !u.IsAbs() {
12+					finURL = cfg.FullPostURL(
13+						post.Username,
14+						nav.URL,
15+						onSubdomain,
16+						withUserName,
17+					)
18+				}
19+				headerTxt.Nav = append(headerTxt.Nav, Link{
20+					URL:  finURL,
21+					Text: nav.Text,
22+				})
23+			}
24+
25 			readmeTxt.Contents = template.HTML(parsedText.Html)
26 			if len(readmeTxt.Contents) > 0 {
27 				readmeTxt.HasText = true
M shared/config.go
+2, -1
 1@@ -6,6 +6,7 @@ import (
 2 	"log"
 3 	"net/url"
 4 	"path"
 5+	"strings"
 6 
 7 	"git.sr.ht/~erock/pico/wish/cms/config"
 8 	"go.uber.org/zap"
 9@@ -67,7 +68,7 @@ func (c *ConfigSite) PostURL(username, slug string) string {
10 }
11 
12 func (c *ConfigSite) FullPostURL(username, slug string, onSubdomain bool, withUserName bool) string {
13-	fname := url.PathEscape(slug)
14+	fname := url.PathEscape(strings.TrimLeft(slug, "/"))
15 	if c.IsSubdomains() && onSubdomain {
16 		return fmt.Sprintf("%s://%s.%s/%s", c.Protocol, username, c.Domain, fname)
17 	}