diff options
author | 2023-09-29 10:39:56 +0200 | |
---|---|---|
committer | 2023-09-29 10:39:56 +0200 | |
commit | 536d9e482d4ebc012855372b9fcfa4f022d1618a (patch) | |
tree | 36079fb403b9a9bb7d3a64ace582c6870bcce77b /internal/text/markdown.go | |
parent | [bugfix] Move follow.show_reblogs check further up to avoid showing unwanted ... (diff) | |
download | gotosocial-536d9e482d4ebc012855372b9fcfa4f022d1618a.tar.xz |
[chore/bugfix] Deinterface text.Formatter, allow underscores in hashtags (#2233)
Diffstat (limited to 'internal/text/markdown.go')
-rw-r--r-- | internal/text/markdown.go | 53 |
1 files changed, 35 insertions, 18 deletions
diff --git a/internal/text/markdown.go b/internal/text/markdown.go index ecc49673b..6fc1bd2f0 100644 --- a/internal/text/markdown.go +++ b/internal/text/markdown.go @@ -28,38 +28,55 @@ import ( "github.com/yuin/goldmark/renderer/html" ) -func (f *formatter) FromMarkdown(ctx context.Context, pmf gtsmodel.ParseMentionFunc, authorID string, statusID string, markdownText string) *FormatResult { - result := &FormatResult{ - Mentions: []*gtsmodel.Mention{}, - Tags: []*gtsmodel.Tag{}, - Emojis: []*gtsmodel.Emoji{}, - } +// FromMarkdown fulfils FormatFunc by parsing +// the given markdown input into a FormatResult. +func (f *Formatter) FromMarkdown( + ctx context.Context, + parseMention gtsmodel.ParseMentionFunc, + authorID string, + statusID string, + input string, +) *FormatResult { + result := new(FormatResult) - // parse markdown text into html, using custom renderer to add hashtag/mention links + // Instantiate goldmark parser for + // markdown, using custom renderer + // to add hashtag/mention links. md := goldmark.New( goldmark.WithRendererOptions( html.WithXHTML(), html.WithHardWraps(), - html.WithUnsafe(), // allows raw HTML + // Allows raw HTML. We sanitize + // at the end so this is OK. + html.WithUnsafe(), ), goldmark.WithExtensions( - &customRenderer{f, ctx, pmf, authorID, statusID, false, result}, - extension.Linkify, // turns URLs into links + &customRenderer{ + ctx, + f.db, + parseMention, + authorID, + statusID, + false, // emojiOnly = false. + result, + }, + extension.Linkify, // Turns URLs into links. extension.Strikethrough, ), ) - var htmlContentBytes bytes.Buffer - err := md.Convert([]byte(markdownText), &htmlContentBytes) - if err != nil { - log.Errorf(ctx, "error formatting markdown to HTML: %s", err) + // Parse input into HTML. + var htmlBytes bytes.Buffer + if err := md.Convert( + []byte(input), + &htmlBytes, + ); err != nil { + log.Errorf(ctx, "error formatting markdown input to HTML: %s", err) } - result.HTML = htmlContentBytes.String() - // clean anything dangerous out of the HTML + // Clean and shrink HTML. + result.HTML = htmlBytes.String() result.HTML = SanitizeToHTML(result.HTML) - - // shrink ray result.HTML = MinifyHTML(result.HTML) return result |