blog/markdown/markdown.go

49 lines
1.1 KiB
Go
Raw Normal View History

package markdown
import (
"bytes"
"io"
chromahtml "github.com/alecthomas/chroma/formatters/html"
"github.com/yuin/goldmark"
emoji "github.com/yuin/goldmark-emoji"
highlighting "github.com/yuin/goldmark-highlighting"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer/html"
)
var gm = goldmark.New(
goldmark.WithExtensions(
extension.GFM,
highlighting.NewHighlighting(
highlighting.WithStyle("monokai"),
highlighting.WithFormatOptions(
chromahtml.WithLineNumbers(true),
chromahtml.LinkableLineNumbers(true, "code-"),
chromahtml.LineNumbersInTable(true),
),
),
emoji.Emoji,
),
goldmark.WithParserOptions(
parser.WithAutoHeadingID(),
),
goldmark.WithRendererOptions(
html.WithHardWraps(),
),
)
// Convert transforms a markdown document into HTML
func Convert(r io.Reader) (string, error) {
content, err := io.ReadAll(r)
if err != nil {
return "", err
}
var buf bytes.Buffer
if err := gm.Convert(content, &buf); err != nil {
return "", err
}
return buf.String(), nil
}