Merge pull request #471 from writefreely/fix-title-lists

Don't render title as list item
This commit is contained in:
Matt Baer 2021-06-29 10:52:39 -04:00 committed by GitHub
commit 273c9cf418
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 1 deletions

View File

@ -11,6 +11,7 @@
package writefreely
import (
"bytes"
"encoding/json"
"fmt"
"html"
@ -181,6 +182,10 @@ func applyMarkdownSpecial(data []byte, skipNoFollow bool, baseURL string, cfg *c
}
func applyBasicMarkdown(data []byte) string {
if len(bytes.TrimSpace(data)) == 0 {
return ""
}
mdExtensions := 0 |
blackfriday.EXTENSION_STRIKETHROUGH |
blackfriday.EXTENSION_SPACE_HEADERS |
@ -191,7 +196,12 @@ func applyBasicMarkdown(data []byte) string {
blackfriday.HTML_SMARTYPANTS_DASHES
// Generate Markdown
md := blackfriday.Markdown([]byte(data), blackfriday.HtmlRenderer(htmlFlags, "", ""), mdExtensions)
// This passes the supplied title into blackfriday.Markdown() as an H1 header, so we only render HTML that
// belongs in an H1.
md := blackfriday.Markdown(append([]byte("# "), data...), blackfriday.HtmlRenderer(htmlFlags, "", ""), mdExtensions)
// Remove H1 markup
md = bytes.TrimSpace(md) // blackfriday.Markdown adds a newline at the end of the <h1>
md = md[len("<h1>") : len(md)-len("</h1>")]
// Strip out bad HTML
policy := bluemonday.UGCPolicy()
policy.AllowAttrs("class", "id").Globally()

43
postrender_test.go Normal file
View File

@ -0,0 +1,43 @@
/*
* Copyright © 2021 A Bunch Tell LLC.
*
* This file is part of WriteFreely.
*
* WriteFreely is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, included
* in the LICENSE file in this source code package.
*/
package writefreely
import "testing"
func TestApplyBasicMarkdown(t *testing.T) {
tests := []struct {
name string
in string
result string
}{
{"empty", "", ""},
{"empty spaces", " ", ""},
{"empty tabs", "\t", ""},
{"empty newline", "\n", ""},
{"nums", "123", "123"},
{"dot", ".", "."},
{"dash", "-", "-"},
{"plain", "Hello, World!", "Hello, World!"},
{"multibyte", "こんにちは", `こんにちは`},
{"bold", "**안녕하세요**", `<strong>안녕하세요</strong>`},
{"link", "[WriteFreely](https://writefreely.org)", `<a href="https://writefreely.org" rel="nofollow">WriteFreely</a>`},
{"date", "12. April", `12. April`},
{"table", "| Hi | There |", `| Hi | There |`},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
res := applyBasicMarkdown([]byte(test.in))
if res != test.result {
t.Errorf("%s: wanted %s, got %s", test.name, test.result, res)
}
})
}
}