GoBlog/markdown.go

37 lines
736 B
Go
Raw Normal View History

2020-07-28 19:38:12 +00:00
package main
import (
"bytes"
_ "bytes"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer/html"
)
var markdown goldmark.Markdown
func init() {
markdown = goldmark.New(
goldmark.WithRendererOptions(
html.WithUnsafe(),
),
goldmark.WithParserOptions(
parser.WithAutoHeadingID(),
),
goldmark.WithExtensions(
extension.GFM,
extension.Footnote,
extension.Typographer,
),
)
}
2020-07-29 14:41:36 +00:00
func renderMarkdown(source string) (content []byte, err error) {
2020-07-28 19:38:12 +00:00
context := parser.NewContext()
var buffer bytes.Buffer
err = markdown.Convert([]byte(source), &buffer, parser.WithContext(context))
2020-07-29 14:41:36 +00:00
content = emojify(buffer.Bytes())
2020-07-28 19:38:12 +00:00
return
}