Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: replace markdown processor #1078

Merged
merged 2 commits into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
chore: replace markdown processor
Replaces package github.com/russross/blackfriday with
github.com/gomarkdown/markdown.
  • Loading branch information
onionltd committed Jun 3, 2022
commit 9a86726ceef9031f79caabd23e3cbf9a2b0f8be9
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
Expand Up @@ -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
Expand Down
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