diff options
| author | Anhgelus Morhtuuzh <william@herges.fr> | 2025-10-05 22:50:27 +0200 |
|---|---|---|
| committer | Anhgelus Morhtuuzh <william@herges.fr> | 2025-10-05 23:18:17 +0200 |
| commit | 4e5ab025f03004d9cc5ecce53371683766495ac6 (patch) | |
| tree | b24b1188226d09db4c5cacb5514a5d9f90cd58f8 | |
| parent | 33cfc1d4e8f3b5b55202588e5b5148a48b43d427 (diff) | |
feat(backend): support file properties in generic root
| -rw-r--r-- | backend/home.go | 15 | ||||
| -rw-r--r-- | backend/logs.go | 5 | ||||
| -rw-r--r-- | backend/parser.go | 5 |
3 files changed, 10 insertions, 15 deletions
diff --git a/backend/home.go b/backend/home.go index 453a6f5..b945313 100644 --- a/backend/home.go +++ b/backend/home.go @@ -1,7 +1,6 @@ package backend import ( - "fmt" "html/template" "log/slog" "net/http" @@ -9,13 +8,12 @@ import ( "path/filepath" "strconv" - "git.anhgelus.world/anhgelus/small-world/markdown" "github.com/go-chi/chi/v5" ) var ( sortedLogs []*logData - rootContent = map[string]template.HTML{} + rootContent = map[string]*rootData{} ) type homeData struct { @@ -73,7 +71,7 @@ func handleGenericRoot(w http.ResponseWriter, r *http.Request, name string) { d := new(rootData) d.data = new(data) if c, ok := rootContent[name]; ok { - d.Content = c + *d = *c } else { cfg := r.Context().Value(configKey).(*Config) path := filepath.Join(cfg.RootFolder, name+".md") @@ -85,15 +83,12 @@ func handleGenericRoot(w http.ResponseWriter, r *http.Request, name string) { } panic(err) } - var errMd *markdown.ParseError - d.Content, errMd = markdown.ParseBytes(b, &markdown.Option{ImageSource: getStatic}) - if errMd != nil { - slog.Error("parsing markdown", "path", path) - fmt.Println(errMd.Pretty()) + d.Content, ok = parse(b, new(EntryInfo), d.data) + if !ok { w.WriteHeader(http.StatusInternalServerError) return } - rootContent[name] = d.Content + rootContent[name] = d } d.handleGeneric(w, r, "simple", d) } diff --git a/backend/logs.go b/backend/logs.go index 46e47c5..2b009c1 100644 --- a/backend/logs.go +++ b/backend/logs.go @@ -140,7 +140,6 @@ func handleLog(w http.ResponseWriter, r *http.Request) { func parseLog(d *logData, path, slug string) bool { d.Article = true - d.EntryInfo.Title = slug d.title = slug d.Slug = slug b, err := os.ReadFile(path + ".md") @@ -151,12 +150,10 @@ func parseLog(d *logData, path, slug string) bool { panic(err) } var ok bool - d.Content, ok = parse(b, &d.EntryInfo) + d.Content, ok = parse(b, &d.EntryInfo, d.data) if !ok { return false } - d.title = d.EntryInfo.Title - d.Image = d.Img.Src logs[path] = d return true } diff --git a/backend/parser.go b/backend/parser.go index a0cb01f..5ef6c05 100644 --- a/backend/parser.go +++ b/backend/parser.go @@ -18,7 +18,7 @@ type EntryInfo struct { PubLocalDate toml.LocalDate `toml:"publication_date"` } -func parse(b []byte, info *EntryInfo) (template.HTML, bool) { +func parse(b []byte, info *EntryInfo, d *data) (template.HTML, bool) { var dd string splits := strings.SplitN(string(b), "---", 2) if len(splits) == 2 && info != nil { @@ -39,5 +39,8 @@ func parse(b []byte, info *EntryInfo) (template.HTML, bool) { fmt.Println(errMd.Pretty()) return "", false } + d.PageDescription = info.Description + d.title = info.Title + d.Image = info.Img.Src return content, true } |
