diff options
| author | a73x <[email protected]> | 2024-09-04 12:09:03 +0100 |
|---|---|---|
| committer | a73x <[email protected]> | 2024-09-04 12:09:03 +0100 |
| commit | 933ef3568992802a565d2e6a8d1c7fb231139663 (patch) | |
| tree | 0528f4f1038b02721e8c6fa0116e7eeb42d6aed4 | |
| parent | 62b7922e0ec4c1a21a9509459e5b7c67e8253c87 (diff) | |
fix(html): nav bar ordering
| -rw-r--r-- | pages/pages.go | 41 | ||||
| -rw-r--r-- | templates/layouts/_default.html | 8 |
2 files changed, 42 insertions, 7 deletions
diff --git a/pages/pages.go b/pages/pages.go index 2caa866..610444c 100644 --- a/pages/pages.go +++ b/pages/pages.go @@ -3,6 +3,7 @@ package pages import ( "bytes" "fmt" + "sort" "text/template" "git.sr.ht/~a73x/home/html" @@ -10,9 +11,13 @@ import ( "git.sr.ht/~a73x/home/templates" ) +type Navigation struct { + Title string + Path string +} type GlobalState struct { Collections map[string][]markdown.Content - Nav map[string]markdown.Content + Nav []Navigation } type ParserPair struct { @@ -68,7 +73,7 @@ func Collect() ([]Page, error) { Collections: map[string][]markdown.Content{ "all": contents, }, - Nav: map[string]markdown.Content{}, + Nav: make([]Navigation, 0), } for _, content := range contents { @@ -86,10 +91,11 @@ func Collect() ([]Page, error) { nav, ok := content.Meta["nav"] if ok { - gs.Nav[nav.(string)] = content + gs.Nav = append(gs.Nav, Navigation{content.Meta["title"].(string), nav.(string)}) } } + sortNavBar(gs.Nav) pages := []Page{} for _, content := range contents { page, err := renderTemplate(gs, content) @@ -110,3 +116,32 @@ func Collect() ([]Page, error) { return pages, nil } + +func sortNavBar(nav []Navigation) { + // see no evil, speak no evil + sort.Slice(nav, func(i, j int) bool { + order := map[string]int{ + "home": 1, + "posts": 2, + "about": 3, + } + + // Get the order value, default to a high number if not in the predefined order + orderI, okI := order[nav[i].Title] + if !okI { + orderI = len(nav) + 1 + } + + orderJ, okJ := order[nav[j].Title] + if !okJ { + orderJ = len(nav) + 1 + } + + // If both strings have the same order value, sort lexicographically + if orderI == orderJ { + return nav[i].Title < nav[j].Title + } + + return orderI < orderJ + }) +} diff --git a/templates/layouts/_default.html b/templates/layouts/_default.html index ae39408..ae6f8a8 100644 --- a/templates/layouts/_default.html +++ b/templates/layouts/_default.html @@ -17,11 +17,11 @@ <sub>high effort, low reward</sub> <nav class="nav"> <ul> - {{ range $key, $value := .Nav}} - {{ if eq $.Path $key }} - <li><a href="{{$key}}">{{$value.Meta.title}}</a></li> + {{ range $vvv, $navPair:= .Nav}} + {{ if eq $.Path $navPair.Path }} + <li><a href="{{$navPair.Path}}">{{$navPair.Title}}</a></li> {{else}} - <li><a class="no-decorations" href="{{$key}}">{{$value.Meta.title}}</a></li> + <li><a class="no-decorations" href="{{$navPair.Path}}">{{$navPair.Title}}</a></li> {{end}} {{end}} </ul> |
