From 17b008f72f284ea989fddb2b6b52c2ca558c985a Mon Sep 17 00:00:00 2001 From: a73x Date: Tue, 3 Sep 2024 08:52:12 +0100 Subject: chore(html): refactor nav to be more dynamic --- content/index.md | 1 + content/posts.md | 1 + markdown/markdown.go | 5 ++++- pages/pages.go | 23 ++++++++++++++--------- templates/layouts/_default.html | 23 +++++++++-------------- web/web.go | 2 +- 6 files changed, 30 insertions(+), 25 deletions(-) diff --git a/content/index.md b/content/index.md index 240517a..94bcd77 100644 --- a/content/index.md +++ b/content/index.md @@ -1,5 +1,6 @@ --- title: home +nav: "/" --- ## me * backend cloud software engineer diff --git a/content/posts.md b/content/posts.md index 020e1cc..011720b 100644 --- a/content/posts.md +++ b/content/posts.md @@ -1,5 +1,6 @@ --- title: posts +nav: /posts --- {{range .Collections.posts}} - [{{.Meta.title}}]({{.Path}}) diff --git a/markdown/markdown.go b/markdown/markdown.go index 658e527..1a529f6 100644 --- a/markdown/markdown.go +++ b/markdown/markdown.go @@ -60,7 +60,10 @@ func parseMarkdownFile(embedded fs.FS, path string) (Content, error) { if err != nil { return Content{}, fmt.Errorf("failed to parse frontmatter: %v", err) } - path = strings.Replace(path, ".md", "", 1) + path = "/" + strings.Replace(path, ".md", "", 1) + if path == "/index" { + path = "/" + } mc := Content{ Body: string(rest), diff --git a/pages/pages.go b/pages/pages.go index 63c3432..2caa866 100644 --- a/pages/pages.go +++ b/pages/pages.go @@ -12,6 +12,7 @@ import ( type GlobalState struct { Collections map[string][]markdown.Content + Nav map[string]markdown.Content } type ParserPair struct { @@ -67,21 +68,25 @@ func Collect() ([]Page, error) { Collections: map[string][]markdown.Content{ "all": contents, }, + Nav: map[string]markdown.Content{}, } for _, content := range contents { tags, ok := content.Meta["tags"] - if !ok { - continue + if ok { + switch tags := tags.(type) { + case string: + gs.Collections[tags] = append(gs.Collections[tags], content) + case []string: + for _, tag := range tags { + gs.Collections[tag] = append(gs.Collections[tag], content) + } + } } - switch tags := tags.(type) { - case string: - gs.Collections[tags] = append(gs.Collections[tags], content) - case []string: - for _, tag := range tags { - gs.Collections[tag] = append(gs.Collections[tag], content) - } + nav, ok := content.Meta["nav"] + if ok { + gs.Nav[nav.(string)] = content } } diff --git a/templates/layouts/_default.html b/templates/layouts/_default.html index 09b2239..2e7ec0c 100644 --- a/templates/layouts/_default.html +++ b/templates/layouts/_default.html @@ -61,20 +61,15 @@ high effort, low reward {{ template "content" . }} diff --git a/web/web.go b/web/web.go index a1d2353..ec097f0 100644 --- a/web/web.go +++ b/web/web.go @@ -38,7 +38,7 @@ func New(logger *zap.Logger) (*http.Server, error) { mux.Handle("GET /static/", http.StripPrefix("/static/", http.FileServer(http.FS(staticFs)))) for _, page := range pages { - mux.HandleFunc("GET /"+page.Path, func(w http.ResponseWriter, r *http.Request) { + mux.HandleFunc("GET "+page.Path, func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(page.Content)) }) } -- cgit v1.2.3