From 8012691d7a4838e77d3b8996fd0ed5e447355d57 Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Tue, 27 Dec 2022 08:29:32 +0100 Subject: [PATCH] Use fallback title generated from summary in HTML HEAD title --- microformats.go | 8 ++------ postsFuncs.go | 4 ++++ ui.go | 6 +++++- utils.go | 7 +++++++ 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/microformats.go b/microformats.go index 3075910..4ee4c59 100644 --- a/microformats.go +++ b/microformats.go @@ -86,12 +86,8 @@ func parseMicroformatsFromReader(u string, r io.Reader) (*microformatsResult, er m.Title = "" } // Shorten content and title if too long - if cr := []rune(m.Content); len(cr) > 500 { - m.Content = string(cr[0:497]) + "…" - } - if tr := []rune(m.Title); len(tr) > 60 { - m.Title = string(tr[0:57]) + "…" - } + m.Content = truncateStringWithEllipsis(m.Content, 500) + m.Title = truncateStringWithEllipsis(m.Title, 60) return m, nil } diff --git a/postsFuncs.go b/postsFuncs.go index ee29813..64a0def 100644 --- a/postsFuncs.go +++ b/postsFuncs.go @@ -128,6 +128,10 @@ func (a *goBlog) postSummary(p *post) (summary string) { return } +func (a *goBlog) fallbackTitle(p *post) string { + return truncateStringWithEllipsis(a.postSummary(p), 30) +} + func (a *goBlog) postTranslations(p *post) []*post { translationkey := p.firstParameter("translationkey") if translationkey == "" { diff --git a/ui.go b/ui.go index 890fbfb..9f2e2bd 100644 --- a/ui.go +++ b/ui.go @@ -887,7 +887,11 @@ func (a *goBlog) renderPost(hb *htmlbuilder.HtmlBuilder, rd *renderData) { a.renderBase( hb, rd, func(hb *htmlbuilder.HtmlBuilder) { - a.renderTitleTag(hb, rd.Blog, p.RenderedTitle) + if p.RenderedTitle != "" { + a.renderTitleTag(hb, rd.Blog, p.RenderedTitle) + } else { + a.renderTitleTag(hb, rd.Blog, a.fallbackTitle(p)) + } hb.WriteElementOpen("link", "rel", "stylesheet", "href", a.assetFileName("css/chroma.css")) a.renderPostHeadMeta(hb, p) if su := a.shortPostURL(p); su != "" { diff --git a/utils.go b/utils.go index d0b20c5..aab6099 100644 --- a/utils.go +++ b/utils.go @@ -403,3 +403,10 @@ func stringToInt(s string) int { func loStringNotEmpty(s string, _ int) bool { return s != "" } + +func truncateStringWithEllipsis(s string, l int) string { + if tr := []rune(s); len(tr) > l && l > 3 { + return string(tr[0:l-3]) + "…" + } + return s +}