Fix rendering Markdown title

This commit is contained in:
Jan-Lukas Else 2021-08-04 23:26:38 +02:00
parent 586bd46b31
commit 663e6932bf
33 changed files with 94 additions and 51 deletions

View File

@ -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 {

2
app.go
View File

@ -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

View File

@ -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,

View File

@ -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

View File

@ -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)"))

View File

@ -45,7 +45,7 @@ func (a *goBlog) initRendering() error {
a.templates = map[string]*template.Template{}
templateFunctions := template.FuncMap{
"md": a.safeRenderMarkdownAsHTML,
"mdtext": a.renderText,
"mdtitle": a.renderMdTitle,
"html": wrapStringAsHTML,
// Post specific
"ps": postParameter,

View File

@ -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)
}

View File

@ -6,11 +6,11 @@
<link rel="stylesheet" href="{{ asset "css/styles.css" }}">
{{ with .Canonical }}<link rel="canonical" href="{{ . }}">{{ end }}
{{ block "title" . }}
<title>{{ mdtext .Blog.Title }}</title>
<title>{{ mdtitle .Blog.Title }}</title>
{{ end }}
<link rel="alternate" type="application/rss+xml" title="RSS ({{ mdtext .Blog.Title }})" href="{{ .Blog.Path }}.rss"/>
<link rel="alternate" type="application/atom+xml" title="Atom ({{ mdtext .Blog.Title }})" href="{{ .Blog.Path }}.atom"/>
<link rel="alternate" type="application/feed+json" title="JSON Feed ({{ mdtext .Blog.Title }})" href="{{ .Blog.Path }}.json"/>
<link rel="alternate" type="application/rss+xml" title="RSS ({{ mdtitle .Blog.Title }})" href="{{ .Blog.Path }}.rss"/>
<link rel="alternate" type="application/atom+xml" title="Atom ({{ mdtitle .Blog.Title }})" href="{{ .Blog.Path }}.atom"/>
<link rel="alternate" type="application/feed+json" title="JSON Feed ({{ mdtitle .Blog.Title }})" href="{{ .Blog.Path }}.json"/>
<link rel="webmention" href="{{ absolute "/webmention" }}" />
<link rel="micropub" href="/micropub" />
<link rel="authorization_endpoint" href="/indieauth" />
@ -18,7 +18,7 @@
{{ with .User }}{{ range .Identities }}<link rel="me" href="{{ . }}" />{{ end }}{{ end }}
{{ $os := opensearch .Blog }}
{{ if $os }}
<link rel="search" type="application/opensearchdescription+xml" href="{{ $os }}" title="{{ mdtext .Blog.Title }}" />
<link rel="search" type="application/opensearchdescription+xml" href="{{ $os }}" title="{{ mdtitle .Blog.Title }}" />
{{ end }}
{{ include "header" . }}
{{ block "main" . }}{{ end }}

View File

@ -1,10 +1,10 @@
{{ define "title" }}
<title>{{ mdtext .Data.Title }} - {{ mdtext .Blog.Title }}</title>
<title>{{ mdtitle .Data.Title }} - {{ mdtitle .Blog.Title }}</title>
{{ end }}
{{ define "main" }}
<main>
{{ with .Data.Title }}<h1>{{ mdtext . }}</h1>{{ end }}
{{ with .Data.Title }}<h1>{{ mdtitle . }}</h1>{{ end }}
{{ with .Data.Description }}{{ md . }}{{ end }}
<p><a href="{{ .Blog.RelativePath .Data.Download }}" class="button" download>{{ string .Blog.Lang "download" }}</a></p>
{{ $lang := .Blog.Lang }}

View File

@ -1,10 +1,10 @@
{{ define "title" }}
<title>{{ with .Blog.BlogStats.Title }}{{ mdtext . }} - {{ end }}{{ mdtext .Blog.Title }}</title>
<title>{{ with .Blog.BlogStats.Title }}{{ mdtitle . }} - {{ end }}{{ mdtitle .Blog.Title }}</title>
{{ end }}
{{ define "main" }}
<main>
{{ with .Blog.BlogStats.Title }}<h1>{{ mdtext . }}</h1>{{ end }}
{{ with .Blog.BlogStats.Title }}<h1>{{ mdtitle . }}</h1>{{ end }}
{{ with .Blog.BlogStats.Description }}{{ md . }}{{ end }}
<p id="loading" data-table="{{.Data.TableUrl}}">{{ string .Blog.Lang "loading" }}</p>
<script defer src="{{ asset "js/blogstats.js" }}"></script>

View File

@ -1,5 +1,5 @@
{{ define "title" }}
<title>{{ string .Blog.Lang "captcha" }} - {{ mdtext .Blog.Title }}</title>
<title>{{ string .Blog.Lang "captcha" }} - {{ mdtitle .Blog.Title }}</title>
{{ end }}
{{ define "main" }}

View File

@ -1,5 +1,5 @@
{{ define "title" }}
<title>{{ string .Blog.Lang "comments" }} - {{ mdtext .Blog.Title }}</title>
<title>{{ string .Blog.Lang "comments" }} - {{ mdtitle .Blog.Title }}</title>
{{ end }}
{{ define "main" }}

View File

@ -7,7 +7,7 @@
{{ if .Data.sent }}
<p>{{ string .Blog.Lang "messagesent" }}</p>
{{ else }}
{{ with .Data.title }}<h1>{{ mdtext . }}</h1>{{ end }}
{{ with .Data.title }}<h1>{{ mdtitle . }}</h1>{{ end }}
{{ with .Data.description }}{{ md . }}{{ end }}
<form class="fw p" method="post">
<input type="text" name="name" placeholder="{{ string .Blog.Lang "nameopt" }}">

View File

@ -1,5 +1,5 @@
{{ define "title" }}
<title>{{ string .Blog.Lang "editor" }} - {{ mdtext .Blog.Title }}</title>
<title>{{ string .Blog.Lang "editor" }} - {{ mdtitle .Blog.Title }}</title>
{{ end }}
{{ define "main" }}

View File

@ -1,5 +1,5 @@
{{ define "title" }}
<title>{{ string .Blog.Lang "mediafiles" }} - {{ mdtext .Blog.Title }}</title>
<title>{{ string .Blog.Lang "mediafiles" }} - {{ mdtitle .Blog.Title }}</title>
{{ end }}
{{ define "main" }}

View File

@ -1,10 +1,10 @@
{{ define "footer" }}
<footer>
{{ with index .Blog.Menus "footer" }}
<nav>{{ range $i, $item := .Items }}{{ if ne $i 0 }} &bull; {{ end }}<a href="{{ $item.Link }}">{{ mdtext $item.Title }}</a>{{ end }}
<nav>{{ range $i, $item := .Items }}{{ if ne $i 0 }} &bull; {{ end }}<a href="{{ $item.Link }}">{{ mdtitle $item.Title }}</a>{{ end }}
</nav>
{{ end }}
<p translate="no">&copy; {{ dateformat now "2006" }} {{ with .User.Name }}{{ . }}{{ else }}{{ mdtext .Blog.Title }}{{ end }}</p>
<p translate="no">&copy; {{ dateformat now "2006" }} {{ with .User.Name }}{{ . }}{{ else }}{{ mdtitle .Blog.Title }}{{ end }}</p>
{{ if .TorUsed }}
<p id="tor">🔐 {{ string .Blog.Lang "connectedviator" }}</p>
{{ else }}

View File

@ -1,5 +1,5 @@
{{ define "title" }}
<title>{{ mdtext .Blog.Title }}</title>
<title>{{ mdtitle .Blog.Title }}</title>
{{ if not .Data.nolocations }}
<link rel="stylesheet" href="{{ .Data.mappath }}/leaflet/leaflet.css"/>
<script src="{{ .Data.mappath }}/leaflet/leaflet.js"></script>

View File

@ -1,10 +1,10 @@
{{ define "header" }}
<header>
<h1><a href="{{ .Blog.RelativePath "/" }}" rel="home" title="{{ mdtext .Blog.Title }}" translate="no">{{ mdtext .Blog.Title }}</a></h1>
<h1><a href="{{ .Blog.RelativePath "/" }}" rel="home" title="{{ mdtitle .Blog.Title }}" translate="no">{{ mdtitle .Blog.Title }}</a></h1>
{{ with .Blog.Description }}<p><i>{{ . }}</i></p>{{ end }}
<nav>
{{ with index .Blog.Menus "main" }}
{{ range $i, $item := .Items }}{{ if ne $i 0 }} &bull; {{ end }}<a href="{{ $item.Link }}">{{ mdtext $item.Title }}</a>{{ end }}
{{ range $i, $item := .Items }}{{ if ne $i 0 }} &bull; {{ end }}<a href="{{ $item.Link }}">{{ mdtitle $item.Title }}</a>{{ end }}
{{ end }}
</nav>
{{ if .LoggedIn }}

View File

@ -1,13 +1,13 @@
{{ define "title" }}
<title>{{ with .Data.Title }}{{ mdtext . }} - {{ end }}{{ mdtext .Blog.Title }}</title>
<link rel="alternate" type="application/rss+xml" title="RSS{{ with .Data.Title }} ({{ mdtext . }}){{ end }}" href="{{ .Data.First }}.rss"/>
<link rel="alternate" type="application/atom+xml" title="Atom{{ with .Data.Title }} ({{ mdtext . }}){{ end }}" href="{{ .Data.First }}.atom"/>
<link rel="alternate" type="application/feed+json" title="JSON Feed{{ with .Data.Title }} ({{ mdtext . }}){{ end }}" href="{{ .Data.First }}.json"/>
<title>{{ with .Data.Title }}{{ mdtitle . }} - {{ end }}{{ mdtitle .Blog.Title }}</title>
<link rel="alternate" type="application/rss+xml" title="RSS{{ with .Data.Title }} ({{ mdtitle . }}){{ end }}" href="{{ .Data.First }}.rss"/>
<link rel="alternate" type="application/atom+xml" title="Atom{{ with .Data.Title }} ({{ mdtitle . }}){{ end }}" href="{{ .Data.First }}.atom"/>
<link rel="alternate" type="application/feed+json" title="JSON Feed{{ with .Data.Title }} ({{ mdtitle . }}){{ end }}" href="{{ .Data.First }}.json"/>
{{ end }}
{{ define "main" }}
<main class="h-feed">
{{ with .Data.Title }}<h1 class="p-name">{{ mdtext . }}</h1>{{ end }}
{{ with .Data.Title }}<h1 class="p-name">{{ mdtitle . }}</h1>{{ end }}
{{ with .Data.Description }}{{ md . }}{{ end }}
{{ if (or .Data.Title .Data.Description) }}
<hr>

View File

@ -1,5 +1,5 @@
{{ define "title" }}
<title>{{ string .Blog.Lang "login" }} - {{ mdtext .Blog.Title }}</title>
<title>{{ string .Blog.Lang "login" }} - {{ mdtitle .Blog.Title }}</title>
{{ end }}
{{ define "main" }}

View File

@ -1,5 +1,5 @@
{{ define "title" }}
<title>{{ string "notifications" }} - {{ mdtext .Blog.Title }}</title>
<title>{{ string "notifications" }} - {{ mdtitle .Blog.Title }}</title>
{{ end }}
{{ define "main" }}

View File

@ -4,7 +4,7 @@
{{ if .Data.Title }}
<h2 class="p-name">
<a class="u-url" href="{{ .Data.Path }}">
{{ mdtext .Data.Title }}
{{ mdtitle .Data.Title }}
</a>
</h2>
{{ end }}

View File

@ -1,5 +1,5 @@
{{ define "title" }}
<title>{{ with .Data.Title }}{{ mdtext . }} - {{end}}{{ mdtext .Blog.Title }}</title>
<title>{{ with .Data.Title }}{{ mdtitle . }} - {{end}}{{ mdtitle .Blog.Title }}</title>
{{ include "postheadmeta" . }}
{{ with shorturl .Data }}<link rel="shortlink" href="{{ . }}">{{ end }}
{{ end }}
@ -8,7 +8,7 @@
<main class=h-entry>
<article>
<data class="u-url hide" value="{{ absolute .Data.Path }}"></data>
{{ with .Data.Title }}<h1 class=p-name>{{ mdtext . }}</h1>{{ end }}
{{ with .Data.Title }}<h1 class=p-name>{{ mdtitle . }}</h1>{{ end }}
{{ include "postmeta" . }}
{{ include "postactions" . }}
{{ if .Data.Content }}

View File

@ -1,6 +1,6 @@
{{ define "postactions" }}
<div class="p flex" id="post-actions">
<a href="https://www.addtoany.com/share#url={{ absolute .Data.Path }}{{ with .Data.Title }}&title={{ mdtext . }}{{ end }}" target="_blank" rel="nofollow noopener noreferrer" class="button">{{ string .Blog.Lang "share" }}</a>&nbsp;
<a href="https://www.addtoany.com/share#url={{ absolute .Data.Path }}{{ with .Data.Title }}&title={{ mdtitle . }}{{ end }}" target="_blank" rel="nofollow noopener noreferrer" class="button">{{ string .Blog.Lang "share" }}</a>&nbsp;
<a id="translateBtn" href="https://translate.google.com/translate?u={{ absolute .Data.Path }}" target="_blank" rel="nofollow noopener noreferrer" class="button">{{ string .Blog.Lang "translate" }}</a>&nbsp;
<script defer src="{{ asset "js/translate.js" }}"></script>
<button id="speakBtn" class="hide" data-speak="{{ string .Blog.Lang "speak" }}" data-stopspeak="{{ string .Blog.Lang "stopspeak" }}"></button>

View File

@ -4,8 +4,8 @@
<meta property="twitter:url" content="{{ . }}">
{{ end }}
{{ with .Data.Title }}
<meta property="og:title" content="{{ mdtext . }}">
<meta property="twitter:title" content="{{ mdtext . }}">
<meta property="og:title" content="{{ mdtitle . }}">
<meta property="twitter:title" content="{{ mdtitle . }}">
{{ end }}
{{ with summary .Data }}
<meta name="description" content="{{ . }}">

View File

@ -3,7 +3,7 @@
{{ include "summaryandpostmeta" . }}
{{ $translations := (translations .Data) }}
{{ if gt (len $translations) 0 }}
<div>{{ string .Blog.Lang "translations" }}: {{ $delimiter := "" }}{{ range $i, $t := $translations }}{{ $delimiter }}<a href="{{ $t.Path }}" translate="no">{{ mdtext $t.Title }}</a>{{ $delimiter = ", " }}{{ end }}</div>
<div>{{ string .Blog.Lang "translations" }}: {{ $delimiter := "" }}{{ range $i, $t := $translations }}{{ $delimiter }}<a href="{{ $t.Path }}" translate="no">{{ mdtitle $t.Title }}</a>{{ $delimiter = ", " }}{{ end }}</div>
{{ end }}
{{ $short := shorturl .Data }}
{{ if $short }}<div>{{ string .Blog.Lang "shorturl" }} <a href="{{ $short }}" rel="shortlink">{{ $short }}</a></div>{{ end }}

View File

@ -4,9 +4,9 @@
{{ range $i, $tax := $blog.Taxonomies }}
{{ $tvs := sort (ps $post $tax.Name) }}
{{ if $tvs }}
<p><b>{{ mdtext $tax.Title }}</b>:
<p><b>{{ mdtitle $tax.Title }}</b>:
{{ range $j, $tv := $tvs }}
<a class="p-category" rel="tag" href="{{ $blog.RelativePath ( printf "/%s/%s" $tax.Name (urlize $tv) ) }}">{{ mdtext $tv }}</a>
<a class="p-category" rel="tag" href="{{ $blog.RelativePath ( printf "/%s/%s" $tax.Name (urlize $tv) ) }}">{{ mdtitle $tv }}</a>
{{ end }}
</p>
{{ end }}

View File

@ -1,16 +1,16 @@
{{ define "title" }}
<title>{{ with .Blog.Search.Title }}{{ mdtext . }} - {{ end }}{{ mdtext .Blog.Title }}</title>
<title>{{ with .Blog.Search.Title }}{{ mdtitle . }} - {{ end }}{{ mdtitle .Blog.Title }}</title>
{{ end }}
{{ define "main" }}
<main>
{{ with .Blog.Search.Title }}<h1>{{ mdtext . }}</h1>{{ end }}
{{ with .Blog.Search.Title }}<h1>{{ mdtitle . }}</h1>{{ end }}
{{ with .Blog.Search.Description }}{{ md . }}{{ end }}
{{ if (or .Blog.Search.Title .Blog.Search.Description) }}
<hr>
{{ end }}
<form class="fw p" method="post">
<input type="text" name="q" {{ with .Blog.Search.Placeholder }}placeholder="{{ mdtext . }}"{{ end }}>
<input type="text" name="q" {{ with .Blog.Search.Placeholder }}placeholder="{{ mdtitle . }}"{{ end }}>
<input type="submit" value="🔍 {{ string .Blog.Lang "search" }}">
</form>
</main>

View File

@ -1,5 +1,5 @@
{{ define "title" }}
<title>{{ mdtext .Blog.Title }}</title>
<title>{{ mdtitle .Blog.Title }}</title>
{{ include "postheadmeta" . }}
{{ end }}

View File

@ -4,7 +4,7 @@
{{ if .Data.Title }}
<h2 class="p-name">
<a class="u-url" href="{{ .Data.Path }}">
{{ mdtext .Data.Title }}
{{ mdtitle .Data.Title }}
</a>
</h2>
{{ end }}

View File

@ -1,6 +1,6 @@
{{ define "summaryandpostmeta" }}
{{ $section := (index .Blog.Sections .Data.Section) }}
{{ if .Data.Published }}<div>{{ string .Blog.Lang "publishedon" }} <time class="dt-published" datetime="{{ dateformat .Data.Published "2006-01-02T15:04:05Z07:00"}}">{{ isodate .Data.Published }}</time>{{ if $section }} in <a href="{{ .Blog.RelativePath $section.Name }}">{{ mdtext $section.Title }}</a>{{ end }}</div>{{ end }}
{{ if .Data.Published }}<div>{{ string .Blog.Lang "publishedon" }} <time class="dt-published" datetime="{{ dateformat .Data.Published "2006-01-02T15:04:05Z07:00"}}">{{ isodate .Data.Published }}</time>{{ if $section }} in <a href="{{ .Blog.RelativePath $section.Name }}">{{ mdtitle $section.Title }}</a>{{ end }}</div>{{ end }}
{{ if .Data.Updated }}<div>{{ string .Blog.Lang "updatedon" }} <time class="dt-updated" datetime="{{ dateformat .Data.Updated "2006-01-02T15:04:05Z07:00"}}">{{ isodate .Data.Updated }}</time></div>{{ end }}
{{ if replylink .Data }}
<div>{{ string .Blog.Lang "replyto" }}: <a class="u-in-reply-to" href="{{ replylink .Data }}" target="_blank" rel="noopener">{{ with (replytitle .Data) }}{{ . }}{{ else }}{{ replylink .Data }}{{ end }}</a></div>

View File

@ -1,10 +1,10 @@
{{ define "title" }}
<title>{{ mdtext .Data.Taxonomy.Title }} - {{ mdtext .Blog.Title }}</title>
<title>{{ mdtitle .Data.Taxonomy.Title }} - {{ mdtitle .Blog.Title }}</title>
{{ end }}
{{ define "main" }}
<main>
{{ with .Data.Taxonomy.Title }}<h1>{{ mdtext . }}</h1>{{ end }}
{{ with .Data.Taxonomy.Title }}<h1>{{ mdtitle . }}</h1>{{ end }}
{{ with .Data.Taxonomy.Description }}{{ md . }}{{ end }}
{{ $blog := .Blog }}
{{ $taxonomy := .Data.Taxonomy.Name }}

View File

@ -1,5 +1,5 @@
{{ define "title" }}
<title>{{ string .Blog.Lang "webmentions" }} - {{ mdtext .Blog.Title }}</title>
<title>{{ string .Blog.Lang "webmentions" }} - {{ mdtitle .Blog.Title }}</title>
{{ end }}
{{ define "main" }}