Added blog posts, and improved homepage

This commit is contained in:
2025-05-23 18:12:16 +02:00
parent 99ab5d388c
commit 847fb6491d
28 changed files with 721 additions and 132 deletions

57
blog/postBuilder.go Normal file
View File

@@ -0,0 +1,57 @@
package blog
import (
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
"html/template"
"regexp"
"time"
)
func makePost(markdownContent []byte) Post {
headerRegex := regexp.MustCompile(`(?ms)^---\n.*?---`)
header := headerRegex.Find(markdownContent)
headerKeysRegex := regexp.MustCompile(`(?m)^(\w+): ([^\n]*)$`)
headerKeys := headerKeysRegex.FindAllSubmatch(header, -1)
postBody := headerRegex.ReplaceAll(markdownContent, []byte(""))
post := Post{}
for _, headerKey := range headerKeys {
key, value := string(headerKey[1]), string(headerKey[2])
switch key {
case "title":
post.Title = value
case "date":
date, err := time.Parse(time.DateOnly, value)
if err != nil {
panic(err)
}
post.Date = date
case "updatedDate":
date, err := time.Parse(time.DateOnly, value)
if err != nil {
panic(err)
}
post.UpdatedDate = date
case "author":
post.Author = value
case "slug":
post.Slug = value
}
}
extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock
p := parser.NewWithExtensions(extensions)
doc := p.Parse(postBody)
// create HTML renderer with extensions
htmlFlags := html.CommonFlags | html.HrefTargetBlank
opts := html.RendererOptions{Flags: htmlFlags}
renderer := html.NewRenderer(opts)
post.Body = template.HTML(markdown.Render(doc, renderer))
return post
}