diff options
| author | Anhgelus Morhtuuzh <william@herges.fr> | 2025-10-02 20:08:54 +0200 |
|---|---|---|
| committer | Anhgelus Morhtuuzh <william@herges.fr> | 2025-10-02 20:08:54 +0200 |
| commit | d949b9ae30695d47e6192d29001c7daab44350e4 (patch) | |
| tree | 616cfdaeb28f5bbc676eb960e6e4d2064e27dedc /backend | |
| parent | 668014f3ee2f6bd60220c04ceb008202d6aa9341 (diff) | |
feat(backend): parse markdown heading to get log info
Diffstat (limited to 'backend')
| -rw-r--r-- | backend/logs.go | 34 |
1 files changed, 24 insertions, 10 deletions
diff --git a/backend/logs.go b/backend/logs.go index 5a0498f..0eaef10 100644 --- a/backend/logs.go +++ b/backend/logs.go @@ -12,16 +12,17 @@ import ( "git.anhgelus.world/anhgelus/small-world/markdown" "github.com/go-chi/chi/v5" + "github.com/pelletier/go-toml/v2" ) var logs = map[string]string{} type logData struct { *data - LogTitle string - Description string - Img image - Content template.HTML + LogTitle string `toml:"title"` + Description string `toml:"description"` + Img image `toml:"image"` + Content template.HTML `toml:"-"` } func (d *logData) SetData(dt *data) { @@ -29,9 +30,9 @@ func (d *logData) SetData(dt *data) { } type image struct { - Src string - Alt string - Legend string + Src string `toml:"src"` + Alt string `toml:"alt"` + Legend string `toml:"legend"` } func LoadLogs(cfg *Config) bool { @@ -100,7 +101,7 @@ func handleLog(w http.ResponseWriter, r *http.Request) { http.NotFoundHandler().ServeHTTP(w, r) return } - d := new(logData) + var d logData d.data = new(data) d.Article = true d.LogTitle = slug @@ -109,7 +110,20 @@ func handleLog(w http.ResponseWriter, r *http.Request) { if err != nil { panic(err) } - d.Content, err = markdown.ParseBytes(b) + var dd string + splits := strings.SplitN(string(b), "---", 2) + if len(splits) == 2 { + dd = splits[1] + err = toml.Unmarshal([]byte(splits[0]), &d) + if err != nil { + panic(err) + } + d.title = d.LogTitle + d.Image = d.Img.Src + } else { + dd = string(b) + } + d.Content, err = markdown.Parse(dd) var errMd *markdown.ParseError errors.As(err, &errMd) if errMd != nil { @@ -117,5 +131,5 @@ func handleLog(w http.ResponseWriter, r *http.Request) { slog.Error("parsing markdown") fmt.Println(errMd.Pretty()) } - d.handleGeneric(w, r, "log", d) + d.handleGeneric(w, r, "log", &d) } |
