maubot/vendor/maunium.net/go/gomatrix/format/markdown.go

38 lines
961 B
Go
Raw Normal View History

2018-09-19 22:28:37 +00:00
package format
import (
"gopkg.in/russross/blackfriday.v2"
"maunium.net/go/gomatrix"
"strings"
)
func RenderMarkdown(text string) gomatrix.Content {
parser := blackfriday.New(
blackfriday.WithExtensions(blackfriday.NoIntraEmphasis |
blackfriday.Tables |
blackfriday.FencedCode |
blackfriday.Strikethrough |
blackfriday.SpaceHeadings |
blackfriday.DefinitionLists))
ast := parser.Parse([]byte(text))
renderer := blackfriday.NewHTMLRenderer(blackfriday.HTMLRendererParameters{
Flags: blackfriday.UseXHTML,
})
var buf strings.Builder
renderer.RenderHeader(&buf, ast)
ast.Walk(func(node *blackfriday.Node, entering bool) blackfriday.WalkStatus {
return renderer.RenderNode(&buf, node, entering)
})
renderer.RenderFooter(&buf, ast)
htmlBody := buf.String()
return gomatrix.Content{
FormattedBody: htmlBody,
Format: gomatrix.FormatHTML,
MsgType: gomatrix.MsgText,
Body: HTMLToText(htmlBody),
}
}