summaryrefslogtreecommitdiff
path: root/markdown/markdown.go
blob: 3a4bebaadf3b8c8e44c850bebb0c33a4d16149be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package markdown

import (
	"bytes"

	"github.com/alecthomas/chroma/formatters/html"
	"github.com/yuin/goldmark"
	highlighting "github.com/yuin/goldmark-highlighting"
	goldmarkHtml "github.com/yuin/goldmark/renderer/html"
)

func Parse(s string) (string, error) {
	markdown := goldmark.New(
		goldmark.WithExtensions(
			highlighting.NewHighlighting(
				highlighting.WithStyle("monokai"),
				highlighting.WithFormatOptions(
					// html.WithAllClasses(true),
					html.WithClasses(true),
					html.WithLineNumbers(false),
				),
			),
		),
		goldmark.WithRendererOptions(
			goldmarkHtml.WithUnsafe(),
		),
	)

	buf := new(bytes.Buffer)
	if err := markdown.Convert([]byte(s), buf); err != nil {
		return "", err
	}

	return buf.String(), nil
}