summaryrefslogtreecommitdiff
path: root/internal/text/markdown.go
diff options
context:
space:
mode:
authorLibravatar tobi <31960611+tsmethurst@users.noreply.github.com>2022-09-27 14:27:53 +0200
committerLibravatar GitHub <noreply@github.com>2022-09-27 14:27:53 +0200
commit00d38855d416e834c2657271823e0ee95397d7ba (patch)
treec87d85e5c5c0be0c002e2757c63fe26253e5467a /internal/text/markdown.go
parent[performance] Update indexes that were causing slow db queries (#855) (diff)
downloadgotosocial-00d38855d416e834c2657271823e0ee95397d7ba.tar.xz
[bugfix] Fix emphasis being added to emoji shortcodes with markdown parsing (#856)v0.5.0
* fix underscored emoji shortcodes being emphasized * remove footnote parsing from md
Diffstat (limited to 'internal/text/markdown.go')
-rw-r--r--internal/text/markdown.go35
1 files changed, 26 insertions, 9 deletions
diff --git a/internal/text/markdown.go b/internal/text/markdown.go
index 1382cbf61..b512e3b0f 100644
--- a/internal/text/markdown.go
+++ b/internal/text/markdown.go
@@ -22,6 +22,7 @@ import (
"bytes"
"context"
"io"
+ "strings"
"github.com/russross/blackfriday/v2"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
@@ -31,7 +32,7 @@ import (
)
var (
- bfExtensions = blackfriday.CommonExtensions | blackfriday.HardLineBreak | blackfriday.Footnotes
+ bfExtensions = blackfriday.NoIntraEmphasis | blackfriday.FencedCode | blackfriday.Autolink | blackfriday.Strikethrough | blackfriday.SpaceHeadings | blackfriday.HardLineBreak
m *minify.M
)
@@ -54,8 +55,7 @@ func (r *renderer) RenderNode(w io.Writer, node *blackfriday.Node, entering bool
html = r.f.ReplaceMentions(r.ctx, html, r.mentions)
// we don't have much recourse if this fails
- _, err := io.WriteString(w, html)
- if err != nil {
+ if _, err := io.WriteString(w, html); err != nil {
log.Errorf("error outputting markdown text: %s", err)
}
return status
@@ -63,7 +63,7 @@ func (r *renderer) RenderNode(w io.Writer, node *blackfriday.Node, entering bool
return r.HTMLRenderer.RenderNode(w, node, entering)
}
-func (f *formatter) FromMarkdown(ctx context.Context, md string, mentions []*gtsmodel.Mention, tags []*gtsmodel.Tag) string {
+func (f *formatter) FromMarkdown(ctx context.Context, markdownText string, mentions []*gtsmodel.Mention, tags []*gtsmodel.Tag, emojis []*gtsmodel.Emoji) string {
renderer := &renderer{
f: f,
@@ -75,11 +75,28 @@ func (f *formatter) FromMarkdown(ctx context.Context, md string, mentions []*gts
}),
}
- // parse markdown, use custom renderer to add hashtag/mention links
- contentBytes := blackfriday.Run([]byte(md), blackfriday.WithExtensions(bfExtensions), blackfriday.WithRenderer(renderer))
+ // Temporarily replace all found emoji shortcodes in the markdown text with
+ // their ID so that they're not parsed as anything by the markdown parser -
+ // this fixes cases where emojis with some underscores in them are parsed as
+ // words with emphasis, eg `:_some_emoji:` becomes `:<em>some</em>emoji:`
+ //
+ // Since the IDs of the emojis are just uppercase letters + numbers they should
+ // be safe to pass through the markdown parser without unexpected effects.
+ for _, e := range emojis {
+ markdownText = strings.ReplaceAll(markdownText, ":"+e.Shortcode+":", ":"+e.ID+":")
+ }
+
+ // parse markdown text into html, using custom renderer to add hashtag/mention links
+ htmlContentBytes := blackfriday.Run([]byte(markdownText), blackfriday.WithExtensions(bfExtensions), blackfriday.WithRenderer(renderer))
+ htmlContent := string(htmlContentBytes)
+
+ // Replace emoji IDs in the parsed html content with their shortcodes again
+ for _, e := range emojis {
+ htmlContent = strings.ReplaceAll(htmlContent, ":"+e.ID+":", ":"+e.Shortcode+":")
+ }
- // clean anything dangerous out of it
- content := SanitizeHTML(string(contentBytes))
+ // clean anything dangerous out of the html
+ htmlContent = SanitizeHTML(htmlContent)
if m == nil {
m = minify.New()
@@ -89,7 +106,7 @@ func (f *formatter) FromMarkdown(ctx context.Context, md string, mentions []*gts
})
}
- minified, err := m.String("text/html", content)
+ minified, err := m.String("text/html", htmlContent)
if err != nil {
log.Errorf("error minifying markdown text: %s", err)
}