From bf28e340381a146e9562a0e65c67e1d4300a0f35 Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Sun, 20 Sep 2020 17:28:24 +0200 Subject: [PATCH] Make menu config map, not list --- config.go | 5 ++--- example-config.yaml | 2 +- render.go | 7 +------ 3 files changed, 4 insertions(+), 10 deletions(-) diff --git a/config.go b/config.go index 0cb680b..6dc787b 100644 --- a/config.go +++ b/config.go @@ -47,7 +47,7 @@ type configBlog struct { // Taxonomies Taxonomies []*taxonomy `mapstructure:"taxonomies"` // Menus - Menus []*menu `mapstructure:"menus"` + Menus map[string]*menu `mapstructure:"menus"` } type section struct { @@ -63,7 +63,6 @@ type taxonomy struct { } type menu struct { - Id string `mapstructure:"id"` Items []*menuItem `mapstructure:"items"` } @@ -113,7 +112,7 @@ func initConfig() error { viper.SetDefault("blog.pagination", 10) viper.SetDefault("blog.sections", []*section{{Name: "posts", Title: "Posts", Description: "**Posts** on this blog"}}) viper.SetDefault("blog.taxonomies", []*taxonomy{{Name: "tags", Title: "Tags", Description: "**Tags** on this blog"}}) - viper.SetDefault("blog.menus", []*menu{{Id: "main", Items: []*menuItem{{Title: "Home", Link: "/"}, {Title: "Post", Link: "Posts"}}}}) + viper.SetDefault("blog.menus", map[string]*menu{"main": {Items: []*menuItem{{Title: "Home", Link: "/"}, {Title: "Post", Link: "Posts"}}}}) viper.SetDefault("user.nick", "admin") viper.SetDefault("user.name", "Admin") viper.SetDefault("user.password", "secret") diff --git a/example-config.yaml b/example-config.yaml index 1333610..f5f2c31 100644 --- a/example-config.yaml +++ b/example-config.yaml @@ -24,7 +24,7 @@ blog: title: Tags description: "**Tags** on this blog" menus: - - id: main + main: items: - title: Home link: / diff --git a/render.go b/render.go index 2ab7f22..0a8fa6c 100644 --- a/render.go +++ b/render.go @@ -27,12 +27,7 @@ func initRendering() { return appConfig.Blog }, "menu": func(id string) *menu { - for _, m := range appConfig.Blog.Menus { - if m.Id == id { - return m - } - } - return nil + return appConfig.Blog.Menus[id] }, "md": func(content string) template.HTML { htmlContent, err := renderMarkdown(content)