-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdown.go
More file actions
43 lines (36 loc) · 950 Bytes
/
Copy pathmarkdown.go
File metadata and controls
43 lines (36 loc) · 950 Bytes
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
36
37
38
39
40
41
42
43
package markup
import (
"bytes"
"sync"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/renderer/html"
)
var bufPool = sync.Pool{
New: func() any { return new(bytes.Buffer) },
}
// MarkdownRenderer renders Markdown to HTML using goldmark.
type MarkdownRenderer struct {
md goldmark.Markdown
}
// NewMarkdownRenderer creates a Markdown renderer with GFM extensions
// (tables, strikethrough, task lists, autolinks) and unsafe HTML passthrough.
func NewMarkdownRenderer() *MarkdownRenderer {
return &MarkdownRenderer{
md: goldmark.New(
goldmark.WithExtensions(extension.GFM),
goldmark.WithRendererOptions(
html.WithUnsafe(),
),
),
}
}
func (r *MarkdownRenderer) Render(content []byte) (string, error) {
buf := bufPool.Get().(*bytes.Buffer)
buf.Reset()
defer bufPool.Put(buf)
if err := r.md.Convert(content, buf); err != nil {
return "", err
}
return buf.String(), nil
}