From 59be7466f365266bb643e2f5b6b9ce2c4701b945 Mon Sep 17 00:00:00 2001 From: tobi <31960611+tsmethurst@users.noreply.github.com> Date: Tue, 19 Jul 2022 10:41:16 +0200 Subject: [PATCH] [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! --- internal/text/markdown.go | 2 +- internal/text/markdown_test.go | 41 ++++++++++++++++++---------------- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/internal/text/markdown.go b/internal/text/markdown.go index be094afd2..01238954f 100644 --- a/internal/text/markdown.go +++ b/internal/text/markdown.go @@ -37,5 +37,5 @@ func (f *formatter) FromMarkdown(ctx context.Context, md string, mentions []*gts // format mentions nicely content = f.ReplaceMentions(ctx, content, mentions) - return postformat(content) + return SanitizeHTML(content) } diff --git a/internal/text/markdown_test.go b/internal/text/markdown_test.go index e086f14f7..111cfe473 100644 --- a/internal/text/markdown_test.go +++ b/internal/text/markdown_test.go @@ -20,30 +20,13 @@ package text_test import ( "context" - "fmt" "testing" "github.com/stretchr/testify/suite" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" ) -const ( - simpleMarkdown = `# Title - -Here's a simple text in markdown. - -Here's a [link](https://example.org).` - - simpleMarkdownExpected = "

Title

Here’s a simple text in markdown.

Here’s a link.

" - - withCodeBlockExpected = "

Title

Below is some JSON.

{\n  \"key\": \"value\",\n  \"another_key\": [\n    \"value1\",\n    \"value2\"\n  ]\n}\n

that was some JSON :)

" - - withHashtag = "# Title\n\nhere's a simple status that uses hashtag #Hashtag!" - withHashtagExpected = "

Title

here’s a simple status that uses hashtag #Hashtag!

" -) - -var ( - withCodeBlock = `# Title +var withCodeBlock = `# Title Below is some JSON. @@ -59,6 +42,17 @@ Below is 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 = "

Title

\n\n

Here’s a simple text in markdown.

\n\n

Here’s a link.

\n" + withCodeBlockExpected = "

Title

\n\n

Below is some JSON.

\n\n
{\n  "key": "value",\n  "another_key": [\n    "value1",\n    "value2"\n  ]\n}\n
\n\n

that was some JSON :)

\n" + withInlineCode = "`Nobody tells you about the SECRET CODE, do they?`" + withInlineCodeExpected = "

Nobody tells you about the <code><del>SECRET CODE</del></code>, do they?

\n" + withInlineCode2 = "`Nobody tells you about the SECRET CODE, do they?`" + withInlineCode2Expected = "

Nobody tells you about the </code><del>SECRET CODE</del><code>, do they?

\n" + withHashtag = "# Title\n\nhere's a simple status that uses hashtag #Hashtag!" + withHashtagExpected = "

Title

\n\n

here’s a simple status that uses hashtag #Hashtag!

\n" ) type MarkdownTestSuite struct { @@ -71,11 +65,20 @@ func (suite *MarkdownTestSuite) TestParseSimple() { } func (suite *MarkdownTestSuite) TestParseWithCodeBlock() { - fmt.Println(withCodeBlock) s := suite.formatter.FromMarkdown(context.Background(), withCodeBlock, nil, nil) 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() { foundTags := []*gtsmodel.Tag{ suite.testTags["Hashtag"],