From b6a88e6bed5d829b6f8dc854ffe97cd83ec003b8 Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Wed, 25 Jan 2023 17:21:21 +0100 Subject: [PATCH] Fix draft posts without section not showing in drafts index --- pkgs/highlighting/highlighting.go | 8 ++++---- posts.go | 15 +++++++-------- uiComponents.go | 14 ++++++-------- 3 files changed, 17 insertions(+), 20 deletions(-) diff --git a/pkgs/highlighting/highlighting.go b/pkgs/highlighting/highlighting.go index c40beea..47ffd49 100644 --- a/pkgs/highlighting/highlighting.go +++ b/pkgs/highlighting/highlighting.go @@ -8,7 +8,7 @@ import ( "github.com/yuin/goldmark/renderer" "github.com/yuin/goldmark/renderer/html" "github.com/yuin/goldmark/util" - "go.goblog.app/app/pkgs/builderpool" + "go.goblog.app/app/pkgs/bufferpool" "github.com/alecthomas/chroma/v2" chromahtml "github.com/alecthomas/chroma/v2/formatters/html" @@ -60,8 +60,8 @@ func (r *htmlRenderer) renderFencedCodeBlock(w util.BufWriter, source []byte, no n := node.(*ast.FencedCodeBlock) // Read code block content. - buf := builderpool.Get() - defer builderpool.Put(buf) + buf := bufferpool.Get() + defer bufferpool.Put(buf) for _, line := range n.Lines().Sliced(0, n.Lines().Len()) { buf.Write(line.Value(source)) } @@ -70,7 +70,7 @@ func (r *htmlRenderer) renderFencedCodeBlock(w util.BufWriter, source []byte, no if highlight(w, buf.String(), string(n.Language(source)), r.formatter) != nil { // Highlight failed, fallback to plain text. _, _ = w.WriteString("
")
-		r.Writer.RawWrite(w, []byte(buf.String()))
+		r.Writer.RawWrite(w, buf.Bytes())
 		_, _ = w.WriteString("
\n") } diff --git a/posts.go b/posts.go index d98de0e..872eacb 100644 --- a/posts.go +++ b/posts.go @@ -10,6 +10,7 @@ import ( "time" "github.com/go-chi/chi/v5" + "github.com/samber/lo" "github.com/vcraescu/go-paginator" "go.goblog.app/app/pkgs/bufferpool" ) @@ -151,13 +152,14 @@ func (p *postPaginationAdapter) Slice(offset, length int, data any) error { } func (a *goBlog) serveHome(w http.ResponseWriter, r *http.Request) { - blog, _ := a.getBlog(r) + blog, bc := a.getBlog(r) if asRequest, ok := r.Context().Value(asRequestKey).(bool); ok && asRequest { a.serveActivityStreams(blog, w, r) return } a.serveIndex(w, r.WithContext(context.WithValue(r.Context(), indexConfigKey, &indexConfig{ - path: a.getRelativePath(blog, ""), + path: a.getRelativePath(blog, ""), + sections: lo.Values(bc.Sections), }))) } @@ -261,6 +263,7 @@ func (a *goBlog) serveDate(w http.ResponseWriter, r *http.Request) { type indexConfig struct { path string section *configSection + sections []*configSection tax *configTaxonomy taxValue string parameter string @@ -284,13 +287,9 @@ func (a *goBlog) serveIndex(w http.ResponseWriter, r *http.Request) { // Decode and sanitize search search = cleanHTMLText(searchDecode(search)) } - var sections []string + sections := lo.Map(ic.sections, func(i *configSection, _ int) string { return i.Name }) if ic.section != nil { - sections = []string{ic.section.Name} - } else { - for sectionKey := range bc.Sections { - sections = append(sections, sectionKey) - } + sections = append(sections, ic.section.Name) } defaultStatus, defaultVisibility := a.getDefaultPostStates(r) status := ic.status diff --git a/uiComponents.go b/uiComponents.go index 926fd6a..9ee0f3c 100644 --- a/uiComponents.go +++ b/uiComponents.go @@ -6,7 +6,6 @@ import ( "time" "github.com/samber/lo" - "go.goblog.app/app/pkgs/builderpool" "go.goblog.app/app/pkgs/htmlbuilder" ) @@ -65,19 +64,18 @@ func (a *goBlog) renderSummary(hb *htmlbuilder.HtmlBuilder, bc *configBlog, p *p } // Show link to full post hb.WriteElementOpen("p") - prefix := builderpool.Get() - defer builderpool.Put(prefix) + prefix := "" if len(photos) > 0 { // Contains photos - prefix.WriteString("🖼️") + prefix += "🖼️" } if p.hasTrack() { // Has GPX track - prefix.WriteString("🗺️") + prefix += "🗺️" } - if prefix.Len() > 0 { - prefix.WriteString(" ") - hb.WriteEscaped(prefix.String()) + if len(prefix) > 0 { + hb.WriteEscaped(" ") + hb.WriteEscaped(prefix) } hb.WriteElementOpen("a", "class", "u-url", "href", p.Path) hb.WriteEscaped(a.ts.GetTemplateStringVariant(bc.Lang, "view"))