[bugfix] Markdown format fixes (#718)

* just sanitize markdown, don't minify or escape

* tidy tests, add one for inline code

* add another test, it works!
This commit is contained in:
tobi 2022-07-19 10:41:16 +02:00 committed by GitHub
parent c3b6a5b0f9
commit 59be7466f3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 20 deletions

View file

@ -37,5 +37,5 @@ func (f *formatter) FromMarkdown(ctx context.Context, md string, mentions []*gts
// format mentions nicely // format mentions nicely
content = f.ReplaceMentions(ctx, content, mentions) content = f.ReplaceMentions(ctx, content, mentions)
return postformat(content) return SanitizeHTML(content)
} }

View file

@ -20,30 +20,13 @@
import ( import (
"context" "context"
"fmt"
"testing" "testing"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
) )
const ( var withCodeBlock = `# Title
simpleMarkdown = `# Title
Here's a simple text in markdown.
Here's a [link](https://example.org).`
simpleMarkdownExpected = "<h1>Title</h1><p>Heres a simple text in markdown.</p><p>Heres a <a href=\"https://example.org\" rel=\"nofollow noreferrer noopener\" target=\"_blank\">link</a>.</p>"
withCodeBlockExpected = "<h1>Title</h1><p>Below is some JSON.</p><pre><code class=\"language-json\">{\n \"key\": \"value\",\n \"another_key\": [\n \"value1\",\n \"value2\"\n ]\n}\n</code></pre><p>that was some JSON :)</p>"
withHashtag = "# Title\n\nhere's a simple status that uses hashtag #Hashtag!"
withHashtagExpected = "<h1>Title</h1><p>heres a simple status that uses hashtag <a href=\"http://localhost:8080/tags/Hashtag\" class=\"mention hashtag\" rel=\"tag nofollow noreferrer noopener\" target=\"_blank\">#<span>Hashtag</span></a>!</p>"
)
var (
withCodeBlock = `# Title
Below is some JSON. Below is some JSON.
@ -59,6 +42,17 @@
that was some JSON :) that was some JSON :)
` `
const (
simpleMarkdown = "# Title\n\nHere's a simple text in markdown.\n\nHere's a [link](https://example.org)."
simpleMarkdownExpected = "<h1>Title</h1>\n\n<p>Heres a simple text in markdown.</p>\n\n<p>Heres a <a href=\"https://example.org\" rel=\"nofollow noreferrer noopener\" target=\"_blank\">link</a>.</p>\n"
withCodeBlockExpected = "<h1>Title</h1>\n\n<p>Below is some JSON.</p>\n\n<pre><code class=\"language-json\">{\n &#34;key&#34;: &#34;value&#34;,\n &#34;another_key&#34;: [\n &#34;value1&#34;,\n &#34;value2&#34;\n ]\n}\n</code></pre>\n\n<p>that was some JSON :)</p>\n"
withInlineCode = "`Nobody tells you about the <code><del>SECRET CODE</del></code>, do they?`"
withInlineCodeExpected = "<p><code>Nobody tells you about the &lt;code&gt;&lt;del&gt;SECRET CODE&lt;/del&gt;&lt;/code&gt;, do they?</code></p>\n"
withInlineCode2 = "`Nobody tells you about the </code><del>SECRET CODE</del><code>, do they?`"
withInlineCode2Expected = "<p><code>Nobody tells you about the &lt;/code&gt;&lt;del&gt;SECRET CODE&lt;/del&gt;&lt;code&gt;, do they?</code></p>\n"
withHashtag = "# Title\n\nhere's a simple status that uses hashtag #Hashtag!"
withHashtagExpected = "<h1>Title</h1>\n\n<p>heres a simple status that uses hashtag <a href=\"http://localhost:8080/tags/Hashtag\" class=\"mention hashtag\" rel=\"tag nofollow noreferrer noopener\" target=\"_blank\">#<span>Hashtag</span></a>!</p>\n"
) )
type MarkdownTestSuite struct { type MarkdownTestSuite struct {
@ -71,11 +65,20 @@ func (suite *MarkdownTestSuite) TestParseSimple() {
} }
func (suite *MarkdownTestSuite) TestParseWithCodeBlock() { func (suite *MarkdownTestSuite) TestParseWithCodeBlock() {
fmt.Println(withCodeBlock)
s := suite.formatter.FromMarkdown(context.Background(), withCodeBlock, nil, nil) s := suite.formatter.FromMarkdown(context.Background(), withCodeBlock, nil, nil)
suite.Equal(withCodeBlockExpected, s) suite.Equal(withCodeBlockExpected, s)
} }
func (suite *MarkdownTestSuite) TestParseWithInlineCode() {
s := suite.formatter.FromMarkdown(context.Background(), withInlineCode, nil, nil)
suite.Equal(withInlineCodeExpected, s)
}
func (suite *MarkdownTestSuite) TestParseWithInlineCode2() {
s := suite.formatter.FromMarkdown(context.Background(), withInlineCode2, nil, nil)
suite.Equal(withInlineCode2Expected, s)
}
func (suite *MarkdownTestSuite) TestParseWithHashtag() { func (suite *MarkdownTestSuite) TestParseWithHashtag() {
foundTags := []*gtsmodel.Tag{ foundTags := []*gtsmodel.Tag{
suite.testTags["Hashtag"], suite.testTags["Hashtag"],