diff options
Diffstat (limited to 'backend')
| -rw-r--r-- | backend/home.go | 36 | ||||
| -rw-r--r-- | backend/logs.go | 2 | ||||
| -rw-r--r-- | backend/templates/home.html | 20 |
3 files changed, 43 insertions, 15 deletions
diff --git a/backend/home.go b/backend/home.go index 41f2fe5..32634a1 100644 --- a/backend/home.go +++ b/backend/home.go @@ -1,13 +1,47 @@ package backend import ( + "maps" "net/http" + "slices" + "time" "github.com/go-chi/chi/v5" ) +var sortedLogs []*logData + +type homeData struct { + *data + Logs []*logData +} + +func (h *homeData) SetData(d *data) { + h.data = d +} + func HandleHome(r *chi.Mux) { r.Get("/", func(w http.ResponseWriter, r *http.Request) { - new(data).handleGeneric(w, r, "home", nil) + d := new(homeData) + d.data = new(data) + if sortedLogs == nil { + sortLogs() + } + d.Logs = sortedLogs + d.handleGeneric(w, r, "home", d) + }) +} + +func sortLogs() { + slices.SortedFunc(maps.Values(logs), func(l *logData, l2 *logData) int { + lt := l.pubDate.AsTime(time.UTC) + l2t := l2.pubDate.AsTime(time.UTC) + // we want it reversed + if lt.Before(l2t) { + return 1 + } else if lt.After(l2t) { + return -1 + } + return 0 }) } diff --git a/backend/logs.go b/backend/logs.go index d15be41..ef44e0a 100644 --- a/backend/logs.go +++ b/backend/logs.go @@ -27,6 +27,7 @@ type logData struct { Img image `toml:"image"` pubDate toml.LocalDate `toml:"publication_date"` Content template.HTML `toml:"-"` + Slug string `toml:"-"` } func (d *logData) SetData(dt *data) { @@ -136,6 +137,7 @@ func parseLog(d *logData, path, slug string) bool { d.Article = true d.LogTitle = slug d.title = slug + d.Slug = slug b, err := os.ReadFile(path + ".md") if err != nil { if os.IsNotExist(err) { diff --git a/backend/templates/home.html b/backend/templates/home.html index fb927eb..f0b9b9c 100644 --- a/backend/templates/home.html +++ b/backend/templates/home.html @@ -6,26 +6,18 @@ </p> </div> <article> + {{ range .Logs }} <article> - <h2><a href="log.html">Article title</a></h2> + <h2><a href="/log/{{ .Slug }}">{{ .LogTitle }}</a></h2> <figure> - <a href="log.html"><img src="https://placehold.co/1920x1080" alt=""></a> - <figcaption>A placeholder.</figcaption> + <a href="/log/{{ .Slug }}"><img src="{{ .Img.Src }}" alt="{{ .Img.Alt }}"></a> + <figcaption>{{ .Img.Legend }}</figcaption> </figure> <p> - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim aeque doleamus animo, cum corpore dolemus, fieri tamen permagna accessio potest, si aliquod aeternum et infinitum impendere malum nobis opinemur. - </p> - </article> - <article> - <h2><a href="log.html">Article title</a></h2> - <figure> - <a href="log.html"><img src="https://placehold.co/1920x1080" alt=""></a> - <figcaption>A placeholder.</figcaption> - </figure> - <p> - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim aeque doleamus animo, cum corpore dolemus, fieri tamen permagna accessio potest, si aliquod aeternum et infinitum impendere malum nobis opinemur. + {{ .Description }} </p> </article> + {{ end }} <div class="pagination"> <a href="">Précédent</a> <p>01/15</p> |
