From 0c0c6fb6df755d8f53d353e8b941e22a6c474b60 Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Sun, 14 Dec 2025 18:50:15 +0100 Subject: feat(backend): parse description as markdown --- backend/parser.go | 21 +++++++++++++++------ backend/templates/components.html | 2 +- backend/templates/data.html | 2 +- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/backend/parser.go b/backend/parser.go index f2537d5..8dba673 100644 --- a/backend/parser.go +++ b/backend/parser.go @@ -3,6 +3,7 @@ package backend import ( "errors" "fmt" + "html" "html/template" "log/slog" "strings" @@ -14,7 +15,7 @@ import ( type EntryInfo struct { Title string `toml:"title"` - Description string `toml:"description"` + Description template.HTML `toml:"description"` Img image `toml:"image"` PubLocalDate toml.LocalDate `toml:"publication_date"` } @@ -38,29 +39,37 @@ func renderLink(content, href, url string) template.HTML { } func parse(b []byte, info *EntryInfo, d *data) (template.HTML, bool) { + opt := defaultMarkdownOption + opt.RenderLink = renderLinkFunc(d.URL) + var dd string + var err error splits := strings.SplitN(string(b), "---", 2) if len(splits) == 2 && info != nil { - err := toml.Unmarshal([]byte(splits[0]), info) + err = toml.Unmarshal([]byte(splits[0]), info) if err != nil { slog.Warn("parsing entry info", "error", err) } else { + info.Description, err = markdown.Parse(string(info.Description), &opt) dd = splits[1] } } else { dd = string(b) } - opt := defaultMarkdownOption - opt.RenderLink = renderLinkFunc(d.URL) - content, err := markdown.Parse(dd, &opt) + var errMd *markdown.ParseError errors.As(err, &errMd) + var content template.HTML + if errMd == nil { + content, err = markdown.Parse(dd, &opt) + errors.As(err, &errMd) + } if errMd != nil { slog.Error("parsing markdown") fmt.Println(errMd.Pretty()) return "", false } - d.PageDescription = info.Description + d.PageDescription = html.UnescapeString(string(info.Description)) d.title = info.Title d.Image = info.Img.Src return content, true diff --git a/backend/templates/components.html b/backend/templates/components.html index b4ffbe6..d9a653f 100644 --- a/backend/templates/components.html +++ b/backend/templates/components.html @@ -7,7 +7,7 @@ {{ .Img.Alt }}
{{ .Img.Legend }}
-

{{ .Description }}

+ {{ .Description }} {{ end }}