diff options
author | 2022-08-07 18:19:16 +0200 | |
---|---|---|
committer | 2022-08-07 18:19:16 +0200 | |
commit | 879b4abde722cb66463ca81a4cf6ac5465ef276d (patch) | |
tree | cdbd98840bca27317ad0860a194072671ed04ebe /internal/text/markdown.go | |
parent | [feature] Photoswipe gallery (#740) (diff) | |
download | gotosocial-879b4abde722cb66463ca81a4cf6ac5465ef276d.tar.xz |
[bugfix] Markdown formatting updates (#743)
* add minify dependency specifically for markdown
* rearrange markdown formatting
* update markdown tests
Diffstat (limited to 'internal/text/markdown.go')
-rw-r--r-- | internal/text/markdown.go | 32 |
1 files changed, 27 insertions, 5 deletions
diff --git a/internal/text/markdown.go b/internal/text/markdown.go index a5c62f23f..8952b99d6 100644 --- a/internal/text/markdown.go +++ b/internal/text/markdown.go @@ -23,17 +23,39 @@ import ( "github.com/russross/blackfriday/v2" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/superseriousbusiness/gotosocial/internal/log" + "github.com/tdewolff/minify/v2" + "github.com/tdewolff/minify/v2/html" ) -func (f *formatter) FromMarkdown(ctx context.Context, md string, mentions []*gtsmodel.Mention, tags []*gtsmodel.Tag) string { - // do the markdown parsing *first* - contentBytes := blackfriday.Run([]byte(md)) +var m *minify.M +func (f *formatter) FromMarkdown(ctx context.Context, md string, mentions []*gtsmodel.Mention, tags []*gtsmodel.Tag) string { // format tags nicely - content := f.ReplaceTags(ctx, string(contentBytes), tags) + content := f.ReplaceTags(ctx, md, tags) // format mentions nicely content = f.ReplaceMentions(ctx, content, mentions) - return SanitizeHTML(content) + // parse markdown + contentBytes := blackfriday.Run([]byte(content)) + + // clean anything dangerous out of it + content = SanitizeHTML(string(contentBytes)) + + if m == nil { + m = minify.New() + m.Add("text/html", &html.Minifier{ + KeepEndTags: true, + KeepQuotes: true, + KeepWhitespace: true, + }) + } + + minified, err := m.String("text/html", content) + if err != nil { + log.Errorf("error minifying markdown text: %s", err) + } + + return minified } |