-
-
Notifications
You must be signed in to change notification settings - Fork 654
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: replace markdown processor (#1078)
Replaces package github.com/russross/blackfriday with github.com/gomarkdown/markdown. Co-authored-by: Guilherme Oenning <[email protected]>
- Loading branch information
Showing
7 changed files
with
99 additions
and
403 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.