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 {
post := Posts[id]
head := fmt.Sprintf(`<title>%s</title><meta name="keywords" content="%s">`, post.Title, strings.Join(post.Tags, ","))
content := ""
var (
post = Posts[id]
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(
`<article><header><h1>%s</h1><time datetime="%s">%s</time></header>%s</article>`,
post.Title,
post.Created,
post.Created[:len("YYYY-MM-DD")],
markdown.Render(post.Content),
title,
created,
created[:len("YYYY-MM-DD")],
markdown.Render(content),
)
} else {
content = fmt.Sprintf(
`<h2>%s</h2>%s`,
post.Title,
markdown.Render(post.Content),
title,
markdown.Render(content),
)
}

View File

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