summaryrefslogtreecommitdiff
path: root/internal/text
diff options
context:
space:
mode:
authorLibravatar tobi <31960611+tsmethurst@users.noreply.github.com>2023-05-09 12:16:10 +0200
committerLibravatar GitHub <noreply@github.com>2023-05-09 11:16:10 +0100
commit0e29f1f5bb68a48d9b837d7f4e0a16370734955b (patch)
treef08d203ec8ca8aeea728e5251b1dc3956524b4f4 /internal/text
parent[chore/performance] Make sender multiplier configurable (#1750) (diff)
downloadgotosocial-0e29f1f5bb68a48d9b837d7f4e0a16370734955b.tar.xz
[feature] Enable federation in/out of profile PropertyValue fields (#1722)
Co-authored-by: kim <grufwub@gmail.com> Co-authored-by: kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com>
Diffstat (limited to 'internal/text')
-rw-r--r--internal/text/formatter.go2
-rw-r--r--internal/text/formatter_test.go4
-rw-r--r--internal/text/goldmark_plaintext.go38
-rw-r--r--internal/text/plain.go52
-rw-r--r--internal/text/plain_test.go28
5 files changed, 101 insertions, 23 deletions
diff --git a/internal/text/formatter.go b/internal/text/formatter.go
index 212d2b58d..0e5e0b554 100644
--- a/internal/text/formatter.go
+++ b/internal/text/formatter.go
@@ -30,6 +30,8 @@ import (
type Formatter interface {
// FromPlain parses an HTML text from a plaintext.
FromPlain(ctx context.Context, pmf gtsmodel.ParseMentionFunc, authorID string, statusID string, plain string) *FormatResult
+ // FromPlainNoParagraph parses an HTML text from a plaintext, without wrapping the resulting text in <p> tags.
+ FromPlainNoParagraph(ctx context.Context, pmf gtsmodel.ParseMentionFunc, authorID string, statusID string, plain string) *FormatResult
// FromMarkdown parses an HTML text from a markdown-formatted text.
FromMarkdown(ctx context.Context, pmf gtsmodel.ParseMentionFunc, authorID string, statusID string, md string) *FormatResult
// FromPlainEmojiOnly parses an HTML text from a plaintext, only parsing emojis and not mentions etc.
diff --git a/internal/text/formatter_test.go b/internal/text/formatter_test.go
index 8498a6408..403ba8e8e 100644
--- a/internal/text/formatter_test.go
+++ b/internal/text/formatter_test.go
@@ -92,3 +92,7 @@ func (suite *TextStandardTestSuite) FromMarkdown(text string) *text.FormatResult
func (suite *TextStandardTestSuite) FromPlain(text string) *text.FormatResult {
return suite.formatter.FromPlain(context.Background(), suite.parseMention, suite.testAccounts["local_account_1"].ID, "status_ID", text)
}
+
+func (suite *TextStandardTestSuite) FromPlainNoParagraph(text string) *text.FormatResult {
+ return suite.formatter.FromPlainNoParagraph(context.Background(), suite.parseMention, suite.testAccounts["local_account_1"].ID, "status_ID", text)
+}
diff --git a/internal/text/goldmark_plaintext.go b/internal/text/goldmark_plaintext.go
index 6f841920c..635fdfc33 100644
--- a/internal/text/goldmark_plaintext.go
+++ b/internal/text/goldmark_plaintext.go
@@ -60,3 +60,41 @@ func (b *plaintextParser) CanInterruptParagraph() bool {
func (b *plaintextParser) CanAcceptIndentedLine() bool {
return true
}
+
+// plaintextParserNoParagraph implements goldmark.parser.BlockParser
+type plaintextParserNoParagraph struct{}
+
+var defaultPlaintextParserNoParagraph = &plaintextParserNoParagraph{}
+
+func newPlaintextParserNoParagraph() parser.BlockParser {
+ return defaultPlaintextParserNoParagraph
+}
+
+func (b *plaintextParserNoParagraph) Trigger() []byte {
+ return nil
+}
+
+func (b *plaintextParserNoParagraph) Open(parent ast.Node, reader text.Reader, pc parser.Context) (ast.Node, parser.State) {
+ _, segment := reader.PeekLine()
+ node := ast.NewDocument()
+ node.Lines().Append(segment)
+ reader.Advance(segment.Len() - 1)
+ return node, parser.NoChildren
+}
+
+func (b *plaintextParserNoParagraph) Continue(node ast.Node, reader text.Reader, pc parser.Context) parser.State {
+ _, segment := reader.PeekLine()
+ node.Lines().Append(segment)
+ reader.Advance(segment.Len() - 1)
+ return parser.Continue | parser.NoChildren
+}
+
+func (b *plaintextParserNoParagraph) Close(node ast.Node, reader text.Reader, pc parser.Context) {}
+
+func (b *plaintextParserNoParagraph) CanInterruptParagraph() bool {
+ return false
+}
+
+func (b *plaintextParserNoParagraph) CanAcceptIndentedLine() bool {
+ return true
+}
diff --git a/internal/text/plain.go b/internal/text/plain.go
index 80916dfad..b1c2a2c33 100644
--- a/internal/text/plain.go
+++ b/internal/text/plain.go
@@ -30,26 +30,28 @@ import (
"github.com/yuin/goldmark/util"
)
-func (f *formatter) FromPlain(ctx context.Context, pmf gtsmodel.ParseMentionFunc, authorID string, statusID string, plain string) *FormatResult {
+func (f *formatter) fromPlain(
+ ctx context.Context,
+ ptParser parser.Parser,
+ pmf gtsmodel.ParseMentionFunc,
+ authorID string,
+ statusID string,
+ plain string,
+) *FormatResult {
result := &FormatResult{
Mentions: []*gtsmodel.Mention{},
Tags: []*gtsmodel.Tag{},
Emojis: []*gtsmodel.Emoji{},
}
- // parse markdown text into html, using custom renderer to add hashtag/mention links
+ // Parse markdown into html, using custom renderer
+ // to add hashtag/mention links and emoji images.
md := goldmark.New(
goldmark.WithRendererOptions(
html.WithXHTML(),
html.WithHardWraps(),
),
- goldmark.WithParser(
- parser.NewParser(
- parser.WithBlockParsers(
- util.Prioritized(newPlaintextParser(), 500),
- ),
- ),
- ),
+ goldmark.WithParser(ptParser), // use parser we were passed
goldmark.WithExtensions(
&customRenderer{f, ctx, pmf, authorID, statusID, false, result},
extension.Linkify, // turns URLs into links
@@ -57,20 +59,40 @@ func (f *formatter) FromPlain(ctx context.Context, pmf gtsmodel.ParseMentionFunc
)
var htmlContentBytes bytes.Buffer
- err := md.Convert([]byte(plain), &htmlContentBytes)
- if err != nil {
+ if err := md.Convert([]byte(plain), &htmlContentBytes); err != nil {
log.Errorf(ctx, "error formatting plaintext to HTML: %s", err)
}
result.HTML = htmlContentBytes.String()
- // clean anything dangerous out of the HTML
+ // Clean anything dangerous out of resulting HTML.
result.HTML = SanitizeHTML(result.HTML)
- // shrink ray
- result.HTML, err = m.String("text/html", result.HTML)
- if err != nil {
+ // Shrink ray!
+ var err error
+ if result.HTML, err = m.String("text/html", result.HTML); err != nil {
log.Errorf(ctx, "error minifying HTML: %s", err)
}
return result
}
+
+func (f *formatter) FromPlain(ctx context.Context, pmf gtsmodel.ParseMentionFunc, authorID string, statusID string, plain string) *FormatResult {
+ ptParser := parser.NewParser(
+ parser.WithBlockParsers(
+ util.Prioritized(newPlaintextParser(), 500),
+ ),
+ )
+
+ return f.fromPlain(ctx, ptParser, pmf, authorID, statusID, plain)
+}
+
+func (f *formatter) FromPlainNoParagraph(ctx context.Context, pmf gtsmodel.ParseMentionFunc, authorID string, statusID string, plain string) *FormatResult {
+ ptParser := parser.NewParser(
+ parser.WithBlockParsers(
+ // Initialize block parser that doesn't wrap in <p> tags.
+ util.Prioritized(newPlaintextParserNoParagraph(), 500),
+ ),
+ )
+
+ return f.fromPlain(ctx, ptParser, pmf, authorID, statusID, plain)
+}
diff --git a/internal/text/plain_test.go b/internal/text/plain_test.go
index c00ae70c3..5a2918563 100644
--- a/internal/text/plain_test.go
+++ b/internal/text/plain_test.go
@@ -25,14 +25,16 @@ import (
)
const (
- simple = "this is a plain and simple status"
- simpleExpected = "<p>this is a plain and simple status</p>"
- withTag = "here's a simple status that uses hashtag #welcome!"
- withTagExpected = "<p>here's a simple status that uses hashtag <a href=\"http://localhost:8080/tags/welcome\" class=\"mention hashtag\" rel=\"tag nofollow noreferrer noopener\" target=\"_blank\">#<span>welcome</span></a>!</p>"
- withHTML = "<div>blah this should just be html escaped blah</div>"
- withHTMLExpected = "<p>&lt;div>blah this should just be html escaped blah&lt;/div></p>"
- moreComplex = "Another test @foss_satan@fossbros-anonymous.io\n\n#Hashtag\n\nText\n\n:rainbow:"
- moreComplexExpected = "<p>Another test <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><br><br><a href=\"http://localhost:8080/tags/Hashtag\" class=\"mention hashtag\" rel=\"tag nofollow noreferrer noopener\" target=\"_blank\">#<span>Hashtag</span></a><br><br>Text<br><br>:rainbow:</p>"
+ simple = "this is a plain and simple status"
+ simpleExpected = "<p>this is a plain and simple status</p>"
+ simpleExpectedNoParagraph = "this is a plain and simple status"
+ withTag = "here's a simple status that uses hashtag #welcome!"
+ withTagExpected = "<p>here's a simple status that uses hashtag <a href=\"http://localhost:8080/tags/welcome\" class=\"mention hashtag\" rel=\"tag nofollow noreferrer noopener\" target=\"_blank\">#<span>welcome</span></a>!</p>"
+ withTagExpectedNoParagraph = "here's a simple status that uses hashtag <a href=\"http://localhost:8080/tags/welcome\" class=\"mention hashtag\" rel=\"tag nofollow noreferrer noopener\" target=\"_blank\">#<span>welcome</span></a>!"
+ withHTML = "<div>blah this should just be html escaped blah</div>"
+ withHTMLExpected = "<p>&lt;div>blah this should just be html escaped blah&lt;/div></p>"
+ moreComplex = "Another test @foss_satan@fossbros-anonymous.io\n\n#Hashtag\n\nText\n\n:rainbow:"
+ moreComplexExpected = "<p>Another test <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><br><br><a href=\"http://localhost:8080/tags/Hashtag\" class=\"mention hashtag\" rel=\"tag nofollow noreferrer noopener\" target=\"_blank\">#<span>Hashtag</span></a><br><br>Text<br><br>:rainbow:</p>"
)
type PlainTestSuite struct {
@@ -44,11 +46,21 @@ func (suite *PlainTestSuite) TestParseSimple() {
suite.Equal(simpleExpected, formatted.HTML)
}
+func (suite *PlainTestSuite) TestParseSimpleNoParagraph() {
+ formatted := suite.FromPlainNoParagraph(simple)
+ suite.Equal(simpleExpectedNoParagraph, formatted.HTML)
+}
+
func (suite *PlainTestSuite) TestParseWithTag() {
formatted := suite.FromPlain(withTag)
suite.Equal(withTagExpected, formatted.HTML)
}
+func (suite *PlainTestSuite) TestParseWithTagNoParagraph() {
+ formatted := suite.FromPlainNoParagraph(withTag)
+ suite.Equal(withTagExpectedNoParagraph, formatted.HTML)
+}
+
func (suite *PlainTestSuite) TestParseWithHTML() {
formatted := suite.FromPlain(withHTML)
suite.Equal(withHTMLExpected, formatted.HTML)