From 0ddca40529c5df6dfa629b7dfa7790c8467e557e Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Tue, 22 Jun 2021 16:06:04 -0400 Subject: [PATCH 1/3] Don't render title as list item This fixes an issue where "12. April" would get rendered as "1. April" because it looks like a Markdown list item to our renderer. Now, we parse titles as titles, instead of standalone text, which causes the renderer to give us the results we want. This also adds some basic tests for the applyBasicMarkdown() func. Closes #470 --- postrender.go | 8 +++++++- postrender_test.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 postrender_test.go diff --git a/postrender.go b/postrender.go index 8e71109..0992d30 100644 --- a/postrender.go +++ b/postrender.go @@ -11,6 +11,7 @@ package writefreely import ( + "bytes" "encoding/json" "fmt" "html" @@ -191,7 +192,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

+ md = md[len("

") : len(md)-len("

")] // Strip out bad HTML policy := bluemonday.UGCPolicy() policy.AllowAttrs("class", "id").Globally() diff --git a/postrender_test.go b/postrender_test.go new file mode 100644 index 0000000..240e755 --- /dev/null +++ b/postrender_test.go @@ -0,0 +1,36 @@ +/* + * 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 + }{ + {"plain", "Hello, World!", "Hello, World!"}, + {"multibyte", "こんにちは", `こんにちは`}, + {"bold", "**안녕하세요**", `안녕하세요`}, + {"link", "[WriteFreely](https://writefreely.org)", `WriteFreely`}, + {"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) + } + }) + } +} From f933b3617086a7995e81855be14576cfc51d8f43 Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Wed, 23 Jun 2021 17:38:22 -0400 Subject: [PATCH 2/3] Prevent out of bounds error when post has no title --- postrender.go | 4 ++++ postrender_test.go | 1 + 2 files changed, 5 insertions(+) diff --git a/postrender.go b/postrender.go index 0992d30..1cb4f0e 100644 --- a/postrender.go +++ b/postrender.go @@ -182,6 +182,10 @@ func applyMarkdownSpecial(data []byte, skipNoFollow bool, baseURL string, cfg *c } func applyBasicMarkdown(data []byte) string { + if len(data) == 0 { + return "" + } + mdExtensions := 0 | blackfriday.EXTENSION_STRIKETHROUGH | blackfriday.EXTENSION_SPACE_HEADERS | diff --git a/postrender_test.go b/postrender_test.go index 240e755..262d550 100644 --- a/postrender_test.go +++ b/postrender_test.go @@ -18,6 +18,7 @@ func TestApplyBasicMarkdown(t *testing.T) { in string result string }{ + {"empty", "", ""}, {"plain", "Hello, World!", "Hello, World!"}, {"multibyte", "こんにちは", `こんにちは`}, {"bold", "**안녕하세요**", `안녕하세요`}, From d37ab544e814b663bede266fa9ae83adf5e67d81 Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Fri, 25 Jun 2021 17:07:42 -0400 Subject: [PATCH 3/3] Prevent out of bounds error on title with only whitespace --- postrender.go | 2 +- postrender_test.go | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/postrender.go b/postrender.go index 1cb4f0e..369ce51 100644 --- a/postrender.go +++ b/postrender.go @@ -182,7 +182,7 @@ func applyMarkdownSpecial(data []byte, skipNoFollow bool, baseURL string, cfg *c } func applyBasicMarkdown(data []byte) string { - if len(data) == 0 { + if len(bytes.TrimSpace(data)) == 0 { return "" } diff --git a/postrender_test.go b/postrender_test.go index 262d550..531c0f1 100644 --- a/postrender_test.go +++ b/postrender_test.go @@ -19,6 +19,12 @@ func TestApplyBasicMarkdown(t *testing.T) { result string }{ {"empty", "", ""}, + {"empty spaces", " ", ""}, + {"empty tabs", "\t", ""}, + {"empty newline", "\n", ""}, + {"nums", "123", "123"}, + {"dot", ".", "."}, + {"dash", "-", "-"}, {"plain", "Hello, World!", "Hello, World!"}, {"multibyte", "こんにちは", `こんにちは`}, {"bold", "**안녕하세요**", `안녕하세요`},