Open platform to collect and prioritize feedback
chore: replace markdown processor (#1078)
Onion Ltd4bdd291ce61e
diff --git a/app/pkg/markdown/markdown.go b/app/pkg/markdown/markdown.goindex 6164d8ec0..183028f0a 100644--- a/app/pkg/markdown/markdown.go+++ b/app/pkg/markdown/markdown.go@@ -1,45 +1,55 @@package markdownimport (- "html"+ "github.com/gomarkdown/markdown"+ "github.com/gomarkdown/markdown/ast""html/template"+ "io""strings"- "github.com/russross/blackfriday"+ htmlrenderer "github.com/gomarkdown/markdown/html"+ mdparser "github.com/gomarkdown/markdown/parser")var mdExtns = 0 |- blackfriday.EXTENSION_TABLES |- blackfriday.EXTENSION_AUTOLINK |- blackfriday.EXTENSION_FENCED_CODE |- blackfriday.EXTENSION_TITLEBLOCK |- blackfriday.EXTENSION_STRIKETHROUGH |- blackfriday.EXTENSION_DEFINITION_LISTS |- blackfriday.EXTENSION_NO_INTRA_EMPHASIS |- blackfriday.EXTENSION_HARD_LINE_BREAK--var fullHTMLExtensions = 0 |- blackfriday.HTML_USE_XHTML |- blackfriday.HTML_USE_SMARTYPANTS |- blackfriday.HTML_SMARTYPANTS_FRACTIONS |- blackfriday.HTML_SMARTYPANTS_DASHES |- blackfriday.HTML_SMARTYPANTS_LATEX_DASHES--var fullRenderer = blackfriday.HtmlRenderer(fullHTMLExtensions, "", "")+ mdparser.Tables |+ mdparser.Autolink |+ mdparser.FencedCode |+ mdparser.Titleblock |+ mdparser.Strikethrough |+ mdparser.DefinitionLists |+ mdparser.NoIntraEmphasis |+ mdparser.HardLineBreak++var htmlFlags = 0 |+ htmlrenderer.UseXHTML |+ htmlrenderer.Smartypants |+ htmlrenderer.SmartypantsFractions |+ htmlrenderer.SmartypantsDashes |+ htmlrenderer.SmartypantsLatexDashes++var fullRenderer = htmlrenderer.NewRenderer(htmlrenderer.RendererOptions{+ Flags: htmlFlags,+ RenderNodeHook: func(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) {+ switch node := node.(type) {+ case *ast.HTMLSpan:+ htmlrenderer.EscapeHTML(w, node.Literal)+ return ast.GoToNext, true+ case *ast.HTMLBlock:+ _, _ = io.WriteString(w, "\n")+ htmlrenderer.EscapeHTML(w, node.Literal)+ _, _ = io.WriteString(w, "\n")+ return ast.GoToNext, true+ }+ return ast.GoToNext, false+ },+})// Full turns a markdown into HTML using all rulesfunc Full(input string) template.HTML {- sanitizedInput := html.EscapeString(input)- output := blackfriday.Markdown([]byte(sanitizedInput), fullRenderer, mdExtns)-+ // Apparently a parser cannot be reused.+ // https://github.com/gomarkdown/markdown/issues/229+ parser := mdparser.NewWithExtensions(mdExtns)+ output := markdown.ToHTML([]byte(input), parser, fullRenderer)return template.HTML(strings.TrimSpace(string(output)))}--var textRenderer = TextRenderer()--// PlainText parses given markdown input and return only the text-func PlainText(input string) string {- sanitizedInput := html.EscapeString(input)- output := blackfriday.Markdown([]byte(sanitizedInput), textRenderer, mdExtns)- return strings.TrimSpace(string(output))-}diff --git a/app/pkg/markdown/markdown_test.go b/app/pkg/markdown/markdown_test.goindex 115c13ce6..953838b11 100644--- a/app/pkg/markdown/markdown_test.go+++ b/app/pkg/markdown/markdown_test.go@@ -25,9 +25,11 @@ func TestFullMarkdown(t *testing.T) {- **Option 1**- *Option 2*- ~~Option 3~~`: `<ul>-<li><strong>Option 1</strong><br /></li>-<li><em>Option 2</em><br /></li>-<li><del>Option 3</del><br /></li>+<li><strong>Option 1</strong><br />+</li>+<li><em>Option 2</em><br />+</li>+<li><del>Option 3</del></li></ul>`,`Please add:– SEND_SMSdiff --git a/app/pkg/markdown/simple_renderer.go b/app/pkg/markdown/simple_renderer.godeleted file mode 100644index 84ea24674..000000000--- a/app/pkg/markdown/simple_renderer.go+++ /dev/null@@ -1,194 +0,0 @@-package markdown--import (- "bytes"-- "github.com/russross/blackfriday"-)--// Ensure we implement the Blackfriday Markdown Renderer interface-var _ blackfriday.Renderer = (*renderer)(nil)--// renderer renders Markdown to plain-text meant for listings and excerpts,-// and implements the blackfriday.Renderer interface.-//-// Many of the methods are stubs with no output to prevent output of HTML markup.-type proxy struct {- renderer blackfriday.Renderer-}--// SimpleRenderer renders most HTML tags, except title-func SimpleRenderer(flags int) proxy {- return proxy{- renderer: blackfriday.HtmlRenderer(flags, "", ""),- }-}--// Blocklevel callbacks--// BlockCode is the code tag callback.-func (p proxy) BlockCode(out *bytes.Buffer, text []byte, land string) {- p.renderer.BlockCode(out, text, land)-}--// BlockQuote is the quote tag callback.-func (p proxy) BlockQuote(out *bytes.Buffer, text []byte) {- p.renderer.BlockQuote(out, text)-}--// BlockHtml is the HTML tag callback.-func (p proxy) BlockHtml(out *bytes.Buffer, text []byte) {- p.renderer.BlockHtml(out, text)-}--// Header is the header tag callback.-func (p proxy) Header(out *bytes.Buffer, text func() bool, level int, id string) {- p.Paragraph(out, text)-}--// HRule is the horizontal rule tag callback.-func (p proxy) HRule(out *bytes.Buffer) {- p.renderer.HRule(out)-}--// List is the list tag callback.-func (p proxy) List(out *bytes.Buffer, text func() bool, flags int) {- p.renderer.List(out, text, flags)-}--// ListItem is the list item tag callback.-func (p proxy) ListItem(out *bytes.Buffer, text []byte, flags int) {- p.renderer.ListItem(out, text, flags)-}--// Paragraph is the paragraph tag callback. This renders simple paragraph text-// into plain text, such that summaries can be easily generated.-func (p proxy) Paragraph(out *bytes.Buffer, text func() bool) {- p.renderer.Paragraph(out, text)-}--// Table is the table tag callback.-func (p proxy) Table(out *bytes.Buffer, header []byte, body []byte, columnData []int) {- p.renderer.Table(out, header, body, columnData)-}--// TableRow is the table row tag callback.-func (p proxy) TableRow(out *bytes.Buffer, text []byte) {- p.renderer.TableRow(out, text)-}--// TableHeaderCell is the table header cell tag callback.-func (p proxy) TableHeaderCell(out *bytes.Buffer, text []byte, flags int) {- p.renderer.TableHeaderCell(out, text, flags)-}--// TableCell is the table cell tag callback.-func (p proxy) TableCell(out *bytes.Buffer, text []byte, flags int) {- p.renderer.TableCell(out, text, flags)-}--// Footnotes is the foot notes tag callback.-func (p proxy) Footnotes(out *bytes.Buffer, text func() bool) {- p.renderer.Footnotes(out, text)-}--// FootnoteItem is the footnote item tag callback.-func (p proxy) FootnoteItem(out *bytes.Buffer, name, text []byte, flags int) {- p.renderer.FootnoteItem(out, name, text, flags)-}--// TitleBlock is the title tag callback.-func (p proxy) TitleBlock(out *bytes.Buffer, text []byte) {- p.renderer.TitleBlock(out, text)-}--// Spanlevel callbacks--// AutoLink is the autolink tag callback.-func (p proxy) AutoLink(out *bytes.Buffer, link []byte, kind int) {- p.renderer.AutoLink(out, link, kind)-}--// CodeSpan is the code span tag callback. Outputs a simple Markdown version-// of the code span.-func (p proxy) CodeSpan(out *bytes.Buffer, text []byte) {- p.renderer.CodeSpan(out, text)-}--// DoubleEmphasis is the double emphasis tag callback. Outputs a simple-// plain-text version of the input.-func (p proxy) DoubleEmphasis(out *bytes.Buffer, text []byte) {- p.renderer.DoubleEmphasis(out, text)-}--// Emphasis is the emphasis tag callback. Outputs a simple plain-text-// version of the input.-func (p proxy) Emphasis(out *bytes.Buffer, text []byte) {- p.renderer.Emphasis(out, text)-}--// Image is the image tag callback.-func (p proxy) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {- p.renderer.Image(out, link, title, alt)-}--// LineBreak is the line break tag callback.-func (p proxy) LineBreak(out *bytes.Buffer) {- p.renderer.LineBreak(out)-}--// Link is the link tag callback. Outputs a simple plain-text version-// of the input.-func (p proxy) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {- p.renderer.Link(out, link, title, content)-}--// RawHtmlTag is the raw HTML tag callback.-func (p proxy) RawHtmlTag(out *bytes.Buffer, tag []byte) {- p.renderer.RawHtmlTag(out, tag)-}--// TripleEmphasis is the triple emphasis tag callback. Outputs a simple plain-text-// version of the input.-func (p proxy) TripleEmphasis(out *bytes.Buffer, text []byte) {- p.renderer.TripleEmphasis(out, text)-}--// StrikeThrough is the strikethrough tag callback.-func (p proxy) StrikeThrough(out *bytes.Buffer, text []byte) {- p.renderer.StrikeThrough(out, text)-}--// FootnoteRef is the footnote ref tag callback.-func (p proxy) FootnoteRef(out *bytes.Buffer, ref []byte, id int) {- p.renderer.FootnoteRef(out, ref, id)-}--// Lowlevel callbacks--// Entity callback. Outputs a simple plain-text version of the input.-func (p proxy) Entity(out *bytes.Buffer, entity []byte) {- p.renderer.Entity(out, entity)-}--// NormalText callback. Outputs a simple plain-text version of the input.-func (p proxy) NormalText(out *bytes.Buffer, text []byte) {- p.renderer.NormalText(out, text)-}--// Header and footer--// DocumentHeader callback.-func (p proxy) DocumentHeader(out *bytes.Buffer) {- p.renderer.DocumentHeader(out)-}--// DocumentFooter callback.-func (p proxy) DocumentFooter(out *bytes.Buffer) {- p.renderer.DocumentFooter(out)-}--// GetFlags returns zero.-func (p proxy) GetFlags() int {- return p.renderer.GetFlags()-}diff --git a/app/pkg/markdown/text.go b/app/pkg/markdown/text.gonew file mode 100644index 000000000..42264a605--- /dev/null+++ b/app/pkg/markdown/text.go@@ -0,0 +1,50 @@+package markdown++import (+ "fmt"+ "github.com/gomarkdown/markdown"+ "github.com/gomarkdown/markdown/ast"+ htmlrenderer "github.com/gomarkdown/markdown/html"+ mdparser "github.com/gomarkdown/markdown/parser"+ "github.com/microcosm-cc/bluemonday"+ "io"+ "regexp"+ "strings"+)++var textRenderer = htmlrenderer.NewRenderer(htmlrenderer.RendererOptions{+ Flags: htmlFlags,+ RenderNodeHook: func(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) {+ switch node := node.(type) {+ case *ast.HTMLSpan:+ htmlrenderer.EscapeHTML(w, node.Literal)+ return ast.GoToNext, true+ case *ast.HTMLBlock:+ _, _ = io.WriteString(w, "\n")+ htmlrenderer.EscapeHTML(w, node.Literal)+ _, _ = io.WriteString(w, "\n")+ return ast.GoToNext, true+ case *ast.Code:+ _, _ = io.WriteString(w, fmt.Sprintf("`%s`", node.Literal))+ return ast.GoToNext, true+ }+ return ast.GoToNext, false+ },+})++// The policy strips all HTML tags from the input text.+var strictPolicy = bluemonday.StrictPolicy()++// The regular expression finds duplicate newlines.+var regexNewlines = regexp.MustCompile(`\n+`)++// PlainText parses given markdown input and return only the text+func PlainText(input string) string {+ // Apparently a parser cannot be reused.+ // https://github.com/gomarkdown/markdown/issues/229+ parser := mdparser.NewWithExtensions(mdExtns)+ output := markdown.ToHTML([]byte(input), parser, textRenderer)+ sanitizedOutput := strictPolicy.Sanitize(string(output))+ sanitizedOutput = regexNewlines.ReplaceAllString(sanitizedOutput, "\n")+ return strings.TrimSpace(sanitizedOutput)+}diff --git a/app/pkg/markdown/text_renderer.go b/app/pkg/markdown/text_renderer.godeleted file mode 100644index b7ddf843b..000000000--- a/app/pkg/markdown/text_renderer.go+++ /dev/null@@ -1,172 +0,0 @@-package markdown--import (- "bytes"-- "github.com/russross/blackfriday"-)--// Ensure we implement the Blackfriday Markdown Renderer interface-var _ blackfriday.Renderer = (*renderer)(nil)--// renderer renders Markdown to plain-text meant for listings and excerpts,-// and implements the blackfriday.Renderer interface.-//-// Many of the methods are stubs with no output to prevent output of HTML markup.-type renderer struct{}--//TextRenderer returns a plain text renderer-func TextRenderer() renderer {- return renderer{}-}--// Blocklevel callbacks--// BlockCode is the code tag callback.-func (r renderer) BlockCode(out *bytes.Buffer, text []byte, land string) {}--// BlockQuote is the quote tag callback.-func (r renderer) BlockQuote(out *bytes.Buffer, text []byte) {}--// BlockHtml is the HTML tag callback.-func (r renderer) BlockHtml(out *bytes.Buffer, text []byte) {}--// Header is the header tag callback.-func (r renderer) Header(out *bytes.Buffer, text func() bool, level int, id string) {- marker := out.Len()- if !text() {- out.Truncate(marker)- }- out.Write([]byte{'\n'})-}--// HRule is the horizontal rule tag callback.-func (r renderer) HRule(out *bytes.Buffer) {}--// List is the list tag callback.-func (r renderer) List(out *bytes.Buffer, text func() bool, flags int) {- // TODO: This is not desired (we'd rather not write lists as part of summary),- // but see this issue: https://github.com/russross/blackfriday/issues/189- marker := out.Len()- if !text() {- out.Truncate(marker)- }- out.Write([]byte{' '})-}--// ListItem is the list item tag callback.-func (r renderer) ListItem(out *bytes.Buffer, text []byte, flags int) {}--// Paragraph is the paragraph tag callback. This renders simple paragraph text-// into plain text, such that summaries can be easily generated.-func (r renderer) Paragraph(out *bytes.Buffer, text func() bool) {- marker := out.Len()- if !text() {- out.Truncate(marker)- }- out.Write([]byte{'\n'})-}--// Table is the table tag callback.-func (r renderer) Table(out *bytes.Buffer, header []byte, body []byte, columnData []int) {}--// TableRow is the table row tag callback.-func (r renderer) TableRow(out *bytes.Buffer, text []byte) {}--// TableHeaderCell is the table header cell tag callback.-func (r renderer) TableHeaderCell(out *bytes.Buffer, text []byte, flags int) {}--// TableCell is the table cell tag callback.-func (r renderer) TableCell(out *bytes.Buffer, text []byte, flags int) {}--// Footnotes is the foot notes tag callback.-func (r renderer) Footnotes(out *bytes.Buffer, text func() bool) {}--// FootnoteItem is the footnote item tag callback.-func (r renderer) FootnoteItem(out *bytes.Buffer, name, text []byte, flags int) {}--// TitleBlock is the title tag callback.-func (r renderer) TitleBlock(out *bytes.Buffer, text []byte) {- out.Write(text)-}--// Spanlevel callbacks--// AutoLink is the autolink tag callback.-func (r renderer) AutoLink(out *bytes.Buffer, link []byte, kind int) {- out.Write(link)-}--// CodeSpan is the code span tag callback. Outputs a simple Markdown version-// of the code span.-func (r renderer) CodeSpan(out *bytes.Buffer, text []byte) {- out.Write([]byte("`"))- out.Write(text)- out.Write([]byte("`"))-}--// DoubleEmphasis is the double emphasis tag callback. Outputs a simple-// plain-text version of the input.-func (r renderer) DoubleEmphasis(out *bytes.Buffer, text []byte) {- out.Write(text)-}--// Emphasis is the emphasis tag callback. Outputs a simple plain-text-// version of the input.-func (r renderer) Emphasis(out *bytes.Buffer, text []byte) {- out.Write(text)-}--// Image is the image tag callback.-func (r renderer) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {}--// LineBreak is the line break tag callback.-func (r renderer) LineBreak(out *bytes.Buffer) {- out.Write([]byte{'\n'})-}--// Link is the link tag callback. Outputs a simple plain-text version-// of the input.-func (r renderer) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {- out.Write(content)-}--// RawHtmlTag is the raw HTML tag callback.-func (r renderer) RawHtmlTag(out *bytes.Buffer, tag []byte) {}--// TripleEmphasis is the triple emphasis tag callback. Outputs a simple plain-text-// version of the input.-func (r renderer) TripleEmphasis(out *bytes.Buffer, text []byte) {- out.Write(text)-}--// StrikeThrough is the strikethrough tag callback.-func (r renderer) StrikeThrough(out *bytes.Buffer, text []byte) {- out.Write(text)-}--// FootnoteRef is the footnote ref tag callback.-func (r renderer) FootnoteRef(out *bytes.Buffer, ref []byte, id int) {}--// Lowlevel callbacks--// Entity callback. Outputs a simple plain-text version of the input.-func (r renderer) Entity(out *bytes.Buffer, entity []byte) {- out.Write(entity)-}--// NormalText callback. Outputs a simple plain-text version of the input.-func (r renderer) NormalText(out *bytes.Buffer, text []byte) {- out.Write(text)-}--// Header and footer--// DocumentHeader callback.-func (r renderer) DocumentHeader(out *bytes.Buffer) {}--// DocumentFooter callback.-func (r renderer) DocumentFooter(out *bytes.Buffer) {}--// GetFlags returns zero.-func (r renderer) GetFlags() int { return 0 }diff --git a/go.mod b/go.modindex 59874f658..da40933e7 100644--- a/go.mod+++ b/go.mod@@ -11,6 +11,7 @@ require (github.com/goenning/letteravatar v0.0.0-20180605200324-553181ed4055github.com/golang-jwt/jwt/v4 v4.1.0github.com/golangci/golangci-lint v1.45.0+ github.com/gomarkdown/markdown v0.0.0-20220527210340-c82b80a9daf2github.com/gosimple/slug v1.11.0github.com/gotnospirit/messageformat v0.0.0-20190719172517-c1d0bdacdea2github.com/joeshaw/envdecode v0.0.0-20200121155833-099f1fc765bd@@ -22,7 +23,6 @@ require (github.com/prometheus/client_golang v1.11.0github.com/prometheus/client_model v0.2.0github.com/robfig/cron v1.2.0- github.com/russross/blackfriday v1.6.0golang.org/x/crypto v0.0.0-20220214200702-86341886e292golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8diff --git a/go.sum b/go.sumindex 6d0995f02..90a39744c 100644--- a/go.sum+++ b/go.sum@@ -300,6 +300,8 @@ github.com/golangci/revgrep v0.0.0-20210930125155-c22e5001d4f2 h1:SgM7GDZTxtTTQPgithub.com/golangci/revgrep v0.0.0-20210930125155-c22e5001d4f2/go.mod h1:LK+zW4MpyytAWQRz0M4xnzEk50lSvqDQKfx304apFkY=github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4 h1:zwtduBRr5SSWhqsYNgcuWO2kFlpdOZbP0+yRjmvPGys=github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4/go.mod h1:Izgrg8RkN3rCIMLGE9CyYmU9pY2Jer6DgANEnZ/L/cQ=+github.com/gomarkdown/markdown v0.0.0-20220527210340-c82b80a9daf2 h1:VJwys0mqRBeVxECc/DyXgveOqOqI81J9sjQFZHZwJvs=+github.com/gomarkdown/markdown v0.0.0-20220527210340-c82b80a9daf2/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA=github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=github.com/google/certificate-transparency-go v1.0.21/go.mod h1:QeJfpSbVSfYc7RgB3gJFj9cbuQMMchQxrWXz8Ruopmg=@@ -686,8 +688,6 @@ github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XFgithub.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o=github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=-github.com/russross/blackfriday v1.6.0 h1:KqfZb0pUVN2lYqZUYRddxF4OR8ZMURnJIG5Y3VRLtww=-github.com/russross/blackfriday v1.6.0/go.mod h1:ti0ldHuxg49ri4ksnFxlkCfN+hvslNlmVHqNRXXJNAY=github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=github.com/ryancurrah/gomodguard v1.2.3 h1:ww2fsjqocGCAFamzvv/b8IsRduuHHeK2MHTcTxZTQX8=