Fix draft posts without section not showing in drafts index

This commit is contained in:
Jan-Lukas Else 2023-01-25 17:21:21 +01:00
parent c8229ab28d
commit b6a88e6bed
3 changed files with 17 additions and 20 deletions

View File

@ -8,7 +8,7 @@ import (
"github.com/yuin/goldmark/renderer" "github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/renderer/html" "github.com/yuin/goldmark/renderer/html"
"github.com/yuin/goldmark/util" "github.com/yuin/goldmark/util"
"go.goblog.app/app/pkgs/builderpool" "go.goblog.app/app/pkgs/bufferpool"
"github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2"
chromahtml "github.com/alecthomas/chroma/v2/formatters/html" 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) n := node.(*ast.FencedCodeBlock)
// Read code block content. // Read code block content.
buf := builderpool.Get() buf := bufferpool.Get()
defer builderpool.Put(buf) defer bufferpool.Put(buf)
for _, line := range n.Lines().Sliced(0, n.Lines().Len()) { for _, line := range n.Lines().Sliced(0, n.Lines().Len()) {
buf.Write(line.Value(source)) 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 { if highlight(w, buf.String(), string(n.Language(source)), r.formatter) != nil {
// Highlight failed, fallback to plain text. // Highlight failed, fallback to plain text.
_, _ = w.WriteString("<pre><code>") _, _ = w.WriteString("<pre><code>")
r.Writer.RawWrite(w, []byte(buf.String())) r.Writer.RawWrite(w, buf.Bytes())
_, _ = w.WriteString("</code></pre>\n") _, _ = w.WriteString("</code></pre>\n")
} }

View File

@ -10,6 +10,7 @@ import (
"time" "time"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"github.com/samber/lo"
"github.com/vcraescu/go-paginator" "github.com/vcraescu/go-paginator"
"go.goblog.app/app/pkgs/bufferpool" "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) { 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 { if asRequest, ok := r.Context().Value(asRequestKey).(bool); ok && asRequest {
a.serveActivityStreams(blog, w, r) a.serveActivityStreams(blog, w, r)
return return
} }
a.serveIndex(w, r.WithContext(context.WithValue(r.Context(), indexConfigKey, &indexConfig{ 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 { type indexConfig struct {
path string path string
section *configSection section *configSection
sections []*configSection
tax *configTaxonomy tax *configTaxonomy
taxValue string taxValue string
parameter string parameter string
@ -284,13 +287,9 @@ func (a *goBlog) serveIndex(w http.ResponseWriter, r *http.Request) {
// Decode and sanitize search // Decode and sanitize search
search = cleanHTMLText(searchDecode(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 { if ic.section != nil {
sections = []string{ic.section.Name} sections = append(sections, ic.section.Name)
} else {
for sectionKey := range bc.Sections {
sections = append(sections, sectionKey)
}
} }
defaultStatus, defaultVisibility := a.getDefaultPostStates(r) defaultStatus, defaultVisibility := a.getDefaultPostStates(r)
status := ic.status status := ic.status

View File

@ -6,7 +6,6 @@ import (
"time" "time"
"github.com/samber/lo" "github.com/samber/lo"
"go.goblog.app/app/pkgs/builderpool"
"go.goblog.app/app/pkgs/htmlbuilder" "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 // Show link to full post
hb.WriteElementOpen("p") hb.WriteElementOpen("p")
prefix := builderpool.Get() prefix := ""
defer builderpool.Put(prefix)
if len(photos) > 0 { if len(photos) > 0 {
// Contains photos // Contains photos
prefix.WriteString("🖼️") prefix += "🖼️"
} }
if p.hasTrack() { if p.hasTrack() {
// Has GPX track // Has GPX track
prefix.WriteString("🗺️") prefix += "🗺️"
} }
if prefix.Len() > 0 { if len(prefix) > 0 {
prefix.WriteString(" ") hb.WriteEscaped(" ")
hb.WriteEscaped(prefix.String()) hb.WriteEscaped(prefix)
} }
hb.WriteElementOpen("a", "class", "u-url", "href", p.Path) hb.WriteElementOpen("a", "class", "u-url", "href", p.Path)
hb.WriteEscaped(a.ts.GetTemplateStringVariant(bc.Lang, "view")) hb.WriteEscaped(a.ts.GetTemplateStringVariant(bc.Lang, "view"))