- commit
- d09a5d8
- parent
- 52c64d4
- author
- Eric Bower
- date
- 2022-09-01 00:42:51 +0000 UTC
fix(parser): use right imgs url for custom domains
8 files changed,
+18,
-19
+1,
-1
1@@ -98,7 +98,7 @@ func main() {
2 logger.Info("updating dates")
3 for _, post := range posts {
4 if post.Space == "prose" {
5- linkify := imgs.NewImgsLinkify(post.Username, true, false)
6+ linkify := imgs.NewImgsLinkify(post.Username)
7 parsed, err := shared.ParseText(post.Text, linkify)
8 if err != nil {
9 logger.Error(err)
1@@ -76,7 +76,7 @@ func main() {
2
3 logger.Info("replacing tags")
4 for _, post := range posts {
5- linkify := imgs.NewImgsLinkify(post.Username, true, false)
6+ linkify := imgs.NewImgsLinkify(post.Username)
7 parsed, err := shared.ParseText(post.Text, linkify)
8 if err != nil {
9 continue
+1,
-1
1@@ -476,7 +476,7 @@ func postHandler(w http.ResponseWriter, r *http.Request) {
2 var data PostPageData
3 post, err := dbpool.FindPostWithSlug(slug, user.ID, cfg.Space)
4 if err == nil {
5- linkify := NewImgsLinkify(username, onSubdomain, withUserName)
6+ linkify := NewImgsLinkify(username)
7 parsed, err := shared.ParseText(post.Text, linkify)
8 if err != nil {
9 logger.Error(err)
+4,
-6
1@@ -14,18 +14,16 @@ type ImgsLinkify struct {
2 WithUsername bool
3 }
4
5-func NewImgsLinkify(username string, onSubdomain, withUsername bool) *ImgsLinkify {
6+func NewImgsLinkify(username string) *ImgsLinkify {
7 cfg := NewConfigSite()
8 return &ImgsLinkify{
9- Cfg: cfg,
10- Username: username,
11- OnSubdomain: onSubdomain,
12- WithUsername: withUsername,
13+ Cfg: cfg,
14+ Username: username,
15 }
16 }
17
18 func (i *ImgsLinkify) Create(fname string) string {
19- return i.Cfg.ImgURL(i.Username, fname, i.OnSubdomain, i.WithUsername)
20+ return i.Cfg.ImgFullURL(i.Username, fname)
21 }
22
23 func NewConfigSite() *shared.ConfigSite {
+5,
-5
1@@ -204,7 +204,7 @@ func blogHandler(w http.ResponseWriter, r *http.Request) {
2
3 readme, err := dbpool.FindPostWithFilename("_readme.md", user.ID, cfg.Space)
4 if err == nil {
5- linkify := imgs.NewImgsLinkify(readme.Username, onSubdomain, withUserName)
6+ linkify := imgs.NewImgsLinkify(readme.Username)
7 parsedText, err := shared.ParseText(readme.Text, linkify)
8 if err != nil {
9 logger.Error(err)
10@@ -354,7 +354,7 @@ func postHandler(w http.ResponseWriter, r *http.Request) {
11 hasCSS := false
12 var data PostPageData
13 post, err := dbpool.FindPostWithSlug(slug, user.ID, cfg.Space)
14- linkify := imgs.NewImgsLinkify(username, onSubdomain, withUserName)
15+ linkify := imgs.NewImgsLinkify(username)
16 if err == nil {
17 parsedText, err := shared.ParseText(post.Text, linkify)
18 if err != nil {
19@@ -617,7 +617,7 @@ func rssBlogHandler(w http.ResponseWriter, r *http.Request) {
20
21 readme, err := dbpool.FindPostWithFilename("_readme.md", user.ID, cfg.Space)
22 if err == nil {
23- linkify := imgs.NewImgsLinkify(readme.Username, true, false)
24+ linkify := imgs.NewImgsLinkify(readme.Username)
25 parsedText, err := shared.ParseText(readme.Text, linkify)
26 if err != nil {
27 logger.Error(err)
28@@ -650,7 +650,7 @@ func rssBlogHandler(w http.ResponseWriter, r *http.Request) {
29 if slices.Contains(cfg.HiddenPosts, post.Filename) {
30 continue
31 }
32- linkify := imgs.NewImgsLinkify(post.Username, true, false)
33+ linkify := imgs.NewImgsLinkify(post.Username)
34 parsed, err := shared.ParseText(post.Text, linkify)
35 if err != nil {
36 logger.Error(err)
37@@ -745,7 +745,7 @@ func rssHandler(w http.ResponseWriter, r *http.Request) {
38
39 var feedItems []*feeds.Item
40 for _, post := range pager.Data {
41- linkify := imgs.NewImgsLinkify(post.Username, onSubdomain, withUserName)
42+ linkify := imgs.NewImgsLinkify(post.Username)
43 parsed, err := shared.ParseText(post.Text, linkify)
44 if err != nil {
45 logger.Error(err)
+1,
-1
1@@ -46,7 +46,7 @@ func (p *MarkdownHooks) FileValidate(data *filehandlers.PostMetaData) (bool, err
2 }
3
4 func (p *MarkdownHooks) FileMeta(data *filehandlers.PostMetaData) error {
5- linkify := imgs.NewImgsLinkify("", true, false)
6+ linkify := imgs.NewImgsLinkify("")
7 parsedText, err := shared.ParseText(data.Text, linkify)
8 // we return nil here because we don't want the file upload to fail
9 if err != nil {
1@@ -143,6 +143,11 @@ func (c *ConfigSite) RawPostURL(username, slug string) string {
2 return fmt.Sprintf("/raw/%s/%s", username, fname)
3 }
4
5+func (c *ConfigSite) ImgFullURL(username, slug string) string {
6+ fname := url.PathEscape(strings.TrimLeft(slug, "/"))
7+ return fmt.Sprintf("%s://%s.%s/%s", c.Protocol, username, c.Domain, fname)
8+}
9+
10 func (c *ConfigSite) ImgURL(username string, slug string, onSubdomain bool, withUserName bool) string {
11 fname := url.PathEscape(strings.TrimLeft(slug, "/"))
12 if c.IsSubdomains() && onSubdomain {
1@@ -166,12 +166,10 @@ func createImgURL(linkify Linkify) func([]byte) []byte {
2 return func(url []byte) []byte {
3 if url[0] == '/' {
4 name := SanitizeFileExt(string(url))
5- // fmt.Sprintf("%s%s", absURL, name)
6 nextURL := linkify.Create(name)
7 return []byte(nextURL)
8 } else if bytes.HasPrefix(url, []byte{'.', '/'}) {
9 name := SanitizeFileExt(string(url[1:]))
10- // nextURL := fmt.Sprintf("%s%s", absURL, name)
11 nextURL := linkify.Create(name)
12 return []byte(nextURL)
13 }
14@@ -222,10 +220,8 @@ func ParseText(text string, linkify Linkify) (*ParsedText, error) {
15 parsed.MetaData.Image = toString(metaData["image"])
16 if strings.HasPrefix(parsed.Image, "/") {
17 parsed.Image = linkify.Create(parsed.Image)
18- // parsed.MetaData.Image = fmt.Sprintf("%s%s", absURL, parsed.Image)
19 } else if strings.HasPrefix(parsed.Image, "./") {
20 parsed.Image = linkify.Create(parsed.Image[1:])
21- // parsed.MetaData.Image = fmt.Sprintf("%s%s", absURL, parsed.Image[1:])
22 }
23 parsed.MetaData.ImageCard = toString(metaData["card"])
24