Skip to content

Commit

Permalink
chore: replace markdown processor (#1078)
Browse files Browse the repository at this point in the history
Replaces package github.com/russross/blackfriday with
github.com/gomarkdown/markdown.

Co-authored-by: Guilherme Oenning <[email protected]>
  • Loading branch information
onionltd and goenning authored Jun 15, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 3570c45 commit 4bdd291
Showing 7 changed files with 99 additions and 403 deletions.
72 changes: 41 additions & 31 deletions app/pkg/markdown/markdown.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,55 @@
package markdown

import (
"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 rules
func 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))
}
8 changes: 5 additions & 3 deletions app/pkg/markdown/markdown_test.go
Original file line number Diff line number Diff line change
@@ -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_SMS
194 changes: 0 additions & 194 deletions app/pkg/markdown/simple_renderer.go

This file was deleted.

50 changes: 50 additions & 0 deletions app/pkg/markdown/text.go
Original file line number Diff line number Diff line change
@@ -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)
}
Loading

0 comments on commit 4bdd291

Please sign in to comment.