forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdown.go
More file actions
97 lines (76 loc) · 1.86 KB
/
markdown.go
File metadata and controls
97 lines (76 loc) · 1.86 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package markdown
import (
"os"
"strings"
"github.com/charmbracelet/glamour"
)
type RenderOpts []glamour.TermRendererOption
func WithoutIndentation() glamour.TermRendererOption {
overrides := []byte(`
{
"document": {
"margin": 0
},
"code_block": {
"margin": 0
}
}`)
return glamour.WithStylesFromJSONBytes(overrides)
}
func WithoutWrap() glamour.TermRendererOption {
return glamour.WithWordWrap(0)
}
func render(text string, opts RenderOpts) (string, error) {
// Glamour rendering preserves carriage return characters in code blocks, but
// we need to ensure that no such characters are present in the output.
text = strings.ReplaceAll(text, "\r\n", "\n")
tr, err := glamour.NewTermRenderer(opts...)
if err != nil {
return "", err
}
return tr.Render(text)
}
func Render(text, style string) (string, error) {
opts := RenderOpts{
glamour.WithStylePath(style),
glamour.WithEmoji(),
}
return render(text, opts)
}
func RenderWithOpts(text, style string, opts RenderOpts) (string, error) {
defaultOpts := RenderOpts{
glamour.WithStylePath(style),
glamour.WithEmoji(),
}
opts = append(defaultOpts, opts...)
return render(text, opts)
}
func RenderWithBaseURL(text, style, baseURL string) (string, error) {
opts := RenderOpts{
glamour.WithStylePath(style),
glamour.WithEmoji(),
glamour.WithBaseURL(baseURL),
}
return render(text, opts)
}
func RenderWithWrap(text, style string, wrap int) (string, error) {
opts := RenderOpts{
glamour.WithStylePath(style),
glamour.WithEmoji(),
glamour.WithWordWrap(wrap),
}
return render(text, opts)
}
func GetStyle(defaultStyle string) string {
style := fromEnv()
if style != "" && style != "auto" {
return style
}
if defaultStyle == "light" || defaultStyle == "dark" {
return defaultStyle
}
return "notty"
}
var fromEnv = func() string {
return os.Getenv("GLAMOUR_STYLE")
}