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