Added a page for nonexistent posts

This commit is contained in:
Eduard Urbach 2025-03-01 13:15:36 +01:00
parent ee6ab6f00d
commit 21a8bfd261
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
2 changed files with 27 additions and 11 deletions

View File

@ -10,23 +10,40 @@ import (
) )
func RenderPost(ctx web.Context, id string) error { func RenderPost(ctx web.Context, id string) error {
post := Posts[id] var (
head := fmt.Sprintf(`<title>%s</title><meta name="keywords" content="%s">`, post.Title, strings.Join(post.Tags, ",")) post = Posts[id]
content := "" title string
content string
created string
tags []string
)
if slices.Contains(post.Tags, "article") { if post != nil {
title = post.Title
content = post.Content
created = post.Created
tags = post.Tags
} else {
title = "Not found"
content = fmt.Sprintf("Post `%s` does not exist.", id)
ctx.Status(404)
}
head := fmt.Sprintf(`<title>%s</title><meta name="keywords" content="%s">`, title, strings.Join(tags, ","))
if slices.Contains(tags, "article") {
content = fmt.Sprintf( content = fmt.Sprintf(
`<article><header><h1>%s</h1><time datetime="%s">%s</time></header>%s</article>`, `<article><header><h1>%s</h1><time datetime="%s">%s</time></header>%s</article>`,
post.Title, title,
post.Created, created,
post.Created[:len("YYYY-MM-DD")], created[:len("YYYY-MM-DD")],
markdown.Render(post.Content), markdown.Render(content),
) )
} else { } else {
content = fmt.Sprintf( content = fmt.Sprintf(
`<h2>%s</h2>%s`, `<h2>%s</h2>%s`,
post.Title, title,
markdown.Render(post.Content), markdown.Render(content),
) )
} }

View File

@ -13,7 +13,6 @@ func Sitemap(ctx web.Context) error {
html := bytes.Buffer{} html := bytes.Buffer{}
html.WriteString("https://urbach.dev\n") html.WriteString("https://urbach.dev\n")
html.WriteString("https://urbach.dev/blog\n") html.WriteString("https://urbach.dev/blog\n")
articles := []*app.Post{} articles := []*app.Post{}
for _, post := range app.Posts { for _, post := range app.Posts {