2023-03-12 18:00:57 +03:00
// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2021-08-16 20:17:56 +03:00
package text_test
import (
"testing"
"github.com/stretchr/testify/suite"
)
2022-07-19 11:41:16 +03:00
var withCodeBlock = ` # Title
2021-08-16 20:17:56 +03:00
Below is some JSON .
` + " ` ` ` " + ` json
{
"key" : "value" ,
"another_key" : [
"value1" ,
"value2"
]
}
` + " ` ` ` " + `
that was some JSON : )
`
2022-07-19 11:41:16 +03:00
const (
2022-08-07 19:19:16 +03:00
simpleMarkdown = "# Title\n\nHere's a simple text in markdown.\n\nHere's a [link](https://example.org)."
2022-11-22 13:08:50 +03:00
simpleMarkdownExpected = "<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>"
2022-08-07 19:19:16 +03:00
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>"
withInlineCode = "`Nobody tells you about the <code><del>SECRET CODE</del></code>, do they?`"
withInlineCodeExpected = "<p><code>Nobody tells you about the <code><del>SECRET CODE</del></code>, do they?</code></p>"
withInlineCode2 = "`Nobody tells you about the </code><del>SECRET CODE</del><code>, do they?`"
withInlineCode2Expected = "<p><code>Nobody tells you about the </code><del>SECRET CODE</del><code>, do they?</code></p>"
withHashtag = "# Title\n\nhere's a simple status that uses hashtag #Hashtag!"
2022-11-22 13:08:50 +03:00
withHashtagExpected = "<h1>Title</h1><p>here's 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>"
2022-08-07 19:19:16 +03:00
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\">"
2022-11-22 13:08:50 +03:00
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>"
2022-08-07 19:19:16 +03:00
mdWithCheekyHTML = "# Title\n\nHere's a simple text in markdown.\n\nHere's a cheeky little script: <script>alert(ahhhh)</script>"
2022-11-22 13:08:50 +03:00
mdWithCheekyHTMLExpected = "<h1>Title</h1><p>Here's a simple text in markdown.</p><p>Here's a cheeky little script:</p>"
2022-08-07 19:19:16 +03:00
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>"
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>"
2022-08-26 14:28:06 +03:00
mdWithFootnote = "fox mulder,fbi.[^1]\n\n[^1]: federated bureau of investigation"
2022-09-27 15:27:53 +03:00
mdWithFootnoteExpected = "<p>fox mulder,fbi.[^1]</p><p>[^1]: federated bureau of investigation</p>"
2022-08-26 14:28:06 +03:00
mdWithBlockQuote = "get ready, there's a block quote coming:\n\n>line1\n>line2\n>\n>line3\n\n"
2022-11-22 13:08:50 +03:00
mdWithBlockQuoteExpected = "<p>get ready, there's a block quote coming:</p><blockquote><p>line1<br>line2</p><p>line3</p></blockquote>"
2022-08-31 18:40:11 +03:00
mdHashtagAndCodeBlock = "#Hashtag\n\n```\n#Hashtag\n```"
mdHashtagAndCodeBlockExpected = "<p><a href=\"http://localhost:8080/tags/Hashtag\" class=\"mention hashtag\" rel=\"tag nofollow noreferrer noopener\" target=\"_blank\">#<span>Hashtag</span></a></p><pre><code>#Hashtag\n</code></pre>"
mdMentionAndCodeBlock = "@the_mighty_zork\n\n```\n@the_mighty_zork\n```"
mdMentionAndCodeBlockExpected = "<p><span class=\"h-card\"><a href=\"http://localhost:8080/@the_mighty_zork\" class=\"u-url mention\" rel=\"nofollow noreferrer noopener\" target=\"_blank\">@<span>the_mighty_zork</span></a></span></p><pre><code>@the_mighty_zork\n</code></pre>"
2022-11-22 13:08:50 +03:00
mdWithSmartypants = "\"you have to quargle the bleepflorp\" they said with 1/2 of nominal speed and 1/3 of the usual glumping"
mdWithSmartypantsExpected = "<p>\"you have to quargle the bleepflorp\" they said with 1/2 of nominal speed and 1/3 of the usual glumping</p>"
2022-12-16 14:20:22 +03:00
mdWithAsciiHeart = "hello <3 old friend <3 i loved u </3 :(( you stole my heart"
mdWithAsciiHeartExpected = "<p>hello <3 old friend <3 i loved u </3 :(( you stole my heart</p>"
mdWithStrikethrough = "I have ~~mdae~~ made an error"
mdWithStrikethroughExpected = "<p>I have <del>mdae</del> made an error</p>"
mdWithLink = "Check out this code, i heard it was written by a sloth https://github.com/superseriousbusiness/gotosocial"
mdWithLinkExpected = "<p>Check out this code, i heard it was written by a sloth <a href=\"https://github.com/superseriousbusiness/gotosocial\" rel=\"nofollow noreferrer noopener\" target=\"_blank\">https://github.com/superseriousbusiness/gotosocial</a></p>"
2023-02-03 13:58:58 +03:00
mdObjectInCodeBlock = "@foss_satan@fossbros-anonymous.io this is how to mention a user\n```\n@the_mighty_zork hey bud! nice #ObjectOrientedProgramming software you've been writing lately! :rainbow:\n```\nhope that helps"
mdObjectInCodeBlockExpected = "<p><span class=\"h-card\"><a href=\"http://fossbros-anonymous.io/@foss_satan\" class=\"u-url mention\" rel=\"nofollow noreferrer noopener\" target=\"_blank\">@<span>foss_satan</span></a></span> this is how to mention a user</p><pre><code>@the_mighty_zork hey bud! nice #ObjectOrientedProgramming software you've been writing lately! :rainbow:\n</code></pre><p>hope that helps</p>"
mdItalicHashtag = "_#hashtag_"
mdItalicHashtagExpected = "<p><em><a href=\"http://localhost:8080/tags/Hashtag\" class=\"mention hashtag\" rel=\"tag nofollow noreferrer noopener\" target=\"_blank\">#<span>hashtag</span></a></em></p>"
mdItalicHashtags = "_#hashtag #hashtag #hashtag_"
mdItalicHashtagsExpected = "<p><em><a href=\"http://localhost:8080/tags/Hashtag\" class=\"mention hashtag\" rel=\"tag nofollow noreferrer noopener\" target=\"_blank\">#<span>hashtag</span></a> <a href=\"http://localhost:8080/tags/Hashtag\" class=\"mention hashtag\" rel=\"tag nofollow noreferrer noopener\" target=\"_blank\">#<span>hashtag</span></a> <a href=\"http://localhost:8080/tags/Hashtag\" class=\"mention hashtag\" rel=\"tag nofollow noreferrer noopener\" target=\"_blank\">#<span>hashtag</span></a></em></p>"
// BEWARE: sneaky unicode business going on.
// the first ö is one rune, the second ö is an o with a combining diacritic.
mdUnnormalizedHashtag = "#hellöthere #hellöthere"
mdUnnormalizedHashtagExpected = "<p><a href=\"http://localhost:8080/tags/hell%C3%B6there\" class=\"mention hashtag\" rel=\"tag nofollow noreferrer noopener\" target=\"_blank\">#<span>hellöthere</span></a> <a href=\"http://localhost:8080/tags/hell%C3%B6there\" class=\"mention hashtag\" rel=\"tag nofollow noreferrer noopener\" target=\"_blank\">#<span>hellöthere</span></a></p>"
2021-08-16 20:17:56 +03:00
)
type MarkdownTestSuite struct {
TextStandardTestSuite
}
func ( suite * MarkdownTestSuite ) TestParseSimple ( ) {
2023-02-03 13:58:58 +03:00
formatted := suite . FromMarkdown ( simpleMarkdown )
suite . Equal ( simpleMarkdownExpected , formatted . HTML )
2021-08-16 20:17:56 +03:00
}
func ( suite * MarkdownTestSuite ) TestParseWithCodeBlock ( ) {
2023-02-03 13:58:58 +03:00
formatted := suite . FromMarkdown ( withCodeBlock )
suite . Equal ( withCodeBlockExpected , formatted . HTML )
2021-08-16 20:17:56 +03:00
}
2022-07-19 11:41:16 +03:00
func ( suite * MarkdownTestSuite ) TestParseWithInlineCode ( ) {
2023-02-03 13:58:58 +03:00
formatted := suite . FromMarkdown ( withInlineCode )
suite . Equal ( withInlineCodeExpected , formatted . HTML )
2022-07-19 11:41:16 +03:00
}
func ( suite * MarkdownTestSuite ) TestParseWithInlineCode2 ( ) {
2023-02-03 13:58:58 +03:00
formatted := suite . FromMarkdown ( withInlineCode2 )
suite . Equal ( withInlineCode2Expected , formatted . HTML )
2022-07-19 11:41:16 +03:00
}
2021-08-16 20:17:56 +03:00
func ( suite * MarkdownTestSuite ) TestParseWithHashtag ( ) {
2023-02-03 13:58:58 +03:00
formatted := suite . FromMarkdown ( withHashtag )
suite . Equal ( withHashtagExpected , formatted . HTML )
2021-08-16 20:17:56 +03:00
}
2022-07-19 16:21:17 +03:00
func ( suite * MarkdownTestSuite ) TestParseWithHTML ( ) {
2023-02-03 13:58:58 +03:00
formatted := suite . FromMarkdown ( mdWithHTML )
suite . Equal ( mdWithHTMLExpected , formatted . HTML )
2022-07-19 16:21:17 +03:00
}
func ( suite * MarkdownTestSuite ) TestParseWithCheekyHTML ( ) {
2023-02-03 13:58:58 +03:00
formatted := suite . FromMarkdown ( mdWithCheekyHTML )
suite . Equal ( mdWithCheekyHTMLExpected , formatted . HTML )
2022-07-19 16:21:17 +03:00
}
2022-08-07 19:19:16 +03:00
func ( suite * MarkdownTestSuite ) TestParseWithHashtagInitial ( ) {
2023-02-03 13:58:58 +03:00
formatted := suite . FromMarkdown ( mdWithHashtagInitial )
suite . Equal ( mdWithHashtagInitialExpected , formatted . HTML )
2022-08-07 19:19:16 +03:00
}
func ( suite * MarkdownTestSuite ) TestParseCodeBlockWithNewlines ( ) {
2023-02-03 13:58:58 +03:00
formatted := suite . FromMarkdown ( mdCodeBlockWithNewlines )
suite . Equal ( mdCodeBlockWithNewlinesExpected , formatted . HTML )
2022-08-07 19:19:16 +03:00
}
2022-08-26 14:28:06 +03:00
func ( suite * MarkdownTestSuite ) TestParseWithFootnote ( ) {
2023-02-03 13:58:58 +03:00
formatted := suite . FromMarkdown ( mdWithFootnote )
suite . Equal ( mdWithFootnoteExpected , formatted . HTML )
2022-08-26 14:28:06 +03:00
}
func ( suite * MarkdownTestSuite ) TestParseWithBlockquote ( ) {
2023-02-03 13:58:58 +03:00
formatted := suite . FromMarkdown ( mdWithBlockQuote )
suite . Equal ( mdWithBlockQuoteExpected , formatted . HTML )
2022-08-26 14:28:06 +03:00
}
2022-08-31 18:40:11 +03:00
func ( suite * MarkdownTestSuite ) TestParseHashtagWithCodeBlock ( ) {
2023-02-03 13:58:58 +03:00
formatted := suite . FromMarkdown ( mdHashtagAndCodeBlock )
suite . Equal ( mdHashtagAndCodeBlockExpected , formatted . HTML )
2022-08-31 18:40:11 +03:00
}
func ( suite * MarkdownTestSuite ) TestParseMentionWithCodeBlock ( ) {
2023-02-03 13:58:58 +03:00
formatted := suite . FromMarkdown ( mdMentionAndCodeBlock )
suite . Equal ( mdMentionAndCodeBlockExpected , formatted . HTML )
2022-08-31 18:40:11 +03:00
}
2022-11-22 13:08:50 +03:00
func ( suite * MarkdownTestSuite ) TestParseSmartypants ( ) {
2023-02-03 13:58:58 +03:00
formatted := suite . FromMarkdown ( mdWithSmartypants )
suite . Equal ( mdWithSmartypantsExpected , formatted . HTML )
2022-11-22 13:08:50 +03:00
}
2022-12-16 14:20:22 +03:00
func ( suite * MarkdownTestSuite ) TestParseAsciiHeart ( ) {
2023-02-03 13:58:58 +03:00
formatted := suite . FromMarkdown ( mdWithAsciiHeart )
suite . Equal ( mdWithAsciiHeartExpected , formatted . HTML )
2022-12-16 14:20:22 +03:00
}
func ( suite * MarkdownTestSuite ) TestParseStrikethrough ( ) {
2023-02-03 13:58:58 +03:00
formatted := suite . FromMarkdown ( mdWithStrikethrough )
suite . Equal ( mdWithStrikethroughExpected , formatted . HTML )
2022-12-16 14:20:22 +03:00
}
func ( suite * MarkdownTestSuite ) TestParseLink ( ) {
2023-02-03 13:58:58 +03:00
formatted := suite . FromMarkdown ( mdWithLink )
suite . Equal ( mdWithLinkExpected , formatted . HTML )
}
func ( suite * MarkdownTestSuite ) TestParseObjectInCodeBlock ( ) {
formatted := suite . FromMarkdown ( mdObjectInCodeBlock )
suite . Equal ( mdObjectInCodeBlockExpected , formatted . HTML )
suite . Len ( formatted . Mentions , 1 )
suite . Equal ( "@foss_satan@fossbros-anonymous.io" , formatted . Mentions [ 0 ] . NameString )
suite . Empty ( formatted . Tags )
suite . Empty ( formatted . Emojis )
}
func ( suite * MarkdownTestSuite ) TestParseItalicHashtag ( ) {
formatted := suite . FromMarkdown ( mdItalicHashtag )
suite . Equal ( mdItalicHashtagExpected , formatted . HTML )
}
func ( suite * MarkdownTestSuite ) TestParseItalicHashtags ( ) {
formatted := suite . FromMarkdown ( mdItalicHashtags )
suite . Equal ( mdItalicHashtagsExpected , formatted . HTML )
}
func ( suite * MarkdownTestSuite ) TestParseUnnormalizedHashtag ( ) {
formatted := suite . FromMarkdown ( mdUnnormalizedHashtag )
suite . Equal ( mdUnnormalizedHashtagExpected , formatted . HTML )
2022-12-16 14:20:22 +03:00
}
2021-08-16 20:17:56 +03:00
func TestMarkdownTestSuite ( t * testing . T ) {
suite . Run ( t , new ( MarkdownTestSuite ) )
}