From c224676ccd7b3f1438cebdfbaaa33942e7c83354 Mon Sep 17 00:00:00 2001 From: Umputun Date: Sat, 29 Jun 2019 21:58:50 -0500 Subject: [PATCH] enable Smartypants for md rendering --- backend/app/store/comment_test.go | 4 ++++ backend/app/store/formatter.go | 15 ++++++++++----- backend/app/store/formatter_test.go | 2 +- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/backend/app/store/comment_test.go b/backend/app/store/comment_test.go index 2829382b..3c712766 100644 --- a/backend/app/store/comment_test.go +++ b/backend/app/store/comment_test.go @@ -34,6 +34,10 @@ func TestComment_Sanitize(t *testing.T) { User: User{ID: "id", Name: "xyz"}, }, }, + { + inp: Comment{Text: "blah & & 123 — —"}, + out: Comment{Text: `blah & & 123 — —`}, + }, } for n, tt := range tbl { diff --git a/backend/app/store/formatter.go b/backend/app/store/formatter.go index 61a386e6..cfd0ff96 100644 --- a/backend/app/store/formatter.go +++ b/backend/app/store/formatter.go @@ -5,7 +5,7 @@ import ( "strings" "github.com/PuerkitoBio/goquery" - blackfriday "gopkg.in/russross/blackfriday.v2" + bf "gopkg.in/russross/blackfriday.v2" ) // CommentFormatter implements all generic formatting ops on comment @@ -40,10 +40,15 @@ func (f *CommentFormatter) Format(c Comment) Comment { // FormatText converts text with markdown processor, applies external converters and shortens links func (f *CommentFormatter) FormatText(txt string) (res string) { - mdExt := blackfriday.NoIntraEmphasis | blackfriday.Tables | blackfriday.FencedCode | - blackfriday.Strikethrough | blackfriday.SpaceHeadings | blackfriday.HardLineBreak | - blackfriday.BackslashLineBreak | blackfriday.Autolink - res = string(blackfriday.Run([]byte(txt), blackfriday.WithExtensions(mdExt))) + mdExt := bf.NoIntraEmphasis | bf.Tables | bf.FencedCode | + bf.Strikethrough | bf.SpaceHeadings | bf.HardLineBreak | + bf.BackslashLineBreak | bf.Autolink + + rend := bf.NewHTMLRenderer(bf.HTMLRendererParameters{ + Flags: bf.Smartypants | bf.SmartypantsFractions | bf.SmartypantsDashes | bf.SmartypantsAngledQuotes, + }) + + res = string(bf.Run([]byte(txt), bf.WithExtensions(mdExt), bf.WithRenderer(rend))) for _, conv := range f.converters { res = conv.Convert(res) diff --git a/backend/app/store/formatter_test.go b/backend/app/store/formatter_test.go index 95959ca3..bea2184c 100644 --- a/backend/app/store/formatter_test.go +++ b/backend/app/store/formatter_test.go @@ -17,7 +17,7 @@ func TestFormatter_FormatText(t *testing.T) { }{ {"", "!converted"}, {"12345 abc", "

12345 abc

\n!converted"}, - {"**xyz** _aaa_", "

xyz aaa

\n!converted"}, + {"**xyz** _aaa_ - \"sfs\"", "

xyz aaa – «sfs»

\n!converted"}, { "http://127.0.0.1/some-long-link/12345/678901234567890", "

http://127.0.0.1/some-long-link/12345/6789012...

\n!converted", },