diff options
| author | Kyren223 <contact@kyren.codes> | 2026-10-09 19:11:34 +0300 |
|---|---|---|
| committer | Kyren223 <contact@kyren.codes> | 2026-10-09 19:11:34 +0300 |
| commit | 660e6fb1b4b4fa57596471d80c4e4264c711f915 (patch) | |
| tree | 6dc9f7329d9c4e724e681d8550105d794e6e90ff /website.go | |
| parent | 66a5c4ea1793886aad80a67ad0e40aca6066bcde (diff) | |
Blog stuff initial version
Diffstat (limited to 'website.go')
| -rw-r--r-- | website.go | 42 |
1 files changed, 41 insertions, 1 deletions
@@ -1,6 +1,7 @@ package main import ( + "bytes" "cmp" "embed" "encoding/json" @@ -17,6 +18,9 @@ import ( "time" "github.com/a-h/templ" + "github.com/yuin/goldmark" + "github.com/yuin/goldmark/extension" + "github.com/yuin/goldmark/renderer/html" "gopkg.in/yaml.v3" ) @@ -159,14 +163,34 @@ type Blog struct { Desc string Date time.Time Body string // raw markdown for rendering + HTML string // rendered body } +var md = goldmark.New( + goldmark.WithExtensions(extension.GFM, extension.Footnote), + goldmark.WithRendererOptions(html.WithUnsafe()), +) + type frontmatter struct { Title string `yaml:"title"` Desc string `yaml:"description"` Date time.Time `yaml:"date"` } +func findBlog(blogs []Blog, slug string) (blog, prev, next *Blog) { + i := slices.IndexFunc(blogs, func(b Blog) bool { return b.Slug == slug }) + if i < 0 { + return nil, nil, nil + } + if i+1 < len(blogs) { + prev = &blogs[i+1] // older + } + if i > 0 { + next = &blogs[i-1] // newer + } + return &blogs[i], prev, next +} + var datePrefix = regexp.MustCompile(`^(\d{4}-\d{2}-\d{2})-(.+)\.md$`) func parseBlog(name, raw string) (Blog, error) { @@ -182,6 +206,12 @@ func parseBlog(name, raw string) (Blog, error) { } } + var buf bytes.Buffer + if err := md.Convert([]byte(body), &buf); err != nil { + return Blog{}, err + } + HTML := buf.String() + slug := strings.TrimSuffix(name, ".md") date := frontmatter.Date // frontmatter wins if match := datePrefix.FindStringSubmatch(name); match != nil { @@ -204,6 +234,7 @@ func parseBlog(name, raw string) (Blog, error) { Date: date, Body: body, Desc: frontmatter.Desc, + HTML: HTML, }, nil } @@ -269,7 +300,7 @@ var projects = []Project{ Slug: "krypton", Title: "Krypton", Category: "Programming Language Compiler", - Desc: "A modern Minecraft creator event. As a developer, I helped with minor improvements to the cosmetics system. ", + Desc: "", // Icon: IconCpu("text-[#F5A670] self-center"), Icon: IconKrypton(), Tags: []ProjectTag{ @@ -564,6 +595,15 @@ func main() { BlogsPage(currentStyle(r), getBlogs()).Render(r.Context(), w) }) + mux.HandleFunc("GET /blogs/{slug}", func(w http.ResponseWriter, r *http.Request) { + blog, prev, next := findBlog(getBlogs(), r.PathValue("slug")) + if blog == nil { + notFoundHandler(w, r) + return + } + BlogPage(currentStyle(r), *blog, prev, next).Render(r.Context(), w) + }) + mux.HandleFunc("GET /theme/{name}", styleHandler("theme", func(n string) (map[string]string, bool) { theme, ok := GetTheme(n) return theme.Vars(), ok |
