repos / pico

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

commit
193567a
parent
91063b5
author
Eric Bower
date
2024-03-01 19:04:38 +0000 UTC
feat(prose): let user customize 404 page

feat(prose): `_404.md` file now supported
2 files changed,  +66, -37
M prose/api.go
+65, -36
  1@@ -351,26 +351,44 @@ func postHandler(w http.ResponseWriter, r *http.Request) {
  2 	ogImageCard := ""
  3 	hasCSS := false
  4 	var data PostPageData
  5-	post, err := dbpool.FindPostWithSlug(slug, user.ID, cfg.Space)
  6+
  7+	css, err := dbpool.FindPostWithFilename("_styles.css", user.ID, cfg.Space)
  8 	if err == nil {
  9-		parsedText, err := shared.ParseText(post.Text)
 10+		if len(css.Text) > 0 {
 11+			hasCSS = true
 12+		}
 13+	}
 14+
 15+	footer, err := dbpool.FindPostWithFilename("_footer.md", user.ID, cfg.Space)
 16+	var footerHTML template.HTML
 17+	if err == nil {
 18+		footerParsed, err := shared.ParseText(footer.Text)
 19 		if err != nil {
 20 			logger.Error(err.Error())
 21 		}
 22+		footerHTML = template.HTML(footerParsed.Html)
 23+	}
 24 
 25-		// we need the blog name from the readme unfortunately
 26-		readme, err := dbpool.FindPostWithFilename("_readme.md", user.ID, cfg.Space)
 27-		if err == nil {
 28-			readmeParsed, err := shared.ParseText(readme.Text)
 29-			if err != nil {
 30-				logger.Error(err.Error())
 31-			}
 32-			if readmeParsed.MetaData.Title != "" {
 33-				blogName = readmeParsed.MetaData.Title
 34-			}
 35-			ogImage = readmeParsed.Image
 36-			ogImageCard = readmeParsed.ImageCard
 37-			favicon = readmeParsed.Favicon
 38+	// we need the blog name from the readme unfortunately
 39+	readme, err := dbpool.FindPostWithFilename("_readme.md", user.ID, cfg.Space)
 40+	if err == nil {
 41+		readmeParsed, err := shared.ParseText(readme.Text)
 42+		if err != nil {
 43+			logger.Error(err.Error())
 44+		}
 45+		if readmeParsed.MetaData.Title != "" {
 46+			blogName = readmeParsed.MetaData.Title
 47+		}
 48+		ogImage = readmeParsed.Image
 49+		ogImageCard = readmeParsed.ImageCard
 50+		favicon = readmeParsed.Favicon
 51+	}
 52+
 53+	post, err := dbpool.FindPostWithSlug(slug, user.ID, cfg.Space)
 54+	if err == nil {
 55+		parsedText, err := shared.ParseText(post.Text)
 56+		if err != nil {
 57+			logger.Error(err.Error())
 58 		}
 59 
 60 		if parsedText.Image != "" {
 61@@ -381,23 +399,6 @@ func postHandler(w http.ResponseWriter, r *http.Request) {
 62 			ogImageCard = parsedText.ImageCard
 63 		}
 64 
 65-		css, err := dbpool.FindPostWithFilename("_styles.css", user.ID, cfg.Space)
 66-		if err == nil {
 67-			if len(css.Text) > 0 {
 68-				hasCSS = true
 69-			}
 70-		}
 71-
 72-		footer, err := dbpool.FindPostWithFilename("_footer.md", user.ID, cfg.Space)
 73-		var footerHTML template.HTML
 74-		if err == nil {
 75-			footerParsed, err := shared.ParseText(footer.Text)
 76-			if err != nil {
 77-				logger.Error(err.Error())
 78-			}
 79-			footerHTML = template.HTML(footerParsed.Html)
 80-		}
 81-
 82 		// validate and fire off analytic event
 83 		if isRequestTrackable(r) {
 84 			_, err := dbpool.AddViewCount(post.ID)
 85@@ -442,20 +443,48 @@ func postHandler(w http.ResponseWriter, r *http.Request) {
 86 			return
 87 		}
 88 
 89+		notFound, err := dbpool.FindPostWithFilename("_404.md", user.ID, cfg.Space)
 90+		contents := template.HTML("Oops!  we can't seem to find this post.")
 91+		title := "Post not found"
 92+		desc := "Post not found"
 93+		if err == nil {
 94+			notFoundParsed, err := shared.ParseText(notFound.Text)
 95+			if err != nil {
 96+				logger.Error(err.Error())
 97+			}
 98+			if notFoundParsed.MetaData.Title != "" {
 99+				title = notFoundParsed.MetaData.Title
100+			}
101+			if notFoundParsed.MetaData.Description != "" {
102+				desc = notFoundParsed.MetaData.Description
103+			}
104+			ogImage = notFoundParsed.Image
105+			ogImageCard = notFoundParsed.ImageCard
106+			favicon = notFoundParsed.Favicon
107+			contents = template.HTML(notFoundParsed.Html)
108+		}
109+
110 		data = PostPageData{
111 			Site:         *cfg.GetSiteData(),
112 			BlogURL:      template.URL(cfg.FullBlogURL(curl, username)),
113-			PageTitle:    "Post not found",
114-			Description:  "Post not found",
115-			Title:        "Post not found",
116+			PageTitle:    title,
117+			Description:  desc,
118+			Title:        title,
119 			PublishAt:    time.Now().Format("02 Jan, 2006"),
120 			PublishAtISO: time.Now().Format(time.RFC3339),
121 			Username:     username,
122 			BlogName:     blogName,
123-			Contents:     "Oops!  we can't seem to find this post.",
124+			HasCSS:       hasCSS,
125+			CssURL:       template.URL(cfg.CssURL(username)),
126+			Image:        template.URL(ogImage),
127+			ImageCard:    ogImageCard,
128+			Favicon:      template.URL(favicon),
129+			Footer:       footerHTML,
130+			Contents:     contents,
131 			Unlisted:     true,
132 		}
133 		logger.Info("post not found", "user", username, "slug", slug)
134+		w.WriteHeader(http.StatusNotFound)
135 	}
136 
137 	ts, err := shared.RenderTemplate(cfg, []string{
M prose/config.go
+1, -1
1@@ -53,7 +53,7 @@ func NewConfigSite() *shared.ConfigSite {
2 				".webp",
3 				".svg",
4 			},
5-			HiddenPosts:   []string{"_readme.md", "_styles.css", "_footer.md"},
6+			HiddenPosts:   []string{"_readme.md", "_styles.css", "_footer.md", "_404.md"},
7 			Logger:        shared.CreateLogger(debug == "1"),
8 			AllowRegister: allowRegister == "1",
9 			MaxSize:       maxSize,