From 663e6932bf486ffe3745c37ea3072727f2edc4e1 Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Wed, 4 Aug 2021 23:26:38 +0200 Subject: [PATCH] Fix rendering Markdown title --- activityStreams.go | 2 +- app.go | 2 +- feeds.go | 2 +- markdown.go | 36 +++++++++++++++++++++++++++++ markdown_test.go | 7 ++++++ render.go | 6 ++--- telegram.go | 2 +- templates/base.gohtml | 10 ++++---- templates/blogroll.gohtml | 4 ++-- templates/blogstats.gohtml | 4 ++-- templates/captcha.gohtml | 2 +- templates/commentsadmin.gohtml | 2 +- templates/contact.gohtml | 2 +- templates/editor.gohtml | 2 +- templates/editorfiles.gohtml | 2 +- templates/footer.gohtml | 4 ++-- templates/geomap.gohtml | 2 +- templates/header.gohtml | 4 ++-- templates/index.gohtml | 10 ++++---- templates/login.gohtml | 2 +- templates/notificationsadmin.gohtml | 2 +- templates/photosummary.gohtml | 2 +- templates/post.gohtml | 4 ++-- templates/postactions.gohtml | 2 +- templates/postheadmeta.gohtml | 4 ++-- templates/postmeta.gohtml | 2 +- templates/posttax.gohtml | 4 ++-- templates/search.gohtml | 6 ++--- templates/statichome.gohtml | 2 +- templates/summary.gohtml | 2 +- templates/summaryandpostmeta.gohtml | 2 +- templates/taxonomy.gohtml | 4 ++-- templates/webmentionadmin.gohtml | 2 +- 33 files changed, 94 insertions(+), 51 deletions(-) diff --git a/activityStreams.go b/activityStreams.go index 3b37d45..311ee7a 100644 --- a/activityStreams.go +++ b/activityStreams.go @@ -106,7 +106,7 @@ func (a *goBlog) toASNote(p *post) *asNote { AttributedTo: a.apIri(a.cfg.Blogs[p.Blog]), } // Name and Type - if title := a.renderText(p.Title()); title != "" { + if title := a.renderMdTitle(p.Title()); title != "" { as.Name = title as.Type = "Article" } else { diff --git a/app.go b/app.go index b11281d..96ac05b 100644 --- a/app.go +++ b/app.go @@ -52,7 +52,7 @@ type goBlog struct { // Logs logf *rotatelogs.RotateLogs // Markdown - md, absoluteMd goldmark.Markdown + md, absoluteMd, titleMd goldmark.Markdown // Media compressorsInit sync.Once compressors []mediaCompression diff --git a/feeds.go b/feeds.go index 67ec340..297445a 100644 --- a/feeds.go +++ b/feeds.go @@ -42,7 +42,7 @@ func (a *goBlog) generateFeed(blog string, f feedType, w http.ResponseWriter, r } for _, p := range posts { feed.Add(&feeds.Item{ - Title: a.renderText(p.Title()), + Title: a.renderMdTitle(p.Title()), Link: &feeds.Link{Href: a.fullPostURL(p)}, Description: a.postSummary(p), Id: p.Path, diff --git a/markdown.go b/markdown.go index fb951ca..6e18428 100644 --- a/markdown.go +++ b/markdown.go @@ -41,6 +41,33 @@ func (a *goBlog) initMarkdown() { absoluteLinks: true, publicAddress: a.cfg.Server.PublicAddress, }))...) + a.titleMd = goldmark.New( + goldmark.WithParser( + // Override to disable lists + parser.NewParser( + parser.WithBlockParsers( + // util.Prioritized(parser.NewSetextHeadingParser(), 100), + util.Prioritized(parser.NewThematicBreakParser(), 200), + // util.Prioritized(parser.NewListParser(), 300), + // util.Prioritized(parser.NewListItemParser(), 400), + util.Prioritized(parser.NewCodeBlockParser(), 500), + // util.Prioritized(parser.NewATXHeadingParser(), 600), + util.Prioritized(parser.NewFencedCodeBlockParser(), 700), + util.Prioritized(parser.NewBlockquoteParser(), 800), + util.Prioritized(parser.NewHTMLBlockParser(), 900), + util.Prioritized(parser.NewParagraphParser(), 1000)), + parser.WithInlineParsers(parser.DefaultInlineParsers()...), + parser.WithParagraphTransformers(parser.DefaultParagraphTransformers()...), + ), + ), + goldmark.WithRendererOptions( + html.WithUnsafe(), + ), + goldmark.WithExtensions( + extension.Typographer, + emoji.Emoji, + ), + ) } func (a *goBlog) renderMarkdown(source string, absoluteLinks bool) (rendered []byte, err error) { @@ -74,6 +101,15 @@ func (a *goBlog) renderText(s string) string { return htmlText(h) } +func (a *goBlog) renderMdTitle(s string) string { + var buffer bytes.Buffer + err := a.titleMd.Convert([]byte(s), &buffer) + if err != nil { + return "" + } + return htmlText(buffer.Bytes()) +} + // Extensions etc... // Links diff --git a/markdown_test.go b/markdown_test.go index 4dbd5a8..561690f 100644 --- a/markdown_test.go +++ b/markdown_test.go @@ -68,6 +68,13 @@ func Test_markdown(t *testing.T) { t.Errorf("Wrong result, got \"%v\"", renderedText) } + // Title + + renderedTitle := app.renderMdTitle("3. **Test**") + if renderedTitle != "3. Test" { + t.Errorf("Wrong result, got \"%v\"", renderedTitle) + } + // Template func renderedText = string(app.safeRenderMarkdownAsHTML("[Relative](/relative)")) diff --git a/render.go b/render.go index 95704e5..6cda5e8 100644 --- a/render.go +++ b/render.go @@ -44,9 +44,9 @@ const ( func (a *goBlog) initRendering() error { a.templates = map[string]*template.Template{} templateFunctions := template.FuncMap{ - "md": a.safeRenderMarkdownAsHTML, - "mdtext": a.renderText, - "html": wrapStringAsHTML, + "md": a.safeRenderMarkdownAsHTML, + "mdtitle": a.renderMdTitle, + "html": wrapStringAsHTML, // Post specific "ps": postParameter, "content": a.postHtml, diff --git a/telegram.go b/telegram.go index 63160ed..8c6b5d7 100644 --- a/telegram.go +++ b/telegram.go @@ -16,7 +16,7 @@ const telegramBaseURL = "https://api.telegram.org/bot" func (a *goBlog) initTelegram() { a.pPostHooks = append(a.pPostHooks, func(p *post) { if tg := a.cfg.Blogs[p.Blog].Telegram; tg.enabled() && p.isPublishedSectionPost() { - if html := tg.generateHTML(a.renderText(p.Title()), a.fullPostURL(p), a.shortPostURL(p)); html != "" { + if html := tg.generateHTML(a.renderMdTitle(p.Title()), a.fullPostURL(p), a.shortPostURL(p)); html != "" { if err := a.send(tg, html, "HTML"); err != nil { log.Printf("Failed to send post to Telegram: %v", err) } diff --git a/templates/base.gohtml b/templates/base.gohtml index 7b83896..86cbf19 100644 --- a/templates/base.gohtml +++ b/templates/base.gohtml @@ -6,11 +6,11 @@ {{ with .Canonical }}{{ end }} {{ block "title" . }} - {{ mdtext .Blog.Title }} + {{ mdtitle .Blog.Title }} {{ end }} - - - + + + @@ -18,7 +18,7 @@ {{ with .User }}{{ range .Identities }}{{ end }}{{ end }} {{ $os := opensearch .Blog }} {{ if $os }} - + {{ end }} {{ include "header" . }} {{ block "main" . }}{{ end }} diff --git a/templates/blogroll.gohtml b/templates/blogroll.gohtml index 6598935..0facfd8 100644 --- a/templates/blogroll.gohtml +++ b/templates/blogroll.gohtml @@ -1,10 +1,10 @@ {{ define "title" }} - {{ mdtext .Data.Title }} - {{ mdtext .Blog.Title }} + {{ mdtitle .Data.Title }} - {{ mdtitle .Blog.Title }} {{ end }} {{ define "main" }}
- {{ with .Data.Title }}

{{ mdtext . }}

{{ end }} + {{ with .Data.Title }}

{{ mdtitle . }}

{{ end }} {{ with .Data.Description }}{{ md . }}{{ end }}

{{ string .Blog.Lang "download" }}

{{ $lang := .Blog.Lang }} diff --git a/templates/blogstats.gohtml b/templates/blogstats.gohtml index ededd84..ed1f0ef 100644 --- a/templates/blogstats.gohtml +++ b/templates/blogstats.gohtml @@ -1,10 +1,10 @@ {{ define "title" }} - {{ with .Blog.BlogStats.Title }}{{ mdtext . }} - {{ end }}{{ mdtext .Blog.Title }} + {{ with .Blog.BlogStats.Title }}{{ mdtitle . }} - {{ end }}{{ mdtitle .Blog.Title }} {{ end }} {{ define "main" }}
- {{ with .Blog.BlogStats.Title }}

{{ mdtext . }}

{{ end }} + {{ with .Blog.BlogStats.Title }}

{{ mdtitle . }}

{{ end }} {{ with .Blog.BlogStats.Description }}{{ md . }}{{ end }}

{{ string .Blog.Lang "loading" }}

diff --git a/templates/captcha.gohtml b/templates/captcha.gohtml index 4307f15..fc43d8a 100644 --- a/templates/captcha.gohtml +++ b/templates/captcha.gohtml @@ -1,5 +1,5 @@ {{ define "title" }} - {{ string .Blog.Lang "captcha" }} - {{ mdtext .Blog.Title }} + {{ string .Blog.Lang "captcha" }} - {{ mdtitle .Blog.Title }} {{ end }} {{ define "main" }} diff --git a/templates/commentsadmin.gohtml b/templates/commentsadmin.gohtml index 7b0496b..7455c54 100644 --- a/templates/commentsadmin.gohtml +++ b/templates/commentsadmin.gohtml @@ -1,5 +1,5 @@ {{ define "title" }} - {{ string .Blog.Lang "comments" }} - {{ mdtext .Blog.Title }} + {{ string .Blog.Lang "comments" }} - {{ mdtitle .Blog.Title }} {{ end }} {{ define "main" }} diff --git a/templates/contact.gohtml b/templates/contact.gohtml index d6edd0f..02e19b0 100644 --- a/templates/contact.gohtml +++ b/templates/contact.gohtml @@ -7,7 +7,7 @@ {{ if .Data.sent }}

{{ string .Blog.Lang "messagesent" }}

{{ else }} - {{ with .Data.title }}

{{ mdtext . }}

{{ end }} + {{ with .Data.title }}

{{ mdtitle . }}

{{ end }} {{ with .Data.description }}{{ md . }}{{ end }}
diff --git a/templates/editor.gohtml b/templates/editor.gohtml index 154bc63..f2b0050 100644 --- a/templates/editor.gohtml +++ b/templates/editor.gohtml @@ -1,5 +1,5 @@ {{ define "title" }} - {{ string .Blog.Lang "editor" }} - {{ mdtext .Blog.Title }} + {{ string .Blog.Lang "editor" }} - {{ mdtitle .Blog.Title }} {{ end }} {{ define "main" }} diff --git a/templates/editorfiles.gohtml b/templates/editorfiles.gohtml index b88051a..86db73d 100644 --- a/templates/editorfiles.gohtml +++ b/templates/editorfiles.gohtml @@ -1,5 +1,5 @@ {{ define "title" }} - {{ string .Blog.Lang "mediafiles" }} - {{ mdtext .Blog.Title }} + {{ string .Blog.Lang "mediafiles" }} - {{ mdtitle .Blog.Title }} {{ end }} {{ define "main" }} diff --git a/templates/footer.gohtml b/templates/footer.gohtml index 849eb59..6fb2b80 100644 --- a/templates/footer.gohtml +++ b/templates/footer.gohtml @@ -1,10 +1,10 @@ {{ define "footer" }}