-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdown.go
45 lines (37 loc) · 1011 Bytes
/
markdown.go
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
44
45
package v1
import (
"errors"
"github.com/charmbracelet/glamour"
"io"
"strings"
)
func Markdown(ops ...glamour.TermRendererOption) (o Option) {
if len(ops) == 0 {
return Wrap(glamour.NewTermRenderer(
glamour.WithEmoji(),
glamour.WithStyles(glamour.DarkStyleConfig),
))
}
return Wrap(glamour.NewTermRenderer(ops...))
}
func MarkdownBlack(ops ...glamour.TermRendererOption) (o Option) {
ops = append(ops, glamour.WithEmoji())
ops = append(ops, glamour.WithStyles(glamour.DarkStyleConfig))
return Wrap(glamour.NewTermRenderer(
ops...,
))
}
func (o Option) RenderFromReader(i io.ReadCloser) Option {
renderer, ok := o.value.(*glamour.TermRenderer)
if !ok {
return Wrap(o.value, errors.New("o.value is not of type *glamour.TermRenderer"))
}
md, err := renderer.Render(Slurp(i).UnwrapString())
if err != nil {
return Wrap(md, renderer.Close())
}
return Wrap(md, err)
}
func (o Option) Render(i string) Option {
return o.RenderFromReader(io.NopCloser(strings.NewReader(i)))
}