52 lines
968 B
Go

package app
import (
"fmt"
"slices"
"strings"
"git.urbach.dev/go/markdown"
"git.urbach.dev/go/web"
)
func RenderPost(ctx web.Context, id string) error {
var (
post = Posts[id]
title string
content string
created string
tags []string
)
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>`,
title,
created,
created[:len("YYYY-MM-DD")],
markdown.Render(content),
)
} else {
content = fmt.Sprintf(
`<h2>%s</h2>%s`,
title,
markdown.Render(content),
)
}
return Render(ctx, head, content)
}