fix attribute inheritance/extension
5 files changed, 43 insertions(+), 10 deletions(-)
M templates/homepage.go → templates/homepage.go
@@ -17,8 +17,8 @@ func Homepage(site SiteSettings, vars HomepageVars, content g.Node) g.Node { return Layout(site, PageSettings{ Title: site.Title, - TitleAttrs: Class("p-name u-url"), - BodyAttrs: Class("h-card"), + TitleAttrs: Attrs{"class": "p-name u-url"}, + BodyAttrs: Attrs{"class": "h-card"}, }, Div( ID("content"),
M templates/layout.go → templates/layout.go
@@ -14,10 +14,37 @@ Menu []config.MenuItem InjectLiveReload bool } +type Attrs map[string]string + type PageSettings struct { Title string - TitleAttrs g.Node - BodyAttrs g.Node + TitleAttrs Attrs + BodyAttrs Attrs +} + +// Map a map of anything to a [Group] (which is just a slice of [Node]-s). +func MapMap[K comparable, T any](ts map[K]T, cb func(K, T) g.Node) g.Group { + nodes := make([]g.Node, 0, len(ts)) + for k, t := range ts { + nodes = append(nodes, cb(k, t)) + } + + return nodes +} + +func extendAttrs(base Attrs, attrs Attrs) g.Node { + m := base + for key, value := range attrs { + if v, found := base[key]; found { + m[key] = v + " " + value + } else { + m[key] = value + } + } + + return MapMap(m, func(k string, v string) g.Node { + return g.Attr(k, v) // can't inline this because it uses ...value, grr + }) } func Layout(site SiteSettings, page PageSettings, children ...g.Node) g.Node {@@ -37,10 +64,16 @@ ), StyleEl(g.Raw(CSS)), ), Body( + extendAttrs(page.BodyAttrs, nil), A(Class("skip"), Href("#main"), g.Text("Skip to main content")), Header( H2( - A(page.TitleAttrs, Class("title p-name"), Href("/"), g.Text(site.Title)), + A( + extendAttrs(Attrs{ + "class": "title p-name", + "href": "/", + }, page.TitleAttrs), + g.Text(site.Title)), ), Nav( g.Map(site.Menu, func(item config.MenuItem) g.Node {
M templates/list.go → templates/list.go
@@ -15,7 +15,7 @@ func TagPage(site SiteSettings, vars TagPageVars) g.Node { return Layout(site, PageSettings{ Title: vars.Tag, - TitleAttrs: Class("p-author h-card"), + TitleAttrs: Attrs{"class": "p-author h-card"}, }, Div( ID("content"), Div(Class("filter"),@@ -35,7 +35,7 @@ func ListPage(site SiteSettings, vars ListPageVars) g.Node { return Layout(site, PageSettings{ Title: site.Title, - TitleAttrs: Class("p-author h-card"), + TitleAttrs: Attrs{"class": "p-author h-card"}, }, Div( ID("content"),
M templates/page.go → templates/page.go
@@ -10,7 +10,7 @@ func Page(site SiteSettings, page *content.Post) g.Node { return Layout(site, PageSettings{ Title: page.Title, - TitleAttrs: g.Group([]g.Node{Class("h-card"), Rel("author")}), + TitleAttrs: Attrs{"class": "h-card", "rel": "author"}, }, Article( Header( H1(Class("p-name"), g.Text(page.Title)),
M templates/post.go → templates/post.go
@@ -13,8 +13,8 @@ func PostPage(site SiteSettings, post *content.Post) g.Node { return Layout(site, PageSettings{ Title: post.Title, - TitleAttrs: g.Group([]g.Node{Class("p-author h-card"), Rel("author")}), - BodyAttrs: Class("h-entry"), + TitleAttrs: Attrs{"class": "p-author h-card", "rel": "author"}, + BodyAttrs: Attrs{"class": "h-entry"}, }, Article( Header( H1(Class("p-name"), g.Text(post.Title)),