summaryrefslogtreecommitdiff
path: root/internal/text/plain.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/text/plain.go')
-rw-r--r--internal/text/plain.go68
1 files changed, 42 insertions, 26 deletions
diff --git a/internal/text/plain.go b/internal/text/plain.go
index a64a14f06..3549200c6 100644
--- a/internal/text/plain.go
+++ b/internal/text/plain.go
@@ -19,40 +19,56 @@
package text
import (
+ "bytes"
"context"
- "html"
- "strings"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
+ "github.com/superseriousbusiness/gotosocial/internal/log"
+ "github.com/yuin/goldmark"
+ "github.com/yuin/goldmark/extension"
+ "github.com/yuin/goldmark/parser"
+ "github.com/yuin/goldmark/renderer/html"
+ "github.com/yuin/goldmark/util"
)
-// breakReplacer replaces new-lines with HTML breaks.
-var breakReplacer = strings.NewReplacer(
- "\r\n", "<br/>",
- "\n", "<br/>",
-)
-
-func (f *formatter) FromPlain(ctx context.Context, plain string, mentions []*gtsmodel.Mention, tags []*gtsmodel.Tag) string {
- // trim any crap
- content := strings.TrimSpace(plain)
-
- // clean 'er up
- content = html.EscapeString(content)
-
- // format links nicely
- content = f.ReplaceLinks(ctx, content)
+func (f *formatter) FromPlain(ctx context.Context, pmf gtsmodel.ParseMentionFunc, authorID string, statusID string, plain string) *FormatResult {
+ result := &FormatResult{
+ Mentions: []*gtsmodel.Mention{},
+ Tags: []*gtsmodel.Tag{},
+ Emojis: []*gtsmodel.Emoji{},
+ }
- // format tags nicely
- content = f.ReplaceTags(ctx, content, tags)
+ // parse markdown text into html, using custom renderer to add hashtag/mention links
+ md := goldmark.New(
+ goldmark.WithRendererOptions(
+ html.WithXHTML(),
+ html.WithHardWraps(),
+ ),
+ goldmark.WithParser(
+ parser.NewParser(
+ parser.WithBlockParsers(
+ util.Prioritized(newPlaintextParser(), 500),
+ ),
+ ),
+ ),
+ goldmark.WithExtensions(
+ &customRenderer{f, ctx, pmf, authorID, statusID, false, result},
+ extension.Linkify, // turns URLs into links
+ ),
+ )
- // format mentions nicely
- content = f.ReplaceMentions(ctx, content, mentions)
+ var htmlContentBytes bytes.Buffer
+ err := md.Convert([]byte(plain), &htmlContentBytes)
+ if err != nil {
+ log.Errorf("error formatting plaintext to HTML: %s", err)
+ }
+ result.HTML = htmlContentBytes.String()
- // replace newlines with breaks
- content = breakReplacer.Replace(content)
+ // clean anything dangerous out of the HTML
+ result.HTML = SanitizeHTML(result.HTML)
- // wrap the whole thing in a pee
- content = `<p>` + content + `</p>`
+ // shrink ray
+ result.HTML = minifyHTML(result.HTML)
- return SanitizeHTML(content)
+ return result
}