mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2024-11-29 07:02:45 +00:00
[feature] Allow footnotes in markdown, use <br>
instead of \n
(#767)
* allow markdown footnotes + hard line breaks * don't keep whitespace w/minify (unnecessary now) * test markdown a bit more
This commit is contained in:
parent
2fe3a2b5b8
commit
79fb8bad04
|
@ -28,7 +28,10 @@
|
||||||
"github.com/tdewolff/minify/v2/html"
|
"github.com/tdewolff/minify/v2/html"
|
||||||
)
|
)
|
||||||
|
|
||||||
var m *minify.M
|
var (
|
||||||
|
bfExtensions = blackfriday.CommonExtensions | blackfriday.HardLineBreak | blackfriday.Footnotes
|
||||||
|
m *minify.M
|
||||||
|
)
|
||||||
|
|
||||||
func (f *formatter) FromMarkdown(ctx context.Context, md string, mentions []*gtsmodel.Mention, tags []*gtsmodel.Tag) string {
|
func (f *formatter) FromMarkdown(ctx context.Context, md string, mentions []*gtsmodel.Mention, tags []*gtsmodel.Tag) string {
|
||||||
// format tags nicely
|
// format tags nicely
|
||||||
|
@ -38,7 +41,7 @@ func (f *formatter) FromMarkdown(ctx context.Context, md string, mentions []*gts
|
||||||
content = f.ReplaceMentions(ctx, content, mentions)
|
content = f.ReplaceMentions(ctx, content, mentions)
|
||||||
|
|
||||||
// parse markdown
|
// parse markdown
|
||||||
contentBytes := blackfriday.Run([]byte(content))
|
contentBytes := blackfriday.Run([]byte(content), blackfriday.WithExtensions(bfExtensions))
|
||||||
|
|
||||||
// clean anything dangerous out of it
|
// clean anything dangerous out of it
|
||||||
content = SanitizeHTML(string(contentBytes))
|
content = SanitizeHTML(string(contentBytes))
|
||||||
|
@ -46,9 +49,8 @@ func (f *formatter) FromMarkdown(ctx context.Context, md string, mentions []*gts
|
||||||
if m == nil {
|
if m == nil {
|
||||||
m = minify.New()
|
m = minify.New()
|
||||||
m.Add("text/html", &html.Minifier{
|
m.Add("text/html", &html.Minifier{
|
||||||
KeepEndTags: true,
|
KeepEndTags: true,
|
||||||
KeepQuotes: true,
|
KeepQuotes: true,
|
||||||
KeepWhitespace: true,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -56,11 +56,15 @@
|
||||||
mdWithHTML = "# Title\n\nHere's a simple text in markdown.\n\nHere's a <a href=\"https://example.org\">link</a>.\n\nHere's an image: <img src=\"https://gts.superseriousbusiness.org/assets/logo.png\" alt=\"The GoToSocial sloth logo.\" width=\"500\" height=\"600\">"
|
mdWithHTML = "# Title\n\nHere's a simple text in markdown.\n\nHere's a <a href=\"https://example.org\">link</a>.\n\nHere's an image: <img src=\"https://gts.superseriousbusiness.org/assets/logo.png\" alt=\"The GoToSocial sloth logo.\" width=\"500\" height=\"600\">"
|
||||||
mdWithHTMLExpected = "<h1>Title</h1><p>Here’s a simple text in markdown.</p><p>Here’s a <a href=\"https://example.org\" rel=\"nofollow noreferrer noopener\" target=\"_blank\">link</a>.</p><p>Here’s an image: <img src=\"https://gts.superseriousbusiness.org/assets/logo.png\" alt=\"The GoToSocial sloth logo.\" width=\"500\" height=\"600\" crossorigin=\"anonymous\"></p>"
|
mdWithHTMLExpected = "<h1>Title</h1><p>Here’s a simple text in markdown.</p><p>Here’s a <a href=\"https://example.org\" rel=\"nofollow noreferrer noopener\" target=\"_blank\">link</a>.</p><p>Here’s an image: <img src=\"https://gts.superseriousbusiness.org/assets/logo.png\" alt=\"The GoToSocial sloth logo.\" width=\"500\" height=\"600\" crossorigin=\"anonymous\"></p>"
|
||||||
mdWithCheekyHTML = "# Title\n\nHere's a simple text in markdown.\n\nHere's a cheeky little script: <script>alert(ahhhh)</script>"
|
mdWithCheekyHTML = "# Title\n\nHere's a simple text in markdown.\n\nHere's a cheeky little script: <script>alert(ahhhh)</script>"
|
||||||
mdWithCheekyHTMLExpected = "<h1>Title</h1><p>Here’s a simple text in markdown.</p><p>Here’s a cheeky little script: </p>"
|
mdWithCheekyHTMLExpected = "<h1>Title</h1><p>Here’s a simple text in markdown.</p><p>Here’s a cheeky little script:</p>"
|
||||||
mdWithHashtagInitial = "#welcome #Hashtag"
|
mdWithHashtagInitial = "#welcome #Hashtag"
|
||||||
mdWithHashtagInitialExpected = "<p><a href=\"http://localhost:8080/tags/welcome\" class=\"mention hashtag\" rel=\"tag nofollow noreferrer noopener\" target=\"_blank\">#<span>welcome</span></a> <a href=\"http://localhost:8080/tags/Hashtag\" class=\"mention hashtag\" rel=\"tag nofollow noreferrer noopener\" target=\"_blank\">#<span>Hashtag</span></a></p>"
|
mdWithHashtagInitialExpected = "<p><a href=\"http://localhost:8080/tags/welcome\" class=\"mention hashtag\" rel=\"tag nofollow noreferrer noopener\" target=\"_blank\">#<span>welcome</span></a> <a href=\"http://localhost:8080/tags/Hashtag\" class=\"mention hashtag\" rel=\"tag nofollow noreferrer noopener\" target=\"_blank\">#<span>Hashtag</span></a></p>"
|
||||||
mdCodeBlockWithNewlines = "some code coming up\n\n```\n\n\n\n```\nthat was some code"
|
mdCodeBlockWithNewlines = "some code coming up\n\n```\n\n\n\n```\nthat was some code"
|
||||||
mdCodeBlockWithNewlinesExpected = "<p>some code coming up</p><pre><code>\n\n\n</code></pre><p>that was some code</p>"
|
mdCodeBlockWithNewlinesExpected = "<p>some code coming up</p><pre><code>\n\n\n</code></pre><p>that was some code</p>"
|
||||||
|
mdWithFootnote = "fox mulder,fbi.[^1]\n\n[^1]: federated bureau of investigation"
|
||||||
|
mdWithFootnoteExpected = "<p>fox mulder,fbi.<sup id=\"fnref:1\"><a href=\"#fn:1\" rel=\"nofollow noreferrer\">1</a></sup></p><div><hr><ol><li id=\"fn:1\">federated bureau of investigation<br></li></ol></div>"
|
||||||
|
mdWithBlockQuote = "get ready, there's a block quote coming:\n\n>line1\n>line2\n>\n>line3\n\n"
|
||||||
|
mdWithBlockQuoteExpected = "<p>get ready, there’s a block quote coming:</p><blockquote><p>line1<br>line2</p><p>line3</p></blockquote>"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MarkdownTestSuite struct {
|
type MarkdownTestSuite struct {
|
||||||
|
@ -119,6 +123,16 @@ func (suite *MarkdownTestSuite) TestParseCodeBlockWithNewlines() {
|
||||||
suite.Equal(mdCodeBlockWithNewlinesExpected, s)
|
suite.Equal(mdCodeBlockWithNewlinesExpected, s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (suite *MarkdownTestSuite) TestParseWithFootnote() {
|
||||||
|
s := suite.formatter.FromMarkdown(context.Background(), mdWithFootnote, nil, nil)
|
||||||
|
suite.Equal(mdWithFootnoteExpected, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *MarkdownTestSuite) TestParseWithBlockquote() {
|
||||||
|
s := suite.formatter.FromMarkdown(context.Background(), mdWithBlockQuote, nil, nil)
|
||||||
|
suite.Equal(mdWithBlockQuoteExpected, s)
|
||||||
|
}
|
||||||
|
|
||||||
func TestMarkdownTestSuite(t *testing.T) {
|
func TestMarkdownTestSuite(t *testing.T) {
|
||||||
suite.Run(t, new(MarkdownTestSuite))
|
suite.Run(t, new(MarkdownTestSuite))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue