[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>
This commit is contained in:
tobi 2023-05-09 12:16:10 +02:00 committed by GitHub
commit 0e29f1f5bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
180 changed files with 9278 additions and 1550 deletions

View file

@ -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.

View file

@ -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)
}

View file

@ -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
}

View file

@ -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)
}

View file

@ -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)